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

Workspace, repository and environment variables for image, pipe and runner name | Dynamic Pipelines

Using variables in pipelines, such as for image and pipe names, is essential for enhancing flexibility and maintainability in CI/CD processes. By incorporating variables, teams can avoid hardcoding values, which often leads to repetitive code and increased risk of errors. This approach allows for easier updates and modifications, as changes can be made in one place rather than throughout the entire pipeline configuration. Additionally, utilizing variables promotes consistency across different pipelines, making it simpler to manage and scale projects, especially in larger organizations where multiple repositories may share similar configurations. Overall, leveraging variables streamlines the development workflow and fosters a more efficient and organized pipeline management strategy. 

 

In this article, I’m showing you how Dynamic Pipelines can substitute the variables in your pipeline with the values defined at the workspace, repository and deployment environment level.

 

Example walkthrough

As a variable name, we are using the IMAGE_TAG and this has been defined in the workspace:

WorkspaceVariables.png

For this example, the variable is not defined at any other levels (repository or deployment).

 

The test pipeline references the IMAGE_TAG variable as the image:

default:
  - step:
      name: 'Basic dynamic pipeline | Default'
      image: node:$IMAGE_TAG

 

When running the pipeline with the forge tunnel command the updated pipeline (logged with the console.log(`updated: ${JSON.stringify(updatedPipelineConfiguration, null, 4)}`); line) shows that the variable is correctly resolved.

While the original pipeline contains the unresolved $IMAGE_TAG:

INFO 13:25:16.086  dde183a8-d559-451d-8a16-0c7c2c466ad5  original: {
        "default": [
          {
             "step": {
               "image": {
                 "name": "node:$IMAGE_TAG"
               }

The variable is substituted as part of the dynamic pipeline function execution:

INFO. 13:25:37.426  59e4afae-1025-47d8-9319-b43e1b45bc8b  updated: {
        "default": [
          {
            "step": {
              "image": {
                "name": "node:20"
              }

 

The resolved variable value can also be seen in the pipelines UI when clicking on the "View the configuration" available when the pipeline is dynamically generate.

DynamicPipelineConfig.png

 

How can you replace a variable in a Bitbucket pipeline? (in less than 5 minutes)

At a high level, there are five steps required:

  • Setup Forge on your workstation

  • Clone the Dynamic Pipelines reference apps repository and follow the setup instructions of the 03.VariableSubstitution app

  • Define the desired variables in workspace, repository and/or deployment environment.

  • Add the app as the dynamic pipeline provider of either the workspace or the repository in the “Dynamic pipelines” menu of the workspace or the repository

  • Run a pipeline in your repository and observe how the variables are resolved based on the logic of the selected app

 

 

Check out our YouTube video  to see this app in action and how to set it up.

VariablesReplacement DynamicPipelines.png

 

Good to know

Before you continue, please note the following:

  • Dynamic pipelines is a Premium feature for Bitbucket Cloud. (Read more about Bitbucket Premium here)

  • Only workspace administrators can install an app so, unless you are the admin, you’ll need to ask an admin to perform the installation

 

 

FAQs

Q: What if I want to only use the variables defined at different levels?

A: The repository (03.VariableSubstitution ) contains three examples: one to substitute the variables defined at every level (workspace, repository and deployment environment), one to only substitute the variables defined at the workspace level and one to substitute the variables at the workspace and repository level but not the ones at the deployment environment level. These examples can be modified to implement the desired logic.

 

 

If you have any questions or get stuck at any stage during the setup, let us know here in the comments.

Cheers,
Caterina

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events