Created
May 26, 2017 08:26
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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