Skip to content

ci(release): publish to hex.pm automatically after GitHub release #5

ci(release): publish to hex.pm automatically after GitHub release

ci(release): publish to hex.pm automatically after GitHub release #5

Workflow file for this run

name: Release
on:
push:
tags: ["v*"]
permissions:
contents: write
jobs:
build_nif:
name: NIF ${{ matrix.job.target }}
runs-on: ${{ matrix.job.os }}
strategy:
fail-fast: false
matrix:
nif: ["2.16"]
job:
# Both Apple targets build on the native Apple Silicon runner;
# x86_64-apple-darwin is a cross-compile from arm64 using the
# stock rustup target, which is fine because no C deps of the
# NIF need native compilation at that target.
- { target: aarch64-apple-darwin, os: macos-14, lib-ext: dylib }
- { target: x86_64-apple-darwin, os: macos-14, lib-ext: dylib }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-22.04, lib-ext: so }
- { target: aarch64-unknown-linux-gnu, os: ubuntu-22.04-arm, lib-ext: so }
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
shell: bash
run: |
rustup toolchain install stable --profile minimal
rustup default stable
rustup target add ${{ matrix.job.target }}
- name: Extract project version
id: version
shell: bash
run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
- name: Compute NIF feature flag
id: feature
shell: bash
run: |
echo "flag=nif_version_$(echo ${{ matrix.nif }} | tr . _)" >> "$GITHUB_OUTPUT"
- name: Build NIF
shell: bash
working-directory: native/javex_nif
run: |
cargo build --release \
--target ${{ matrix.job.target }} \
--no-default-features \
--features ${{ steps.feature.outputs.flag }}
- name: Package artifact
id: package
shell: bash
run: |
set -euo pipefail
VERSION="${{ steps.version.outputs.version }}"
NIF="${{ matrix.nif }}"
TARGET="${{ matrix.job.target }}"
SRC_EXT="${{ matrix.job.lib-ext }}"
# rustler_precompiled expects the artifact to be named with
# a `.so` suffix in the tarball regardless of the host
# platform, because Rustler renames all NIF shared libraries
# to .so inside priv/native at load time.
NAME="libjavex_nif-v${VERSION}-nif-${NIF}-${TARGET}"
ARCHIVE="${NAME}.so.tar.gz"
SRC="native/javex_nif/target/${TARGET}/release/libjavex_nif.${SRC_EXT}"
STAGE="$(mktemp -d)"
cp "${SRC}" "${STAGE}/${NAME}.so"
tar -C "${STAGE}" -czf "${ARCHIVE}" "${NAME}.so"
echo "archive=${ARCHIVE}" >> "$GITHUB_OUTPUT"
echo "name=${ARCHIVE}" >> "$GITHUB_OUTPUT"
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.package.outputs.name }}
path: ${{ steps.package.outputs.archive }}
if-no-files-found: error
publish_release:
name: Publish GitHub Release
needs: build_nif
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install git-cliff
shell: bash
run: |
set -euo pipefail
VERSION="2.6.1"
URL="https://github.com/orhun/git-cliff/releases/download/v${VERSION}/git-cliff-${VERSION}-x86_64-unknown-linux-gnu.tar.gz"
curl -sSL "$URL" -o /tmp/git-cliff.tar.gz
mkdir -p /tmp/git-cliff && tar -xzf /tmp/git-cliff.tar.gz -C /tmp/git-cliff --strip-components 1
sudo install /tmp/git-cliff/git-cliff /usr/local/bin/git-cliff
git-cliff --version
- name: Generate changelog
shell: bash
run: git-cliff --config cliff.toml --latest --strip header > CHANGES.md
- name: Download NIF artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: Publish release
uses: softprops/action-gh-release@v2
with:
body_path: CHANGES.md
files: artifacts/*
fail_on_unmatched_files: true
tag_name: ${{ github.ref_name }}
name: ${{ github.ref_name }}
draft: false
prerelease: false
publish_hex:
name: Publish to hex.pm
needs: publish_release
runs-on: ubuntu-22.04
env:
MIX_ENV: prod
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
steps:
- uses: actions/checkout@v4
- uses: jdx/mise-action@v2
with:
experimental: true
- name: Install hex / rebar
run: |
mix local.hex --force
mix local.rebar --force
- name: Download NIF artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: Generate checksum file from real artifacts
shell: bash
run: |
set -euo pipefail
OUT="checksum-Elixir.Javex.Native.exs"
{
echo "%{"
for f in artifacts/*.tar.gz; do
name="$(basename "$f")"
sum="$(sha256sum "$f" | cut -d' ' -f1)"
echo " \"${name}\" => \"sha256:${sum}\","
done
echo "}"
} > "$OUT"
echo "--- ${OUT} ---"
cat "$OUT"
- name: Fetch deps
run: mix deps.get --only prod
- name: Publish to hex
run: mix hex.publish package --yes