Use a variable exported in a custom script in Bitbucket Pipelines
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
Let's say the user has an external script to do some deployment work. The user calls the script in the script section of the pipeline as below:
pipelines:
default:
- step:
script:
- ./script.sh
In the script, they want to export some variables to use in other commands within the same step in Bitbucket Pipelines.
Here is an example of the script content:
#!/bin/bash
export TEST_VARIABLE='Test variable.'
Environment
Bitbucket Pipelines
Diagnosis
In this scenario, if the user tries to use $TEST_VARIABLE outside the script it won't work. Example:
pipelines:
default:
- step:
script:
- ./ script.sh
- echo $TEST_VARIABLE
Cause
To have those variables available, the script must be executed in the context of the calling shell.
Solution
Do not call the script with the "./" syntax, use one of the following options instead:
. script.sh
or
source script.sh
Example of a working Pipeline for this scenario:
pipelines:
default:
- step:
script:
- source script.sh
- echo $TEST_VARIABLE
If the above instructions do not assist in achieving the desired outcome, please raise a support ticket or raise a community support ticket for further assistance with this.