Skip to content

Merge pull request #20 from rajpalc7/dependabot #10

Merge pull request #20 from rajpalc7/dependabot

Merge pull request #20 from rajpalc7/dependabot #10

Workflow file for this run

# Copyright © 2023 Province of British Columbia
# https://digital.gov.bc.ca/digital-trust
name: Release
on:
push:
branches: [main]
permissions:
contents: write
jobs:
checks:
name: Check version & existing release
runs-on: ubuntu-latest
outputs:
current_version: ${{ steps.current_version.outputs.current_version }}
existing_release_info: ${{ steps.check_existing_release.outputs.release_info }}
existing_assets: ${{ steps.check_existing_release.outputs.release_assets }}
already_in_crates_io: ${{ steps.check_in_crates_io.outputs.already_in_crates_io != '' }}
defaults:
run:
working-directory: .
steps:
- uses: actions/checkout@v4
- name: Get current version
id: current_version
run: |
version="$(cargo -q metadata --no-deps \
| jq -r '.packages[] | select(.name == "indy-cli-rs") | .version')"
echo current_version=$version >> $GITHUB_OUTPUT
echo "$version"
shell: bash
- name: Check existing release
id: check_existing_release
run: |
release_info="$(curl -s https://api.github.com/repos/${{ github.repository }}/releases \
| jq '.[] | select(.name == "v${{ steps.current_version.outputs.current_version }}")')"
# Generate random delimiter as recommended in docs
# See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
delimiter="$(openssl rand -hex 8)"
echo "release_info<<${delimiter}" >> $GITHUB_OUTPUT
echo "$release_info" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
echo "existing release info: $release_info"
release_assets="$(echo "$release_info" | jq -c '[.assets[].name]')"
echo "release_assets=${release_assets}" >> $GITHUB_OUTPUT
echo "existing release assets: $release_assets"
shell: bash
- name: Check if already deployed to crates.io
id: check_in_crates_io
run: |
out="$(curl -s https://crates.io/api/v1/crates/indy-cli-rs | jq -r '.versions[] | .num' \
| grep '^${{ steps.current_version.outputs.current_version }}$')"
echo already_in_crates_io=$out >> $GITHUB_OUTPUT
echo "in crates.io check: $out"
shell: bash {0} # to opt-out of default fail-fast behavior
create_release:
name: Create release if needed
runs-on: ubuntu-latest
needs: checks
steps:
- name: Create GitHub Release
if: ${{ !needs.checks.outputs.existing_release_info }}
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ needs.checks.outputs.current_version }}
name: v${{ needs.checks.outputs.current_version }}
publish_crate:
name: Publish crate
# Enable this when all dependencies are published
if: ${{ false }}
runs-on: ubuntu-latest
needs: [ checks, create_release ]
defaults:
run:
working-directory: .
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Package and verify crate
id: build_crate
run: |
cargo package
# TODO
# - verify that it's not more than crates.io limit (10 MB)
# - explore whether we need to upload another artifact (without extension)
ls -la target/package
cargo package --list
asset_name="$(find target/package -name '*.crate' -printf '%f')"
echo asset_name=$asset_name >> $GITHUB_OUTPUT
shell: bash
- name: Upload crate to GitHub
if: ${{ !needs.checks.outputs.existing_assets || !contains(fromJSON(needs.checks.outputs.existing_assets), steps.build_crate.outputs.asset_name)}}
uses: svenstaro/upload-release-action@v2
with:
file: target/package/${{ steps.build_crate.outputs.asset_name }}
asset_name: ${{ steps.build_crate.outputs.asset_name }}
tag: v${{ needs.checks.outputs.current_version }}
release_name: v${{ needs.checks.outputs.current_version }}
- name: Publish to crates.io
if: ${{ needs.checks.outputs.already_in_crates_io == 'false' }}
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish
shell: bash
publish_assets:
name: Publish assets
needs: [ checks, create_release ]
strategy:
matrix:
include:
- arch: linux-x86_64
os: ubuntu-latest
artifact: indy-cli-rs
target: x86_64-unknown-linux-gnu
# using cross here to build against an older glibc for compatibility
use_cross: true
- arch: windows-x86_64
os: windows-latest
artifact: indy-cli-rs.exe
target: x86_64-pc-windows-msvc
- arch: darwin-x86_64
os: macos-11
artifact: indy-cli-rs
target: x86_64-apple-darwin
- arch: darwin-aarch64
os: macos-11
artifact: indy-cli-rs
target: aarch64-apple-darwin
# beta or nightly required for aarch64-apple-darwin target
toolchain: beta
defaults:
run:
working-directory: .
env:
asset_name: indy-cli-rs-${{ needs.checks.outputs.current_version }}-${{ matrix.arch }}.tar.gz
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.toolchain || 'stable' }}
targets: ${{ matrix.target }}
- name: Cache cargo resources
uses: Swatinem/rust-cache@v2
- name: Check existing release asset
id: check_existing
run: |
# GitHub actions do not support skipping subsequent job steps (see https://github.com/actions/runner/issues/662)
# So here we're using output value with separate conditions for every subsequent step
echo "asset_exists=${{ needs.checks.outputs.existing_assets && contains(fromJSON(needs.checks.outputs.existing_assets), env.asset_name) }}" >> $GITHUB_OUTPUT
shell: bash
- name: Build
if: ${{ steps.check_existing.outputs.asset_exists != 'true' && !matrix.use_cross }}
run: cargo build --target ${{ matrix.target }} --locked --release
- name: Build (cross)
if: ${{ steps.check_existing.outputs.asset_exists != 'true' && matrix.use_cross }}
run: |
cargo install --bins --git https://github.com/rust-embedded/cross --tag v0.2.4 cross
cross build --target ${{ matrix.target }} --locked --release
- name: Create release assets directory
if: ${{ steps.check_existing.outputs.asset_exists != 'true' }}
run: |
mkdir release-assets
cp target/${{ matrix.target }}/release/${{ matrix.artifact }} release-assets/
- name: Pack asset
if: ${{ steps.check_existing.outputs.asset_exists != 'true' }}
uses: a7ul/[email protected]
with:
command: c
cwd: ./release-assets
files: .
outPath: "indy-cli-rs.tar.gz"
- name: Upload asset to GitHub
if: ${{ steps.check_existing.outputs.asset_exists != 'true' }}
uses: svenstaro/upload-release-action@v2
with:
file: indy-cli-rs.tar.gz
tag: v${{ needs.checks.outputs.current_version }}
release_name: v${{ needs.checks.outputs.current_version }}
asset_name: ${{ env.asset_name }}