-
-
Notifications
You must be signed in to change notification settings - Fork 0
185 lines (164 loc) · 5.88 KB
/
deploy_to_npm.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
name: Deploy to npm
on:
workflow_dispatch:
concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true
permissions:
contents: write
pages: write
id-token: write
jobs:
open-release:
name: Open release
runs-on: ubuntu-latest
outputs:
INITIAL_MAIN_POSITION: ${{ steps.create-tag.outputs.INITIAL_MAIN_POSITION }}
TAG: ${{ steps.create-tag.outputs.TAG }}
VERSION: ${{ steps.create-tag.outputs.VERSION }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Configure Git User
run: |
git config user.email "[email protected]"
git config user.name "Gili Tzabari"
- name: Parse the release version from package.json
uses: martinbeentjes/[email protected]
id: parse-version
# Setting a GitHub Action output parameter:
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
# Extracting the release version number: https://stackoverflow.com/a/16623897/14731
- name: Create tag
id: create-tag
run: |
echo "INITIAL_MAIN_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
VERSION=${{ steps.parse-version.outputs.current-version }}
TAG=release-${VERSION}
echo "TAG=${TAG}" >> "$GITHUB_OUTPUT"
echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT"
git tag ${TAG}
git push origin tag ${TAG}
deploy:
name: Deploy
needs: open-release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.open-release.outputs.TAG }}
fetch-depth: 0
- name: Configure Git User
run: |
git config user.email "[email protected]"
git config user.name "Gili Tzabari"
- name: Install node
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: "https://registry.npmjs.org"
- name: Install pnpm
run: |
corepack enable
corepack prepare pnpm@latest --activate
# pnpm dependencies cannot be cached until pnpm is installed
# WORKAROUND: https://github.com/actions/setup-node/issues/531
- name: Extract cached dependencies
uses: actions/setup-node@v4
with:
cache: pnpm
- name: Test build
run: |
pnpm install
pnpm run build
- name: Deploy to npm
run: |
cd target/publish
cp ../../pnpm-lock.yaml .
pnpm publish --provenance --access=public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Generate documentation
run: |
VERSION=${{ needs.open-release.outputs.VERSION }}
rm -rf "docs/api/${VERSION}"
mkdir --parents "docs/api/${VERSION}"
mv target/apidocs/* "docs/api/${VERSION}"
- name: Commit documentation
run: |
git checkout ${{ github.ref_name }} -f
git add "docs/api/${{ needs.open-release.outputs.VERSION }}"
git commit -m "Committing documentation for version ${{ needs.open-release.outputs.VERSION }}"
git push
document:
name: Document
needs: deploy
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: "docs/api"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Publish documentation
run: |
git checkout ${{ github.ref_name }} -f
echo "INITIAL_GH_PAGES_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
mkdir --parents "${{ needs.open-release.outputs.VERSION }}/docs"
mv target/apidocs "${{ needs.open-release.outputs.VERSION }}/docs/api"
git add "${{ needs.open-release.outputs.VERSION }}/docs/api"
git commit -m "Released version ${{ needs.open-release.outputs.VERSION }}"
git push
# Cleanup on failure: https://stackoverflow.com/a/74562058/14731
on-failure:
name: On failure
needs: [ open-release, deploy, document ]
runs-on: ubuntu-latest
if: ${{ failure() || cancelled() }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
fetch-depth: 0
- name: Install node
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: "https://registry.npmjs.org"
- name: Install pnpm
run: |
corepack enable
corepack prepare pnpm@latest --activate
# pnpm dependencies cannot be cached until pnpm is installed
# WORKAROUND: https://github.com/actions/setup-node/issues/531
- name: Extract cached dependencies
uses: actions/setup-node@v4
with:
cache: pnpm
- name: Restore the main ref to its original position
if: needs.open-release.outputs.INITIAL_MAIN_POSITION != ''
run: |
CURRENT_REF_POSITION=$(git rev-parse HEAD)
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_MAIN_POSITION }}" ]; then
git reset --hard ${{ needs.open-release.outputs.INITIAL_MAIN_POSITION }}
if [ "${{ github.ref_type }}" == "tag" ]; then
git ${{ github.ref_type }} -f ${{ github.ref_name }}
fi
git push -f origin ${{ github.ref_name }}
fi
- name: Delete tag
if: needs.open-release.outputs.TAG != ''
run: |
git push --delete origin ${{ needs.open-release.outputs.TAG }}