|
1 | 1 | #!/bin/bash
|
2 |
| -echo "nothing to see yet" |
| 2 | +set -euo pipefail |
| 3 | +set +x |
| 4 | + |
| 5 | +# This script should only be invoked by the Buildkite PR bot |
| 6 | +if [ -z ${GITHUB_PR_BRANCH+set} ] || [ -z ${GITHUB_PR_TARGET_BRANCH+set} ] || [ -z ${GITHUB_PR_NUMBER+set} ] || [ -z ${GITHUB_PR_BASE_REPO+set} ];then |
| 7 | + echo "One of the following env. variable GITHUB_PR_BRANCH, GITHUB_PR_TARGET_BRANCH, GITHUB_PR_NUMBER, GITHUB_PR_BASE_REPO is missing - exiting." |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +# Configure the git author and committer information |
| 12 | +export GIT_AUTHOR_NAME='Buildkite CI' |
| 13 | +export GIT_AUTHOR_EMAIL= '[email protected]' |
| 14 | +export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME |
| 15 | +export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL |
| 16 | + |
| 17 | +# Set some metadata for build filtering capabilities |
| 18 | +# https://buildkite.com/elastic/docs-build-pr/builds?meta_data[repo]=tech-content |
| 19 | +# https://buildkite.com/elastic/docs-build-pr/builds?meta_data[repo_pr]=tech-content_123 |
| 20 | +buildkite-agent meta-data set "repo" "${GITHUB_PR_BASE_REPO}" |
| 21 | +buildkite-agent meta-data set "repo_pr" "${GITHUB_PR_BASE_REPO}_${GITHUB_PR_NUMBER}" |
| 22 | + |
| 23 | +rebuild_opt="" |
| 24 | +build_args="" |
| 25 | +TARGET_BRANCH="" |
| 26 | + |
| 27 | +# Define build docs arguments |
| 28 | +if [[ ${GITHUB_PR_COMMENT_VAR_REBUILD_OPT:="unset"} == "rebuild" ]];then |
| 29 | + rebuild_opt=" --rebuild" |
| 30 | +elif [[ ${GITHUB_PR_COMMENT_VAR_SKIP_OPT:="unset"} == "skiplinkcheck" ]];then |
| 31 | + build_args+=" --skiplinkcheck" |
| 32 | + if [[ ${GITHUB_PR_COMMENT_VAR_WARN_OPT:="unset"} == "warnlinkcheck" ]];then |
| 33 | + build_args+=" --warnlinkcheck" |
| 34 | + fi |
| 35 | +fi |
| 36 | + |
| 37 | +buildkite-agent \ |
| 38 | + annotate \ |
| 39 | + --style "info" \ |
| 40 | + --context 'docs-info' \ |
| 41 | + "Triggered by a doc change in elastic/$GITHUB_PR_BASE_REPO PR: [#$GITHUB_PR_NUMBER](https://github.com/elastic/$GITHUB_PR_BASE_REPO/pull/$GITHUB_PR_NUMBER)" |
| 42 | + |
| 43 | + |
| 44 | +if [[ "${GITHUB_PR_BASE_REPO}" != 'docs' ]]; then |
| 45 | + # Buildkite PR bot for repositories other than the `elastic/docs` repo are configured to |
| 46 | + # always checkout the master branch of the `elastic/docs` repo (where the build logic resides). |
| 47 | + # We first need to checkout the product repo / branch in a sub directory, that we'll reference |
| 48 | + # in the build process. |
| 49 | + echo "Cloning the ${GITHUB_PR_BASE_REPO} PR locally" |
| 50 | + |
| 51 | + git clone --reference /opt/git-mirrors/elastic-$GITHUB_PR_BASE_REPO \ |
| 52 | + [email protected]:elastic/ $GITHUB_PR_BASE_REPO.git ./product-repo |
| 53 | + |
| 54 | + cd ./product-repo && |
| 55 | + git fetch origin pull/$GITHUB_PR_NUMBER/head:$GITHUB_PR_BRANCH && |
| 56 | + git switch $GITHUB_PR_BRANCH && |
| 57 | + cd .. |
| 58 | + |
| 59 | + build_args+=" --sub_dir $GITHUB_PR_BASE_REPO:$GITHUB_PR_TARGET_BRANCH:./product-repo" |
| 60 | +else |
| 61 | + # Buildkite PR bot for the `elastic/docs` repo is configured to checkout the PR directly into the workspace |
| 62 | + # We don't have to do anything else in this case. |
| 63 | + |
| 64 | + # Per https://github.com/elastic/docs/issues/1821, always rebuild all |
| 65 | + # books for PRs to the docs repo, for now. |
| 66 | + # When https://github.com/elastic/docs/issues/1823 is fixed, this |
| 67 | + # should be removed and the original behavior restored. |
| 68 | + rebuild_opt=" --rebuild" |
| 69 | +fi |
| 70 | + |
| 71 | + |
| 72 | +# Set the target branch and preview options |
| 73 | +TARGET_BRANCH="${GITHUB_PR_BASE_REPO}_bk_${GITHUB_PR_NUMBER}" |
| 74 | +PREVIEW_URL="https://${TARGET_BRANCH}.docs-preview.app.elstc.co" |
| 75 | + |
| 76 | +build_cmd="./build_docs --all --keep_hash \ |
| 77 | + --target_repo [email protected]:elastic/built-docs \ |
| 78 | + --reference /opt/git-mirrors/ \ |
| 79 | + --target_branch ${TARGET_BRANCH} \ |
| 80 | + --push \ |
| 81 | + --announce_preview ${PREVIEW_URL}/diff \ |
| 82 | + ${rebuild_opt} \ |
| 83 | + ${build_args}" |
| 84 | + |
| 85 | +echo "The following build command will be used" |
| 86 | +echo $build_cmd |
| 87 | + |
| 88 | +# Kick off the build |
| 89 | +ssh-agent bash -c "ssh-add && $build_cmd" |
| 90 | + |
| 91 | +buildkite-agent annotate \ |
| 92 | + --style "success" \ |
| 93 | + --context 'docs-info' \ |
| 94 | + --append \ |
| 95 | + "<br>Preview url: ${PREVIEW_URL}" |
| 96 | + |
| 97 | +buildkite-agent meta-data set pr_comment:doc-preview:head " * Documentation preview |
| 98 | + - 📚 [HTML diff](${PREVIEW_URL}/diff) |
| 99 | + - 📙 [Preview](${PREVIEW_URL})" |
0 commit comments