Sharing varibles between pipelines

Stevedan Ogochukwu Omodolor Omodia
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!
January 17, 2025

At the moment, I have pipelines that run during a pull request and one when we merge to main. I want to be able to share data between them for version control but I am not able to do so. Just for more context, see example below:

pipelines:
pull-requests:
develop*:
-step: export dummy_data=1
branch:
main:
script:
- echo "$dummy_data"

 I tried a suggestions in one of the forums that recommended to use the trigger pipeline( if you have any other suggestions please let me know. 

Current the trigger pipeline is as follows(i include the promote pipe in the pull request pipe. 

 

step: &promote
name: Promote
script:
- source version.env
- BB_TOKEN=secret
- pipe: atlassian/trigger-pipeline:5.8.1
variables:
BITBUCKET_ACCESS_TOKEN: $BB_TOKEN
REPOSITORY: <name-of-repo>
REF_TYPE: 'branch'
REF_NAME: develop_branch
PIPELINE_VARIABLES: >
[
{
"key": "dummy_data",
"value": "1"
}
]
WAIT: 'true'


Every time I run with this step I get the following error, I am not sure what I am setting wrongly. 


✖ Error: {"error": {"message": "Bad request", "detail": "Requested selector is not found in bitbucket-pipelines.yml.", "data": {"key": "result-service.pipeline.selector-not-found", "arguments": {}}}} 


Is there anyway I can also do this without pasting the token in code, I am fund of that due to safety reasons. Is there a way to store this as a global variable that can be accessed by all repositories?

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 20, 2025

Hi and welcome to the community!

The variable PIPELINE_VARIABLES in the pipe is only available for custom pipelines. You cannot use it with any other type of pipeline.

You can use the pipe atlassian/trigger-pipeline to trigger a custom pipeline for the main branch while the pull-requests build is running, and also pass data to that custom pipeline. Please keep in mind that this build on main won't get triggered when there is a merge on main, it will get triggered when you call the pipe during the pull-requests build.

 

If you want to do this, you can do it the following way:

In the bitbucket-pipelines.yml file define a custom pipeline with variables. You can check the documentation here:

This is an example:

pipelines:
custom:
my-custom-pipeline:
- variables:
- name: dummy_data
- step:
script:
- echo "This is the dummy data: $dummy_data"

You can remove the definition for the main branch pipeline if you don't intend to run builds on main automatically on every push or merge.

It's important that the definition is present in the bitbucket-pipelines.yml file of the main branch, if you want to trigger this custom pipeline on main.

 

Then, in the bitbucket-pipelines.yml file of any develop* branches, you can add the atlassian/trigger-pipeline pipe so that it triggers the custom pipeline on the main branch as follows:

script:
- pipe: atlassian/trigger-pipeline:5.8.1
variables:
BITBUCKET_ACCESS_TOKEN: $BB_TOKEN
REPOSITORY: '<name-of-repo>'
REF_TYPE: 'branch'
REF_NAME: 'main'
CUSTOM_PIPELINE_NAME: 'my-custom-pipeline'
PIPELINE_VARIABLES: >
[{
"key": "dummy_data",
"value": "some-value-here"
}]
WAIT: 'true'

 

The value of the variable CUSTOM_PIPELINE_NAME needs to be the same as the name of the custom pipeline you want to trigger. I have highlighted both in bold.

In the variable PIPELINE_VARIABLES, the key of the variable needs to be the same as the name of the custom pipeline's variable. I have also highlighted these in bold.


If this solution doesn't work for you because you want to trigger a pipeline on main only when there is a push to main (and not during the pull-requests build), you can look into saving the data from the pull-requests build into a file that you store externally (e.g. in the Downloads section of the Bitbucket repo) and then have the main pipeline download and read this file. You can use our APIs for uploading and downloading a file from the Downloads of the repo:


With regards to the access token, it is certainly possible to store it as a variable. You need to have admin permissions in the repo to do that.

  1. Open the repository on Bitbucket's website

  2. Select Repository settings from the left sidebar

  3. In the new left sidebar, select the option Repository variables under the section titled Pipelines.

  4. In there, you can create a variable BB_TOKEN and you can reference it as $BB_TOKEN in your yml. I suggest leaving the option Secured checked when creating the variable, so the variable's value won't be visible in the build log. If a value matching a secured variable appears in the logs, Pipelines will replace it with $VARIABLE_NAME.

 

Please feel free to let me know how it goes and if you have any questions!

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events