Skip to content

Commit 90850bd

Browse files
committed
Set up cargo-dist.
1 parent 565570b commit 90850bd

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed

.github/workflows/release.yml

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
# Copyright 2022-2024, 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.
13+
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:
45+
46+
jobs:
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.13.1/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') }}
90+
strategy:
91+
fail-fast: false
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
107+
steps:
108+
- uses: actions/checkout@v4
109+
with:
110+
submodules: recursive
111+
- uses: swatinem/rust-cache@v2
112+
with:
113+
key: ${{ join(matrix.targets, '-') }}
114+
- name: Install cargo-dist
115+
run: ${{ matrix.install_dist }}
116+
# Get the dist-manifest
117+
- name: Fetch local artifacts
118+
uses: actions/download-artifact@v4
119+
with:
120+
pattern: artifacts-*
121+
path: target/distrib/
122+
merge-multiple: true
123+
- name: Install dependencies
124+
run: |
125+
${{ matrix.packages_install }}
126+
- name: Build artifacts
127+
run: |
128+
# Actually do builds and make zips and whatnot
129+
cargo dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json
130+
echo "cargo dist ran successfully"
131+
- id: cargo-dist
132+
name: Post-build
133+
# We force bash here just because github makes it really hard to get values up
134+
# to "real" actions without writing to env-vars, and writing to env-vars has
135+
# inconsistent syntax between shell and powershell.
136+
shell: bash
137+
run: |
138+
# Parse out what we just built and upload it to scratch storage
139+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
140+
jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT"
141+
echo "EOF" >> "$GITHUB_OUTPUT"
142+
143+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
144+
- name: "Upload artifacts"
145+
uses: actions/upload-artifact@v4
146+
with:
147+
name: artifacts-build-local-${{ join(matrix.targets, '_') }}
148+
path: |
149+
${{ steps.cargo-dist.outputs.paths }}
150+
${{ env.BUILD_MANIFEST_NAME }}
151+
152+
# Build and package all the platform-agnostic(ish) things
153+
build-global-artifacts:
154+
needs:
155+
- plan
156+
- build-local-artifacts
157+
runs-on: "ubuntu-20.04"
158+
env:
159+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
160+
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
161+
steps:
162+
- uses: actions/checkout@v4
163+
with:
164+
submodules: recursive
165+
- name: Install cargo-dist
166+
shell: bash
167+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.13.1/cargo-dist-installer.sh | sh"
168+
# Get all the local artifacts for the global tasks to use (for e.g. checksums)
169+
- name: Fetch local artifacts
170+
uses: actions/download-artifact@v4
171+
with:
172+
pattern: artifacts-*
173+
path: target/distrib/
174+
merge-multiple: true
175+
- id: cargo-dist
176+
shell: bash
177+
run: |
178+
cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json
179+
echo "cargo dist ran successfully"
180+
181+
# Parse out what we just built and upload it to scratch storage
182+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
183+
jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT"
184+
echo "EOF" >> "$GITHUB_OUTPUT"
185+
186+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
187+
- name: "Upload artifacts"
188+
uses: actions/upload-artifact@v4
189+
with:
190+
name: artifacts-build-global
191+
path: |
192+
${{ steps.cargo-dist.outputs.paths }}
193+
${{ env.BUILD_MANIFEST_NAME }}
194+
# Determines if we should publish/announce
195+
host:
196+
needs:
197+
- plan
198+
- build-local-artifacts
199+
- build-global-artifacts
200+
# Only run if we're "publishing", and only if local and global didn't fail (skipped is fine)
201+
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') }}
202+
env:
203+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
204+
runs-on: "ubuntu-20.04"
205+
outputs:
206+
val: ${{ steps.host.outputs.manifest }}
207+
steps:
208+
- uses: actions/checkout@v4
209+
with:
210+
submodules: recursive
211+
- name: Install cargo-dist
212+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.13.1/cargo-dist-installer.sh | sh"
213+
# Fetch artifacts from scratch-storage
214+
- name: Fetch artifacts
215+
uses: actions/download-artifact@v4
216+
with:
217+
pattern: artifacts-*
218+
path: target/distrib/
219+
merge-multiple: true
220+
# This is a harmless no-op for GitHub Releases, hosting for that happens in "announce"
221+
- id: host
222+
shell: bash
223+
run: |
224+
cargo dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json
225+
echo "artifacts uploaded and released successfully"
226+
cat dist-manifest.json
227+
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
228+
- name: "Upload dist-manifest.json"
229+
uses: actions/upload-artifact@v4
230+
with:
231+
# Overwrite the previous copy
232+
name: artifacts-dist-manifest
233+
path: dist-manifest.json
234+
235+
# Create a GitHub Release while uploading all files to it
236+
announce:
237+
needs:
238+
- plan
239+
- host
240+
# use "always() && ..." to allow us to wait for all publish jobs while
241+
# still allowing individual publish jobs to skip themselves (for prereleases).
242+
# "host" however must run to completion, no skipping allowed!
243+
if: ${{ always() && needs.host.result == 'success' }}
244+
runs-on: "ubuntu-20.04"
245+
env:
246+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
247+
steps:
248+
- uses: actions/checkout@v4
249+
with:
250+
submodules: recursive
251+
- name: "Download GitHub Artifacts"
252+
uses: actions/download-artifact@v4
253+
with:
254+
pattern: artifacts-*
255+
path: artifacts
256+
merge-multiple: true
257+
- name: Cleanup
258+
run: |
259+
# Remove the granular manifests
260+
rm -f artifacts/*-dist-manifest.json
261+
- name: Create GitHub Release
262+
uses: ncipollo/release-action@v1
263+
with:
264+
tag: ${{ needs.plan.outputs.tag }}
265+
name: ${{ fromJson(needs.host.outputs.val).announcement_title }}
266+
body: ${{ fromJson(needs.host.outputs.val).announcement_github_body }}
267+
prerelease: ${{ fromJson(needs.host.outputs.val).announcement_is_prerelease }}
268+
artifacts: "artifacts/*"

Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@ members = [
1313
]
1414
resolver = "2"
1515
default-members = ["samply"]
16+
17+
# Config for 'cargo dist'
18+
[workspace.metadata.dist]
19+
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
20+
cargo-dist-version = "0.13.1"
21+
# CI backends to support
22+
ci = ["github"]
23+
# The installers to generate for each app
24+
installers = ["shell", "powershell"]
25+
# Target platforms to build apps for (Rust target-triple syntax)
26+
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
27+
# Publish jobs to run in CI
28+
pr-run-mode = "plan"
29+
# Whether to install an updater program
30+
install-updater = false
31+
32+
# The profile that 'cargo dist' will build with
33+
[profile.dist]
34+
inherits = "release"
35+
lto = "thin"

0 commit comments

Comments
 (0)