Skip to content

[uni] update tests.yml #17

[uni] update tests.yml

[uni] update tests.yml #17

name: Auto Version Tagging
on:
push:
branches:
- master
permissions:
contents: write # Required to push new tags
jobs:
tag-version:
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.get_version.outputs.value }} # Pass version to next job
tag_created: ${{ steps.create_tag.outputs.tag_created }} # Pass if tag was created
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history to compare versions
- name: Extract Version from Cargo.toml
id: get_version
uses: sravinet/toml-select@v1.0.1
with:
file: "Cargo.toml"
field: "package.version"
- name: Check if Tag Exists
id: check_tag
run: |
VERSION=v${{ steps.get_version.outputs.value }}
if git rev-parse "$VERSION" >/dev/null 2>&1; then
echo "TAG_EXISTS=true" >> $GITHUB_ENV
else
echo "TAG_EXISTS=false" >> $GITHUB_ENV
fi
- name: Create and Push Git Tag via GitHub API
if: env.TAG_EXISTS == 'false'
id: push_tag
run: |
VERSION=v${{ steps.get_version.outputs.value }}
SHA=$(git rev-parse HEAD) # Get the latest commit SHA
# Push the tag using GitHub API
curl -X POST -H "Authorization: token ${{ secrets.PAT }}" \
-H "Content-Type: application/json" \
-d "{\"ref\": \"refs/tags/$VERSION\", \"sha\": \"$SHA\"}" \
"https://api.github.com/repos/${{ github.repository }}/git/refs"
echo "tag_created=true" >> $GITHUB_ENV
- name: Print Confirmation
run: |
if [ "${{ env.TAG_EXISTS }}" == "true" ]; then
echo "✅ Tag already exists: v${{ steps.get_version.outputs.value }}"
else
echo "🚀 New tag created: v${{ steps.get_version.outputs.value }}"
fi
confirm-tag:
runs-on: ubuntu-latest
needs: tag-version # Ensure this runs only after the tagging job
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Ensure we have the full tag history
- name: Verify Tag Exists in Remote
run: |
VERSION=v${{ needs.tag-version.outputs.new_version }}
if git ls-remote --tags origin | grep -q "refs/tags/$VERSION"; then
echo "✅ Tag $VERSION exists in remote repository."
else
echo "❌ Tag $VERSION does NOT exist in the remote repository!"
exit 1 # Fail the workflow if the tag isn't found
fi
- name: Verify Tag Matches Cargo.toml Version
run: |
VERSION=v${{ needs.tag-version.outputs.new_version }}
TAGGED_VERSION=$(git describe --tags --abbrev=0)
echo "🔍 Expected version from Cargo.toml: $VERSION"
echo "🔍 Latest Git tag: $TAGGED_VERSION"
if [ "$VERSION" != "$TAGGED_VERSION" ]; then
echo "❌ Version mismatch! Expected $VERSION but found $TAGGED_VERSION."
exit 1 # Fail the workflow
else
echo "✅ Tag correctly matches Cargo.toml version."
fi