| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
set -ex |
| 4 |
|
| 5 |
neutral_status=0 |
| 6 |
source_revision=$(git rev-parse HEAD) |
| 7 |
# The token available in the `GITHUB_TOKEN` variable may be used to push to the |
| 8 |
# repository, but GitHub Pages will not rebuild the website in response to such |
| 9 |
# events. Use an access token generated for the project's machine user, |
| 10 |
# wpt-pr-bot. |
| 11 |
# |
| 12 |
# https://help.github.com/en/articles/generic-jekyll-build-failures |
| 13 |
remote_url=https://${DEPLOY_TOKEN}@github.com/${GITHUB_REPOSITORY}.git |
| 14 |
wpt_root=$PWD |
| 15 |
|
| 16 |
function json_property { |
| 17 |
cat ${1} | \ |
| 18 |
python -c "import json, sys; print(json.load(sys.stdin).get(\"${2}\", \"\"))" |
| 19 |
} |
| 20 |
|
| 21 |
function is_pull_request { |
| 22 |
test -n "$(json_property ${GITHUB_EVENT_PATH} pull_request)" |
| 23 |
} |
| 24 |
|
| 25 |
function targets_master { |
| 26 |
test $(json_property ${GITHUB_EVENT_PATH} ref) == 'refs/heads/master' |
| 27 |
} |
| 28 |
|
| 29 |
git config --global user.email "[email protected]" |
| 30 |
git config --global user.name "wpt-pr-bot" |
| 31 |
|
| 32 |
# Prepare the output directory so that the new build can be pushed to the |
| 33 |
# repository as an incremental change to the prior build. |
| 34 |
mkdir -p docs/_build |
| 35 |
cd docs/_build |
| 36 |
git init |
| 37 |
git fetch --depth 1 ${remote_url} gh-pages |
| 38 |
git checkout FETCH_HEAD |
| 39 |
git rm -rf . |
| 40 |
|
| 41 |
# Build the website |
| 42 |
unset NODE_ENV |
| 43 |
cd ${wpt_root}/docs |
| 44 |
npm install . |
| 45 |
export PATH="$PWD/node_modules/.bin:$PATH" |
| 46 |
cd ${wpt_root} |
| 47 |
|
| 48 |
./wpt build-docs |
| 49 |
|
| 50 |
cd docs/_build |
| 51 |
# Configure DNS |
| 52 |
echo web-platform-tests.org > CNAME |
| 53 |
# Disable Jekyll |
| 54 |
# https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/ |
| 55 |
touch .nojekyll |
| 56 |
|
| 57 |
# Publish the website by pushing the built contents to the `gh-pages` branch |
| 58 |
git add . |
| 59 |
|
| 60 |
echo This submission alters the compiled files as follows |
| 61 |
|
| 62 |
git diff --staged |
| 63 |
|
| 64 |
if is_pull_request ; then |
| 65 |
echo Submission comes from a pull request. Exiting without publishing. |
| 66 |
|
| 67 |
exit ${neutral_status} |
| 68 |
fi |
| 69 |
|
| 70 |
if ! targets_master ; then |
| 71 |
echo Submission does not target the 'master' branch. Exiting without publishing. |
| 72 |
|
| 73 |
exit ${neutral_status} |
| 74 |
fi |
| 75 |
|
| 76 |
if git diff --exit-code --quiet --staged ; then |
| 77 |
echo No change to the website contents. Exiting without publishing. |
| 78 |
|
| 79 |
exit ${neutral_status} |
| 80 |
fi |
| 81 |
|
| 82 |
git commit --message "Build documentation |
| 83 |
|
| 84 |
These files were generated from commit ${source_revision}" |
| 85 |
|
| 86 |
git push --force ${remote_url} HEAD:gh-pages |