Skip to content

Commit 1faf8c1

Browse files
authored
Merge pull request #305 from scratchfoundation/pages-without-publish
Publish branches to GH Pages
2 parents ff867a3 + d9911f7 commit 1faf8c1

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

.github/workflows/ci.yml

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ jobs:
2727
- name: Debug info
2828
# https://docs.github.com/en/actions/reference/security/secure-use#use-an-intermediate-environment-variable
2929
env:
30-
GH_HEAD_REF: ${{ github.head_ref }}
30+
# `env:` values are printed to the log even without using them in `run:`
31+
GH_CONTEXT: ${{ toJson(github) }}
3132
run: |
3233
cat <<EOF
33-
Scratch environment: ${{ vars.SCRATCH_ENV || '<none>' }}
34+
Working directory: $(pwd)
3435
Node version: $(node --version)
3536
NPM version: $(npm --version)
36-
GitHub ref: ${{ github.ref }}
37-
GitHub head ref: ${GH_HEAD_REF}
38-
Working directory: $(pwd)
37+
Scratch environment: ${{ vars.SCRATCH_ENV || '<none>' }}
3938
EOF
4039
4140
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3
@@ -87,8 +86,61 @@ jobs:
8786
with:
8887
package_name: ${{ matrix.package }}
8988

89+
preview:
90+
runs-on: ubuntu-latest
91+
needs: build
92+
# We don't want to give forks free reign to publish to our GitHub Pages, so run this job only if both:
93+
# - any workspace changed (otherwise there's no work to do)
94+
# - and either
95+
# - this is not a PR (so it's some other event that happened in our fork, like a push or merge group)
96+
# - or it's a PR from our fork (not some other fork)
97+
if: ${{
98+
(needs.build.outputs.any-workspace == 'true') &&
99+
(
100+
(!github.event.pull_request) ||
101+
(github.event.pull_request.head.repo.full_name == github.repository)
102+
)
103+
}}
104+
name: Publish preview playgrounds to GitHub Pages
105+
steps:
106+
- name: Determine GitHub Pages directory name
107+
id: branch_dir_name
108+
run: |
109+
if [ "$GITHUB_REF_NAME" == "develop" ]; then
110+
echo "result=."
111+
else
112+
echo "result=${GITHUB_REF_NAME//[^A-Za-z0-9._-]/_}"
113+
fi | tee --append "$GITHUB_OUTPUT"
114+
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
115+
with:
116+
name: build
117+
path: packages
118+
- name: Prepare playgrounds for GitHub Pages
119+
working-directory: ./packages
120+
run: |
121+
mkdir -p ../pages/
122+
for pkg in *; do
123+
if [ -d "${pkg}/playground" ]; then
124+
# using symlinks is quick and artifact generation will dereference them
125+
# if the GitHub Pages action stops dereferencing these links, we'll need to copy the files instead
126+
ln -s "../packages/${pkg}/playground" "../pages/${pkg}"
127+
fi
128+
done
129+
130+
# scratch-gui doesn't follow the pattern above
131+
ln -s "../packages/scratch-gui/build" "../pages/scratch-gui"
132+
133+
ls -l ../pages/
134+
- name: Deploy playgrounds to GitHub Pages
135+
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
136+
with:
137+
github_token: ${{ secrets.GITHUB_TOKEN }}
138+
publish_dir: ./pages
139+
destination_dir: "${{steps.branch_dir_name.outputs.result}}"
140+
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
141+
90142
results:
91-
name: Results
143+
name: Test Results
92144
runs-on: ubuntu-latest
93145
needs: test
94146
if: ${{ !cancelled() }}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: GitHub Pages Cleanup
2+
3+
on:
4+
schedule:
5+
- cron: 0 0 * * 6 # midnight on Saturdays
6+
workflow_dispatch:
7+
8+
jobs:
9+
cleanup:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Check out GitHub Pages branch
13+
uses: actions/checkout@v4
14+
with:
15+
# replace `fetch-depth` with `shallow-since` if and when actions/checkout#619 (or an equivalent) gets merged
16+
fetch-depth: 0
17+
ref: gh-pages
18+
- name: Mark stale directories for removal
19+
run: |
20+
for dir in */; do
21+
if [ -z "$(git log -n 1 --since "1 month ago" -- "$dir")" ]; then
22+
git rm -r "$dir"
23+
fi
24+
done
25+
git status
26+
- name: Commit
27+
run: "git commit -m 'chore: remove stale GitHub Pages branches'"
28+
- name: Push
29+
run: "git push"

.github/workflows/publish.yml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -145,35 +145,3 @@ jobs:
145145
run: |
146146
git tag -f "${{github.event.release.tag_name}}" HEAD
147147
git push -f origin "refs/tags/${{github.event.release.tag_name}}"
148-
149-
- name: Deploy scratch-svg-renderer to GitHub Pages
150-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
151-
with:
152-
github_token: ${{ secrets.GITHUB_TOKEN }}
153-
publish_dir: ./packages/scratch-svg-renderer/playground
154-
destination_dir: scratch-svg-renderer
155-
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
156-
157-
- name: Deploy scratch-render to GitHub Pages
158-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
159-
with:
160-
github_token: ${{ secrets.GITHUB_TOKEN }}
161-
publish_dir: ./packages/scratch-render/playground
162-
destination_dir: scratch-render
163-
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
164-
165-
- name: Deploy scratch-vm to GitHub Pages
166-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
167-
with:
168-
github_token: ${{ secrets.GITHUB_TOKEN }}
169-
publish_dir: ./packages/scratch-vm/playground
170-
destination_dir: scratch-vm
171-
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
172-
173-
- name: Deploy scratch-gui to GitHub Pages
174-
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4
175-
with:
176-
github_token: ${{ secrets.GITHUB_TOKEN }}
177-
publish_dir: ./packages/scratch-gui/build
178-
destination_dir: scratch-gui
179-
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"

0 commit comments

Comments
 (0)