6

I am new to bitbucket pipelines and trying to deploy my code via bitbucket pipelines by using javaScript. My question is can we declare variables like (ex: var flag = false) and then write if/else statements based the flag value.

Below is my pipelines.yml file

# This is a sample build configuration for JavaScript.
# Check our guides at https://confluence.atlassian.com/x/14UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: node:12.18.2

pipelines:
branches: # deploying as per branches
 feature/pocDepTerex: # poc master branch
    - step: 
       caches:
        - node
       script: 
         - node -v

Here, I want to declare a flag and run the script node -v only when the flag is true.

Please let me know if there is any way to do it in pipelines

nz_pree
  • 105
  • 1
  • 1
  • 7

2 Answers2

15

You sure can! Whilst you're probably used to writing a series of one-line commands in bitbucket-pipelines.yml, you can use standard YAML syntax to insert multiline content. For example:

image: node:12.18.2

pipelines:
branches: # deploying as per branches
 feature/pocDepTerex: # poc master branch
    - step: 
       caches:
        - node
       script: 
         - export MY_FLAG=true
         - |
           if [ "$MYFLAG" = true ]; then
             node -v
           fi

Full disclosure: I work for Atlassian, though not on the Bitbucket Pipelines team :)

daveruinseverything
  • 4,775
  • 28
  • 40
  • Hi, thanks for your answer. I got the below error on trying this way bash: /opt/atlassian/pipelines/agent/tmp/bashScript7015344215178746479.sh: line 5: syntax error near unexpected token `then' – nz_pree Jul 20 '20 at 09:36
  • That might be my bad - YAML multiline syntax suggests `>` to use "folded" style, where newlines are interpreted as spaces; or `|` to use "literal" style, where newlines are preserved. In an if/then statement, a newline or `;` is expected after `then`. I updated my answer, give it another try – daveruinseverything Jul 20 '20 at 10:47
  • now its giving following error: + export MY_FLAG=true - | if [ "$MYFLAG" = true ]; then node -v fi bash: /opt/atlassian/pipelines/agent/tmp/bashScript6392172108976259935.sh: line 7: syntax error: unexpected end of file – nz_pree Jul 21 '20 at 09:00
  • hey it seems a '-' was missing before fi. it worked when i added - fi :) thanks for your help – nz_pree Jul 21 '20 at 09:27
  • still wondering why bitbucket is not supporting conditional steps in their pipelines (only based on changesets...) – dstibbe Apr 17 '23 at 08:36
  • they don't even allow setting multiple directories for one cache. and their official documentation on "how to monorepo" suggests not to use a monorepo. LOL – Phil May 21 '23 at 14:40
12

The answer posted by daveruninseverything works perfectly fine when the steps involved are shell-type commands but not helpful when Bitbucket pipes are being used. (For more information on Bitbucket pipes click here)

Following line can be added just before an execution of Bitbucket pipe as shown below in order to implement a conditional:

- if [ "$BITBUCKET_WORKSPACE" != test ]; then exit 0 ; fi
- pipe: atlassian/aws-s3-deploy:0.2.4

The above added lines ensure that the pipe will be executed only if the workspace name is test.

Vishwas M.R
  • 1,341
  • 16
  • 23