|
| 1 | +name: Update Major Version Tag |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*.*.*" |
| 7 | + workflow_dispatch: # Allow manual triggering |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write # Required to push tags |
| 11 | + |
| 12 | +jobs: |
| 13 | + update-major-tag: |
| 14 | + # Run this job only for tags that start with 'v', which is a common convention for versioning. |
| 15 | + if: startsWith(github.ref, 'refs/tags/v') |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + # For tag push events, checkout@v4 automatically checks out the tagged commit. |
| 23 | + # fetch-depth: 0 ensures we have full git history and tag information for reliable tag operations. |
| 24 | + |
| 25 | + - name: Debug checkout information |
| 26 | + run: | |
| 27 | + echo "=== Debug Information ===" |
| 28 | + echo "GITHUB_REF: $GITHUB_REF" |
| 29 | + echo "GITHUB_SHA: $GITHUB_SHA" |
| 30 | + echo "Current commit: $(git rev-parse HEAD)" |
| 31 | + echo "Current branch: $(git branch --show-current || echo 'detached HEAD')" |
| 32 | + echo "Tags pointing to current commit:" |
| 33 | + git tag --points-at HEAD || echo "No tags found" |
| 34 | + echo "Recent commits:" |
| 35 | + git log --oneline -5 |
| 36 | + echo "========================" |
| 37 | +
|
| 38 | + - name: Extract major version from tag |
| 39 | + id: extract_major_version |
| 40 | + run: | |
| 41 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 42 | + # We use a regex to ensure we only process tags that follow semantic versioning, like v1.2.3 |
| 43 | + if ! [[ "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 44 | + echo "Tag $TAG_NAME does not match the format vX.Y.Z." |
| 45 | + echo "Skipping major tag update." |
| 46 | + exit 0 # Skip without error if the tag is not a valid semver tag |
| 47 | + fi |
| 48 | +
|
| 49 | + # Extract the major version (e.g., 'v1' from 'v1.2.3') |
| 50 | + MAJOR_VERSION=$(echo "$TAG_NAME" | cut -d'.' -f1) |
| 51 | + echo "Version tag: $TAG_NAME" |
| 52 | + echo "Extracted major version: $MAJOR_VERSION" |
| 53 | +
|
| 54 | + # Pass the major version to subsequent steps |
| 55 | + echo "major_version=$MAJOR_VERSION" >> $GITHUB_OUTPUT |
| 56 | +
|
| 57 | + - name: Update and push major version tag |
| 58 | + run: | |
| 59 | + MAJOR_VERSION=${{ steps.extract_major_version.outputs.major_version }} |
| 60 | + echo "Updating tag '$MAJOR_VERSION' to point to this commit..." |
| 61 | +
|
| 62 | + # Configure git to associate the push with the github-actions bot |
| 63 | + git config user.name "github-actions[bot]" |
| 64 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 65 | +
|
| 66 | + # Move the major version tag to the current commit |
| 67 | + # The -f flag ensures that the tag is moved if it already exists |
| 68 | + git tag -f "$MAJOR_VERSION" |
| 69 | +
|
| 70 | + # Force-push the updated tag to the remote repository |
| 71 | + git push origin "$MAJOR_VERSION" --force |
| 72 | +
|
| 73 | + echo "Successfully updated and pushed tag '$MAJOR_VERSION'." |
0 commit comments