-
Notifications
You must be signed in to change notification settings - Fork 6
116 lines (107 loc) · 3.66 KB
/
release.yml
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
name: Release
on:
push:
branches: [ main ]
jobs:
release:
runs-on: ubuntu-latest
steps:
# checkout
- name: Checkout repository code
uses: actions/checkout@v4
# install
- name: Setup PNPM
if:
uses: pnpm/action-setup@v2
with:
version: 8.7.4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: pnpm install --frozen-lockfile
# get latest version number
- name: Install semver package
run: pnpm add semver
- name: Retrieve the latest release and current package versions
id: get-releases
uses: actions/github-script@v6
with:
script: |
const semver = require('semver')
// get latest version
let latestVersion = 'v0.0.0'
try {
const latestRelease = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo,
})
latestVersion =
typeof latestRelease === 'undefined' ||
typeof latestRelease.data === 'undefined'
? 'v0.0.0'
: `v${semver.clean(latestRelease.data.tag_name)}`
console.info(`Found latest release version: ${latestVersion}`)
} catch (e) {
console.info(
'Could not evaluate latest release version, using v0.0.0'
)
}
// get version from package json
let packageVersion = 'v0.0.0'
try {
const packageJsonRecord = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: 'package.json',
ref: context.ref,
})
const packageJson =
typeof packageJsonRecord === 'undefined' ||
typeof packageJsonRecord.data === 'undefined'
? '{ "version": "0.0.0" }'
: Buffer
.from(packageJsonRecord.data.content, 'base64')
.toString()
const package = JSON.parse(packageJson)
packageVersion = `v${semver.clean(package.version)}`
console.info(`Found current package version: ${packageVersion}`)
} catch (e) {
console.info(
'Could not evaluate current package version, using v0.0.0'
)
}
return {
package: packageVersion,
latest: latestVersion,
latestIsNew: semver.gt(packageVersion, latestVersion)
}
- name: Uninstall semver package
run: git restore . && pnpm install --frozen-lockfile
# release to npm
- name: Release to NPM
if: ${{ fromJSON(steps.get-releases.outputs.result).latestIsNew }}
run: pnpm release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPMJS_ACCESS_TOKEN }}
# tag release in github
- name: Tag release in GitHub
if: ${{ fromJSON(steps.get-releases.outputs.result).latestIsNew }}
uses: actions/github-script@v6
env:
RELEASE_TAG_NAME: ${{ fromJSON(steps.get-releases.outputs.result).package }}
with:
result-encoding: string
script: |
const { RELEASE_TAG_NAME: tag_name } = process.env
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name,
target_commitish: context.sha,
name: tag_name,
})
console.info(`Created release ${tag_name}`)