I am implementing a GitOps solution using Bitbucket pipelines but am having difficulties incorporating GitOps commands using empty commits.
I set up a `default` pipeline that inspects the commit message to execute the GitOps action in order to deploy to specific environments. There is also a pipeline to handle pull requests to `main`. The issue is that the default pipeline always runs, but there is no graceful way to prematurely stop this pipeline if a command is not issued. Take this (paraphrased) example pipeline:
pipelines:
default:
- step:
name: Check GitOps Command
runs-on:
- self.hosted
script:
- export COMMIT_MESSAGE=$(git log --format=%B -n 1 $BITBUCKET_COMMIT)
- if [[ "${COMMIT_MESSAGE,,}" != *"[deploy]"* ]]; then
- echo "STOP_PIPELINE=true" >> $BITBUCKET_PIPELINES_VARIABLES_PATH
- exit 0
- fi
- export ENV=`echo $COMMIT_MESSAGE | cut -d ']' -f2 | xargs`
- echo "ENV=$ENV" >> $BITBUCKET_PIPELINES_VARIABLES_PATH
output-variables:
- ENV
- STOP_PIPELINE
- step:
name: 'Run Deployment'
trigger: manual # The trigger will always activate, unless exit 1 in previous step
script:
- if [ $STOP_PIPELINE ]; then exit 0; fi
- ./deploy_command $ENV
pull-requests:
'**':
- step:
name: 'Check branch and run tests'
script:
- if [ "${BITBUCKET_PR_DESTINATION_BRANCH}" != "main" ]; then printf 'target branch not main, skipping test'; exit; fi
- run_test_command
The `default` pipeline will run on every commit regardless if there is a gitops command in the commit message. A variable can be set in to prematurely end subsequent steps, but it will not prevent a manual trigger from being activated.
Is there a way to gracefully stop a pipeline without creating a failure event and subsequent notifications? That or a way to run pipelines for empty commits outside of "default"?
Welcome to the community.
Perhaps using an after-script may help in your case.
The idea is to gracefully exit the specific step using the STOP_PIPELINE condition.
Then add your deployment script in the after-script validating the "BITBUCKET_EXIT_CODE" of the actual step.
Regards,
Mark C
I think we were in a slightly similar situation years ago.
As far as I remember, Pipelines decides the "success or failure" state based on the OS level exit code of the last command. And you can maybe do tricks like piping the command output to an empty sink to "swallow" the exit code of the command...
Just a hint.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Aron - yeah, the pipeline will stop if a non-zero exit code is issued. This technique "works" but it causes a pipe failure which results in alerts and clutters the pipeline history with false-negative events.
I also tried to pre-empt execution of other steps by checking for the variable "STOP_PIPELINE" -- this, however, will not prevent a step with a manual trigger from executing and then preventing other pipelines from completing until the step is run.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.