2022-01-16 16:20:15 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -ueo pipefail
|
|
|
|
|
|
|
|
# Each Buildkite build stores the derivation target map as a pipeline
|
|
|
|
# artifact. This script determines the most appropriate commit (the
|
2022-01-19 15:56:01 +01:00
|
|
|
# fork point of the current chain from HEAD) and fetches the artifact.
|
2022-01-16 16:20:15 +01:00
|
|
|
#
|
2022-01-19 15:56:01 +01:00
|
|
|
# New builds can be based on HEAD before the pipeline for the last
|
|
|
|
# commit has finished, in which case it is possible that the fork
|
|
|
|
# point has no derivation map. To account for this, up to 3 commits
|
|
|
|
# prior to HEAD are also queried to find a map.
|
2022-01-16 16:20:15 +01:00
|
|
|
#
|
|
|
|
# If no map is found, the failure mode is not critical: We simply
|
|
|
|
# build all targets.
|
|
|
|
|
2022-01-19 15:56:01 +01:00
|
|
|
: ${DRVMAP_PATH:=pipeline/drvmap.json}
|
2022-03-30 11:52:05 +02:00
|
|
|
: ${BUILDKITE_TOKEN_PATH:=~/buildkite-token}
|
2022-01-19 15:56:01 +01:00
|
|
|
|
2022-01-20 10:49:33 +01:00
|
|
|
git fetch -v origin "${BUILDKITE_PIPELINE_DEFAULT_BRANCH}"
|
|
|
|
|
2022-01-20 19:11:59 +01:00
|
|
|
FIRST=$(git merge-base FETCH_HEAD "${BUILDKITE_COMMIT}")
|
2022-01-20 10:49:33 +01:00
|
|
|
SECOND=$(git rev-parse "$FIRST~1")
|
|
|
|
THIRD=$(git rev-parse "$FIRST~2")
|
2022-01-16 16:20:15 +01:00
|
|
|
|
2022-01-20 10:49:33 +01:00
|
|
|
function most_relevant_builds {
|
|
|
|
set -u
|
2022-01-16 16:20:15 +01:00
|
|
|
curl 'https://graphql.buildkite.com/v1' \
|
|
|
|
--silent \
|
2022-03-30 11:52:05 +02:00
|
|
|
-H "Authorization: Bearer $(cat ${BUILDKITE_TOKEN_PATH})" \
|
2022-01-19 15:56:01 +01:00
|
|
|
-d "{\"query\": \"query { pipeline(slug: \\\"$BUILDKITE_ORGANIZATION_SLUG/$BUILDKITE_PIPELINE_SLUG\\\") { builds(commit: [\\\"$FIRST\\\",\\\"$SECOND\\\",\\\"$THIRD\\\"]) { edges { node { uuid }}}}}\"}" | \
|
2022-01-16 16:20:15 +01:00
|
|
|
jq -r '.data.pipeline.builds.edges[] | .node.uuid'
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p tmp
|
|
|
|
for build in $(most_relevant_builds); do
|
|
|
|
echo "Checking artifacts for build $build"
|
2022-01-19 15:56:01 +01:00
|
|
|
buildkite-agent artifact download --build "${build}" "${DRVMAP_PATH}" 'tmp/' || true
|
2022-01-16 16:20:15 +01:00
|
|
|
|
2022-01-19 15:56:01 +01:00
|
|
|
if [[ -f "tmp/${DRVMAP_PATH}" ]]; then
|
2022-01-16 16:20:15 +01:00
|
|
|
echo "Fetched target map from build ${build}"
|
2022-01-19 15:56:01 +01:00
|
|
|
mv "tmp/${DRVMAP_PATH}" tmp/parent-target-map.json
|
2022-01-16 16:20:15 +01:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|