update workflow #3
Workflow file for this run
This file contains hidden or 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: 'Release' | |
on: | |
push: | |
tags: | |
- 'v*' | |
jobs: | |
release: | |
name: Create Release | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Install dependencies | |
run: npm ci | |
- name: Build | |
run: npm run build | |
- name: Check dist is up-to-date | |
run: | | |
git add dist/ | |
git diff --staged --exit-code || (echo "❌ dist/ is not up-to-date. Please run 'npm run build' and commit the changes before creating a release." && exit 1) | |
- name: Extract version from tag | |
id: version | |
run: | | |
echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT | |
echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT | |
echo "major=$(echo ${GITHUB_REF#refs/tags/v} | cut -d. -f1)" >> $GITHUB_OUTPUT | |
- name: Generate release notes | |
id: notes | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { data: releases } = await github.rest.repos.listReleases({ | |
...context.repo, | |
per_page: 1 | |
}); | |
const previousTag = releases.length > 0 ? releases[0].tag_name : null; | |
const currentTag = '${{ steps.version.outputs.tag }}'; | |
let compareUrl = ''; | |
let commits = []; | |
if (previousTag) { | |
compareUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/compare/${previousTag}...${currentTag}`; | |
const { data: comparison } = await github.rest.repos.compareCommits({ | |
...context.repo, | |
base: previousTag, | |
head: currentTag | |
}); | |
commits = comparison.commits; | |
} else { | |
const { data: recentCommits } = await github.rest.repos.listCommits({ | |
...context.repo, | |
per_page: 10 | |
}); | |
commits = recentCommits; | |
} | |
let releaseNotes = `## What's Changed\n\n`; | |
for (const commit of commits) { | |
const message = commit.commit.message.split('\n')[0]; | |
const author = commit.author ? commit.author.login : commit.commit.author.name; | |
const sha = commit.sha.substring(0, 7); | |
releaseNotes += `* ${message} (${sha}) @${author}\n`; | |
} | |
if (previousTag) { | |
releaseNotes += `\n**Full Changelog**: ${compareUrl}`; | |
} | |
return releaseNotes; | |
- name: Create Release | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { data: release } = await github.rest.repos.createRelease({ | |
...context.repo, | |
tag_name: '${{ steps.version.outputs.tag }}', | |
name: '${{ steps.version.outputs.tag }}', | |
body: `${{ steps.notes.outputs.result }}`, | |
make_latest: 'true' | |
}); | |
console.log(`Created release: ${release.html_url}`); | |
- name: Update major version tag | |
run: | | |
MAJOR_TAG="v${{ steps.version.outputs.major }}" | |
echo "Updating major version tag: $MAJOR_TAG" | |
# Check if major version tag exists | |
if git ls-remote --tags origin | grep -q "refs/tags/$MAJOR_TAG$"; then | |
echo "Major version tag $MAJOR_TAG exists, updating it" | |
git tag -f $MAJOR_TAG ${{ steps.version.outputs.tag }} | |
git push origin $MAJOR_TAG --force | |
else | |
echo "Major version tag $MAJOR_TAG does not exist, creating it" | |
git tag $MAJOR_TAG ${{ steps.version.outputs.tag }} | |
git push origin $MAJOR_TAG | |
fi |