fix(ci): Triggering? #37
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) { | |
const changelogNotes = JSON.stringify(result.nextRelease.notes); | |
console.log('Next version:', result.nextRelease.version); | |
console.log('Changelog:', changelogNotes); | |
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'nextRelease=' + JSON.stringify(result.nextRelease) + '\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.nextRelease != '' }} | |
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.nextRelease != '' }} | |
env: | |
NEXT_RELEASE: ${{ steps.get_version.outputs.nextRelease }} | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Extract the next version and changelog notes from the output | |
NEW_VERSION=$(echo $NEXT_RELEASE | jq -r '.version') | |
CHANGELOG=$(echo $NEXT_RELEASE | jq -r '.notes') | |
# Update the Changelog to handle the new lines. | |
CHANGELOG_NEWLINES=$(echo -e "$CHANGELOG") | |
# Output everything. | |
echo "New version: $NEW_VERSION" | |
echo "Changelog: $CHANGELOG" | |
echo "Changelog with newlines: $CHANGELOG_NEWLINES" | |
# 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_NEWLINES" | |
# 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" -m "$$CHANGELOG_NEWLINES" | |
git push origin "v$NEW_VERSION" | |
- name: Create Release Draft | |
if: ${{ steps.get_version.outputs.nextRelease != '' }} | |
env: | |
NEXT_RELEASE: ${{ steps.get_version.outputs.nextRelease }} | |
GH_TOKEN: ${{ secrets.PAT }} | |
run: | | |
# Extract the next version and changelog notes from the output | |
NEW_VERSION=$(echo $NEXT_RELEASE | jq -r '.version') | |
CHANGELOG=$(echo $NEXT_RELEASE | jq -r '.notes') | |
# Update the Changelog to handle the new lines. | |
CHANGELOG_NEWLINES=$(echo -e "$CHANGELOG") | |
# Create the release draft | |
echo "Creating release draft for v$NEW_VERSION" | |
echo "Changelog: $CHANGELOG" | |
echo "Changelog with newlines: $CHANGELOG_NEWLINES" | |
gh release create "v$NEW_VERSION" --title "v$NEW_VERSION" --notes "$CHANGELOG_NEWLINES" --draft |