Skip to content

Buildkite PR job for assembling docs #2804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .buildkite/hooks/post-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -euo pipefail

# This hook should only be invoked for builds triggered by the Buildkite PR bot
if [ -z ${GITHUB_PR_OWNER+set} ] || [ -z ${GITHUB_PR_REPO+set} ] || [ -z ${GITHUB_PR_TRIGGERED_SHA+set} ];then
exit 0
fi

gitHubToken=$(vault read -field=value secret/ci/elastic-docs/docs_preview_cleaner)

if [ -n "$BUILDKITE_COMMAND_EXIT_STATUS" ] && [ "$BUILDKITE_COMMAND_EXIT_STATUS" -eq "0" ];then
status_state="success"
else
status_state="failure"
fi

githubPublishStatus="https://api.github.com/repos/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}/statuses/${GITHUB_PR_TRIGGERED_SHA}"
data='{"state":"'$status_state'","target_url":"'$BUILDKITE_BUILD_URL'","description":"Build finished.","context":"buildkite/docs"}'
echo "Setting buildkite/docs commit status to ${status_state}"
curl -s -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${gitHubToken}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${githubPublishStatus}" \
-d "${data}"
27 changes: 27 additions & 0 deletions .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -euo pipefail

# Configure the git author and committer information
export GIT_AUTHOR_NAME='Buildkite CI'
export GIT_AUTHOR_EMAIL='[email protected]'
export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL

# The reminder of this hook should only be invoked for builds triggered by the Buildkite PR bot
if [ -z ${GITHUB_PR_OWNER+set} ] || [ -z ${GITHUB_PR_REPO+set} ] || [ -z ${GITHUB_PR_TRIGGERED_SHA+set} ];then
exit 0
fi

gitHubToken=$(vault read -field=value secret/ci/elastic-docs/docs_preview_cleaner)

githubPublishStatus="https://api.github.com/repos/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}/statuses/${GITHUB_PR_TRIGGERED_SHA}"
data='{"state":"pending","target_url":"'$BUILDKITE_BUILD_URL'","description":"Build started.","context":"buildkite/docs"}'
echo "Setting buildkite/docs commit status to pending"
curl -s -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${gitHubToken}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${githubPublishStatus}" \
-d "${data}"
7 changes: 0 additions & 7 deletions .buildkite/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
set -euo pipefail
set +x

# Configure the git author and committer information
export GIT_AUTHOR_NAME='Buildkite CI'
export GIT_AUTHOR_EMAIL='[email protected]'
export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL


build_args=""
rebuild_opt=""
broken_links_opt=""
Expand Down
91 changes: 90 additions & 1 deletion .buildkite/scripts/build_pr.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,91 @@
#!/bin/bash
echo "nothing to see yet"
set -euo pipefail
set +x

# This script should only be invoked by the Buildkite PR bot
if [ -z ${GITHUB_PR_BRANCH+set} ] || [ -z ${GITHUB_PR_TARGET_BRANCH+set} ] || [ -z ${GITHUB_PR_NUMBER+set} ] || [ -z ${GITHUB_PR_BASE_REPO+set} ];then
echo "One of the following env. variable GITHUB_PR_BRANCH, GITHUB_PR_TARGET_BRANCH, GITHUB_PR_NUMBER, GITHUB_PR_BASE_REPO is missing - exiting."
exit 1
fi

# Set some metadata for build filtering capabilities
# https://buildkite.com/elastic/docs-build-pr/builds?meta_data[repo_pr]=tech-content_123
buildkite-agent meta-data set "repo_pr" "${GITHUB_PR_BASE_REPO}_${GITHUB_PR_NUMBER}"

rebuild_opt=""
build_args=""
TARGET_BRANCH=""

# Define build docs arguments
if [[ ${GITHUB_PR_COMMENT_VAR_REBUILD_OPT:="unset"} == "rebuild" ]];then
rebuild_opt=" --rebuild"
elif [[ ${GITHUB_PR_COMMENT_VAR_SKIP_OPT:="unset"} == "skiplinkcheck" ]];then
build_args+=" --skiplinkcheck"
if [[ ${GITHUB_PR_COMMENT_VAR_WARN_OPT:="unset"} == "warnlinkcheck" ]];then
build_args+=" --warnlinkcheck"
fi
fi

buildkite-agent \
annotate \
--style "info" \
--context 'docs-info' \
"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)"


if [[ "${GITHUB_PR_BASE_REPO}" != 'docs' ]]; then
# Buildkite PR bot for repositories other than the `elastic/docs` repo are configured to
# always checkout the master branch of the `elastic/docs` repo (where the build logic resides).
# We first need to checkout the product repo / branch in a sub directory, that we'll reference
# in the build process.
echo "Cloning the ${GITHUB_PR_BASE_REPO} PR locally"

git clone --reference /opt/git-mirrors/elastic-$GITHUB_PR_BASE_REPO \
[email protected]:elastic/$GITHUB_PR_BASE_REPO.git ./product-repo

cd ./product-repo &&
git fetch origin pull/$GITHUB_PR_NUMBER/head:$GITHUB_PR_BRANCH &&
git switch $GITHUB_PR_BRANCH &&
cd ..

build_args+=" --sub_dir $GITHUB_PR_BASE_REPO:$GITHUB_PR_TARGET_BRANCH:./product-repo"
else
# Buildkite PR bot for the `elastic/docs` repo is configured to checkout the PR directly into the workspace
# We don't have to do anything else in this case.

# Per https://github.com/elastic/docs/issues/1821, always rebuild all
# books for PRs to the docs repo, for now.
# When https://github.com/elastic/docs/issues/1823 is fixed, this
# should be removed and the original behavior restored.
rebuild_opt=" --rebuild"
fi


# Set the target branch and preview options
TARGET_BRANCH="${GITHUB_PR_BASE_REPO}_bk_${GITHUB_PR_NUMBER}"
PREVIEW_URL="https://${TARGET_BRANCH}.docs-preview.app.elstc.co"

build_cmd="./build_docs --all --keep_hash \
--target_repo [email protected]:elastic/built-docs \
--reference /opt/git-mirrors/ \
--target_branch ${TARGET_BRANCH} \
--push \
--announce_preview ${PREVIEW_URL}/diff \
${rebuild_opt} \
${build_args}"

echo "The following build command will be used"
echo $build_cmd

# Kick off the build
ssh-agent bash -c "ssh-add && $build_cmd"

buildkite-agent annotate \
--style "success" \
--context 'docs-info' \
--append \
"<br>Preview url: ${PREVIEW_URL}"

buildkite-agent meta-data set pr_comment:doc-preview:head " * Documentation preview
- 📚 [HTML diff](${PREVIEW_URL}/diff)
- 📙 [Preview](${PREVIEW_URL})"