name: Release
on:
push:
branches:
- main
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
paths:
- Cargo.toml
permissions:
contents: read
concurrency:
group: publish-crate-${{ github.repository }}
cancel-in-progress: false
jobs:
publish:
name: Publish ${{ github.ref_name }}
runs-on: ubuntu-24.04
environment: crates-io
permissions:
contents: write
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 with:
persist-credentials: false
- run: rustup toolchain install stable --profile minimal
- name: Verify tag and package version
shell: bash
run: |
package_version="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -n 1)"
if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
test "$GITHUB_REF_NAME" = "v$package_version"
else
test "$GITHUB_REF_NAME" = "main"
fi
- run: cargo +stable test --all-targets --locked
- name: Check whether this immutable version already exists
id: registry
shell: bash
run: |
VERSION="$(cargo metadata --locked --no-deps --format-version 1 | jq -r '.packages[0].version')"
if curl --fail --silent --show-error \
--user-agent "weavatrix-release-verifier/1.0 (https://github.com/sergii-ziborov/weavatrix-search-vector)" \
"https://crates.io/api/v1/crates/weavatrix-search-vector/$VERSION" |
jq -e --arg version "$VERSION" '.version.num == $version' >/dev/null; then
echo "published=true" >> "$GITHUB_OUTPUT"
else
echo "published=false" >> "$GITHUB_OUTPUT"
fi
- name: Publish to crates.io
if: steps.registry.outputs.published != 'true'
run: cargo +stable publish --locked
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Verify immutable crates.io artifact
shell: bash
run: |
VERSION="$(cargo metadata --locked --no-deps --format-version 1 | jq -r '.packages[0].version')"
for attempt in $(seq 1 24); do
if curl --fail --silent --show-error \
--user-agent "weavatrix-release-verifier/1.0 (https://github.com/sergii-ziborov/weavatrix-search-vector)" \
"https://crates.io/api/v1/crates/weavatrix-search-vector/$VERSION" |
jq -e --arg version "$VERSION" '.version.num == $version' >/dev/null; then
exit 0
fi
sleep 5
done
echo "weavatrix-search-vector@$VERSION did not become visible on crates.io" >&2
exit 1
- name: Create GitHub release
if: steps.registry.outputs.published != 'true'
shell: bash
run: >-
VERSION="$(cargo metadata --locked --no-deps --format-version 1 | jq -r '.packages[0].version')";
TAG="v$VERSION";
if git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1;
then gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --verify-tag --title "$TAG" --generate-notes;
else gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "$TAG" --generate-notes;
fi
env:
GH_TOKEN: ${{ github.token }}