Skip to content

Commit 4326983

Browse files
committed
Use cargo-dist to release the Rust CLI
1 parent c8d82c8 commit 4326983

File tree

9 files changed

+395
-51
lines changed

9 files changed

+395
-51
lines changed

.github/workflows/cli-release.yml

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
# This file was autogenerated by dist: https://github.com/astral-sh/cargo-dist
2+
#
3+
# Copyright 2022-2024, axodotdev
4+
# Copyright 2025 Astral Software Inc.
5+
# SPDX-License-Identifier: MIT or Apache-2.0
6+
#
7+
# CI that:
8+
#
9+
# * checks for a Git Tag that looks like a release
10+
# * builds artifacts with dist (archives, installers, hashes)
11+
# * uploads those artifacts to temporary workflow zip
12+
# * on success, uploads the artifacts to a GitHub Release
13+
#
14+
# Note that the GitHub Release will be created with a generated
15+
# title/body based on your changelogs.
16+
17+
name: Release
18+
permissions:
19+
"attestations": "write"
20+
"contents": "write"
21+
"id-token": "write"
22+
23+
# This task will run whenever you push a git tag that looks like a version
24+
# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc.
25+
# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where
26+
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
27+
# must be a Cargo-style SemVer Version (must have at least major.minor.patch).
28+
#
29+
# If PACKAGE_NAME is specified, then the announcement will be for that
30+
# package (erroring out if it doesn't have the given version or isn't dist-able).
31+
#
32+
# If PACKAGE_NAME isn't specified, then the announcement will be for all
33+
# (dist-able) packages in the workspace with that version (this mode is
34+
# intended for workspaces with only one dist-able package, or with all dist-able
35+
# packages versioned/released in lockstep).
36+
#
37+
# If you push multiple tags at once, separate instances of this workflow will
38+
# spin up, creating an independent announcement for each one. However, GitHub
39+
# will hard limit this to 3 tags per commit, as it will assume more tags is a
40+
# mistake.
41+
#
42+
# If there's a prerelease-style suffix to the version, then the release(s)
43+
# will be marked as a prerelease.
44+
on:
45+
pull_request:
46+
push:
47+
tags:
48+
- 'cli**[0-9]+.[0-9]+.[0-9]+*'
49+
50+
jobs:
51+
# Run 'dist plan' (or host) to determine what tasks we need to do
52+
plan:
53+
runs-on: "ubuntu-latest"
54+
outputs:
55+
val: ${{ steps.plan.outputs.manifest }}
56+
tag: ${{ !github.event.pull_request && github.ref_name || '' }}
57+
tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }}
58+
publishing: ${{ !github.event.pull_request }}
59+
env:
60+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
steps:
62+
- uses: actions/checkout@v4
63+
with:
64+
persist-credentials: false
65+
submodules: recursive
66+
- name: Install dist
67+
# we specify bash to get pipefail; it guards against the `curl` command
68+
# failing. otherwise `sh` won't catch that `curl` returned non-0
69+
shell: bash
70+
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/astral-sh/cargo-dist/releases/download/v0.28.7-prerelease.1/cargo-dist-installer.sh | sh"
71+
- name: Cache dist
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: cargo-dist-cache
75+
path: ~/.cargo/bin/dist
76+
# sure would be cool if github gave us proper conditionals...
77+
# so here's a doubly-nested ternary-via-truthiness to try to provide the best possible
78+
# functionality based on whether this is a pull_request, and whether it's from a fork.
79+
# (PRs run on the *source* but secrets are usually on the *target* -- that's *good*
80+
# but also really annoying to build CI around when it needs secrets to work right.)
81+
- id: plan
82+
run: |
83+
dist ${{ (!github.event.pull_request && format('host --steps=create --tag={0}', github.ref_name)) || 'plan' }} --output-format=json > plan-dist-manifest.json
84+
echo "dist ran successfully"
85+
cat plan-dist-manifest.json
86+
echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT"
87+
- name: "Upload dist-manifest.json"
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: artifacts-plan-dist-manifest
91+
path: plan-dist-manifest.json
92+
93+
# Build and packages all the platform-specific things
94+
build-local-artifacts:
95+
name: build-local-artifacts (${{ join(matrix.targets, ', ') }})
96+
# Let the initial task tell us to not run (currently very blunt)
97+
needs:
98+
- plan
99+
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') }}
100+
strategy:
101+
fail-fast: false
102+
# Target platforms/runners are computed by dist in create-release.
103+
# Each member of the matrix has the following arguments:
104+
#
105+
# - runner: the github runner
106+
# - dist-args: cli flags to pass to dist
107+
# - install-dist: expression to run to install dist on the runner
108+
#
109+
# Typically there will be:
110+
# - 1 "global" task that builds universal installers
111+
# - N "local" tasks that build each platform's binaries and platform-specific installers
112+
matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }}
113+
runs-on: ${{ matrix.runner }}
114+
container: ${{ matrix.container && matrix.container.image || null }}
115+
env:
116+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
117+
BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json
118+
steps:
119+
- name: enable windows longpaths
120+
run: |
121+
git config --global core.longpaths true
122+
- uses: actions/checkout@v4
123+
with:
124+
persist-credentials: false
125+
submodules: recursive
126+
- name: Install Rust non-interactively if not already installed
127+
if: ${{ matrix.container }}
128+
run: |
129+
if ! command -v cargo > /dev/null 2>&1; then
130+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
131+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
132+
fi
133+
- name: Install dist
134+
run: ${{ matrix.install_dist.run }}
135+
# Get the dist-manifest
136+
- name: Fetch local artifacts
137+
uses: actions/download-artifact@v4
138+
with:
139+
pattern: artifacts-*
140+
path: target/distrib/
141+
merge-multiple: true
142+
- name: Install dependencies
143+
run: |
144+
${{ matrix.packages_install }}
145+
- name: Build artifacts
146+
run: |
147+
# Actually do builds and make zips and whatnot
148+
dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json
149+
echo "dist ran successfully"
150+
- name: Attest
151+
uses: actions/attest-build-provenance@v2
152+
with:
153+
subject-path: "target/distrib/*${{ join(matrix.targets, ', ') }}*"
154+
- id: cargo-dist
155+
name: Post-build
156+
# We force bash here just because github makes it really hard to get values up
157+
# to "real" actions without writing to env-vars, and writing to env-vars has
158+
# inconsistent syntax between shell and powershell.
159+
shell: bash
160+
run: |
161+
# Parse out what we just built and upload it to scratch storage
162+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
163+
dist print-upload-files-from-manifest --manifest dist-manifest.json >> "$GITHUB_OUTPUT"
164+
echo "EOF" >> "$GITHUB_OUTPUT"
165+
166+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
167+
- name: "Upload artifacts"
168+
uses: actions/upload-artifact@v4
169+
with:
170+
name: artifacts-build-local-${{ join(matrix.targets, '_') }}
171+
path: |
172+
${{ steps.cargo-dist.outputs.paths }}
173+
${{ env.BUILD_MANIFEST_NAME }}
174+
175+
# Build and package all the platform-agnostic(ish) things
176+
build-global-artifacts:
177+
needs:
178+
- plan
179+
- build-local-artifacts
180+
runs-on: "ubuntu-latest"
181+
env:
182+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
183+
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
184+
steps:
185+
- uses: actions/checkout@v4
186+
with:
187+
persist-credentials: false
188+
submodules: recursive
189+
- name: Install cached dist
190+
uses: actions/download-artifact@v4
191+
with:
192+
name: cargo-dist-cache
193+
path: ~/.cargo/bin/
194+
- run: chmod +x ~/.cargo/bin/dist
195+
# Get all the local artifacts for the global tasks to use (for e.g. checksums)
196+
- name: Fetch local artifacts
197+
uses: actions/download-artifact@v4
198+
with:
199+
pattern: artifacts-*
200+
path: target/distrib/
201+
merge-multiple: true
202+
- id: cargo-dist
203+
shell: bash
204+
run: |
205+
dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json
206+
echo "dist ran successfully"
207+
208+
# Parse out what we just built and upload it to scratch storage
209+
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
210+
jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT"
211+
echo "EOF" >> "$GITHUB_OUTPUT"
212+
213+
cp dist-manifest.json "$BUILD_MANIFEST_NAME"
214+
- name: "Upload artifacts"
215+
uses: actions/upload-artifact@v4
216+
with:
217+
name: artifacts-build-global
218+
path: |
219+
${{ steps.cargo-dist.outputs.paths }}
220+
${{ env.BUILD_MANIFEST_NAME }}
221+
# Determines if we should publish/announce
222+
host:
223+
needs:
224+
- plan
225+
- build-local-artifacts
226+
- build-global-artifacts
227+
# Only run if we're "publishing", and only if local and global didn't fail (skipped is fine)
228+
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') }}
229+
env:
230+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
231+
runs-on: "ubuntu-latest"
232+
outputs:
233+
val: ${{ steps.host.outputs.manifest }}
234+
steps:
235+
- uses: actions/checkout@v4
236+
with:
237+
persist-credentials: false
238+
submodules: recursive
239+
- name: Install cached dist
240+
uses: actions/download-artifact@v4
241+
with:
242+
name: cargo-dist-cache
243+
path: ~/.cargo/bin/
244+
- run: chmod +x ~/.cargo/bin/dist
245+
# Fetch artifacts from scratch-storage
246+
- name: Fetch artifacts
247+
uses: actions/download-artifact@v4
248+
with:
249+
pattern: artifacts-*
250+
path: target/distrib/
251+
merge-multiple: true
252+
- id: host
253+
shell: bash
254+
run: |
255+
dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json
256+
echo "artifacts uploaded and released successfully"
257+
cat dist-manifest.json
258+
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
259+
- name: "Upload dist-manifest.json"
260+
uses: actions/upload-artifact@v4
261+
with:
262+
# Overwrite the previous copy
263+
name: artifacts-dist-manifest
264+
path: dist-manifest.json
265+
# Create a GitHub Release while uploading all files to it
266+
- name: "Download GitHub Artifacts"
267+
uses: actions/download-artifact@v4
268+
with:
269+
pattern: artifacts-*
270+
path: artifacts
271+
merge-multiple: true
272+
- name: Cleanup
273+
run: |
274+
# Remove the granular manifests
275+
rm -f artifacts/*-dist-manifest.json
276+
- name: Create GitHub Release
277+
env:
278+
PRERELEASE_FLAG: "${{ fromJson(steps.host.outputs.manifest).announcement_is_prerelease && '--prerelease' || '' }}"
279+
ANNOUNCEMENT_TITLE: "${{ fromJson(steps.host.outputs.manifest).announcement_title }}"
280+
ANNOUNCEMENT_BODY: "${{ fromJson(steps.host.outputs.manifest).announcement_github_body }}"
281+
RELEASE_COMMIT: "${{ github.sha }}"
282+
run: |
283+
# Write and read notes from a file to avoid quoting breaking things
284+
echo "$ANNOUNCEMENT_BODY" > $RUNNER_TEMP/notes.txt
285+
286+
gh release create "${{ needs.plan.outputs.tag }}" --target "$RELEASE_COMMIT" $PRERELEASE_FLAG --title "$ANNOUNCEMENT_TITLE" --notes-file "$RUNNER_TEMP/notes.txt" artifacts/*
287+
288+
announce:
289+
needs:
290+
- plan
291+
- host
292+
# use "always() && ..." to allow us to wait for all publish jobs while
293+
# still allowing individual publish jobs to skip themselves (for prereleases).
294+
# "host" however must run to completion, no skipping allowed!
295+
if: ${{ always() && needs.host.result == 'success' }}
296+
runs-on: "ubuntu-latest"
297+
env:
298+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
299+
steps:
300+
- uses: actions/checkout@v4
301+
with:
302+
persist-credentials: false
303+
submodules: recursive

.github/workflows/rust-package.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

dist-workspace.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[workspace]
2+
members = ["cargo:rust/cli"]
3+
4+
# Config for 'dist'
5+
[dist]
6+
# The preferred dist version to use in CI (Cargo.toml SemVer syntax)
7+
cargo-dist-version = "0.28.7-prerelease.1"
8+
# CI backends to support
9+
ci = "github"
10+
# The installers to generate for each app
11+
installers = ["shell", "powershell"]
12+
# Target platforms to build apps for (Rust target-triple syntax)
13+
targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
14+
# Path that installers should place binaries in
15+
install-path = "CARGO_HOME"
16+
# Whether to install an updater program
17+
install-updater = true
18+
# Generate and dist a source tarball
19+
source-tarball = false
20+
# Whether to auto-include files like READMEs, LICENSEs, and CHANGELOGs (default true)
21+
auto-includes = false
22+
# A prefix git tags must include for dist to care about them
23+
tag-namespace = "cli"
24+
# Whether +crt-static should be used on msvc
25+
msvc-crt-static = false
26+
# Whether to enable GitHub Attestations
27+
github-attestations = true
28+
29+
[dist.github-custom-runners]
30+
global = "ubuntu-latest"
31+
aarch64-unknown-linux-gnu = "ubuntu-24.04-arm"
32+
x86_64-unknown-linux-gnu = "ubuntu-latest"
33+
aarch64-apple-darwin = "macos-latest"
34+
x86_64-apple-darwin = "macos-13"
35+
x86_64-pc-windows-msvc = "windows-latest"

0 commit comments

Comments
 (0)