Not for the first time I am looking for ways to cancel a pipeline from within a job.
True, I can have a job with a script
that returns a non-zero exit code like so.
my-job:
script:
- |
echo something
if whatever; then
echo else
exit 42
fi
This will fail the job and thus also the pipeline. It’ll be marked as failed rather than cancelled.
So, I tried to be clever and cancel the pipeline through the API like so:
my-job:
script:
- |
echo something
if whatever; then
echo else
curl --request POST --header "PRIVATE-TOKEN: $MY_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/pipelines/$CI_PIPELINE_ID/cancel"
fi
This works just fine but…the pipeline gets cancelled so quickly that not even the output buffer is flushed. Hence, I loose all echo ...
output.
So, is there a better way to cancel the pipeline on dynamic conditions? If not, how could I ensure the stdout buffer is flushed before I cancel it?