Skip to content

Commit 6819c1d

Browse files
authored
Merge pull request #2835 from elastic/buildkite-build-pr-job
Buildkite build PR job
2 parents e0ff806 + 68954f7 commit 6819c1d

File tree

5 files changed

+180
-5
lines changed

5 files changed

+180
-5
lines changed

.buildkite/build_pr_pipeline.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
steps:
2-
- label: ":white_check_mark: Build docs PR"
3-
command: |
4-
bash .buildkite/scripts/build_pr.sh
2+
- key: "build-pr-setup"
3+
label: "setup"
4+
command: ".buildkite/scripts/build_pr_commit_status.sh pending"
5+
- key: "build-pr"
6+
label: ":hammer: Build docs PR"
7+
command: ".buildkite/scripts/build_pr.sh"
8+
depends_on:
9+
- step: build-pr-setup
10+
allow_failure: true
511
agents:
612
provider: "gcp"
713
image: family/docs-ubuntu-2204
14+
- key: "teardown"
15+
label: "teardown"
16+
command: |
17+
if [ $(buildkite-agent step get "outcome" --step "build-pr") == "passed" ]; then
18+
.buildkite/scripts/build_pr_commit_status.sh success
19+
else
20+
.buildkite/scripts/build_pr_commit_status.sh failure
21+
fi
22+
depends_on:
23+
- step: "build-pr"

.buildkite/hooks/pre-command

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
function retry {
6+
local retries=$1
7+
shift
8+
9+
local count=0
10+
until "$@"; do
11+
exit=$?
12+
wait=$((2 ** count))
13+
count=$((count + 1))
14+
if [ $count -lt "$retries" ]; then
15+
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
16+
sleep $wait
17+
else
18+
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
19+
return $exit
20+
fi
21+
done
22+
return 0
23+
}
24+
25+
# Secrets must be redacted
26+
# https://buildkite.com/docs/pipelines/managing-log-output#redacted-environment-variables
27+
if [[ "$BUILDKITE_PIPELINE_SLUG" == "docs-build-pr" ]];then
28+
export GITHUB_TOKEN=$(retry 5 vault kv get -field=value secret/ci/elastic-docs/docs_preview_cleaner)
29+
fi

.buildkite/scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set +x
55

66
# Configure the git author and committer information
77
export GIT_AUTHOR_NAME='Buildkite CI'
8-
export GIT_AUTHOR_EMAIL='buildkite@elasticsearch-ci.elastic.co'
8+
export GIT_AUTHOR_EMAIL='docs-status+[email protected]'
99
export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
1010
export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
1111

.buildkite/scripts/build_pr.sh

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,99 @@
11
#!/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})"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This hook should only be invoked for builds triggered by the Buildkite PR bot
6+
if [ -z ${GITHUB_PR_OWNER+set} ] || [ -z ${GITHUB_PR_REPO+set} ] || [ -z ${GITHUB_PR_TRIGGERED_SHA+set} ];then
7+
exit 0
8+
fi
9+
10+
status_state=$1
11+
description=''
12+
13+
case $status_state in
14+
pending)
15+
description='Build started';;
16+
success|failure|error)
17+
description='Build finished';;
18+
*)
19+
echo "Invalid state $status_state"
20+
exit 1;;
21+
esac
22+
23+
githubPublishStatus="https://api.github.com/repos/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}/statuses/${GITHUB_PR_TRIGGERED_SHA}"
24+
data='{"state":"'$status_state'","target_url":"'$BUILDKITE_BUILD_URL'","description":"'$description'","context":"buildkite/'$BUILDKITE_PIPELINE_SLUG'"}'
25+
26+
echo "Setting buildkite/${BUILDKITE_PIPELINE_SLUG} commit status to ${status_state}"
27+
curl -s -L \
28+
-X POST \
29+
-H "Accept: application/vnd.github+json" \
30+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
31+
-H "X-GitHub-Api-Version: 2022-11-28" \
32+
"${githubPublishStatus}" \
33+
-d "${data}"

0 commit comments

Comments
 (0)