name: GitHub Release
on:
push:
branches: ["master"]
paths: ["Cargo.toml"]
permissions: {}
concurrency:
group: github-release-${{ github.ref }}
jobs:
release:
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: write
steps:
- name: Detect package version change
id: version
env:
BEFORE_SHA: ${{ github.event.before }}
AFTER_SHA: ${{ github.sha }}
GH_TOKEN: ${{ github.token }}
run: |
if [[ "$BEFORE_SHA" =~ ^0+$ ]]; then
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
gh api \
-H "Accept: application/vnd.github.raw+json" \
"/repos/$GITHUB_REPOSITORY/contents/Cargo.toml?ref=$BEFORE_SHA" \
> "$RUNNER_TEMP/before.toml"
gh api \
-H "Accept: application/vnd.github.raw+json" \
"/repos/$GITHUB_REPOSITORY/contents/Cargo.toml?ref=$AFTER_SHA" \
> "$RUNNER_TEMP/after.toml"
old_version=$(python3 -c 'import sys, tomllib; print(tomllib.load(open(sys.argv[1], "rb"))["package"]["version"])' "$RUNNER_TEMP/before.toml")
new_version=$(python3 -c 'import sys, tomllib; print(tomllib.load(open(sys.argv[1], "rb"))["package"]["version"])' "$RUNNER_TEMP/after.toml")
if [[ "$old_version" == "$new_version" ]]; then
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [[ ! "$new_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$ ]]; then
echo "Invalid Cargo package version: $new_version" >&2
exit 1
fi
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "tag=v$new_version" >> "$GITHUB_OUTPUT"
- name: Create GitHub release
if: steps.version.outputs.changed == 'true'
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ steps.version.outputs.tag }}
run: |
if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
echo "Release $TAG already exists"
exit 0
fi
if gh api "/repos/$GITHUB_REPOSITORY/git/ref/tags/$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists without a GitHub release" >&2
exit 1
fi
gh release create "$TAG" \
--repo "$GITHUB_REPOSITORY" \
--target "$GITHUB_SHA" \
--title "$TAG" \
--generate-notes