Skip to content

Instantly share code, notes, and snippets.

@raindev
Created May 26, 2017 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raindev/9df322599ffb5674d2d671b40be4ec9d to your computer and use it in GitHub Desktop.
Save raindev/9df322599ffb5674d2d671b40be4ec9d to your computer and use it in GitHub Desktop.
Buildkite checkout hook with retry (to mitigate "reference is not a tree" Git error)
#!/bin/bash
set -ex
if [ -d .git/ ]; then
git remote set-url origin "$BUILDKITE_REPO"
else
git clone "$BUILDKITE_REPO" .
fi
git clean -fdqx
git submodule foreach --recursive git clean -fdqx
if [ "$BUILDKITE_PULL_REQUEST" != false ]; then
git fetch -v origin refs/pull/"$BUILDKITE_PULL_REQUEST"/head
counter=1 limit=5;
while ! git checkout -f "$BUILDKITE_COMMIT" && [ "$counter" -lt "$limit" ]; do
sleep 5;
git fetch -v origin refs/pull/"$BUILDKITE_PULL_REQUEST"/head
counter="$(( counter+1 ))";
done
if [ $counter -eq $limit ]; then
exit 1
fi
elif [ "$BUILDKITE_COMMIT" = "HEAD" ]; then
git fetch -v origin "$BUILDKITE_BRANCH"
git checkout -f FETCH_HEAD
else
git fetch -v origin "$BUILDKITE_COMMIT"
git checkout -f "$BUILDKITE_COMMIT"
fi
git submodule sync --recursive
git submodule update --init --recursive --force
git submodule foreach --recursive git reset --hard
git clean -fdqx
git submodule foreach --recursive git clean -fdqx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment