Skip to content

Instantly share code, notes, and snippets.

@folex
Created December 31, 2020 07:44
Show Gist options
  • Save folex/0d8c8b99d29633b863b887bf31f28921 to your computer and use it in GitHub Desktop.
Save folex/0d8c8b99d29633b863b887bf31f28921 to your computer and use it in GitHub Desktop.
Github Actions Publish a crate from a branch
name: "publish-branch"
on:
push:
branches-ignore:
- "master"
jobs:
npm-publish:
name: "Publish branch"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
### Extract branch name
- name: Extract branch name
if: github.event_name != 'pull_request'
run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
id: extract_branch
- name: Extract branch name
if: github.event_name == 'pull_request'
run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v2
### Prepare cargo & toolchains
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
~/.cargo/bin
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Rust toolchain with wasm32-unknown-unknown
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
profile: minimal
override: true
- uses: actions-rs/cargo@v1
with:
toolchain: nightly
command: update
args: --aggressive
### Calculate FINAL_VERSION
- name: Install jq & sponge
run: sudo apt-get update && sudo apt-get --yes --force-yes install jq moreutils
- name: Install cargo-show cargo-workspaces toml-cli
run: cargo install cargo-show cargo-workspaces toml-cli || true
- name: Calculate the version
run: |
set -euo pipefail
set -x
# install semver and add it to PATH
yarn global add semver
PATH="$(yarn global bin):$PATH"
# sanitize branch name so it can be used as a semver suffix (replace [^0-9a-zA-Z-] with hyphen)
SANITIZED_BRANCH="$(echo -n "${{ env.BRANCH_NAME }}" | tr -C '[:alnum:]-' -)"
CARGO_TOML="Cargo.toml"
# get package name from Cargo.toml
PKG_NAME="$(toml get "$CARGO_TOML" package.name | tr -d \")"
### CRATES.IO
# Define accumulator array variable
ALL_CRATES_VERSIONS=()
# For all crates in the workspace gather all versions, filtered by branch
for CRATE_NAME in $(cargo ws list)
do
CRATE_VERSIONS=$(cargo show --json "$CRATE_NAME")
CRATE_VERSIONS_FILTERED=$(echo $CRATE_VERSIONS | jq -r ".versions[] | .num | select(contains(\"$SANITIZED_BRANCH\"))")
ALL_CRATES_VERSIONS+=($CRATE_VERSIONS_FILTERED)
done
# Take the highest version of all
MAX_CRATES_VERSION="$(semver -p ${ALL_CRATES_VERSIONS[@]} | tail -n1 || true)"
# Increment prerelease part of the highest version
PRERELEASE_CRATE_VERSION="$(semver --increment prerelease --preid "$SANITIZED_BRANCH" "${MAX_CRATES_VERSION}" || true)"
### LOCAL
### (NOTE: the following code assumes that local versions do not contain prerelease suffix; existing suffix will be ignored)
# take local Rust version of the "root" crate
LOCAL_RUST_VERSION="$(toml get "$CARGO_TOML" package.version | tr -d \")"
LOCAL_RUST_PRERELEASE_VERSION="$(semver --increment prerelease --preid "$SANITIZED_BRANCH" "${LOCAL_RUST_VERSION}-0")" # added '-0' here to avoid semver erroneously increment patch octet. Any suffix works, '-0' is chosen deliberately.
# take the highest version
MAX_VERSION="$(semver "$PRERELEASE_CRATE_VERSION" "$LOCAL_RUST_PRERELEASE_VERSION" | tail -n1)"
echo "FINAL_VERSION=$MAX_VERSION" | tee -a $GITHUB_ENV
echo "PKG_NAME=$PKG_NAME" | tee -a $GITHUB_ENV
### === Rust package release ===
- name: Login to crates.io
run: cargo login ${{ secrets.CRATES_IO_TOKEN }}
- name: Publish to crates.io
run: cargo ws publish --no-git-commit --allow-dirty --yes custom "${{ env.FINAL_VERSION }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment