Skip to content

Commit 9033ae3

Browse files
committed
feat: optionally apply github-build-setup steps to additional jobs.
For some use cases, such as using private Git crate dependencies in a project's Cargo.toml manifest, Github Actions requires a certain amount of ceremony to alias the private repo's Git URL to to something that can be cloned using an access token. For the `build-local-artifacts` job, this can be accomplished by adding the authentication step in the `github-build-setup` YML file. There are additional jobs that necessitate these credentials be set, mainly the `plan`, `build-global-artifacts`, and `host`. This appears to be due to the fact they invoke `cargo metadata` (which attempts to pull dependencies). Support these use-cases by introducing a `github-build-setup-jobs` collection which allows users to opt-in to including the `github-build-setup` steps in various jobs. Fixes #2065
1 parent b1ca557 commit 9033ae3

File tree

11 files changed

+5140
-56
lines changed

11 files changed

+5140
-56
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Unreleased
22

3-
Nothing Yet!
3+
## `github-build-setup` Job Customization
4+
5+
It's now possible to specify to which jobs `github-build-setup` steps are added.
6+
7+
```toml
8+
github-build-setup = "build-steps.yml"
9+
github-build-setup-jobs = ["plan", "build-local-artifacts", "build-global-artifacts", "host"]
10+
```
11+
12+
If not configured, this defaults to `["build-local-artifacts"]`.
13+
14+
- impl @arusahni [feat: optionally apply `github-build-setup` steps to additional jobs](https://github.com/axodotdev/cargo-dist/pull/2070)
415

516

617
# Version 0.30.2 (2025-10-31)

book/src/reference/config.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ We're currently in the middle of [a major config migration](https://github.com/a
9696
* [`github-custom-job-permissions`](#github-custom-job-permissions)
9797
* [`github-custom-runners`](#github-custom-runners)
9898
* [`github-build-setup`](#github-build-setup)
99+
* [`github-build-setup-jobs`](#github-build-setup-jobs)
99100
* [`github-action-commits`](#github-action-commits)
100101
* [custom ci jobs](#custom-ci-jobs)
101102
* [`plan-jobs`](#plan-jobs)
@@ -1384,8 +1385,28 @@ These settings are specific to [your dist GitHub CI][github-ci].
13841385
13851386
This configuration value should be a path relative to the repository your `.github/workflows` directory.
13861387
The file located at that path should contain a yaml array of [steps][github-workflow-step] which will be
1387-
performed before we call `dist build`.
1388+
performed before we call `dist build` in the `build-local-artifacts` job. These
1389+
steps can be added to additional jobs by defining
1390+
[`github-build-setup-jobs`](#github-build-setup-jobs).
13881391
1392+
#### `github-build-setup-jobs`
1393+
1394+
> <span style="float:right">since 1.0.0<br>[global-only][]</span>
1395+
> 🔧 this is an experimental feature! \
1396+
> [📖 read the ci customization guide!][github-ci] \
1397+
> default = `<none>`
1398+
>
1399+
> *in your dist-workspace.toml or dist.toml:*
1400+
> ```toml
1401+
> [dist]
1402+
> # Defaults to "build-local-artifacts"
1403+
> github-build-setup-jobs = ["plan", "build-local-artifacts"]
1404+
> ```
1405+
1406+
This configuration value should accompany
1407+
[`github-build-setup`](#github-build-setup). When not specified, it defaults to
1408+
`["build-local-artifacts"]`. Otherwise, the listed jobs will have the steps
1409+
defined in `github-build-setup` added.
13891410
13901411
#### `github-custom-job-permissions`
13911412

cargo-dist/src/backend/ci/github.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ pub struct GithubCiInfo {
100100
pub root_permissions: Option<GithubPermissionMap>,
101101
/// Extra build steps
102102
pub github_build_setup: Vec<GithubJobStep>,
103+
/// The jobs to which the [`GithubCiInfo::github_build_setup`] steps should be prepended
104+
pub github_build_setup_jobs: Vec<String>,
103105
/// Info about making a GitHub Release (if we're making one)
104106
#[serde(flatten)]
105107
pub github_release: Option<GithubReleaseInfo>,
@@ -364,6 +366,7 @@ impl GithubCiInfo {
364366
})
365367
.transpose()?
366368
.unwrap_or_default();
369+
let github_build_setup_jobs = ci_config.build_setup_jobs.clone();
367370

368371
let default_action_versions = [
369372
("actions/checkout", "v4"),
@@ -412,6 +415,7 @@ impl GithubCiInfo {
412415
hosting_providers,
413416
root_permissions,
414417
github_build_setup,
418+
github_build_setup_jobs,
415419
github_release,
416420
actions,
417421
need_cargo_auditable,

cargo-dist/src/config/v0.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,14 @@ pub struct DistMetadata {
466466
#[serde(default, with = "opt_string_or_vec")]
467467
pub install_libraries: Option<Vec<LibraryStyle>>,
468468

469-
/// Any additional steps that need to be performed before building local artifacts
469+
/// Any additional steps that need to be performed before executing certain job steps
470470
#[serde(default)]
471471
pub github_build_setup: Option<String>,
472472

473+
/// The jobs to which the [`DistMetadata::github_build_setup`] steps should be prepended
474+
#[serde(default)]
475+
pub github_build_setup_jobs: Option<Vec<String>>,
476+
473477
/// Configuration specific to Mac .pkg installers
474478
#[serde(skip_serializing_if = "Option::is_none")]
475479
#[serde(default)]
@@ -572,6 +576,7 @@ impl DistMetadata {
572576
package_libraries: _,
573577
install_libraries: _,
574578
github_build_setup: _,
579+
github_build_setup_jobs: _,
575580
mac_pkg_config: _,
576581
min_glibc_version: _,
577582
binaries: _,
@@ -679,6 +684,7 @@ impl DistMetadata {
679684
package_libraries,
680685
install_libraries,
681686
github_build_setup,
687+
github_build_setup_jobs,
682688
mac_pkg_config,
683689
min_glibc_version,
684690
binaries,
@@ -807,6 +813,9 @@ impl DistMetadata {
807813
if github_build_setup.is_some() {
808814
warn!("package.metadata.dist.github-build-setup is set, but this is only accepted in workspace.metadata (value is being ignored): {}", package_manifest_path);
809815
}
816+
if github_build_setup_jobs.is_some() {
817+
warn!("package.metadata.dist.github-build-setup-jobs is set, but this is only accepted in workspace.metadata (value is being ignored): {}", package_manifest_path);
818+
}
810819

811820
// Merge non-global settings
812821
if installers.is_none() {

cargo-dist/src/config/v0_to_v1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl DistMetadata {
8888
package_libraries,
8989
install_libraries,
9090
github_build_setup,
91+
github_build_setup_jobs,
9192
min_glibc_version,
9293
binaries,
9394
cargo_auditable,
@@ -165,13 +166,15 @@ impl DistMetadata {
165166
if github_custom_runners.is_some()
166167
|| github_custom_job_permissions.is_some()
167168
|| github_build_setup.is_some()
169+
|| github_build_setup_jobs.is_some()
168170
|| github_action_commits.is_some()
169171
{
170172
Some(GithubCiLayer {
171173
common: CommonCiLayer::default(),
172174
runners: github_custom_runners,
173175
permissions: github_custom_job_permissions,
174176
build_setup: github_build_setup,
177+
build_setup_jobs: github_build_setup_jobs,
175178
action_commits: github_action_commits,
176179
})
177180
} else {

cargo-dist/src/config/v1/ci/github.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ pub struct GithubCiLayer {
2525
#[serde(skip_serializing_if = "Option::is_none")]
2626
pub permissions: Option<SortedMap<String, GithubPermissionMap>>,
2727

28-
/// Custom permissions for jobs
28+
/// Custom steps for jobs
2929
#[serde(skip_serializing_if = "Option::is_none")]
3030
pub build_setup: Option<String>,
3131

32+
/// What jobs to which the [`GithubCiLayer::build_setup`] steps should be prepended
33+
#[serde(skip_serializing_if = "Option::is_none")]
34+
pub build_setup_jobs: Option<Vec<String>>,
35+
3236
/// Use these commits for actions
3337
#[serde(skip_serializing_if = "Option::is_none")]
3438
pub action_commits: Option<SortedMap<String, String>>,
@@ -46,9 +50,12 @@ pub struct GithubCiConfig {
4650
/// Custom permissions for jobs
4751
pub permissions: SortedMap<String, GithubPermissionMap>,
4852

49-
/// Custom permissions for jobs
53+
/// Custom steps for jobs
5054
pub build_setup: Option<String>,
5155

56+
/// What jobs to which the [`GithubCiConfig::build_setup`] steps should be prepended
57+
pub build_setup_jobs: Vec<String>,
58+
5259
/// Use these commits for github actions
5360
pub action_commits: SortedMap<String, String>,
5461
}
@@ -62,6 +69,7 @@ impl GithubCiConfig {
6269
permissions: Default::default(),
6370
action_commits: Default::default(),
6471
build_setup: None,
72+
build_setup_jobs: vec!["build-local-artifacts".to_string()],
6573
}
6674
}
6775
}
@@ -75,6 +83,7 @@ impl ApplyLayer for GithubCiConfig {
7583
runners,
7684
permissions,
7785
build_setup,
86+
build_setup_jobs,
7887
action_commits,
7988
}: Self::Layer,
8089
) {
@@ -143,6 +152,7 @@ impl ApplyLayer for GithubCiConfig {
143152
}));
144153
self.permissions.apply_val(permissions);
145154
self.build_setup.apply_opt(build_setup);
155+
self.build_setup_jobs.apply_val(build_setup_jobs);
146156
self.action_commits.apply_val(action_commits);
147157
}
148158
}
@@ -155,13 +165,15 @@ impl ApplyLayer for GithubCiLayer {
155165
runners,
156166
permissions,
157167
build_setup,
168+
build_setup_jobs,
158169
action_commits,
159170
}: Self::Layer,
160171
) {
161172
self.common.apply_layer(common);
162173
self.runners.apply_opt(runners);
163174
self.permissions.apply_opt(permissions);
164175
self.build_setup.apply_opt(build_setup);
176+
self.build_setup_jobs.apply_opt(build_setup_jobs);
165177
self.action_commits.apply_opt(action_commits);
166178
}
167179
}

cargo-dist/src/init.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn get_new_dist_metadata(
545545
package_libraries: None,
546546
install_libraries: None,
547547
github_build_setup: None,
548+
github_build_setup_jobs: None,
548549
mac_pkg_config: None,
549550
min_glibc_version: None,
550551
binaries: None,
@@ -1058,6 +1059,7 @@ fn apply_dist_to_metadata(metadata: &mut toml_edit::Item, meta: &DistMetadata) {
10581059
bin_aliases: _,
10591060
system_dependencies: _,
10601061
github_build_setup: _,
1062+
github_build_setup_jobs: _,
10611063
binaries: _,
10621064
} = &meta;
10631065

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{{%- for step in github_build_setup %}}
2+
- name: {{{ step.name }}}
3+
{{%- if step.id is not undefined %}}
4+
id: {{{ step.id }}}
5+
{{%- endif %}}
6+
{{%- if step.uses is not undefined %}}
7+
uses: {{{ step.uses }}}
8+
{{%- endif %}}
9+
{{%- if step.if is not undefined %}}
10+
if: {{{ step.if }}}
11+
{{%- endif %}}
12+
{{%- if step.run is not undefined %}}
13+
{{%- if step.run is multiline %}}
14+
run: |
15+
{{{ step.run|indent(10) }}}
16+
{{%- else %}}
17+
run: {{{ step.run }}}
18+
{{%- endif %}}
19+
{{%- endif %}}
20+
{{%- if step.working_directory is not undefined %}}
21+
working-directory: {{{ step.working_directory }}}
22+
{{%- endif %}}
23+
{{%- if step.shell is not undefined %}}
24+
shell: {{{ step.shell }}}
25+
{{%- endif %}}
26+
{{%- if not step.with is empty %}}
27+
with:
28+
{{%- for (var,value) in step.with|items %}}
29+
{{%- if value is mapping %}}
30+
{{{ var }}}:
31+
{{%- for (var,value) in value|items %}}
32+
{{{ var }}}: {{{ value }}}
33+
{{%- endfor %}}
34+
{{%- else %}}
35+
{{{ var }}}: {{{ value }}}
36+
{{%- endif %}}
37+
{{%- endfor %}}
38+
{{%- endif %}}
39+
{{%- if not step.env is empty %}}
40+
env:
41+
{{%- for (var,value) in step.env|items %}}
42+
{{{ var }}}: {{{ value }}}
43+
{{%- endfor %}}
44+
{{%- endif %}}
45+
{{%- endfor %}}

cargo-dist/templates/ci/github/release.yml.j2

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ jobs:
128128
with:
129129
name: cargo-dist-cache
130130
path: ~/.cargo/bin/dist
131+
{{%- if "plan" in github_build_setup_jobs %}}
132+
{{%- include 'ci/github/partials/build_setup_jobs.yml' %}}
133+
{{%- endif %}}
131134
# sure would be cool if github gave us proper conditionals...
132135
# so here's a doubly-nested ternary-via-truthiness to try to provide the best possible
133136
# functionality based on whether this is a pull_request, and whether it's from a fork.
@@ -235,57 +238,9 @@ jobs:
235238
- name: Use rustup to set correct Rust version
236239
run: rustup update {{{ rust_version }}} --no-self-update && rustup default {{{ rust_version }}}
237240
{{%- endif %}}
238-
{{%- for step in github_build_setup %}}
239-
- name: {{{ step.name }}}
240-
{{%- if step.id is not undefined %}}
241-
id: {{{ step.id }}}
242-
{{%- endif %}}
243-
{{%- if step.uses is not undefined %}}
244-
uses: {{{ step.uses }}}
245-
{{%- endif %}}
246-
{{%- if step.if is not undefined %}}
247-
if: {{{ step.if }}}
248-
{{%- endif %}}
249-
{{%- if step.run is not undefined %}}
250-
{{%- if step.run is multiline %}}
251-
run: |
252-
{{{ step.run|indent(10) }}}
253-
{{%- else %}}
254-
run: {{{ step.run }}}
255-
{{%- endif %}}
256-
{{%- endif %}}
257-
{{%- if step.working_directory is not undefined %}}
258-
working-directory: {{{ step.working_directory }}}
259-
{{%- endif %}}
260-
{{%- if step.shell is not undefined %}}
261-
shell: {{{ step.shell }}}
262-
{{%- endif %}}
263-
{{%- if not step.with is empty %}}
264-
with:
265-
{{%- for (var,value) in step.with|items %}}
266-
{{%- if value is mapping %}}
267-
{{{ var }}}:
268-
{{%- for (var,value) in value|items %}}
269-
{{{ var }}}: {{{ value }}}
270-
{{%- endfor %}}
271-
{{%- else %}}
272-
{{{ var }}}: {{{ value }}}
273-
{{%- endif %}}
274-
{{%- endfor %}}
275-
{{%- endif %}}
276-
{{%- if not step.env is empty %}}
277-
env:
278-
{{%- for (var,value) in step.env|items %}}
279-
{{{ var }}}: {{{ value }}}
280-
{{%- endfor %}}
281-
{{%- endif %}}
282-
{{%- if step.continue_on_error is not undefined %}}
283-
continue-on-error: {{{ step.continue_on_error }}}
284-
{{%- endif %}}
285-
{{%- if step.timeout_minutes is not undefined %}}
286-
timeout-minutes: {{{ step.timeout_minutes }}}
287-
{{%- endif %}}
288-
{{%- endfor %}}
241+
{{%- if "build-local-artifacts" in github_build_setup_jobs %}}
242+
{{%- include 'ci/github/partials/build_setup_jobs.yml' %}}
243+
{{%- endif %}}
289244
{{%- if cache_builds %}}
290245
- uses: {{{actions["swatinem/rust-cache"] | safe }}}
291246
with:
@@ -421,6 +376,9 @@ jobs:
421376
pattern: artifacts-*
422377
path: target/distrib/
423378
merge-multiple: true
379+
{{%- if "build-global-artifacts" in github_build_setup_jobs %}}
380+
{{%- include 'ci/github/partials/build_setup_jobs.yml' %}}
381+
{{%- endif %}}
424382
- id: cargo-dist
425383
shell: bash
426384
run: |
@@ -536,6 +494,9 @@ jobs:
536494
merge-multiple: true
537495
{{%- if "github" in hosting_providers and release_phase == "announce" %}}
538496
# This is a harmless no-op for GitHub Releases, hosting for that happens in "announce"
497+
{{%- endif %}}
498+
{{%- if "host" in github_build_setup_jobs %}}
499+
{{%- include 'ci/github/partials/build_setup_jobs.yml' %}}
539500
{{%- endif %}}
540501
- id: host
541502
shell: bash

0 commit comments

Comments
 (0)