Create Tag and Increment Version #23
This file contains 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: Create Tag and Increment Version | |
on: | |
pull_request: | |
branches: | |
- main | |
types: [closed] | |
workflow_dispatch: # Allow manual triggering | |
jobs: | |
create-tag: | |
if: ${{ github.event.pull_request.merged }} || github.event_name == 'workflow_dispatch | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Get latest tag and increment version | |
id: tag_version | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
// Get the latest tag | |
const tags = await github.rest.repos.listTags({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}).then(res => res.data); | |
let latestTag = 'v0.0.0'; // Default if no tags exist | |
if (tags.length > 0) { | |
latestTag = tags[0].name; // Assuming tags are sorted by date (newest first) | |
} | |
// Extract version components | |
const [, year, month, minor] = latestTag.match(/v(\d{4})\.(\d{2})\.(\d+)/) || [null, 0, 0, 0]; | |
// Get current year and month | |
const now = new Date(); | |
const currentYear = now.getFullYear(); | |
const currentMonth = now.getMonth() + 1; // Months are 0-indexed | |
// Determine the new version | |
let newMinor = parseInt(minor) + 1; | |
if (currentYear > parseInt(year) || currentMonth > parseInt(month)) { | |
newMinor = 0; | |
} | |
const newVersion = `v${currentYear}.${currentMonth.toString().padStart(2, '0')}.${newMinor}`; | |
// Output the new version | |
core.setOutput('new_version', newVersion); | |
- name: Create Tag | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
// Access the output directly using the step id and output name | |
const newVersion = '${{ steps.tag_version.outputs.new_version }}'; | |
// Create the new tag | |
await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `refs/tags/${newVersion}`, | |
sha: context.sha, | |
}); | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Trigger Tag Workflow | |
uses: peter-evans/repository-dispatch@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
event-type: tag_created # Choose a descriptive event type | |
client-payload: '{"tag_name": "${{ steps.tag_version.outputs.new_version }}"}' |