Skip to content

Commit 3515da7

Browse files
committed
Better secrets handling
Secrets should be defined in pre-command hooks to benefit from redaction See https://buildkite.com/docs/pipelines/managing-log-output#redacted-environment-variables
1 parent bbde3c8 commit 3515da7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

.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_pr_commit_status.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ case $status_state in
2020
exit 1;;
2121
esac
2222

23-
gitHubToken=$(vault read -field=value secret/ci/elastic-docs/docs_preview_cleaner)
24-
2523
githubPublishStatus="https://api.github.com/repos/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}/statuses/${GITHUB_PR_TRIGGERED_SHA}"
2624
data='{"state":"'$status_state'","target_url":"'$BUILDKITE_BUILD_URL'","description":"'$description'","context":"buildkite/'$BUILDKITE_PIPELINE_SLUG'"}'
2725

2826
echo "Setting buildkite/docs commit status to ${status_state}"
2927
curl -s -L \
3028
-X POST \
3129
-H "Accept: application/vnd.github+json" \
32-
-H "Authorization: Bearer ${gitHubToken}" \
30+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
3331
-H "X-GitHub-Api-Version: 2022-11-28" \
3432
"${githubPublishStatus}" \
3533
-d "${data}"

0 commit comments

Comments
 (0)