Repository navigation
223 lines (192 loc) · 8.45 KB
/
Copy pathdeploy-storybooks.yml
File metadata and controls
223 lines (192 loc) · 8.45 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
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
219
220
221
222
223
name: Deploy Storybooks to GitHub Pages
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: write
pull-requests: write
statuses: write
pages: write
id-token: write
jobs:
build:
name: Build Storybooks
runs-on: ubuntu-latest
outputs:
has_storybooks: ${{ steps.check-affected.outputs.has_storybooks || 'true' }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
with:
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
fetch-depth: 0
filter: tree:0
persist-credentials: false
- name: Set NX_BASE and NX_HEAD for affected detection
if: github.event_name == 'pull_request'
run: |
git fetch origin ${GITHUB_BASE_REF}
MERGE_BASE=$(git merge-base HEAD origin/${GITHUB_BASE_REF})
echo "NX_BASE=$MERGE_BASE" >> $GITHUB_ENV
echo "NX_HEAD=HEAD" >> $GITHUB_ENV
echo "Comparing against merge-base: $MERGE_BASE"
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Install dependencies
run: yarn install
- name: Clone l10n repository
run: _scripts/l10n/clone.sh
- name: Check if any projects with storybook are affected (PR)
id: check-affected
if: github.event_name == 'pull_request'
run: |
AFFECTED=$(npx nx show projects --affected -t build-storybook)
if [ -z "$AFFECTED" ]; then
echo "has_storybooks=false" >> $GITHUB_OUTPUT
echo "No storybook projects affected"
else
echo "has_storybooks=true" >> $GITHUB_OUTPUT
echo "Affected storybook projects:"
echo "$AFFECTED"
fi
- name: Build Storybooks for affected packages (PR)
if: github.event_name == 'pull_request' && steps.check-affected.outputs.has_storybooks == 'true'
run: npx nx affected -t build-storybook --output-style=stream
- name: Build all Storybooks (main branch)
if: github.event_name == 'push'
run: npx nx run-many -t build-storybook --output-style=stream
- name: Organize Storybooks for deployment
if: github.event_name == 'push' || steps.check-affected.outputs.has_storybooks == 'true'
run: node _scripts/organize-storybooks.js
env:
DEPLOY_DIR: deploy/
- name: Upload storybooks artifact
if: github.event_name == 'push' || steps.check-affected.outputs.has_storybooks == 'true'
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: storybooks-${{ github.event_name == 'pull_request' && github.event.pull_request.number || 'main' }}
path: deploy/
retention-days: 1
prepare:
name: Prepare Storybooks for Deployment
runs-on: ubuntu-latest
needs: build
if: needs.build.outputs.has_storybooks == 'true'
outputs:
deploy_dir: ${{ steps.deploy-dir.outputs.path }}
# Per-PR (or per-ref) concurrency: different PRs no longer share one global
# lock, so they stop cancelling each other. The non-destructive push below
# makes concurrent updates to gh-pages safe without a global serialization group.
concurrency:
group: gh-pages-deploy-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
steps:
- name: Checkout repository for scripts
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
with:
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
path: repo
persist-credentials: false
- name: Checkout gh-pages branch
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
with:
ref: gh-pages
path: gh-pages
fetch-depth: 1
- name: Set deployment directory
id: deploy-dir
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
DEPLOY_DIR="storybooks/pr-${{ github.event.pull_request.number }}"
else
DEPLOY_DIR="storybooks/main"
fi
echo "path=$DEPLOY_DIR" >> $GITHUB_OUTPUT
echo "DEPLOY_DIR=$DEPLOY_DIR" >> $GITHUB_ENV
- name: Download storybooks artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: storybooks-${{ github.event_name == 'pull_request' && github.event.pull_request.number || 'main' }}
# Stage outside the gh-pages checkout so the reset --hard below can't wipe it
path: ${{ runner.temp }}/storybooks-artifact
- name: Update gh-pages branch for artifact storage
env:
DEPLOY_DIR: ${{ steps.deploy-dir.outputs.path }}
ARTIFACT_DIR: ${{ runner.temp }}/storybooks-artifact
STORYBOOKS_DIR: gh-pages/storybooks
REPO_NAME: ${{ github.event.repository.name }}
REPO_OWNER: ${{ github.repository_owner }}
run: |
git -C gh-pages config user.name "github-actions[bot]"
git -C gh-pages config user.email "github-actions[bot]@users.noreply.github.com"
if [ "${{ github.event_name }}" == "pull_request" ]; then
COMMIT_MSG="Store storybooks for PR ${{ github.event.pull_request.number }}"
else
COMMIT_MSG="Store storybooks from main branch"
fi
# Optimistic-concurrency update: re-apply our storybooks onto the latest
# gh-pages and push, retrying if another PR landed first. Replaces the old
# orphan force-push, which required a single global concurrency lock and made
# concurrent PRs cancel each other ("higher priority waiting request").
for attempt in 1 2 3 4 5; do
git -C gh-pages fetch --depth 1 origin gh-pages
git -C gh-pages reset --hard origin/gh-pages
rm -rf "gh-pages/${DEPLOY_DIR}"
mkdir -p "gh-pages/${DEPLOY_DIR}"
cp -R "${ARTIFACT_DIR}/." "gh-pages/${DEPLOY_DIR}/"
node repo/_scripts/generate-storybook-index.js
git -C gh-pages add -A
git -C gh-pages commit -m "$COMMIT_MSG" || { echo "Nothing to update"; break; }
if git -C gh-pages push origin HEAD:gh-pages; then
echo "Pushed storybooks to gh-pages (attempt ${attempt})"
break
fi
if [ "${attempt}" -eq 5 ]; then
echo "Failed to update gh-pages after ${attempt} attempts" >&2
exit 1
fi
echo "gh-pages advanced during run; retrying (attempt ${attempt})"
done
- name: Upload GitHub Pages artifact
uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
path: gh-pages/
deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
needs: [build, prepare]
if: needs.build.outputs.has_storybooks == 'true'
continue-on-error: true
# GitHub Pages allows only one in-progress deployment per environment, so
# serialize deploy jobs to stop concurrent runs colliding with an HTTP 400.
concurrency:
group: gh-pages-publish
cancel-in-progress: false
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128 # v5.0.0
- name: Create GitHub status check
if: github.event_name == 'pull_request'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const deployDir = "${{ needs.prepare.outputs.deploy_dir }}";
const deployUrl = `https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/${deployDir}/`;
const sha = context.payload.pull_request.head.sha;
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: sha,
state: 'success',
context: 'Link to Storybooks',
description: 'Storybooks deployed',
target_url: deployUrl
});
console.log(`Created status check for ${sha} with URL: ${deployUrl}`);