Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mullnerz/4c15647b5ed7c88c6409e6cf346e2c33 to your computer and use it in GitHub Desktop.
Save mullnerz/4c15647b5ed7c88c6409e6cf346e2c33 to your computer and use it in GitHub Desktop.
GitLab CI - cancel redundant running pipelines for branches
#!/usr/bin/env bash
set -euo pipefail
# Get GitLab API URL from project URL
GITLAB_API_URL="$(echo $CI_PROJECT_URL | cut -d'/' -f1,2,3)/api/v4"
# Query running pipelines for the same branch,
# process + filter them,
# finally cancel them with another API call
curl -sS -H "PRIVATE-TOKEN: $GITLAB_API_TOKEN" "${GITLAB_API_URL}/projects/$CI_PROJECT_ID/pipelines?ref=${CI_COMMIT_REF_NAME//\//%2F}&status=running" | \
jq '.[] | .id' | \
tail -n +2 | \
xargs -n 1 -I {} \
curl -sS -H "PRIVATE-TOKEN: $GITLAB_API_TOKEN" -X POST "${GITLAB_API_URL}/projects/$CI_PROJECT_ID/pipelines/{}/cancel"
@artemptushkin
Copy link

Here is an alternative version if someone interested in https://gist.github.com/artemptushkin/c932f9ab668457de0551371739fc4db1

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