chore(test): Testing Semantic Version. #29
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: Automatic Semantic Versioning | |
# Restrict the workflow to the main branch and ignore tags | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
jobs: | |
tag_version: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Cache node_modules | |
id: cache-node-modules | |
uses: actions/cache@v4 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} | |
- name: Initialize NPM | |
if: steps.cache-node-modules.outputs.cache-hit != 'true' | |
run: npm install | |
- name: Semantic Versioning | |
id: get_version | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Run semantic-release in dry-run mode and capture the next version and changelog notes | |
node -e " | |
const fs = require('fs'); | |
const semanticRelease = require('semantic-release'); | |
(async () => { | |
const result = await semanticRelease({ | |
dryRun: true, | |
ci: false, | |
branches: ['main'], | |
repositoryUrl: 'https://github.com/${{ github.repository }}', | |
plugins: [ | |
'@semantic-release/commit-analyzer', | |
'@semantic-release/release-notes-generator', | |
], | |
}); | |
console.log('Result:', result); | |
if (result && result.nextRelease) { | |
console.log('Next version:', result.nextRelease.version); | |
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'new_version=' + result.nextRelease.version + '\n'); | |
const changelogNotes = JSON.stringify(result.nextRelease.notes); | |
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'changelog=' + changelogNotes + '\n'); | |
} else { | |
console.log('No release necessary.'); | |
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'new_version=\n'); | |
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'changelog=\n'); | |
} | |
})(); | |
" | |
- name: Configure Git | |
if: ${{ steps.get_version.outputs.new_version != '' }} | |
run: | | |
git config user.name "${{ github.actor }}" | |
git config user.email "${{ github.actor }}@users.noreply.github.com" | |
- name: Create Release Commit | |
if: ${{ steps.get_version.outputs.new_version != '' }} | |
env: | |
NEW_VERSION: ${{ steps.get_version.outputs.new_version }} | |
CHANGELOG: ${{ steps.get_version.outputs.changelog }} | |
run: | | |
# Decode the changelog from JSON format | |
CHANGELOG_DECODED=$(echo "$CHANGELOG" | node -e "console.log(JSON.parse(process.stdin.read()))") | |
# Create the commit message | |
COMMIT_MSG="chore(release): v$NEW_VERSION" | |
# Create an empty commit with the changelog as the commit body | |
git commit --allow-empty -m "$COMMIT_MSG" -m "$CHANGELOG_DECODED" | |
# Push the commit back to the branch | |
git push origin HEAD:${{ github.ref }} | |
# Create a new tag for the release | |
git tag -a "v$NEW_VERSION" -m "Release version $NEW_VERSION" | |
git push origin "v$NEW_VERSION" |