@@ -36,25 +36,26 @@ impl CiEnv {
36
36
}
37
37
38
38
pub mod gha {
39
- use std:: sync:: atomic :: { AtomicBool , Ordering } ;
39
+ use std:: sync:: Mutex ;
40
40
41
- static GROUP_ACTIVE : AtomicBool = AtomicBool :: new ( false ) ;
41
+ static ACTIVE_GROUPS : Mutex < Vec < String > > = Mutex :: new ( Vec :: new ( ) ) ;
42
42
43
43
/// All github actions log messages from this call to the Drop of the return value
44
- /// will be grouped and hidden by default in logs. Note that nesting these does
45
- /// not really work.
44
+ /// will be grouped and hidden by default in logs. Note that since github actions doesn't
45
+ /// support group nesting, any active group will be first finished when a subgroup is started,
46
+ /// and then re-started when the subgroup finishes.
46
47
#[ track_caller]
47
48
pub fn group ( name : impl std:: fmt:: Display ) -> Group {
48
- if std:: env:: var_os ( "GITHUB_ACTIONS" ) . is_some ( ) {
49
- eprintln ! ( "::group::{name}" ) ;
50
- } else {
51
- eprintln ! ( "{name}" )
49
+ let mut groups = ACTIVE_GROUPS . lock ( ) . unwrap ( ) ;
50
+
51
+ // A group is currently active. End it first to avoid nesting.
52
+ if !groups. is_empty ( ) {
53
+ end_group ( ) ;
52
54
}
53
- // https://github.com/actions/toolkit/issues/1001
54
- assert ! (
55
- !GROUP_ACTIVE . swap( true , Ordering :: Relaxed ) ,
56
- "nested groups are not supported by GHA!"
57
- ) ;
55
+
56
+ let name = name. to_string ( ) ;
57
+ start_group ( & name) ;
58
+ groups. push ( name) ;
58
59
Group ( ( ) )
59
60
}
60
61
@@ -64,13 +65,36 @@ pub mod gha {
64
65
65
66
impl Drop for Group {
66
67
fn drop ( & mut self ) {
67
- if std:: env:: var_os ( "GITHUB_ACTIONS" ) . is_some ( ) {
68
- eprintln ! ( "::endgroup::" ) ;
68
+ end_group ( ) ;
69
+
70
+ let mut groups = ACTIVE_GROUPS . lock ( ) . unwrap ( ) ;
71
+ // Remove the current group
72
+ groups. pop ( ) ;
73
+
74
+ // If there was some previous group, restart it
75
+ if is_in_gha ( ) {
76
+ if let Some ( name) = groups. last ( ) {
77
+ start_group ( format ! ( "{name} (continued)" ) ) ;
78
+ }
69
79
}
70
- assert ! (
71
- GROUP_ACTIVE . swap( false , Ordering :: Relaxed ) ,
72
- "group dropped but no group active!"
73
- ) ;
74
80
}
75
81
}
82
+
83
+ fn start_group ( name : impl std:: fmt:: Display ) {
84
+ if is_in_gha ( ) {
85
+ eprintln ! ( "::group::{name}" ) ;
86
+ } else {
87
+ eprintln ! ( "{name}" )
88
+ }
89
+ }
90
+
91
+ fn end_group ( ) {
92
+ if is_in_gha ( ) {
93
+ eprintln ! ( "::endgroup::" ) ;
94
+ }
95
+ }
96
+
97
+ fn is_in_gha ( ) -> bool {
98
+ std:: env:: var_os ( "GITHUB_ACTIONS" ) . is_some ( )
99
+ }
76
100
}
0 commit comments