Skip to content

Create GitHub Release #10

Create GitHub Release

Create GitHub Release #10

Workflow file for this run

name: Create GitHub Release
on:
push:
tags:
- "v*"
workflow_dispatch:
inputs:
tag:
description: "Existing tag (e.g., v1.5.3) to create/update the release for"
required: true
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Determine version
id: version
run: |
if [ -n "${{ github.event.inputs.tag }}" ]; then
TAG="${{ github.event.inputs.tag }}"
else
TAG="${GITHUB_REF_NAME}"
fi
if [ -z "$TAG" ]; then
echo "Unable to determine tag name"
exit 1
fi
VERSION="${TAG#v}"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Generate release notes
run: |
echo "Extracting release notes for version ${{ steps.version.outputs.version }}"
node scripts/extract-release-notes.cjs "${{ steps.version.outputs.version }}" > release-notes.md
echo "--- Release notes content ---"
cat release-notes.md
echo "--- End of release notes ---"
- name: Publish GitHub release
uses: actions/github-script@v7
env:
RELEASE_TAG: ${{ steps.version.outputs.tag }}
with:
script: |
const fs = require('fs');
const tag = process.env.RELEASE_TAG;
const owner = context.repo.owner;
const repo = context.repo.repo;
const name = `Release ${tag}`;
let body = '';
try {
body = fs.readFileSync('release-notes.md', 'utf8').trim();
} catch (error) {
core.warning(`Unable to read release notes file: ${error.message}`);
}
const fallbackBody = 'Release notes are unavailable. Please refer to CHANGELOG.md.';
const payloadBody = body.length > 0 ? body : fallbackBody;
async function createRelease() {
core.info(`Creating release ${tag}`);
await github.rest.repos.createRelease({
owner,
repo,
tag_name: tag,
name,
body: payloadBody,
make_latest: 'true'
});
}
async function updateRelease(releaseId) {
core.info(`Updating existing release ${tag}`);
await github.rest.repos.updateRelease({
owner,
repo,
release_id: releaseId,
tag_name: tag,
name,
body: payloadBody,
make_latest: 'true'
});
}
try {
const existing = await github.rest.repos.getReleaseByTag({ owner, repo, tag });
await updateRelease(existing.data.id);
} catch (error) {
if (error.status === 404) {
await createRelease();
} else {
core.error(`Failed to publish release: ${error.message}`);
throw error;
}
}