-
-
Save rbramwell/3ac407c4bf6eeaf7a9df71dc8c1fe9a6 to your computer and use it in GitHub Desktop.
git-trigger-build: Triggers a CI build by pushing an empty commit
This file contains 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 -e | |
# Stash the staged files if any. | |
NEEDS_UNSTASH=0 | |
if ! git diff --staged --exit-code >/dev/null; then | |
echo -ne '\033[1;32m' | |
echo -n 'Stashing the staged files' | |
echo -e '\033[0m' | |
git stash | |
NEEDS_UNSTASH=1 | |
# Ensure there are no staged files anymore | |
git diff --staged --exit-code >/dev/null | |
fi | |
COMMIT_DATE="$(date)" | |
COMMIT_MSG="Trigger build ($COMMIT_DATE)" | |
if git diff --exit-code HEAD~ >/dev/null; then | |
# Amend the last commit since it's empty already. | |
echo -ne '\033[1;32m' | |
echo -n 'Force-pushing the last empty commit' | |
echo -e '\033[0m' | |
git commit --allow-empty --amend --date="$COMMIT_DATE" --message="$COMMIT_MSG" | |
git push --force | |
else | |
# Create a new empty commit since the last commit is not empty. | |
echo -ne '\033[1;32m' | |
echo -n 'Pushing an empty commit' | |
echo -e '\033[0m' | |
git commit --allow-empty --message="$COMMIT_MSG" | |
git push | |
fi | |
# Unstash if stashed above. | |
if [[ "$NEEDS_UNSTASH" -ne 0 ]]; then | |
echo -ne '\033[1;32m' | |
echo -n 'Unstashing the staged files' | |
echo -e '\033[0m' | |
git stash pop | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment