Skip to content

Commit 57952d3

Browse files
[CDRIVER-5535] Create asset groups in Silk (#1619)
* Add a script and Earthly target to create a Silk asset group * Define new EVG tasks: - Add a "Miscellaneous" "variant" to group one-off tasks that don't vary. - Add a "create-silk-asset-group" task that creates a Silk asset group for the current branch if it does not already exist. * Ignore a `.secret` file in the repo directory * Run the asset group creation on all builds
1 parent 25047b6 commit 57952d3

File tree

7 files changed

+351
-28
lines changed

7 files changed

+351
-28
lines changed

.evergreen/config_generator/components/earthly.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Iterable, Literal, Mapping, NamedTuple, TypeVar
66

77
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
99
from shrub.v3.evg_task import EvgTaskRef
1010

1111
from ..etc.utils import Task, all_possible
@@ -164,6 +164,30 @@ def variants_for(config: Configuration) -> Iterable[EarthlyVariant]:
164164
return filter(allow_env_for_config, all_envs)
165165

166166

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+
167191
def earthly_task(
168192
*,
169193
name: str,
@@ -184,40 +208,30 @@ def earthly_task(
184208
return
185209
# Generate the build-arg arguments based on the configuration options. The
186210
# 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+
}
193217
return Task(
194218
name=name,
195219
commands=[
196220
# First, just build the "env-warmup" which will prepare the build environment.
197221
# This won't generate any output, but allows EVG to track it as a separate build step
198222
# for timing and logging purposes. The subequent build step will cache-hit the
199223
# 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,
209228
),
210229
# 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,
221235
),
222236
],
223237
tags=[f"earthly", "pr-merge-gate", *env_tags],
@@ -249,5 +263,5 @@ def tasks() -> Iterable[Task]:
249263
yield task
250264

251265

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))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Iterable
2+
3+
from shrub.v3.evg_build_variant import BuildVariant
4+
from shrub.v3.evg_task import EvgTaskRef
5+
6+
from ..etc.utils import Task
7+
from . import earthly
8+
9+
10+
def tasks() -> Iterable[Task]:
11+
yield Task(
12+
name="create-silk-asset-group",
13+
commands=[
14+
earthly.earthly_exec(
15+
kind="setup",
16+
target="create-silk-asset-group",
17+
args={
18+
"branch": r"${branch_name}",
19+
},
20+
secrets={
21+
"SILK_CLIENT_ID": r"${silk_client_id}",
22+
"SILK_CLIENT_SECRET": r"${silk_client_secret}",
23+
},
24+
)
25+
],
26+
run_on=earthly.CONTAINER_RUN_DISTROS,
27+
tags=["misc", "pr-merge-gate"],
28+
)
29+
30+
31+
def variants() -> Iterable[BuildVariant]:
32+
yield BuildVariant(
33+
name="misc",
34+
tasks=[EvgTaskRef(name=".misc")],
35+
display_name="Miscellaneous",
36+
)

.evergreen/generated_configs/tasks.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,30 @@ tasks:
11691169
tags: [clang-format]
11701170
commands:
11711171
- func: clang-format
1172+
- name: create-silk-asset-group
1173+
run_on:
1174+
- ubuntu2204-small
1175+
- ubuntu2204-large
1176+
- ubuntu2004-small
1177+
- ubuntu2004
1178+
- ubuntu1804
1179+
- ubuntu1804-medium
1180+
- debian10
1181+
- debian11
1182+
- amazon2
1183+
tags: [misc, pr-merge-gate]
1184+
commands:
1185+
- command: subprocess.exec
1186+
type: setup
1187+
params:
1188+
binary: bash
1189+
working_dir: mongoc
1190+
env:
1191+
EARTHLY_SECRETS: SILK_CLIENT_ID=${silk_client_id},SILK_CLIENT_SECRET=${silk_client_secret}
1192+
args:
1193+
- tools/earthly.sh
1194+
- +create-silk-asset-group
1195+
- --branch=${branch_name}
11721196
- name: cse-sasl-cyrus-darwinssl-macos-1100-clang-compile
11731197
run_on: macos-1100
11741198
tags: [cse-matrix-darwinssl, compile, macos-1100, clang, cse, sasl-cyrus]

.evergreen/generated_configs/variants.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ buildvariants:
101101
display_name: loadbalanced
102102
tasks:
103103
- name: .loadbalanced
104+
- name: misc
105+
display_name: Miscellaneous
106+
tasks:
107+
- name: .misc
104108
- name: mock-server-test
105109
display_name: Mock Server Test
106110
expansions:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ dist
3838
# `secrets-export.sh` is created by the drivers-evergreen-tools `setup_secrets.sh` script.
3939
# Ignore this file to reduce risk of committing credentials during local development.
4040
secrets-export.sh
41+
42+
# A file that can be used to store secrets for Earthly
43+
.secret

Earthfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ sbom-generate:
191191
# Save the result back to the host:
192192
SAVE ARTIFACT /s/cyclonedx.sbom.json AS LOCAL etc/cyclonedx.sbom.json
193193

194+
# create-silk-asset-group :
195+
# Create an asset group in Silk for the Git branch if one is not already defined.
196+
#
197+
# Requires credentials for Silk access.
198+
#
199+
# If --branch is not specified, it will be inferred from the current Git branch
200+
create-silk-asset-group:
201+
ARG branch
202+
# Get a default value for $branch
203+
FROM alpine:3.19
204+
IF test "${branch}" = ""
205+
LOCALLY
206+
LET branch=$(git rev-parse --abbrev-ref HEAD)
207+
RUN --no-cache echo "Inferred asset-group name from Git HEAD to be “${branch}”"
208+
END
209+
# Reset to alpine from the LOCALLY above
210+
FROM alpine:3.19
211+
RUN apk add python3
212+
# Copy in the script
213+
COPY tools/create-silk-asset-group.py /opt/
214+
# # Run the creation script. Refer to tools/create-silk-asset-group.py for details
215+
RUN --no-cache \
216+
--secret SILK_CLIENT_ID \
217+
--secret SILK_CLIENT_SECRET \
218+
python /opt/create-silk-asset-group.py \
219+
--branch=${branch} \
220+
--project=mongo-c-driver \
221+
--code-repo-url=https://github.com/mongodb/mongo-c-driver \
222+
--sbom-lite-path=etc/cyclonedx.sbom.json \
223+
--exist-ok
224+
194225
# test-vcpkg-classic :
195226
# Builds src/libmongoc/examples/cmake/vcpkg by using vcpkg to download and
196227
# install a mongo-c-driver build in "classic mode". *Does not* use the local

0 commit comments

Comments
 (0)