Repository navigation
111 lines (107 loc) · 4.11 KB
/
Copy pathrelease.yml
File metadata and controls
111 lines (107 loc) · 4.11 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
name: Release
on:
push:
branches: [ master ]
paths: [ package.json ]
permissions:
contents: read
jobs:
version:
name: Check version change
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.version.outputs.changed }}
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
persist-credentials: false
- uses: actions/setup-node@v7
with:
node-version: '24'
package-manager-cache: false
- name: Compare package versions
id: version
env:
BEFORE_SHA: ${{ github.event.before }}
run: |
node <<'NODE'
const fs = require('node:fs');
const { execFileSync } = require('node:child_process');
const previous = JSON.parse(execFileSync('git', [
'show', `${process.env.BEFORE_SHA}:package.json`,
], { encoding: 'utf8' }));
const current = JSON.parse(fs.readFileSync('package.json', 'utf8'));
if (typeof previous.version !== 'string' || typeof current.version !== 'string') {
throw new Error('Both package versions must be strings');
}
const changed = previous.version !== current.version;
fs.appendFileSync(process.env.GITHUB_OUTPUT, `changed=${changed}\n`);
console.log(`Package version: ${previous.version} -> ${current.version}`);
NODE
release:
name: Publish to npm and GitHub
needs: version
if: needs.version.outputs.changed == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
concurrency:
group: npm-publish-${{ github.ref }}
cancel-in-progress: false
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-node@v7
with:
node-version: '24'
registry-url: https://registry.npmjs.org
package-manager-cache: false
- name: Install npm with trusted publishing support
run: npm install --global npm@11
- name: Check whether the version is already published
id: registry
run: |
node <<'NODE'
const fs = require('node:fs');
const { execFileSync } = require('node:child_process');
const { name, version } = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const versions = JSON.parse(execFileSync('npm', [
'view', name, 'versions', '--json', '--registry=https://registry.npmjs.org',
], { encoding: 'utf8' }));
const published = [].concat(versions).includes(version);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `published=${published}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=${version.includes('-') ? 'next' : 'latest'}\n`);
console.log(`${name}@${version}: ${published ? 'already published, skipping' : 'ready to publish'}`);
NODE
- name: Install dependencies
if: steps.registry.outputs.published == 'false'
run: npm install --no-package-lock --no-fund
- name: Run checks
if: steps.registry.outputs.published == 'false'
run: npm run ci
- name: Publish with npm trusted publishing
if: steps.registry.outputs.published == 'false'
env:
RELEASE_TAG: ${{ steps.registry.outputs.tag }}
run: npm publish --provenance --access public --tag "$RELEASE_TAG"
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
run: |
version=$(node -p "require('./package.json').version")
tag="v$version"
gh api --paginate "repos/$GITHUB_REPOSITORY/releases" \
--jq '.[].tag_name' > "$RUNNER_TEMP/release-tags.txt"
if grep -Fxq -- "$tag" "$RUNNER_TEMP/release-tags.txt"; then
echo "GitHub release $tag already exists, skipping"
exit 0
fi
args=("$tag" --repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA"
--title "$tag" --generate-notes)
if [[ "$version" == *-* ]]; then
args+=(--prerelease)
fi
gh release create "${args[@]}"