Created
July 24, 2018 19:17
-
-
Save mullnerz/4c15647b5ed7c88c6409e6cf346e2c33 to your computer and use it in GitHub Desktop.
GitLab CI - cancel redundant running pipelines for branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
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
Thank you it help people but keep in mind that it won't list and cancel the merge request MRs because the have
ref
values like this: