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"?
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.