How to share a script across multiple repositories using the shared pipelines in Bitbucket Pipelines.

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

In Bitbucket Pipelines you can share your YML definition across multiple repositories using the Share Pipelines Configurations feature.

When using this feature, you can only share the YML definition. The repository importing the configuration does not have access to the content of the repository that's exporting the configuration.

Learn how to share a script across multiple repositories using the shared pipelines feature in Bitbucket Pipelines.

Solution

To address this issue, you can download the necessary file during build time execution without adding it to every repository where you want to run it.

Let's suppose you have a RepoA where you export a YML configuration and have a script file named test_script.sh at the root folder of that repository. You want to run the test_script.sh on RepoB.

On RepoA you can use the following YML configuration:

1 2 3 4 5 6 7 8 9 10 11 12 13 export: true image: atlassian/default-image:4 definitions: pipelines: share-pipeline-1: - step: name: 'Export script pipeline' script: - curl -s -S --user $BITBUCKET_USER:$BITBUCKET_APP_PASSWORD -L -O https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/RepoA/src/main/test_script.sh - chmod +x test.sh - sh test.sh

Explaining the script section:

  1. First, download the script file using curl. You can authenticate the request by storing your Username and App Password as environment variables.

  2. Make sure to use the chmod command to grant the script execution permission.

  3. Finally, you can execute the script as originally intended.

On RepoB, you can use the shared pipelines syntax to execute the pipeline build:

1 2 3 4 pipelines: custom: import-pipeline: import: RepoA:main:share-pipeline-1

Updated on March 17, 2025

Still need help?

The Atlassian Community is here for you.