Skip to content

Instantly share code, notes, and snippets.

@chilicat
Last active November 16, 2021 22:08
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 chilicat/ef5e80b482b1cc4d36d37cccc767a225 to your computer and use it in GitHub Desktop.
Save chilicat/ef5e80b482b1cc4d36d37cccc767a225 to your computer and use it in GitHub Desktop.
Gitlab: Helper script to push changes as part of a Gitlab-CI pipeline
#!/bin/bash -e
# Helper script to push changes as part of a Gitlab-CI pipeline
# The script requires a user name (GITLAB_PUSH_USER) and token (GITLAB_PUSH_TOKEN)
# Example Usage:
# gitlab-push.sh "My commit message 1" gen-doc/*.html
# gitlab-push.sh "My commit message 2" my/file1 and/another/file2
COMMENT=$1
# remove comment from arguments
shift
function fata() {
echo "$1"
exit 1
}
[[ "$GITLAB_PUSH_USER" == "" ]] && fatal "[ERROR] GITLAB_PUSH_USER is not set."
[[ "$GITLAB_PUSH_TOKEN" == "" ]] && fatal "[ERROR] GITLAB_PUSH_TOKEN is not set."
[[ "$CI_SERVER_HOST" == "" ]] && fatal "[ERROR] CI_SERVER_HOST is not set."
[[ "$CI_PROJECT_PATH" == "" ]] && fatal "[ERROR] CI_PROJECT_PATH is not set."
git config user.email "${GITLAB_PUSH_USER}@myemail.de"
git config user.name "${GITLAB_PUSH_USER}"
git add $@ ||:
CHANGES=$(git status --porcelain | wc -l)
if [ "$CHANGES" -gt "0" ]; then
git status
git commit -m "$COMMENT"
# Do not fail if push_origin already set
git remote | grep push_origin > /dev/null || {
git remote add push_origin https://${GITLAB_PUSH_USER}:${GITLAB_PUSH_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git
}
git push push_origin HEAD:${CI_COMMIT_REF_NAME} -o ci.skip
echo "[INFO] Success"
else
echo "[INFO] Nothing to commit/push"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment