-
Notifications
You must be signed in to change notification settings - Fork 0
127 lines (110 loc) · 3.66 KB
/
Copy pathgithub-release.yml
File metadata and controls
127 lines (110 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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"
registry-url: "https://registry.npmjs.org"
scope: "@programinglive"
- 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: Install dependencies
run: npm ci
- name: Build package
run: npm run build
- name: Publish to npm
run: npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- 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;
}
}