Skip to content

Commit 73e62fa

Browse files
Add minimal cargo-dist settings
1 parent 38e27f2 commit 73e62fa

File tree

3 files changed

+3351
-481
lines changed

3 files changed

+3351
-481
lines changed

.github/workflows/release.yml

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

0 commit comments

Comments
 (0)