Skip to content

Commit 1769bef

Browse files
authored
Merge pull request #129 from jwodder/dist
Set up `cargo-dist` etc.
2 parents 9bbca07 + 79992ad commit 1769bef

File tree

8 files changed

+37562
-3
lines changed

8 files changed

+37562
-3
lines changed

.github/workflows/release.yml

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

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,30 @@ jobs:
178178
- name: Check formatting
179179
run: cargo fmt --check
180180

181+
deny:
182+
runs-on: ubuntu-latest
183+
steps:
184+
- name: Check out repository
185+
uses: actions/checkout@v4
186+
187+
- name: Run `cargo deny check`
188+
uses: EmbarkStudios/cargo-deny-action@v2
189+
190+
thirdparty:
191+
runs-on: ubuntu-latest
192+
steps:
193+
- name: Check out repository
194+
uses: actions/checkout@v4
195+
196+
- name: Install cargo-bundle-licenses
197+
uses: taiki-e/cache-cargo-install-action@v2
198+
with:
199+
tool: cargo-bundle-licenses
200+
locked: false # Ensure we get the latest version of the spdx crate
201+
202+
- name: Check that third party licenses haven't changed
203+
run: |
204+
cargo bundle-licenses --check-previous \
205+
-f toml -o CI.toml -p THIRDPARTY.toml
206+
181207
# vim:set et sts=2:

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ repos:
33
rev: v4.4.0
44
hooks:
55
- id: check-added-large-files
6+
exclude: THIRDPARTY.toml
67
- id: check-json
78
- id: check-toml
89
- id: check-yaml
910
- id: end-of-file-fixer
1011
- id: trailing-whitespace
12+
exclude: THIRDPARTY.toml
1113

1214
- repo: https://github.com/doublify/pre-commit-rust
1315
rev: v1.0

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ rustls = [
4949
"reqwest/rustls-tls-webpki-roots",
5050
]
5151

52+
[profile.dist]
53+
inherits = "release"
54+
lto = "thin"
55+
5256
[lints.rust]
5357
# Lint groups:
5458
deprecated_safe = { level = "deny", priority = -1 }

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,28 @@ find the Python version at <https://github.com/jwodder/demagnetize>.
3636
Installation
3737
============
3838

39-
In order to install `demagnetize`, you first need to have [Rust and Cargo
40-
installed](https://www.rust-lang.org/tools/install). You can then build the
41-
latest release of `demagnetize` and install it in `~/.cargo/bin` by running:
39+
Release Assets
40+
--------------
41+
42+
Prebuilt binaries for the most common platforms are available as GitHub release
43+
assets. [The page for the latest
44+
release](https://github.com/jwodder/demagnetize/releases/latest) lists these
45+
under "Assets", along with installer scripts for both Unix-like systems and
46+
Windows.
47+
48+
As an alternative to the installer scripts, if you have
49+
[`cargo-binstall`](https://github.com/cargo-bins/cargo-binstall) on your
50+
system, you can use it to download & install the appropriate release asset for
51+
your system for the latest version of `demagnetize` by running `cargo binstall
52+
demagnetize`.
53+
54+
Installing from Source
55+
----------------------
56+
57+
If you have [Rust and Cargo
58+
installed](https://www.rust-lang.org/tools/install), you can build the latest
59+
release of `demagnetize` from source and install it in `~/.cargo/bin` by
60+
running:
4261

4362
cargo install demagnetize
4463

@@ -59,6 +78,8 @@ latest release of `demagnetize` and install it in `~/.cargo/bin` by running:
5978
When selecting this feature, be sure to also supply the
6079
`--no-default-features` option in order to disable `native-tls`.
6180

81+
- The release assets are built using this feature.
82+
6283

6384
Usage
6485
=====

0 commit comments

Comments
 (0)