Multi-line reusable scripts in pipeline file
Asked by Steven Hayhurst:

I'm trying to tidy up my pipeline file but I'm having trouble using a script when it has multiple lines.

Take this example which works fine:

```
image: xxxx

definitions:
  # reusable scripts
  scripts:
    setQaEnvVariables: &setQaEnvVariables export USERNAME=$QA_USERNAME && export CONSUMERKEY=$QA_CONSUMER_KEY && export INSTANCEURL=$TEST_URL
    validate: &validate if ! grep -q '<types>' package/package.xml && ! grep -q '<types>' destructiveChanges/destructiveChanges.xml; then echo "Packages are blank, there's no changes to validate"; exit 0; fi
  # Reusable steps
  steps:
    - step: &validate-against-qa
        name: Validate Against QA
        script:
          - *setQaEnvVariables
          - *validate
pipelines:
  custom: # Pipelines that can only be triggered manually
    QA:
      - step: *validate-against-qa?
```

As soon as I try to use a multiline script block in **validate**, it throws compilation errors:

```
image: xxxx

definitions:
  # reusable scripts
  scripts:
    setQaEnvVariables: &setQaEnvVariables export USERNAME=$QA_USERNAME && export CONSUMERKEY=$QA_CONSUMER_KEY && export INSTANCEURL=$TEST_URL
    validate: &validate
      - |
        if ! grep -q '<types>' package/package.xml && ! grep -q '<types>' destructiveChanges/destructiveChanges.xml; then
          echo "Packages are blank, there's no changes to validate"
          exit 0
        fi
  # Reusable steps
  steps:
    - step: &validate-against-qa
        name: Validate Against QA
        script:
          - *setQaEnvVariables
          - *validate
pipelines:
  custom: # Pipelines that can only be triggered manually
  QA:
    - step: *validate-against-qa?
```

Its the same regardless of if I indent each line with its own **-** or use **- \|**

If I use exactly the same script directly inside **validate-against-qa** it works just fine, so I can only conclude that there's some restriction I'm not aware of around multi line scripts. Is what I'm trying to do possible? I'd really like to be able to use several scripts that are multiple lines long!

**Edit**: pasting code into a code block in here takes away all my spacing and indentation, so I've had to do it manually. If you spot an indentation error it's likely not in my actual file


Answers:
author: Steven Hayhurst
body: -duplicate comment that I dont seem to be able to delete


author: Tiago Jesus
body: I think it's not possible to do that.

I cannot use block merge on the script with bitbucket pipelines.

Please check this issue and vote, I believe it's your case too:

<https://jira.atlassian.com/browse/BCLOUD-21420>

comments: author: Steven Hayhurst
body: Thanks. I have managed to find a slightly less pretty and DRY way to get this to work however. Maybe this isn't officially supported, but it does work!

And oddly the solution to get the **validate** script working was to not demark lines with **-** or **- \|** at all. I still can't call scripts from other scripts, so thats where I lose some DRYness, but the steps are now way, way DRYer than I originally had it.

(note this is a sample of a much larger file. Previously it was over 250 lines, now this method has reduced it to around 180)

```
image: xxxx

definitions:
  # reusable scripts
  scripts:
    setQaEnvVariables: &setQaEnvVariables export USERNAME=$QA_USERNAME && export CONSUMERKEY=$QA_CONSUMER_KEY && export INSTANCEURL=$TEST_URL
    validate: &validate
      # if there's nothing to deploy or delete, skip step
      if ! grep -q '<types>' package/package.xml && ! grep -q '<types>' destructiveChanges/destructiveChanges.xml; then
        echo "Packages are blank, there's no changes to validate";
        exit 0;
      fi
  # Reusable steps
  steps:
    - step: &validate-against-qa
        name: Validate Against QA
        script:
          - *setQaEnvVariables
          - *validate
pipelines:
  custom: # Pipelines that can only be triggered manually
  QA:
    - step: *validate-against-qa?
```


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

yaml