name: Release
on:
push:
branches:
- main
concurrency: release-${{ github.ref }}
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
RUSTFLAGS: -D warnings
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
CRATES_IO_USER_AGENT: molpha-verifier-ci (https://github.com/molpha/molpha-verifier)
jobs:
publish:
name: Publish release
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo
uses: Swatinem/rust-cache@v2
- name: Test
run: cargo test --all-features
- name: Package
run: cargo package --locked
- name: Read version
id: version
run: |
version=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2)
echo "value=${version}" >> "$GITHUB_OUTPUT"
- name: Check publication and tag status
id: check
run: |
crate=molpha-verifier
version="${{ steps.version.outputs.value }}"
tag="v${version}"
if curl -sf -H "User-Agent: ${CRATES_IO_USER_AGENT}" \
"https://crates.io/api/v1/crates/${crate}/${version}" >/dev/null; then
echo "already_published=true" >> "$GITHUB_OUTPUT"
echo "Version ${version} is already on crates.io; skipping publish."
else
echo "already_published=false" >> "$GITHUB_OUTPUT"
fi
git fetch --tags origin
if git ls-remote --exit-code --tags origin "refs/tags/${tag}" >/dev/null 2>&1; then
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
echo "Tag ${tag} already exists on origin; skipping tag creation."
else
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Publish to crates.io
if: steps.check.outputs.already_published != 'true'
run: cargo publish --locked
- name: Create git tag
if: steps.check.outputs.tag_exists != 'true'
run: |
version="${{ steps.version.outputs.value }}"
tag="v${version}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "${tag}" -m "Release ${tag}"
git push origin "${tag}"