-
-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (137 loc) · 5.29 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
name: Deploy to npm
on:
workflow_dispatch:
concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true
permissions:
contents: write
id-token: write
jobs:
open-release:
runs-on: ubuntu-latest
outputs:
INITIAL_MASTER_POSITION: ${{ steps.create-tag.outputs.INITIAL_MASTER_POSITION }}
INITIAL_GH_PAGES_POSITION: ${{ steps.create-tag.outputs.INITIAL_GH_PAGES_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_MASTER_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: 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 .
git status
pnpm publish --provenance --access=public --no-git-checks
mkdir --parents "${{ needs.open-release.outputs.VERSION }}/docs"
mv target/apidocs "${{ needs.open-release.outputs.VERSION }}/docs/api"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Commit changes
run: |
git checkout gh-pages -f
echo "INITIAL_GH_PAGES_POSITION=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
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:
needs: [ open-release, deploy ]
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 master ref to its original position
if: needs.open-release.outputs.INITIAL_MASTER_POSITION != ''
run: |
CURRENT_REF_POSITION=$(git rev-parse HEAD)
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_MASTER_POSITION }}" ]; then
git reset --hard ${{ needs.open-release.outputs.INITIAL_MASTER_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 }}
- name: Restore the gh-pages ref to its original position
if: needs.open-release.outputs.INITIAL_GH_PAGES_POSITION != ''
run: |
CURRENT_REF_POSITION=$(git rev-parse HEAD)
if [ "${CURRENT_REF_POSITION}" != "${{ needs.open-release.outputs.INITIAL_GH_PAGES_POSITION }}" ]; then
git reset --hard ${{ needs.open-release.outputs.INITIAL_GH_PAGES_POSITION }}
if [ "${{ github.ref_type }}" == "tag" ]; then
git ${{ github.ref_type }} -f ${{ github.ref_name }}
fi
git push -f origin ${{ github.ref_name }}
fi