Tag on Version Change #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Tag on Version Change | |
| on: | |
| workflow_run: | |
| workflows: ["Test and Coverage"] | |
| types: [completed] | |
| permissions: | |
| contents: write | |
| actions: write | |
| concurrency: | |
| group: tag-on-version-${{ github.event.workflow_run.head_sha }} | |
| cancel-in-progress: false | |
| jobs: | |
| tag: | |
| if: >- | |
| github.event.workflow_run.conclusion == 'success' && | |
| github.event.workflow_run.head_branch == 'main' && | |
| github.event.workflow_run.event == 'push' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout tested commit | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| fetch-depth: 0 | |
| - name: Decide whether to tag | |
| id: decide | |
| run: | | |
| version="$(awk -F'"' '/^\[/ { p = ($0 == "[package]") } p && /^version[ \t]*=/ { print $2; exit }' Cargo.toml)" | |
| echo "Detected package version: ${version:-<none>}" | |
| if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "Not a strict X.Y.Z release version; skipping." | |
| echo "should_tag=false" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| if git rev-parse -q --verify "refs/tags/$version" >/dev/null; then | |
| echo "Tag '$version' already exists; skipping." | |
| echo "should_tag=false" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "version=$version" >> "$GITHUB_OUTPUT" | |
| echo "should_tag=true" >> "$GITHUB_OUTPUT" | |
| - name: Create and push tag | |
| if: steps.decide.outputs.should_tag == 'true' | |
| env: | |
| VERSION: ${{ steps.decide.outputs.version }} | |
| HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git tag -a "$VERSION" -m "Release $VERSION" "$HEAD_SHA" | |
| git push origin "refs/tags/$VERSION" | |
| echo "Created and pushed tag $VERSION" | |
| - name: Trigger release workflow | |
| if: steps.decide.outputs.should_tag == 'true' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| VERSION: ${{ steps.decide.outputs.version }} | |
| run: | | |
| gh workflow run release.yml --ref "$VERSION" -f tag="$VERSION" | |
| echo "Dispatched release.yml at tag $VERSION" |