Skip to content

Commit d972dcc

Browse files
ci: only allow pages deploy on the main repo
It's getting very annoying to have a bunch of failed workflow runs when I push to master on my own fork (since my fork of course doesn't have GitHub Pages configured)
1 parent 9405a69 commit d972dcc

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

.github/workflows/doc_update.yml

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
steps:
2828
- uses: actions/checkout@v6
2929
- name: Setup Pages
30+
if: ${{ github.repository == 'google/jsonnet' }}
3031
uses: actions/configure-pages@v5
3132
- name: Create Gemfile
3233
run: |
@@ -50,8 +51,16 @@ jobs:
5051
id: make-output-dir
5152
run: |
5253
WORKDIR="$(mktemp -d)"
53-
git fetch origin gh-pages
54-
git worktree add --no-checkout --detach "$WORKDIR"/work FETCH_HEAD
54+
# If the workflow runs on a fork that doesn't have a gh-pages branch
55+
# (which should be most forks) then we still want to be able to build
56+
# the docs (so the repo owner can examine them), but we have no baseline.
57+
# So we just build as though this is the first time.
58+
if git fetch origin gh-pages; then
59+
git worktree add --no-checkout --detach "$WORKDIR"/work FETCH_HEAD
60+
else
61+
echo "Repo has no gh-pages! Creating new orphan branch instead."
62+
git worktree add --orphan -b gh-pages "$WORKDIR"/work
63+
fi
5564
echo "doc_output_dir=$WORKDIR" >> "$GITHUB_OUTPUT"
5665
- name: Build website
5766
env:
@@ -61,7 +70,7 @@ jobs:
6170
bundler exec jekyll build -s doc -d "$WORKDIR"/work
6271
cd "$WORKDIR"/work
6372
# Keep existing libjsonnet.wasm.
64-
git checkout HEAD -- js/libjsonnet.wasm
73+
git checkout HEAD -- js/libjsonnet.wasm || echo "Could not find existing js/libjsonnet.wasm"
6574
# Want a .nojekyll file while we're still publishing by pushing to gh-pages branch.
6675
touch .nojekyll
6776
git add .
@@ -71,15 +80,24 @@ jobs:
7180
WORKDIR: ${{ steps.make-output-dir.outputs.doc_output_dir }}
7281
run: |
7382
cd "$WORKDIR"/work
74-
git diff --cached --stat
75-
git diff --cached
76-
TREE_SHA1=$(git write-tree)
77-
echo "git_tree_hash=$TREE_SHA1" >> "$GITHUB_OUTPUT"
78-
if git diff-index --quiet HEAD; then
79-
echo "diff_status=clean" >> "$GITHUB_OUTPUT"
83+
if git rev-parse --quiet --verify HEAD > /dev/null; then
84+
git diff --cached --stat
85+
# Only show the first 5000 lines of the diff output, for sanity.
86+
git diff --cached | head -n5000
87+
if git diff-index --quiet HEAD; then
88+
echo "Generated docs are identical to existing gh-pages branch."
89+
echo "diff_status=clean" >> "$GITHUB_OUTPUT"
90+
else
91+
echo "Generated docs differ from existing gh-pages branch."
92+
echo "diff_status=changed" >> "$GITHUB_OUTPUT"
93+
fi
8094
else
95+
echo "No HEAD revision; probably on an orphan branch. Skipping diff step."
8196
echo "diff_status=changed" >> "$GITHUB_OUTPUT"
8297
fi
98+
TREE_SHA1=$(git write-tree)
99+
echo "Computed sha1 for new gh-pages tree: $TREE_SHA1"
100+
echo "git_tree_hash=$TREE_SHA1" >> "$GITHUB_OUTPUT"
83101
git archive --format=tar -o ../gh-pages-content.tar "$TREE_SHA1"
84102
- name: Upload full archive
85103
id: upload-full-archive
@@ -93,15 +111,15 @@ jobs:
93111
include-hidden-files: true
94112
- name: Upload Pages package
95113
id: upload-pages-package
96-
if: ${{ steps.diff-generated.outputs.diff_status == 'changed' }}
114+
if: ${{ github.repository == 'google/jsonnet' && steps.diff-generated.outputs.diff_status == 'changed' }}
97115
uses: actions/upload-pages-artifact@v4
98116
with:
99117
path: ${{ steps.make-output-dir.outputs.doc_output_dir }}/work/
100118

101119
push_pages_branch:
102120
name: Update gh-pages branch
103121
needs: build_website
104-
if: ${{ contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.outputs.diff_status == 'changed' }}
122+
if: ${{ github.repository == 'google/jsonnet' && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.outputs.diff_status == 'changed' }}
105123
permissions:
106124
contents: write
107125
environment:
@@ -118,13 +136,13 @@ jobs:
118136
path: built
119137
- name: Extract and push
120138
env:
121-
TREE_SHA1: ${{ needs.build_website.outputs.git_tree_hash }}
139+
WANT_TREE_SHA1: ${{ needs.build_website.outputs.git_tree_hash }}
122140
GIT_AUTHOR_NAME: "github-actions[bot]"
123141
GIT_AUTHOR_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
124142
GIT_COMMITTER_NAME: "github-actions[bot]"
125143
GIT_COMMITTER_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
126144
run: |
127-
[[ ! -z "$TREE_SHA1" ]] || {
145+
[[ ! -z "$WANT_TREE_SHA1" ]] || {
128146
>&2 echo "did not get valid sha1 hash for expected git tree from builder job"
129147
exit 1
130148
}
@@ -136,10 +154,10 @@ jobs:
136154
tar -xf ../built/gh-pages-content.tar
137155
git add .
138156
git diff --cached --stat
139-
git diff --cached
140-
UNPACKED_TREE_SHA1=$(git write-tree)
157+
UNPACKED_WANT_TREE_SHA1=$(git write-tree)
158+
echo "Expect tree SHA1: $WANT_TREE_SHA1 (computed in build step)"
141159
echo "Unpacked tree SHA1: $UNPACKED_TREE_SHA1"
142-
[[ "$TREE_SHA1" = "$UNPACKED_TREE_SHA1" ]] || {
160+
[[ "$WANT_TREE_SHA1" = "$UNPACKED_TREE_SHA1" ]] || {
143161
>&2 echo "builder-produced tree differs from extracted tree"
144162
exit 1
145163
}
@@ -150,7 +168,7 @@ jobs:
150168
publish_pages:
151169
name: Direct publish pages
152170
needs: build_website
153-
if: ${{ false && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.outputs.diff_status == 'changed' }}
171+
if: ${{ false && github.repository == 'google/jsonnet' && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.outputs.diff_status == 'changed' }}
154172
permissions:
155173
pages: write
156174
id-token: write

0 commit comments

Comments
 (0)