Skip to content

Commit bcf96b7

Browse files
committed
cargo-dist ci
1 parent a557d8e commit bcf96b7

File tree

4 files changed

+281
-12
lines changed

4 files changed

+281
-12
lines changed

.github/workflows/release.yml

Lines changed: 258 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,265 @@
1-
name: Main
1+
# Copyright 2022-2023, axodotdev
2+
# SPDX-License-Identifier: MIT or Apache-2.0
3+
#
4+
# CI that:
5+
#
6+
# * checks for a Git Tag that looks like a release
7+
# * builds artifacts with cargo-dist (archives, installers, hashes)
8+
# * uploads those artifacts to temporary workflow zip
9+
# * on success, uploads the artifacts to a Github Release
10+
#
11+
# Note that the Github Release will be created with a generated
12+
# title/body based on your changelogs.
213

3-
on: push
14+
name: Release
15+
16+
permissions:
17+
contents: write
18+
19+
# This task will run whenever you push a git tag that looks like a version
20+
# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc.
21+
# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where
22+
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
23+
# must be a Cargo-style SemVer Version (must have at least major.minor.patch).
24+
#
25+
# If PACKAGE_NAME is specified, then the announcement will be for that
26+
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
27+
#
28+
# If PACKAGE_NAME isn't specified, then the announcement will be for all
29+
# (cargo-dist-able) packages in the workspace with that version (this mode is
30+
# intended for workspaces with only one dist-able package, or with all dist-able
31+
# packages versioned/released in lockstep).
32+
#
33+
# If you push multiple tags at once, separate instances of this workflow will
34+
# spin up, creating an independent announcement for each one. However Github
35+
# will hard limit this to 3 tags per commit, as it will assume more tags is a
36+
# mistake.
37+
#
38+
# If there's a prerelease-style suffix to the version, then the release(s)
39+
# will be marked as a prerelease.
40+
on:
41+
push:
42+
tags:
43+
- '**[0-9]+.[0-9]+.[0-9]+*'
44+
pull_request:
445

546
jobs:
6-
release:
7-
name: release ${{ matrix.os }}
8-
runs-on: ${{ matrix.os }}
47+
# Run 'cargo dist plan' (or host) to determine what tasks we need to do
48+
plan:
49+
runs-on: ubuntu-latest
50+
outputs:
51+
val: ${{ steps.plan.outputs.manifest }}
52+
tag: ${{ !github.event.pull_request && github.ref_name || '' }}
53+
tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }}
54+
publishing: ${{ !github.event.pull_request }}
55+
env:
56+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57+
steps:
58+
- uses: actions/checkout@v4
59+
with:
60+
submodules: recursive
61+
- name: Install cargo-dist
62+
# we specify bash to get pipefail; it guards against the `curl` command
63+
# failing. otherwise `sh` won't catch that `curl` returned non-0
64+
shell: bash
65+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.10.0/cargo-dist-installer.sh | sh"
66+
# sure would be cool if github gave us proper conditionals...
67+
# so here's a doubly-nested ternary-via-truthiness to try to provide the best possible
68+
# functionality based on whether this is a pull_request, and whether it's from a fork.
69+
# (PRs run on the *source* but secrets are usually on the *target* -- that's *good*
70+
# but also really annoying to build CI around when it needs secrets to work right.)
71+
- id: plan
72+
run: |
73+
cargo dist ${{ (!github.event.pull_request && format('host --steps=create --tag={0}', github.ref_name)) || 'plan' }} --output-format=json > plan-dist-manifest.json
74+
echo "cargo dist ran successfully"
75+
cat plan-dist-manifest.json
76+
echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT"
77+
- name: "Upload dist-manifest.json"
78+
uses: actions/upload-artifact@v4
79+
with:
80+
name: artifacts-plan-dist-manifest
81+
path: plan-dist-manifest.json
82+
83+
# Build and packages all the platform-specific things
84+
build-local-artifacts:
85+
name: build-local-artifacts (${{ join(matrix.targets, ', ') }})
86+
# Let the initial task tell us to not run (currently very blunt)
87+
needs:
88+
- plan
89+
if: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix.include != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }}
990
strategy:
1091
fail-fast: false
11-
matrix:
12-
os: [ubuntu-latest, windows-latest, macos-latest]
92+
# Target platforms/runners are computed by cargo-dist in create-release.
93+
# Each member of the matrix has the following arguments:
94+
#
95+
# - runner: the github runner
96+
# - dist-args: cli flags to pass to cargo dist
97+
# - install-dist: expression to run to install cargo-dist on the runner
98+
#
99+
# Typically there will be:
100+
# - 1 "global" task that builds universal installers
101+
# - N "local" tasks that build each platform's binaries and platform-specific installers
102+
matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }}
103+
runs-on: ${{ matrix.runner }}
104+
env:
105+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
106+
BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json
13107
steps:
14-
- uses: actions/checkout@master
108+
- uses: actions/checkout@v4
109+
with:
110+
submodules: recursive
111+
- uses: swatinem/rust-cache@v2
112+
- name: Install cargo-dist
113+
run: ${{ matrix.install_dist }}
114+
# Get the dist-manifest
115+
- name: Fetch local artifacts
116+
uses: actions/download-artifact@v4
117+
with:
118+
pattern: artifacts-*
119+
path: target/distrib/
120+
merge-multiple: true
121+
- name: Install dependencies
122+
run: |
123+
${{ matrix.packages_install }}
124+
- name: Build artifacts
125+
run: |
126+
# Actually do builds and make zips and whatnot
127+
cargo dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json
128+
echo "cargo dist ran successfully"
129+
- id: cargo-dist
130+
name: Post-build
131+
# We force bash here just because github makes it really hard to get values up
132+
# to "real" actions without writing to env-vars, and writing to env-vars has
133+
# inconsistent syntax between shell and powershell.
134+
shell: bash
135+
run: |
136+
# Parse out what we just built and upload it to scratch storage
137+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
138+
jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT"
139+
echo "EOF" >> "$GITHUB_OUTPUT"
140+
141+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
142+
- name: "Upload artifacts"
143+
uses: actions/upload-artifact@v4
144+
with:
145+
name: artifacts-build-local-${{ join(matrix.targets, '_') }}
146+
path: |
147+
${{ steps.cargo-dist.outputs.paths }}
148+
${{ env.BUILD_MANIFEST_NAME }}
15149
16-
- name: Compile and release
17-
uses: softprops/action-gh-release@v1
18-
if: startsWith(github.ref, 'refs/tags/')
150+
# Build and package all the platform-agnostic(ish) things
151+
build-global-artifacts:
152+
needs:
153+
- plan
154+
- build-local-artifacts
155+
runs-on: "ubuntu-20.04"
156+
env:
157+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
158+
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
159+
steps:
160+
- uses: actions/checkout@v4
161+
with:
162+
submodules: recursive
163+
- name: Install cargo-dist
164+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.10.0/cargo-dist-installer.sh | sh"
165+
# Get all the local artifacts for the global tasks to use (for e.g. checksums)
166+
- name: Fetch local artifacts
167+
uses: actions/download-artifact@v4
168+
with:
169+
pattern: artifacts-*
170+
path: target/distrib/
171+
merge-multiple: true
172+
- id: cargo-dist
173+
shell: bash
174+
run: |
175+
cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json
176+
echo "cargo dist ran successfully"
177+
178+
# Parse out what we just built and upload it to scratch storage
179+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
180+
jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT"
181+
echo "EOF" >> "$GITHUB_OUTPUT"
182+
183+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
184+
- name: "Upload artifacts"
185+
uses: actions/upload-artifact@v4
186+
with:
187+
name: artifacts-build-global
188+
path: |
189+
${{ steps.cargo-dist.outputs.paths }}
190+
${{ env.BUILD_MANIFEST_NAME }}
191+
# Determines if we should publish/announce
192+
host:
193+
needs:
194+
- plan
195+
- build-local-artifacts
196+
- build-global-artifacts
197+
# Only run if we're "publishing", and only if local and global didn't fail (skipped is fine)
198+
if: ${{ always() && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.build-local-artifacts.result == 'skipped' || needs.build-local-artifacts.result == 'success') }}
199+
env:
200+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
201+
runs-on: "ubuntu-20.04"
202+
outputs:
203+
val: ${{ steps.host.outputs.manifest }}
204+
steps:
205+
- uses: actions/checkout@v4
206+
with:
207+
submodules: recursive
208+
- name: Install cargo-dist
209+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.10.0/cargo-dist-installer.sh | sh"
210+
# Fetch artifacts from scratch-storage
211+
- name: Fetch artifacts
212+
uses: actions/download-artifact@v4
213+
with:
214+
pattern: artifacts-*
215+
path: target/distrib/
216+
merge-multiple: true
217+
# This is a harmless no-op for Github Releases, hosting for that happens in "announce"
218+
- id: host
219+
shell: bash
220+
run: |
221+
cargo dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json
222+
echo "artifacts uploaded and released successfully"
223+
cat dist-manifest.json
224+
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
225+
- name: "Upload dist-manifest.json"
226+
uses: actions/upload-artifact@v4
227+
with:
228+
# Overwrite the previous copy
229+
name: artifacts-dist-manifest
230+
path: dist-manifest.json
231+
232+
# Create a Github Release while uploading all files to it
233+
announce:
234+
needs:
235+
- plan
236+
- host
237+
# use "always() && ..." to allow us to wait for all publish jobs while
238+
# still allowing individual publish jobs to skip themselves (for prereleases).
239+
# "host" however must run to completion, no skipping allowed!
240+
if: ${{ always() && needs.host.result == 'success' }}
241+
runs-on: "ubuntu-20.04"
242+
env:
243+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
244+
steps:
245+
- uses: actions/checkout@v4
246+
with:
247+
submodules: recursive
248+
- name: "Download Github Artifacts"
249+
uses: actions/download-artifact@v4
250+
with:
251+
pattern: artifacts-*
252+
path: artifacts
253+
merge-multiple: true
254+
- name: Cleanup
255+
run: |
256+
# Remove the granular manifests
257+
rm -f artifacts/*-dist-manifest.json
258+
- name: Create Github Release
259+
uses: ncipollo/release-action@v1
260+
with:
261+
tag: ${{ needs.plan.outputs.tag }}
262+
name: ${{ fromJson(needs.host.outputs.val).announcement_title }}
263+
body: ${{ fromJson(needs.host.outputs.val).announcement_github_body }}
264+
prerelease: ${{ fromJson(needs.host.outputs.val).announcement_is_prerelease }}
265+
artifacts: "artifacts/*"

Cargo.toml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
workspace = { members = ["locust-cli", "locust-core"] }
1+
[workspace]
2+
members = ["locust-cli", "locust-core"]
3+
4+
# Config for 'cargo dist'
5+
[workspace.metadata.dist]
6+
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
7+
cargo-dist-version = "0.10.0"
8+
# CI backends to support
9+
ci = ["github"]
10+
# The installers to generate for each app
11+
installers = []
12+
# Target platforms to build apps for (Rust target-triple syntax)
13+
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
14+
# Publish jobs to run in CI
15+
pr-run-mode = "plan"
216

317
[package]
418
name = "locust"
19+
repository = "https://github.com/maxmindlin/locust"
520
version = "0.1.0"
621
edition = "2021"
722

@@ -51,3 +66,8 @@ rustls-pemfile = "2.0.0"
5166
tracing-subscriber = "0.3.0"
5267
tabled = "0.15.0"
5368
sqlx = { version = "0.7", features = ["runtime-tokio", "postgres"] }
69+
70+
# The profile that 'cargo dist' will build with
71+
[profile.dist]
72+
inherits = "release"
73+
lto = "thin"

locust-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "locust-cli"
33
version = "0.1.0"
4+
repository = "https://github.com/maxmindlin/locust"
45
edition = "2021"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

locust-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "locust-core"
33
version = "0.1.0"
4+
repository = "https://github.com/maxmindlin/locust"
45
edition = "2021"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

0 commit comments

Comments
 (0)