Bitbucket Cloud : Conditionally skip stage or step using variables

524 Views Asked by At

We have several stages consisting of one or more steps. We would like ability to define if a stage should be executed based on condition declared as workspace or repository variable.

Looked at the schema but condition does not let us use variables as intended. https://bitbucket.org/atlassianlabs/intellij-bitbucket-references-plugin/raw/master/src/main/resources/schemas/bitbucket-pipelines.schema.json

pipelines:
  default:
     - stage:          
         name: Deploy to QA
         deployment: qa
         condition: ${QA_STAGE_ENABLED} == "true"
         trigger: manual
         steps:
           - step:

condition: ${QA_STAGE_ENABLED} == "true" is what is being recommended. At the moment the only way to skip a stage is to comment it out in bitbucket-pipelines.yaml file which is not the most pleasant experience.

1

There are 1 best solutions below

2
Asif On

Bitbucket Pipeline's condition clause only works for changesets that too for just includePath condition as of now. If you want to use a variable or an artifact derived value to skip an step, you can implement a bash-style IF condition as the first step of your script, it will execute the step and mark it as done but you can skip any lines of code you do not want to execute as part of this step, for example:

pipelines:
  default:
     - stage:          
         name: Deploy to QA
         deployment: qa
         trigger: manual
         steps:
           - step:
            name: Deploy
            script:
              - if [[ ${QA_STAGE_ENABLED} != "true" ]]; then printf 'QA Stage deployment is not enabled, exiting.'; exit; fi
              - sh ./deploy-app.sh