Skip to content
Snippets Groups Projects
Unverified Commit 064e6f09 authored by Nicolas Quiniou-Briand's avatar Nicolas Quiniou-Briand
Browse files

cancel created jobs if a failure has been detected during a test

fail if GITLAB_API_TOKEN is null

don't use SCRIPT_DIR because it's used in functions.sh

tests jobs not started are in pending state
parent 3a636b33
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,7 @@ configure_and_check() {
COMMIT_REF_NAME_ENCODED=$(urlencode "$CI_COMMIT_REF_NAME")
COMMIT_SHA=$(git rev-parse HEAD~0)
[ -z "${GITLAB_API_TOKEN}" ] && die "not set: GITLAB_API_TOKEN"
[ -n "${GITLAB_API_TOKEN}" ] && die "not set: GITLAB_API_TOKEN"
# get SHA of latest pipeline scheduled with status=succes for that branch
SHA_LATEST_PIPELINE=$(curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
......
#!/bin/bash
set -o nounset -o pipefail -o errexit
# full path to dir of current script
SCRIPT_DIR=$(readlink -e $(dirname ${BASH_SOURCE[0]}))
# full path to root of PF sources
PF_SRC_DIR=$(echo ${SCRIPT_DIR} | grep -oP '.*?(?=\/ci\/)')
# path to all functions
FUNCTIONS_FILE=${PF_SRC_DIR}/ci/lib/common/functions.sh
source ${FUNCTIONS_FILE}
# GitLab API: https://docs.gitlab.com/ee/api/
configure_and_check() {
CI_PROJECT_ID=${CI_PROJECT_ID:-}
CI_PIPELINE_ID=${CI_PIPELINE_ID:-}
GITLAB_API_TOKEN=${GITLAB_API_TOKEN:-}
[ -n "${GITLAB_API_TOKEN}" ] || die "not set: GITLAB_API_TOKEN"
}
get_created_jobs() {
curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
-s "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs?scope[]=pending&per_page=100" \
| jq -r '.[].id'
}
cancel_job() {
local job_id=$1
curl --request POST \
--header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
-s "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/jobs/${job_id}/cancel" \
| jq -r '.status'
}
cancel_jobs() {
jobs_id=$(get_created_jobs)
if [ -z "$jobs_id" ]; then
echo "No jobs to cancel"
else
for job_id in $jobs_id; do
if [ $(cancel_job $job_id) = "canceled" ]; then
echo "$job_id canceled"
else
echo "Unable to cancel $job_id"
fi
done
fi
}
log_section "Configure and check"
configure_and_check
log_section "Cancelling jobs not started"
cancel_jobs
......@@ -31,7 +31,8 @@ configure_and_check() {
MAKE_TARGET=clean make -e -C ${TEST_DIR} ${CI_JOB_NAME}
fi
else
echo 'Failed tests, only halting VM'
echo 'Failed tests: cancelling jobs not started and halting VM'
${PF_SRC_DIR}/ci/lib/test/cancel-created-jobs.sh
MAKE_TARGET=halt make -e -C ${TEST_DIR} ${CI_JOB_NAME}
fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment