| 1 |
name: Rebuild Docs |
| 2 |
|
| 3 |
on: |
| 4 |
pull_request: |
| 5 |
types: [opened, reopened, synchronize, ready_for_review, review_requested] |
| 6 |
branches-ignore: |
| 7 |
- "gh-pages" |
| 8 |
push: |
| 9 |
branches: |
| 10 |
- "master" |
| 11 |
workflow_dispatch: |
| 12 |
inputs: |
| 13 |
build_wasm: |
| 14 |
description: "Rebuild libjsonnet.wasm from source" |
| 15 |
default: false |
| 16 |
type: boolean |
| 17 |
required: false |
| 18 |
build_wasm_from_branch: |
| 19 |
description: "go-jsonnet branch, tag or SHA to build from" |
| 20 |
default: "master" |
| 21 |
type: string |
| 22 |
required: false |
| 23 |
|
| 24 |
permissions: |
| 25 |
contents: read |
| 26 |
|
| 27 |
jobs: |
| 28 |
build_libjsonnet_wasm: |
| 29 |
name: Build libjsonnet.wasm for website |
| 30 |
if: ${{ inputs.build_wasm }} |
| 31 |
runs-on: "ubuntu-24.04" |
| 32 |
outputs: |
| 33 |
libjsonnet_wasm_sha256: ${{ steps.build.outputs.libjsonnet_wasm_sha256 }} |
| 34 |
libjsonnet_wasm_artifact_id: ${{ steps.upload-wasm.outputs.artifact_id }} |
| 35 |
steps: |
| 36 |
- uses: actions/checkout@v6 |
| 37 |
with: |
| 38 |
repository: google/go-jsonnet |
| 39 |
ref: ${{ inputs.build_wasm_from_branch }} |
| 40 |
submodules: false |
| 41 |
- uses: actions/cache@v5 |
| 42 |
with: |
| 43 |
path: | |
| 44 |
~/.cache/bazel |
| 45 |
~/.cache/bazelisk |
| 46 |
key: ${{ runner.os }}-bazel-cache |
| 47 |
- id: build |
| 48 |
run: | |
| 49 |
bazelisk test --lockfile_mode=error //... |
| 50 |
bazelisk build --lockfile_mode=error //cmd/wasm:libjsonnet.wasm |
| 51 |
WHERE="$(bazelisk cquery //cmd/wasm:libjsonnet.wasm --output files)" |
| 52 |
cp "${WHERE}" ./ |
| 53 |
{ printf 'libjsonnet_wasm_sha256='; sha256sum --binary ./libjsonnet.wasm; } >> "$GITHUB_OUTPUT" |
| 54 |
- name: Upload libjsonnet.wasm |
| 55 |
id: upload-wasm |
| 56 |
uses: actions/upload-artifact@v4 |
| 57 |
with: |
| 58 |
path: ./libjsonnet.wasm |
| 59 |
name: libjsonnet-wasm |
| 60 |
if-no-files-found: "error" |
| 61 |
retention-days: 4 |
| 62 |
include-hidden-files: false |
| 63 |
|
| 64 |
build_website: |
| 65 |
name: Build website |
| 66 |
# Ordering dependency - this must run after building libjsonnet.wasm. |
| 67 |
needs: build_libjsonnet_wasm |
| 68 |
# Don't run if libjsonnet.wasm build _failed_ or the workflow cancelled, but run if it was skipped. |
| 69 |
if: always() && !failure() && !cancelled() |
| 70 |
runs-on: "ubuntu-24.04" |
| 71 |
outputs: |
| 72 |
diff_status: ${{ steps.diff-generated.outputs.diff_status }} |
| 73 |
# Note upload-pages-artifact output name is artifact_id (with underscore). |
| 74 |
pages_artifact_id: ${{ steps.upload-pages-package.outputs.artifact_id }} |
| 75 |
# Note upload-artifact output name is artifact-id (with dash). |
| 76 |
branch_content_artifact_id: ${{ steps.upload-full-archive.outputs.artifact-id }} |
| 77 |
git_tree_hash: ${{ steps.diff-generated.outputs.git_tree_hash }} |
| 78 |
steps: |
| 79 |
- uses: actions/checkout@v6 |
| 80 |
- name: Setup Pages |
| 81 |
if: ${{ github.repository == 'google/jsonnet' }} |
| 82 |
uses: actions/configure-pages@v5 |
| 83 |
- name: Create Gemfile |
| 84 |
run: | |
| 85 |
echo $'source "https://rubygems.org"\n\ngem "jekyll"\n' > Gemfile |
| 86 |
- name: Setup Ruby and Jekyll |
| 87 |
uses: ruby/setup-ruby@8d27f39a5e7ad39aebbcbd1324f7af020229645c # v1.287.0 |
| 88 |
with: |
| 89 |
ruby-version: "4.0.1" |
| 90 |
bundler-cache: true |
| 91 |
- name: Cache jsonnet binary |
| 92 |
id: cache-jsonnet-binary |
| 93 |
uses: actions/cache@v5 |
| 94 |
with: |
| 95 |
key: jsonnet-bin-${{ hashFiles('include/*', 'core/*', 'cmd/*', 'cpp/*', 'third_party/**/*', 'stdlib/std.jsonnet') }} |
| 96 |
path: ./jsonnet |
| 97 |
- name: Build jsonnet binary |
| 98 |
if: ${{ steps.cache-jsonnet-binary.outputs.cache-hit != 'true' }} |
| 99 |
run: | |
| 100 |
make jsonnet |
| 101 |
- id: get_built_libjsonnet_wasm |
| 102 |
continue-on-error: true |
| 103 |
uses: actions/download-artifact@v7 |
| 104 |
with: |
| 105 |
artifact-ids: ${{ needs.build_libjsonnet_wasm.outputs.libjsonnet_wasm_artifact_id }} |
| 106 |
path: built_libjsonnet_wasm |
| 107 |
- name: Set up output dir |
| 108 |
id: make-output-dir |
| 109 |
run: | |
| 110 |
WORKDIR="$(mktemp -d)" |
| 111 |
# If the workflow runs on a fork that doesn't have a gh-pages branch |
| 112 |
# (which should be most forks) then we still want to be able to build |
| 113 |
# the docs (so the repo owner can examine them), but we have no baseline. |
| 114 |
# So we just build as though this is the first time. |
| 115 |
if git fetch origin gh-pages; then |
| 116 |
git worktree add --no-checkout --detach "$WORKDIR"/work FETCH_HEAD |
| 117 |
else |
| 118 |
echo "Repo has no gh-pages! Creating new orphan branch instead." |
| 119 |
git worktree add --orphan -b gh-pages "$WORKDIR"/work |
| 120 |
fi |
| 121 |
echo "doc_output_dir=$WORKDIR" >> "$GITHUB_OUTPUT" |
| 122 |
- name: Build website |
| 123 |
env: |
| 124 |
WORKDIR: ${{ steps.make-output-dir.outputs.doc_output_dir }} |
| 125 |
WANT_BUILT_LIBJSONNET_WASM_PATH: ${{ steps.get_built_libjsonnet_wasm.outputs.download-path }} |
| 126 |
WANT_BUILT_LIBJSONNET_WASM_SHA256: ${{ needs.build_libjsonnet_wasm.outputs.libjsonnet_wasm_sha256 }} |
| 127 |
run: | |
| 128 |
tools/scripts/update_web_content.sh |
| 129 |
bundler exec jekyll build -s doc -d "$WORKDIR"/work |
| 130 |
cd "$WORKDIR"/work |
| 131 |
if [[ -z "${WANT_BUILT_LIBJSONNET_WASM_SHA256}" ]]; then |
| 132 |
# Keep existing libjsonnet.wasm. |
| 133 |
git checkout HEAD -- js/libjsonnet.wasm || echo "Could not find existing js/libjsonnet.wasm" |
| 134 |
else |
| 135 |
# Use a newly build libjsonnet.wasm. |
| 136 |
cp "${WANT_BUILT_LIBJSONNET_WASM_PATH}"/libjsonnet.wasm ./js/libjsonnet.wasm |
| 137 |
# Safety check - check that we transferred the file correctly. |
| 138 |
echo "${WANT_BUILT_LIBJSONNET_WASM_SHA256}" | (cd ./js && sha256sum --check --strict) |
| 139 |
fi |
| 140 |
# Want a .nojekyll file while we're still publishing by pushing to gh-pages branch. |
| 141 |
touch .nojekyll |
| 142 |
git add . |
| 143 |
- name: Diff docs |
| 144 |
id: diff-generated |
| 145 |
env: |
| 146 |
WORKDIR: ${{ steps.make-output-dir.outputs.doc_output_dir }} |
| 147 |
run: | |
| 148 |
cd "$WORKDIR"/work |
| 149 |
if git rev-parse --quiet --verify HEAD > /dev/null; then |
| 150 |
git diff --cached --stat |
| 151 |
# Only show the first 5000 lines of the diff output, for sanity. |
| 152 |
git diff --cached | head -n5000 |
| 153 |
if git diff-index --quiet HEAD; then |
| 154 |
echo "Generated docs are identical to existing gh-pages branch." |
| 155 |
echo "diff_status=clean" >> "$GITHUB_OUTPUT" |
| 156 |
else |
| 157 |
echo "Generated docs differ from existing gh-pages branch." |
| 158 |
echo "diff_status=changed" >> "$GITHUB_OUTPUT" |
| 159 |
fi |
| 160 |
else |
| 161 |
echo "No HEAD revision; probably on an orphan branch. Skipping diff step." |
| 162 |
echo "diff_status=changed" >> "$GITHUB_OUTPUT" |
| 163 |
fi |
| 164 |
TREE_SHA1=$(git write-tree) |
| 165 |
echo "Computed sha1 for new gh-pages tree: $TREE_SHA1" |
| 166 |
echo "git_tree_hash=$TREE_SHA1" >> "$GITHUB_OUTPUT" |
| 167 |
git archive --format=tar -o ../gh-pages-content.tar "$TREE_SHA1" |
| 168 |
- name: Upload full archive |
| 169 |
id: upload-full-archive |
| 170 |
if: ${{ steps.diff-generated.outputs.diff_status == 'changed' }} |
| 171 |
uses: actions/upload-artifact@v4 |
| 172 |
with: |
| 173 |
path: ${{ steps.make-output-dir.outputs.doc_output_dir }}/gh-pages-content.tar |
| 174 |
name: full-gh-pages-content |
| 175 |
if-no-files-found: "error" |
| 176 |
retention-days: 4 |
| 177 |
include-hidden-files: true |
| 178 |
- name: Upload Pages package |
| 179 |
id: upload-pages-package |
| 180 |
if: ${{ github.repository == 'google/jsonnet' && steps.diff-generated.outputs.diff_status == 'changed' }} |
| 181 |
uses: actions/upload-pages-artifact@v4 |
| 182 |
with: |
| 183 |
path: ${{ steps.make-output-dir.outputs.doc_output_dir }}/work/ |
| 184 |
|
| 185 |
push_pages_branch: |
| 186 |
name: Update gh-pages branch |
| 187 |
needs: build_website |
| 188 |
if: always() && github.repository == 'google/jsonnet' && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.result == 'success' && needs.build_website.outputs.diff_status == 'changed' |
| 189 |
permissions: |
| 190 |
contents: write |
| 191 |
environment: |
| 192 |
name: pages-via-branch-push |
| 193 |
runs-on: "ubuntu-24.04" |
| 194 |
steps: |
| 195 |
- uses: actions/checkout@v6 |
| 196 |
with: |
| 197 |
ref: gh-pages |
| 198 |
path: repo |
| 199 |
- uses: actions/download-artifact@v7 |
| 200 |
with: |
| 201 |
artifact-ids: ${{ needs.build_website.outputs.branch_content_artifact_id }} |
| 202 |
path: built |
| 203 |
- name: Extract and push |
| 204 |
env: |
| 205 |
WANT_TREE_SHA1: ${{ needs.build_website.outputs.git_tree_hash }} |
| 206 |
GIT_AUTHOR_NAME: "github-actions[bot]" |
| 207 |
GIT_AUTHOR_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com" |
| 208 |
GIT_COMMITTER_NAME: "github-actions[bot]" |
| 209 |
GIT_COMMITTER_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com" |
| 210 |
run: | |
| 211 |
[[ ! -z "$WANT_TREE_SHA1" ]] || { |
| 212 |
>&2 echo "did not get valid sha1 hash for expected git tree from builder job" |
| 213 |
exit 1 |
| 214 |
} |
| 215 |
cd repo |
| 216 |
rm --one-file-system -r -- $(git ls-tree --name-only HEAD) |
| 217 |
echo "Cleared working dir. Content is now:" |
| 218 |
ls -R "$GITHUB_WORKSPACE" |
| 219 |
echo "Unpacking artifact from builder job." |
| 220 |
tar -xf ../built/gh-pages-content.tar |
| 221 |
git add . |
| 222 |
git diff --cached --stat |
| 223 |
UNPACKED_TREE_SHA1=$(git write-tree) |
| 224 |
echo "Expect tree SHA1: $WANT_TREE_SHA1 (computed in build step)" |
| 225 |
echo "Unpacked tree SHA1: $UNPACKED_TREE_SHA1" |
| 226 |
[[ "$WANT_TREE_SHA1" = "$UNPACKED_TREE_SHA1" ]] || { |
| 227 |
>&2 echo "builder-produced tree differs from extracted tree" |
| 228 |
exit 1 |
| 229 |
} |
| 230 |
git commit -m 'Update docs.' |
| 231 |
git log -n1 |
| 232 |
git push |
| 233 |
|
| 234 |
publish_pages: |
| 235 |
name: Direct publish pages |
| 236 |
needs: build_website |
| 237 |
if: false && github.repository == 'google/jsonnet' && contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && needs.build_website.result == 'success' && needs.build_website.outputs.diff_status == 'changed' |
| 238 |
permissions: |
| 239 |
pages: write |
| 240 |
id-token: write |
| 241 |
environment: |
| 242 |
name: github-pages |
| 243 |
url: ${{ steps.deployment.outputs.page_url }} |
| 244 |
runs-on: "ubuntu-24.04" |
| 245 |
steps: |
| 246 |
- name: Deploy to GitHub Pages |
| 247 |
id: deployment |
| 248 |
uses: actions/deploy-pages@v4 |