Skip to content

Commit 9b7d1bd

Browse files
committed
cargo-distify
1 parent 5f48287 commit 9b7d1bd

File tree

3 files changed

+312
-0
lines changed

3 files changed

+312
-0
lines changed

.github/workflows/release.yml

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ name = "sl"
44
version = "0.1.0"
55
authors = [ "florian.gilcher@asquera.de" ]
66

7+
repository = "https://github.com/skade/rust-sl"
8+
79
[[bin]]
810

911
name = "sl"
@@ -18,3 +20,27 @@ path = "src/sl.rs"
1820
getopts = "0.2"
1921
libc = "0.2"
2022
ncurses = "5.99"
23+
24+
# The profile that 'cargo dist' will build with
25+
[profile.dist]
26+
inherits = "release"
27+
lto = "thin"
28+
29+
# Config for 'cargo dist'
30+
[workspace.metadata.dist]
31+
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
32+
cargo-dist-version = "0.7.2"
33+
# CI backends to support
34+
ci = ["github"]
35+
# The installers to generate for each app
36+
installers = ["shell", "npm", "homebrew"]
37+
# Target platforms to build apps for (Rust target-triple syntax)
38+
targets = ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-musl"]
39+
# The archive format to use for windows builds (defaults .zip)
40+
windows-archive = ".tar.gz"
41+
# The archive format to use for non-windows builds (defaults .tar.xz)
42+
unix-archive = ".tar.gz"
43+
# Publish jobs to run in CI
44+
pr-run-mode = "plan"
45+
# Where to host releases
46+
hosting = ["axodotdev", "github"]

0 commit comments

Comments
 (0)