|
| 1 | +name: Auto Tag with SemVer |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | +jobs: |
| 9 | + tag-main: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v3 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Fetch all tags |
| 21 | + run: git fetch --tags --force |
| 22 | + |
| 23 | + - name: Get latest semver tag |
| 24 | + id: get_latest_tag |
| 25 | + run: | |
| 26 | + # Get all tags and filter for valid semver tags (vX.Y.Z format) |
| 27 | + LATEST_TAG=$(git tag -l '[0-9]*.[0-9]*.[0-9]*' | sort -V | tail -n 1) |
| 28 | + |
| 29 | + # If no valid semver tags exist, start with 0.0.0 |
| 30 | + if [ -z "$LATEST_TAG" ]; then |
| 31 | + echo "::error::No semantic versioning tags found in the repository. Please create an initial tag (e.g., 0.0.0) manually." |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + |
| 35 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 36 | + |
| 37 | + # Extract the version numbers |
| 38 | + VERSION=${LATEST_TAG} |
| 39 | + MAJOR=$(echo $VERSION | cut -d. -f1) |
| 40 | + MINOR=$(echo $VERSION | cut -d. -f2) |
| 41 | + PATCH=$(echo $VERSION | cut -d. -f3) |
| 42 | + |
| 43 | + # Increment patch version |
| 44 | + NEW_PATCH=$((PATCH + 1)) |
| 45 | + NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH" |
| 46 | + |
| 47 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 48 | + |
| 49 | + - name: Create new tag |
| 50 | + id: create_tag |
| 51 | + run: | |
| 52 | + NEW_TAG="v${{ steps.get_latest_tag.outputs.new_version }}" |
| 53 | + echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT |
| 54 | + |
| 55 | + git config --local user.email "[email protected]" |
| 56 | + git config --local user.name "GitHub Action" |
| 57 | + git tag -a $NEW_TAG -m "Auto-incremented patch version" |
| 58 | + |
| 59 | + - name: Push tag |
| 60 | + run: | |
| 61 | + git push origin ${{ steps.create_tag.outputs.new_tag }} |
| 62 | + env: |
| 63 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + |
0 commit comments