@@ -3,12 +3,15 @@ mod tests;
3
3
4
4
use std:: collections:: BTreeMap ;
5
5
6
+ use anyhow:: Context as _;
6
7
use serde_yaml:: Value ;
7
8
8
9
use crate :: GitHubContext ;
10
+ use crate :: utils:: load_env_var;
9
11
10
12
/// Representation of a job loaded from the `src/ci/github-actions/jobs.yml` file.
11
13
#[ derive( serde:: Deserialize , Debug , Clone ) ]
14
+ #[ serde( deny_unknown_fields) ]
12
15
pub struct Job {
13
16
/// Name of the job, e.g. mingw-check
14
17
pub name : String ,
@@ -26,6 +29,8 @@ pub struct Job {
26
29
pub free_disk : Option < bool > ,
27
30
/// Documentation link to a resource that could help people debug this CI job.
28
31
pub doc_url : Option < String > ,
32
+ /// Whether the job is executed on AWS CodeBuild.
33
+ pub codebuild : Option < bool > ,
29
34
}
30
35
31
36
impl Job {
@@ -80,7 +85,7 @@ impl JobDatabase {
80
85
}
81
86
82
87
pub fn load_job_db ( db : & str ) -> anyhow:: Result < JobDatabase > {
83
- let mut db: Value = serde_yaml:: from_str ( & db) ?;
88
+ let mut db: Value = serde_yaml:: from_str ( db) ?;
84
89
85
90
// We need to expand merge keys (<<), because serde_yaml can't deal with them
86
91
// `apply_merge` only applies the merge once, so do it a few times to unwrap nested merges.
@@ -107,6 +112,29 @@ struct GithubActionsJob {
107
112
free_disk : Option < bool > ,
108
113
#[ serde( skip_serializing_if = "Option::is_none" ) ]
109
114
doc_url : Option < String > ,
115
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
116
+ codebuild : Option < bool > ,
117
+ }
118
+
119
+ /// Replace GitHub context variables with environment variables in job configs.
120
+ /// Used for codebuild jobs like
121
+ /// `codebuild-ubuntu-22-8c-$github.run_id-$github.run_attempt`
122
+ fn substitute_github_vars ( jobs : Vec < Job > ) -> anyhow:: Result < Vec < Job > > {
123
+ let run_id = load_env_var ( "GITHUB_RUN_ID" ) ?;
124
+ let run_attempt = load_env_var ( "GITHUB_RUN_ATTEMPT" ) ?;
125
+
126
+ let jobs = jobs
127
+ . into_iter ( )
128
+ . map ( |mut job| {
129
+ job. os = job
130
+ . os
131
+ . replace ( "$github.run_id" , & run_id)
132
+ . replace ( "$github.run_attempt" , & run_attempt) ;
133
+ job
134
+ } )
135
+ . collect ( ) ;
136
+
137
+ Ok ( jobs)
110
138
}
111
139
112
140
/// Skip CI jobs that are not supposed to be executed on the given `channel`.
@@ -177,6 +205,8 @@ fn calculate_jobs(
177
205
}
178
206
RunType :: AutoJob => ( db. auto_jobs . clone ( ) , "auto" , & db. envs . auto_env ) ,
179
207
} ;
208
+ let jobs = substitute_github_vars ( jobs. clone ( ) )
209
+ . context ( "Failed to substitute GitHub context variables in jobs" ) ?;
180
210
let jobs = skip_jobs ( jobs, channel) ;
181
211
let jobs = jobs
182
212
. into_iter ( )
@@ -207,6 +237,7 @@ fn calculate_jobs(
207
237
continue_on_error : job. continue_on_error ,
208
238
free_disk : job. free_disk ,
209
239
doc_url : job. doc_url ,
240
+ codebuild : job. codebuild ,
210
241
}
211
242
} )
212
243
. collect ( ) ;
0 commit comments