Skip to content

Instantly share code, notes, and snippets.

@trustin
Last active January 22, 2024 05:14
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save trustin/05cbb70e22fc5e7c8b5ffbd1f0d99c8b to your computer and use it in GitHub Desktop.
Save trustin/05cbb70e22fc5e7c8b5ffbd1f0d99c8b to your computer and use it in GitHub Desktop.
git-trigger-build: Triggers a CI build by pushing an empty commit
#!/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