Skip to content

Instantly share code, notes, and snippets.

@c3rb3ru5d3d53c
Created June 22, 2021 15:26
Show Gist options
  • Save c3rb3ru5d3d53c/f90a4b5022026fb0db0f870b4ada299e to your computer and use it in GitHub Desktop.
Save c3rb3ru5d3d53c/f90a4b5022026fb0db0f870b4ada299e to your computer and use it in GitHub Desktop.
GitLab Script to Check Pipeline Status of Current Branch
#!/bin/env bash
GITLAB_URL=<your-gitlab-url>
GITLAB_API_PRIVATE_TOKEN=<yout-gitlab-private-token>
if ! git status > /dev/null 2>&1; then
echo "ERROR: current directory is not a git repo" >&2
exit 1
fi
if [ -z "$GITLAB_API_PRIVATE_TOKEN" ]; then
echo "ERROR: private token not found" >&2
echo "Please set GITLAB_API_PRIVATE_TOKEN and try again" >&2
echo "Hint: visit $GITLAB_URL/profile/personal_access_tokens" >&2
exit 1
fi
if [ -n "$GITLAB_REMOTE" ]; then
REMOTE=$GITLAB_REMOTE
elif git remote | grep gitlab >/dev/null 2>&1; then
REMOTE=gitlab
elif git remote | grep origin >/dev/null 2>&1; then
REMOTE=origin
else
echo "ERROR: no suitable branch found. Checked 'gitlab' and 'origin'." >&2
echo "Try setting GITLAB_REMOTE variable to remote name and try again." >&2
exit 1
fi
if ! [ -x "$(command -v jq)" ]; then
echo 'ERROR: jq JSON parser required but not found. See https://stedolan.github.io/jq/' >&2
exit 1
fi
PROJECT=$(git remote get-url origin | sed 's/https\?:\/\/[^\/]*\///;s/.git$//;')
PROJECT_ENCODED=$(echo $PROJECT | sed 's/\//%2F/g;')
BRANCH=$(git rev-parse --abbrev-ref HEAD)
SHA=$(git rev-parse $REMOTE/$BRANCH)
JSON=$(curl --insecure --header "PRIVATE-TOKEN: $GITLAB_API_PRIVATE_TOKEN" "$GITLAB_URL/api/v4/projects/$PROJECT_ENCODED/repository/commits/$SHA" 2> /dev/null)
JSON=`echo $JSON | jq -r .last_pipeline.status`
echo "$PROJECT $REMOTE/$BRANCH: $JSON"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment