Skip to content

Instantly share code, notes, and snippets.

@claytonrcarter
Last active December 18, 2023 15:31
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save claytonrcarter/76741031312bc95cbd975007744856bf to your computer and use it in GitHub Desktop.
Save claytonrcarter/76741031312bc95cbd975007744856bf to your computer and use it in GitHub Desktop.
Bash script to check GitLab pipeline status

A super simple bash script to check the status of a GitLab CI pipeline.

$ git push
...
$ git pipeline-status
Status of last pipeline for user/project on gitlab/master:
"pending"
...
$ git pipeline-status
Status of last pipeline for user/project on gitlab/master:
"running"
...
$ git pipeline-status
Status of last pipeline for user/project on gitlab/master:
"success"

Prerequisites

Aside from git and curl, you will need jq (see https://stedolan.github.io/jq/, or just brew install jq).

Your GitLab private access token needs to be set in an environment variable called GITLAB_API_PRIVATE_TOKEN.

Your GitLab remote needs to be named either gitlab or origin. If it's not, you can override those via the GITLAB_REMOTE environment variable.

~/bin needs to be in your $PATH

Install

  1. Copy the attached git-pipeline-status into your ~/bin directory
  2. Make it executable: chmod +x ~/bin/git-pipeline-status

Usage

git pipeline-status

How?!

"How does putting a random script in my ~/bin make this work?" See git source and this coderwall blog post

#!/bin/bash
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 https://gitlab.com/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 $REMOTE | sed -Ee 's/.*:(.+)\.git/\1/')
PROJECT_ENCODED=$(echo $PROJECT | sed -Ee 's/\//%2F/')
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# we could also use HEAD so long as no local commits have been made since push
SHA=$(git rev-parse $REMOTE/$BRANCH)
echo "Status of last pipeline for $PROJECT on $REMOTE/$BRANCH:" >&2
JSON=$(curl --header "PRIVATE-TOKEN: $GITLAB_API_PRIVATE_TOKEN" "https://gitlab.com/api/v4/projects/$PROJECT_ENCODED/repository/commits/$SHA" 2> /dev/null)
echo $JSON | jq .last_pipeline.status
@GusAntoniassi
Copy link

For GitLab grouped projects (eg. foo/bar/my-project) it's giving a 404 error.

I fixed it by adding a global flag to the regex in this line:

PROJECT_ENCODED=$(echo $PROJECT | sed -Ee 's/\//%2F/g')

@jpmc3630
Copy link

Thanks! One thing ..was getting error:
jq parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 2, column 1

So updated last line:
echo $JSON | tr '\r\n' ' ' | jq .last_pipeline.status

@c3rb3ru5d3d53c
Copy link

c3rb3ru5d3d53c commented Jun 22, 2021

This does not work with groups still if you have a stand-alone instance (not gitlab.com), to fix this change the following variables

PROJECT=$(git remote get-url origin | sed 's/https\?:\/\/[^\/]*\///;s/.git$//;')
PROJECT_ENCODED=$(echo $PROJECT | sed 's/\//%2F/g;')

This should fix any issues, the domain was being kept in the project path, because of this GitLab will return a 404 error.

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment