@@ -3,9 +3,11 @@ mod tests;
33
44use std:: collections:: BTreeMap ;
55
6+ use anyhow:: Context as _;
67use serde_yaml:: Value ;
78
89use crate :: GitHubContext ;
10+ use crate :: utils:: load_env_var;
911
1012/// Representation of a job loaded from the `src/ci/github-actions/jobs.yml` file.
1113#[ derive( serde:: Deserialize , Debug , Clone ) ]
@@ -109,6 +111,27 @@ struct GithubActionsJob {
109111 doc_url : Option < String > ,
110112}
111113
114+ /// Replace GitHub context variables with environment variables in job configs.
115+ /// Useful for codebuild jobs like
116+ /// `codebuild-ubuntu-22-8c-${{ github.run_id }}-${{ github.run_attempt }}`
117+ fn substitute_github_vars ( jobs : Vec < Job > ) -> anyhow:: Result < Vec < Job > > {
118+ let run_id = load_env_var ( "GITHUB_RUN_ID" ) ?;
119+ let run_attempt = load_env_var ( "GITHUB_RUN_ATTEMPT" ) ?;
120+
121+ let jobs = jobs
122+ . into_iter ( )
123+ . map ( |mut job| {
124+ job. os = job
125+ . os
126+ . replace ( "${{ github.run_id }}" , & run_id)
127+ . replace ( "${{ github.run_attempt }}" , & run_attempt) ;
128+ job
129+ } )
130+ . collect ( ) ;
131+
132+ Ok ( jobs)
133+ }
134+
112135/// Skip CI jobs that are not supposed to be executed on the given `channel`.
113136fn skip_jobs ( jobs : Vec < Job > , channel : & str ) -> Vec < Job > {
114137 jobs. into_iter ( )
@@ -177,6 +200,8 @@ fn calculate_jobs(
177200 }
178201 RunType :: AutoJob => ( db. auto_jobs . clone ( ) , "auto" , & db. envs . auto_env ) ,
179202 } ;
203+ let jobs = substitute_github_vars ( jobs. clone ( ) )
204+ . context ( "Failed to substitute GitHub context variables in jobs" ) ?;
180205 let jobs = skip_jobs ( jobs, channel) ;
181206 let jobs = jobs
182207 . into_iter ( )
0 commit comments