Skip to content

Auto Version and Tag #20

Auto Version and Tag

Auto Version and Tag #20

name: Auto Version and Tag
on:
push:
branches:
- 'main'
schedule:
- cron: '0 0 1,15 * *' # Runs at midnight on the 1st and 15th of every month
permissions: write-all
jobs:
update-version:
runs-on: ubuntu-latest
outputs:
new_commit: ${{ steps.commit.outputs.NEW_COMMIT }} # Define job-level output
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Get latest tag
id: latest_tag
run: |
git fetch --tags
latest_tag=$(git tag --sort=-v:refname | head -n 1)
if [ -z "$latest_tag" ]; then
latest_tag="1.0.0"
fi
echo "::set-output name=tag::$latest_tag"
- name: Parse version
id: parse_version
run: |
IFS='.' read -ra version <<< "${{ steps.latest_tag.outputs.tag }}"
x=${version[0]}
y=${version[1]}
z=${version[2]}
if [[ "$(date +'%d')" == "01" || "$(date +'%d')" == "15" ]]; then
y=$((y+1))
z=0
else
z=$((z+1))
fi
new_version="$x.$y.$z"
echo "::set-output name=version::$new_version"
- name: Read and Increment package.json Version
id: version
run: |
# Update package.json with new version
jq --arg version "${{ steps.parse_version.outputs.version }}" '.version = $version' package.json > tmp.json && mv tmp.json package.json
- name: Commit Updated package.json
id: commit
run: |
git config user.name "GitHub Actions Bot"
git config user.email "actions@github.com"
git add package.json
git commit -m "Bump version to ${{ steps.parse_version.outputs.version }}" || true
git push origin main
# Output the current HEAD SHA regardless of commit
NEW_COMMIT=$(git rev-parse HEAD)
echo "NEW_COMMIT=$NEW_COMMIT" >> $GITHUB_OUTPUT
update-tag:
needs: update-version # Wait for update-version job to complete
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.update-version.outputs.new_commit }} # Use the new commit SHA
fetch-depth: 0
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Read package.json Version and Tag
id: parse_version
run: |
VERSION=$(jq -r '.version' package.json)
echo "::set-output name=version::$VERSION"
- name: Create tag
run: |
git tag ${{ steps.parse_version.outputs.version }}
git push origin ${{ steps.parse_version.outputs.version }}
- name: Create release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.parse_version.outputs.version }}
name: Release ${{ steps.parse_version.outputs.version }}
body: |
Release version ${{ steps.parse_version.outputs.version }}
Date: ${{ steps.date.outputs.date }}
draft: false
prerelease: false