How can I write a multi-line if block on Bitbucket Pipeline?
Asked Answered
F

2

23

From here I learned that Bitbucket Pipeline supports ifs statements.

How do I do multi-line blocks inside if statements?

This doesn't compute:

    script:
      - if [ $BITBUCKET_BRANCH == "master" ];
        then;
          echo Line1
          echo line2
        fi;
Fauman answered 14/8, 2018 at 12:37 Comment(0)
G
18

I found that this works:

- if [ $BITBUCKET_BRANCH == 'master' ]; then
- echo "We are on master"
- fi
Geehan answered 14/8, 2018 at 16:12 Comment(0)
O
54

Bitbucket pipelines are written in YAML, so you can take full advantage of the YAML language.

For multiline, you can also use either | or > operators.

- >
  if [ $BITBUCKET_BRANCH == 'master' ]; then
    echo "We are on master :)"
  else
    echo "We are not on master :("
  fi

More information: https://yaml-multiline.info/

NB: I guess this use-case was just an example, but you can also filter pipelines steps by branches directly: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html#Configurebitbucket-pipelines.yml-SectionDescription

Omor answered 25/10, 2019 at 10:55 Comment(2)
this should be the accepted answer, so much better as in the pipeline runs as single statementCosgrove
for some reason using > operator was throwing syntax errors for the if else statement but using | worked perfectlyAtlantic
G
18

I found that this works:

- if [ $BITBUCKET_BRANCH == 'master' ]; then
- echo "We are on master"
- fi
Geehan answered 14/8, 2018 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.