Skip to content

Commit

Permalink
chore: set up cargo-dist
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Oct 6, 2023
1 parent 684013d commit d790ca4
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 168 deletions.
304 changes: 136 additions & 168 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,188 +1,156 @@
# Copyright 2022-2023, axodotdev
# SPDX-License-Identifier: MIT or Apache-2.0
#
# (file taken from [ripgrep](https://github.com/BurntSushi/ripgrep/raw/master/.github/workflows/release.yml)
# CI that:
#
# The way this works is the following:
# * checks for a Git Tag that looks like a release
# * builds artifacts with cargo-dist (archives, installers, hashes)
# * uploads those artifacts to temporary workflow zip
# * on success, uploads the artifacts to a Github Release™
#
# The create-release job runs purely to initialize the GitHub release itself
# and to output upload_url for the following job.
# Note that the Github Release™ will be created with a generated
# title/body based on your changelogs.
name: Release

permissions:
contents: write

# This task will run whenever you push a git tag that looks like a version
# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc.
# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
# must be a Cargo-style SemVer Version (must have at least major.minor.patch).
#
# The build-release job runs only once create-release is finished. It gets the
# release upload URL from create-release job outputs, then builds the release
# executables for each supported platform and attaches them as release assets
# to the previously created release.
# If PACKAGE_NAME is specified, then the release will be for that
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
#
# The key here is that we create the release only once.
# If PACKAGE_NAME isn't specified, then the release will be for all
# (cargo-dist-able) packages in the workspace with that version (this mode is
# intended for workspaces with only one dist-able package, or with all dist-able
# packages versioned/released in lockstep).
#
# Reference:
# https://eugene-babichenko.github.io/blog/2020/05/09/github-actions-cross-platform-auto-releases/

name: release
# If you push multiple tags at once, separate instances of this workflow will
# spin up, creating an independent Github Release™ for each one. However Github
# will hard limit this to 3 tags per commit, as it will assume more tags is a
# mistake.
#
# If there's a prerelease-style suffix to the version, then the Github Release™
# will be marked as a prerelease.
on:
push:
# Enable when testing release infrastructure on a branch.
#branches:
#- release_test
tags:
- "[0-9]+.[0-9]+.[0-9]+"
- '**[0-9]+.[0-9]+.[0-9]+*'
pull_request:

jobs:
create-release:
name: create-release
# Run 'cargo dist plan' to determine what tasks we need to do
plan:
runs-on: ubuntu-latest
#env:
# Set to force version number, e.g., when no tag exists.
#LAZE_VERSION: TEST-0.0.0
outputs:
upload_url: ${{ steps.release.outputs.upload_url }}
laze_version: ${{ env.LAZE_VERSION }}
val: ${{ steps.plan.outputs.manifest }}
tag: ${{ !github.event.pull_request && github.ref_name || '' }}
tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }}
publishing: ${{ !github.event.pull_request }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Get the release version from the tag
shell: bash
if: env.LAZE_VERSION == ''
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install cargo-dist
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.3.1/cargo-dist-installer.sh | sh"
- id: plan
run: |
# Apparently, this is the right way to get a tag name. Really?
#
# See: https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027
echo "LAZE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
echo "version is: ${{ env.LAZE_VERSION }}"
- name: Create GitHub release
id: release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
cargo dist plan ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} --output-format=json > dist-manifest.json
echo "cargo dist plan ran successfully"
cat dist-manifest.json
echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT"
- name: "Upload dist-manifest.json"
uses: actions/upload-artifact@v3
with:
tag_name: ${{ env.LAZE_VERSION }}
release_name: ${{ env.LAZE_VERSION }}
name: artifacts
path: dist-manifest.json

build-release:
name: build-release
needs: ['create-release']
runs-on: ${{ matrix.os }}
env:
# For some builds, we use cross to test on 32-bit and big-endian
# systems.
CARGO: cargo
# When CARGO is set to CROSS, this is set to `--target matrix.target`.
TARGET_FLAGS: ""
# When CARGO is set to CROSS, TARGET_DIR includes matrix.target.
TARGET_DIR: ./target
# Emit backtraces on panics.
RUST_BACKTRACE: 1
# use clang for mimalloc
CC_arm_unknown_linux_musleabihf: clang
# Build and packages all the platform-specific things
upload-local-artifacts:
# Let the initial task tell us to not run (currently very blunt)
needs: plan
if: ${{ fromJson(needs.plan.outputs.val).releases != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }}
strategy:
matrix:
#build: [linux, linux-arm, macos, win-msvc, win-gnu, win32-msvc]
build: [linux, linux-arm, macos]
include:
- build: linux
os: ubuntu-22.04
rust: nightly
target: x86_64-unknown-linux-musl
- build: linux-arm
os: ubuntu-22.04
rust: nightly
target: armv7-unknown-linux-musleabihf
- build: macos
os: macos-latest
rust: nightly
target: x86_64-apple-darwin
# - build: win-msvc
# os: windows-2019
# rust: nightly
# target: x86_64-pc-windows-msvc
# - build: win-gnu
# os: windows-2019
# rust: nightly-x86_64-gnu
# target: x86_64-pc-windows-gnu
# - build: win32-msvc
# os: windows-2019
# rust: nightly
# target: i686-pc-windows-msvc

fail-fast: false
# Target platforms/runners are computed by cargo-dist in create-release.
# Each member of the matrix has the following arguments:
#
# - runner: the github runner
# - dist-args: cli flags to pass to cargo dist
# - install-dist: expression to run to install cargo-dist on the runner
#
# Typically there will be:
# - 1 "global" task that builds universal installers
# - N "local" tasks that build each platform's binaries and platform-specific installers
matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }}
runs-on: ${{ matrix.runner }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 1

- name: Install packages (Ubuntu)
if: matrix.os == 'ubuntu-18.04'
run: |
ci/ubuntu-install-packages
- name: Install packages (macOS)
if: matrix.os == 'macos-latest'
run: |
ci/macos-install-packages
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
profile: minimal
override: true
target: ${{ matrix.target }}

- name: Use Cross
shell: bash
run: |
cargo install cross
echo "CARGO=cross" >> $GITHUB_ENV
echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV
echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV
- name: Show command used for Cargo
run: |
echo "cargo command is: ${{ env.CARGO }}"
echo "target flag is: ${{ env.TARGET_FLAGS }}"
echo "target dir is: ${{ env.TARGET_DIR }}"
- name: Build release binary
run: ${{ env.CARGO }} build --verbose --release ${{ env.TARGET_FLAGS }}

- name: Strip release binary (linux and macos)
if: matrix.build == 'linux' || matrix.build == 'macos'
run: strip "target/${{ matrix.target }}/release/laze"

- name: Strip release binary (arm)
if: matrix.build == 'linux-arm'
run: |
docker run --rm -v \
"$PWD/target:/target:Z" \
rustembedded/cross:armv7-unknown-linux-musleabihf \
arm-linux-musleabihf-strip \
/target/armv7-unknown-linux-musleabihf/release/laze
- name: Build archive
shell: bash
run: |
outdir="$(ci/cargo-out-dir "${{ env.TARGET_DIR }}")"
staging="laze-${{ needs.create-release.outputs.laze_version }}-${{ matrix.target }}"
mkdir -p "$staging"/doc
cp {README.md,LICENSE} "$staging/"
cp CHANGELOG.md "$staging/doc/"
#cp "$outdir"/{rg.bash,rg.fish,_rg.ps1} "$staging/complete/"
#cp complete/_rg "$staging/complete/"
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: swatinem/rust-cache@v2
- name: Install cargo-dist
run: ${{ matrix.install_dist }}
- name: Build artifacts
run: |
# Actually do builds and make zips and whatnot
cargo dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json ${{ matrix.dist_args }} > dist-manifest.json
echo "cargo dist ran successfully"
- id: cargo-dist
name: Post-build
# We force bash here just because github makes it really hard to get values up
# to "real" actions without writing to env-vars, and writing to env-vars has
# inconsistent syntax between shell and powershell.
shell: bash
run: |
# Parse out what we just built and upload it to the Github Release™
echo "paths<<EOF" >> "$GITHUB_OUTPUT"
jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
- name: "Upload artifacts"
uses: actions/upload-artifact@v3
with:
name: artifacts
path: ${{ steps.cargo-dist.outputs.paths }}

if [ "${{ matrix.os }}" = "windows-2019" ]; then
cp "target/${{ matrix.target }}/release/laze.exe" "$staging/"
7z a "$staging.zip" "$staging"
echo "ASSET=$staging.zip" >> $GITHUB_ENV
else
# The man page is only generated on Unix systems. ¯\_(ツ)_/¯
#cp "$outdir"/laze.1 "$staging/doc/"
cp "target/${{ matrix.target }}/release/laze" "$staging/"
tar czf "$staging.tar.gz" "$staging"
echo "ASSET=$staging.tar.gz" >> $GITHUB_ENV
fi
should-publish:
needs:
- plan
- upload-local-artifacts
if: ${{ needs.plan.outputs.publishing == 'true' }}
runs-on: ubuntu-latest
steps:
- name: print tag
run: echo "ok we're publishing!"

- name: Upload release archive
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.create-release.outputs.upload_url }}
asset_path: ${{ env.ASSET }}
asset_name: ${{ env.ASSET }}
asset_content_type: application/octet-stream
# Create a Github Release with all the results once everything is done,
publish-release:
needs: [plan, should-publish]
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: "Download artifacts"
uses: actions/download-artifact@v3
with:
name: artifacts
path: artifacts
- name: Create Release
uses: ncipollo/release-action@v1
with:
tag: ${{ needs.plan.outputs.tag }}
name: ${{ fromJson(needs.plan.outputs.val).announcement_title }}
body: ${{ fromJson(needs.plan.outputs.val).announcement_github_body }}
prerelease: ${{ fromJson(needs.plan.outputs.val).announcement_is_prerelease }}
artifacts: "artifacts/*"
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ lto = "fat"
incremental = false
codegen-units = 1

# The profile that 'cargo dist' will build with
[profile.dist]
inherits = "release"
lto = "thin"

# Config for 'cargo dist'
[workspace.metadata.dist]
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
cargo-dist-version = "0.3.1"
# CI backends to support
ci = ["github"]
# The installers to generate for each app
installers = []
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-pc-windows-msvc"]
# Publish jobs to run in CI
pr-run-mode = "upload"

[package.metadata.release]
sign-commit = true
sign-tag = true
Expand Down

0 comments on commit d790ca4

Please sign in to comment.