Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Sharing custom variables between script and after-script blocks

Pavan Prasad
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 9, 2024

I have a step that sets variables in the script section and gets accessed in the after-script section. However I have found that variables set in the script block are not available in the after-script block. I have read that both the blocks are run in the same container. Environment variables are accessible in both blocks.

Is it just not possible or am doing something wrong?

Here's an example:

```
- step: &run-variable-tests
     name: Run Variable Sharing Tests
       script:
         - export VARIABLE_1=mytestrun
         - VARIABLE_2=mytestenv-${ENV_NAME}
       after-script:
         - echo "VARIABLE_1=${VARIABLE_1}"
         - echo "VARIABLE_2=${VARIABLE_2}"
```
Output:
VARIABLE_1=
VARIABLE_2=

1 answer

1 accepted

0 votes
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 13, 2024

Hi @Pavan Prasad and welcome to the community!

This is expected behavior. Both blocks indeed run on the same container. However, Pipelines creates a script with all commands of your step's script and runs it. Then, it creates a new script with all the commands of your step's after-script, and runs it in a new session. Therefore, any variables created during the script will not be available in the after-script.

As a workaround, you can save the export command in a file and then source the file in the after-script.

We have a feature request for defining variables per step in the yml file (this, making them available to the after-script) that you can vote for here:

Please feel free to reach out if you have any questions.

Kind regards,
Theodora

Pavan Prasad
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 6, 2024

Thank you for your response, @Theodora Boudale. I am really looking forward to this feature being implemented. It will help make our pipeline more dynamic and simpler.

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 7, 2024

You are very welcome, and thank you for the feedback; I see that you also left a comment on the feature request. When there is an update, it is going to be posted in that feature reuqest.

Please feel free to reach out if you ever need anything else!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events