5
5
from typing import Iterable , Literal , Mapping , NamedTuple , TypeVar
6
6
7
7
from shrub .v3 .evg_build_variant import BuildVariant
8
- from shrub .v3 .evg_command import EvgCommandType , subprocess_exec
8
+ from shrub .v3 .evg_command import BuiltInCommand , EvgCommandType , subprocess_exec
9
9
from shrub .v3 .evg_task import EvgTaskRef
10
10
11
11
from ..etc .utils import Task , all_possible
@@ -164,6 +164,30 @@ def variants_for(config: Configuration) -> Iterable[EarthlyVariant]:
164
164
return filter (allow_env_for_config , all_envs )
165
165
166
166
167
+ def earthly_exec (
168
+ * ,
169
+ kind : Literal ["test" , "setup" , "system" ],
170
+ target : str ,
171
+ secrets : Mapping [str , str ] | None = None ,
172
+ args : Mapping [str , str ] | None = None ,
173
+ ) -> BuiltInCommand :
174
+ """Create a subprocess_exec command that runs Earthly with the given arguments"""
175
+ env : dict [str , str ] = {}
176
+ if secrets :
177
+ env ["EARTHLY_SECRETS" ] = "," .join (f"{ k } ={ v } " for k , v in secrets .items ())
178
+ return subprocess_exec (
179
+ "bash" ,
180
+ args = [
181
+ "tools/earthly.sh" ,
182
+ f"+{ target } " ,
183
+ * (f"--{ arg } ={ val } " for arg , val in (args or {}).items ()),
184
+ ],
185
+ command_type = EvgCommandType (kind ),
186
+ env = env if env else None ,
187
+ working_dir = "mongoc" ,
188
+ )
189
+
190
+
167
191
def earthly_task (
168
192
* ,
169
193
name : str ,
@@ -184,40 +208,30 @@ def earthly_task(
184
208
return
185
209
# Generate the build-arg arguments based on the configuration options. The
186
210
# NamedTuple field names must match with the ARG keys in the Earthfile!
187
- earthly_args = [ f"-- { key } = { val } " for key , val in config ._asdict (). items ()]
188
- # Add arguments that come from parameter expansions defined in the build variant
189
- earthly_args += [
190
- f"-- env= ${{{ _ENV_PARAM_NAME } }}" ,
191
- f"-- c_compiler= ${{{ _CC_PARAM_NAME } }}" ,
192
- ]
211
+ earthly_args = config ._asdict ()
212
+ earthly_args |= {
213
+ # Add arguments that come from parameter expansions defined in the build variant
214
+ " env" : f" ${{{ _ENV_PARAM_NAME } }}" ,
215
+ " c_compiler" : f" ${{{ _CC_PARAM_NAME } }}" ,
216
+ }
193
217
return Task (
194
218
name = name ,
195
219
commands = [
196
220
# First, just build the "env-warmup" which will prepare the build environment.
197
221
# This won't generate any output, but allows EVG to track it as a separate build step
198
222
# for timing and logging purposes. The subequent build step will cache-hit the
199
223
# warmed-up build environments.
200
- subprocess_exec (
201
- "bash" ,
202
- args = [
203
- "tools/earthly.sh" ,
204
- "+env-warmup" ,
205
- * earthly_args ,
206
- ],
207
- working_dir = "mongoc" ,
208
- command_type = EvgCommandType .SETUP ,
224
+ earthly_exec (
225
+ kind = "setup" ,
226
+ target = "env-warmup" ,
227
+ args = earthly_args ,
209
228
),
210
229
# Now execute the main tasks:
211
- subprocess_exec (
212
- "bash" ,
213
- args = [
214
- "tools/earthly.sh" ,
215
- "+run" ,
216
- f"--targets={ ' ' .join (targets )} " ,
217
- * earthly_args ,
218
- ],
219
- working_dir = "mongoc" ,
220
- command_type = EvgCommandType .TEST ,
230
+ earthly_exec (
231
+ kind = "test" ,
232
+ target = "run" ,
233
+ # The "targets" arg is for +run to specify which targets to run
234
+ args = {"targets" : " " .join (targets )} | earthly_args ,
221
235
),
222
236
],
223
237
tags = [f"earthly" , "pr-merge-gate" , * env_tags ],
@@ -249,5 +263,5 @@ def tasks() -> Iterable[Task]:
249
263
yield task
250
264
251
265
252
- def variants () -> list [BuildVariant ]:
253
- return [ ev .as_evg_variant () for ev in all_possible (EarthlyVariant )]
266
+ def variants () -> Iterable [BuildVariant ]:
267
+ yield from ( ev .as_evg_variant () for ev in all_possible (EarthlyVariant ))
0 commit comments