-
Notifications
You must be signed in to change notification settings - Fork 2
218 lines (173 loc) · 7.53 KB
/
npm-release-reusable.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# See https://docs.github.com/en/actions/learn-github-actions/reusing-workflows
on:
workflow_call:
inputs:
slack_channel:
required: false
type: string
secrets:
NPM_TOKEN:
required: true
SLACK_ACCESS_TOKEN:
required: false
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# Fetch full history to get all tags so we can get PREV_VERSION
fetch-depth: 0
# https://github.com/actions/runner/issues/520
- run: |
if [ "${{ secrets.SLACK_ACCESS_TOKEN }}" != "" ]; then
echo HAS_SLACK=1 >> $GITHUB_ENV
fi
- uses: valu-digital/slack-action@master
if: ${{ env.HAS_SLACK == '1' }}
with:
token: "${{ secrets.SLACK_ACCESS_TOKEN }}"
channel: "${{ inputs.slack_channel }}"
- name: Notify start
if: ${{ env.HAS_SLACK == '1' }}
run: slack-message "New npm release"
- name: Set version enviroment variables
run: |
set -eu
release_mode=stable
pkg=$(echo "$GITHUB_REF" | cut -d / -f 4)
next_version=${GITHUB_REF##*/}
next_tag="$pkg/v${next_version}"
npm_name=$pkg
current_version=0.0.0
if [ -f "packages/$pkg/package.json" ]; then
current_version="$(jq -r .version "packages/$pkg/package.json")"
npm_name="$(jq -r .name "packages/$pkg/package.json")"
fi
next_tag="$pkg/v${next_version}"
if [ "$next_version" = "prerelease" ]; then
next_version="${current_version}-dev.${GITHUB_SHA:0:10}"
# No tag for prereleases
next_tag=
release_mode=prerelease
fi
prev_tag="$(git describe --abbrev=0 --match "$pkg/v*" || true)"
prev_version=$(echo "$prev_tag" | sed -E "s/.+v([0-9\.]+)\$/\1/")
link_ver="$(echo $next_version | tr -cd '[:alnum:]')"
changelog_link="https://github.com/${GITHUB_REPOSITORY}/blob/master/packages/$pkg/CHANGELOG.md#v${link_ver}"
if [ "$next_version" = "$prev_version" ]; then
echo "Cannot release $prev_version again"
exit 9
fi
is_pnpm=0
if [ -f pnpm-lock.yaml ]; then
is_pnpm=1
fi
if [ -f "packages/$pkg/pnpm-lock.yaml" ]; then
is_pnpm=1
fi
set -x
echo "pkg=$pkg" >> $GITHUB_ENV
echo "is_pnpm=$is_pnpm" >> $GITHUB_ENV
echo "npm_name=$npm_name" >> $GITHUB_ENV
echo "next_version=$next_version" >> $GITHUB_ENV
echo "next_tag=$next_tag" >> $GITHUB_ENV
echo "prev_tag=$prev_tag" >> $GITHUB_ENV
echo "prev_version=$prev_version" >> $GITHUB_ENV
echo "release_mode=$release_mode" >> $GITHUB_ENV
echo "changelog_link=$changelog_link" >> $GITHUB_ENV
- name: Set version number
run: |
set -eu
cd "packages/$pkg"
jq ".version = \"$next_version\"" package.json > tmp.json
mv tmp.json package.json
- name: Commit version numbers
if: ${{ env.release_mode == 'stable' }}
run: |
set -eu
cd "packages/$pkg"
git config user.email "[email protected]"
git config user.name "${{ github.actor }}"
git add package.json
git commit -m "$pkg: v${next_version}"
git tag -a "$next_tag" -m "$pkg v$next_version"
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: "14.x"
- name: Setup pnpm
uses: pnpm/[email protected]
with:
version: 6.25.1
if: ${{ env.is_pnpm == '1' }}
- name: "Install node modules"
run: |
set -eu
cd "packages/$pkg"
if [ "$is_pnpm" = "1" ]; then
pnpm install --frozen-lockfile
elif [ -f package-lock.json ]; then
npm ci
else
npm install
fi
- name: "Build the package"
run: |
set -eu
cd "packages/$pkg"
task=build
if [ "$(jq -r '.scripts."publish-build"' package.json)" != "null" ]; then
task=publish-build
fi
if [ "$is_pnpm" = "1" ]; then
pnpm run $task
pnpm run test
else
npm run $task
npm run test
fi
- name: Make STABLE release
if: ${{ env.release_mode == 'stable' }}
run: |
set -eu
cd "packages/$pkg"
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > "$HOME/.npmrc"
if [ "$is_pnpm" = "1" ]; then
pnpm publish --access public --no-git-checks
else
npm publish --access public
fi
rm "$HOME/.npmrc"
git push origin HEAD:master
git push origin --tags
- name: Notify about STABLE release
if: ${{ env.release_mode == 'stable' && env.HAS_SLACK == '1' }}
run: slack-message "New \`$npm_name\` stable release. See <$changelog_link|Changelog>. \n\nInstall with \`npm install $npm_name@^${next_version}\`"
- name: Make PRERELEASE release
if: ${{ env.release_mode == 'prerelease' }}
run: |
set -eu
cd "packages/$pkg"
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > "$HOME/.npmrc"
if [ "$is_pnpm" = "1" ]; then
pnpm publish --tag dev --access public --no-git-checks
else
npm publish --tag dev --access public
fi
rm "$HOME/.npmrc"
- name: Log install command
run: |
echo
if [ "$release_mode" = "stable" ]; then
echo "npm install $npm_name@${next_version}"
else
echo "npm install --save-exact $npm_name@${next_version}"
fi
echo
- name: Notify about PRERELEASE release
if: ${{ env.release_mode == 'prerelease' && env.HAS_SLACK == '1'}}
run: slack-message "New \`$npm_name\` prelease. \n\nInstall with \`npm install --save-exact $npm_name@${next_version}\`"
- name: Notify about failure
if: failure()
run: slack-message "Failed to make npm release for ${pkg:-} / ${npm_name:-}"