Skip to content

Commit

Permalink
feat: add cargo-multivers GitHub action
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnychevalier committed Dec 27, 2023
1 parent c07656d commit 5c266e3
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/action/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

ARGS="${INPUT_OTHER_ARGS:-}"
if [[ -n "${INPUT_MANIFEST_PATH:-}" ]]; then
ARGS+=" --manifest-path ${INPUT_MANIFEST_PATH}"
fi
if [[ -n "${INPUT_TARGET:-}" ]]; then
ARGS+=" --target ${INPUT_TARGET}"
fi
if [[ -n "${INPUT_RUNNER_VERSION:-}" ]]; then
ARGS+=" --runner-version ${INPUT_RUNNER_VERSION}"
fi
if [[ -n "${INPUT_PROFILE:-}" ]]; then
ARGS+=" --profile ${INPUT_PROFILE}"
fi
if [[ -n "${INPUT_OUT_DIR:-}" ]]; then
ARGS+=" --out-dir ${INPUT_OUT_DIR}"
fi

cargo multivers ${ARGS} -- ${INPUT_BUILD_ARGS:-}
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- "!/LICENSE-*"
push:
branches:
- main
- '**'
paths:
- '**'
- '!/*.md'
Expand Down Expand Up @@ -98,3 +98,17 @@ jobs:
- uses: EmbarkStudios/cargo-deny-action@v1
with:
rust-version: "1.74.0"
test-action:
name: Test cargo-multivers action
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust nightly
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
- uses: ./
with:
manifest_path: tests/test-argv/Cargo.toml
version: main
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

### Added

- A GitHub Action `cargo-multivers` is available to help build a binary in your CI and make it available during a release.
- `--out-dir` to copy the final binaries to a directory.

### Fixed
Expand Down
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,64 @@ For instance, you can add to your `Cargo.toml` the following section if you buil
cpus = ["x86-64", "x86-64-v2", "x86-64-v3", "x86-64-v4"]
```

## GitHub Actions Integration

If you want to publish an optimized portable binary built by `cargo-multivers` when releasing a new version of your project,
you can use the `cargo-multivers` GitHub Action.
To do that you need to have a Rust nightly toolchain
and add a step to your job:

```yaml
- uses: ronnychevalier/cargo-multivers@main
```
For example, this can look like:
```yaml
jobs:
cargo-multivers-build:
name: Build with cargo-multivers
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust nightly
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
- uses: ronnychevalier/cargo-multivers@main
with:
manifest_path: path/to-your/Cargo.toml
- name: Upload release archive
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: the-name-of-your-binary.exe

... [other config fields]
```

### Inputs

You can set two types of inputs.
The ones that are related to how `cargo-multivers` is installed:

| Name | Description | Required | Default |
|----------------|-------------------------------------------------|----------|----------------------------------------------------|
| version | Version of cargo-multivers to use (e.g., 0.7.0) | false | Latest published version on [crates.io][crates.io] |

And the ones that are related to the arguments given to `cargo multivers` (e.g., `target` configures the `--target` option):

| Name | Description | Required | Default |
|----------------|-------------------------------------------------|----------|----------------------------------------------------|
| manifest_path | Path to Cargo.toml | false | |
| target | Build for the target triple | false | |
| out_dir | Copy final artifacts to this directory | true | . |
| profile | Build artifacts with the specified profile | false | |
| runner_version | Specify the version of the runner to use | false | |
| other_args | Other arguments given to cargo multivers | false | |
| build_args | Arguments given to cargo build | false | |

## Related Work

- If you want to apply this approach only at the function level, take a look at the [multiversion](https://crates.io/crates/multiversion) crate.
Expand Down
65 changes: 65 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: "cargo-multivers"
description: "Build multiple versions of the same binary, each with a different CPU features set, merged into a single portable optimized binary"
branding:
icon: "cpu"
color: "orange"

inputs:
version:
description: "Version of cargo-multivers to use (e.g., 0.7.0)"
required: false
manifest_path:
description: "Path to Cargo.toml"
required: false
target:
description: "Build for the target triple"
required: false
out_dir:
description: "Copy final artifacts to this directory"
required: true
default: "."
profile:
description: "Build artifacts with the specified profile (default: release)"
required: false
runner_version:
description: "Specify the version of the runner to use"
required: false
other_args:
description: "Other arguments given to cargo multivers"
required: false
build_args:
description: "Arguments given to cargo build"
required: false

runs:
using: 'composite'
steps:
- name: Install cargo-multivers (git main)
uses: baptiste0928/cargo-install@v2
with:
crate: cargo-multivers
git: https://github.com/ronnychevalier/cargo-multivers
branch: main
if: "${{ inputs.version == 'main' }}"
- name: Install cargo-multivers (version)
uses: baptiste0928/cargo-install@v2
with:
crate: cargo-multivers
version: ${{ inputs.version }}
if: "${{ inputs.version != '' && inputs.version != 'main' }}"
- name: Install cargo-multivers (latest)
uses: baptiste0928/cargo-install@v2
with:
crate: cargo-multivers
if: "${{ inputs.version == '' }}"
- id: cargo-multivers
run: $GITHUB_ACTION_PATH/.github/action/entrypoint.sh
shell: bash
env:
INPUT_MANIFEST_PATH: ${{ inputs.manifest_path }}
INPUT_TARGET: ${{ inputs.target }}
INPUT_OUT_DIR: ${{ inputs.out_dir }}
INPUT_PROFILE: ${{ inputs.profile }}
INPUT_RUNNER_VERSION: ${{ inputs.runner_version }}
INPUT_OTHER_ARGS: ${{ inputs.other_args }}
INPUT_BUILD_ARGS: ${{ inputs.build_args }}

0 comments on commit 5c266e3

Please sign in to comment.