@@ -3,9 +3,10 @@ mod tests;
33
44use std:: collections:: BTreeMap ;
55
6+ use anyhow:: Context as _;
67use serde_yaml:: Value ;
78
8- use crate :: GitHubContext ;
9+ use crate :: { GitHubContext , utils :: load_env_var } ;
910
1011/// Representation of a job loaded from the `src/ci/github-actions/jobs.yml` file.
1112#[ derive( serde:: Deserialize , Debug , Clone ) ]
@@ -109,6 +110,27 @@ struct GithubActionsJob {
109110 doc_url : Option < String > ,
110111}
111112
113+ /// Replace GitHub context variables with environment variables in job configs.
114+ /// Useful for codebuild jobs like
115+ /// `codebuild-ubuntu-22-8c-${{ github.run_id }}-${{ github.run_attempt }}`
116+ fn substitute_github_vars ( jobs : Vec < Job > ) -> anyhow:: Result < Vec < Job > > {
117+ let run_id = load_env_var ( "GITHUB_RUN_ID" ) ?;
118+ let run_attempt = load_env_var ( "GITHUB_RUN_ATTEMPT" ) ?;
119+
120+ let jobs = jobs
121+ . into_iter ( )
122+ . map ( |mut job| {
123+ job. os = job
124+ . os
125+ . replace ( "${{ github.run_id }}" , & run_id)
126+ . replace ( "${{ github.run_attempt }}" , & run_attempt) ;
127+ job
128+ } )
129+ . collect ( ) ;
130+
131+ Ok ( jobs)
132+ }
133+
112134/// Skip CI jobs that are not supposed to be executed on the given `channel`.
113135fn skip_jobs ( jobs : Vec < Job > , channel : & str ) -> Vec < Job > {
114136 jobs. into_iter ( )
@@ -177,6 +199,8 @@ fn calculate_jobs(
177199 }
178200 RunType :: AutoJob => ( db. auto_jobs . clone ( ) , "auto" , & db. envs . auto_env ) ,
179201 } ;
202+ let jobs = substitute_github_vars ( jobs. clone ( ) )
203+ . context ( "Failed to substitute GitHub context variables in jobs" ) ?;
180204 let jobs = skip_jobs ( jobs, channel) ;
181205 let jobs = jobs
182206 . into_iter ( )
0 commit comments