Parametrize Your Custom Bitbucket Pipeline With Variables

montells

Michel Sánchez Montells

Posted on December 30, 2023

Parametrize Your Custom Bitbucket Pipeline With Variables

Bitbucket Pipelines provides a seamless integration for continuous integration and deployment (CI/CD) processes.

In this article, we'll go through the process of setting up a basic custom pipeline, using variables, and configuring default values for a smoother and more personalized workflow.

USE CASE

Sometimes we want to automate some amount of steps. Those steps will execute steps with different values.

One example I faced was to build automation for upgrading Postgres version.

The process consists of some regular steps. And the bitbucket pipeline may look like this:



pipelines:
  custom:
    - upgrade_postgres:
      - step:
        name: "some saving of old version"
        script:
          - ./dump_10.sh
      - step: 
        name: "Some steps for raising up new version"
        script:
          - ./load_11.sh


Enter fullscreen mode Exit fullscreen mode

The process consists of some regular steps that only differ on the version I want to upgrade from (10) and the version I want to upgrade to (11).

Instead of changing the bitbucket-pipeline file, for every iteration of upgrading version, we can use variables.



pipelines:
  custom:
    - variables: #list variable names under here
       - name: UP_PG_FROM
         default: '11'
         allowed-values:
           - '11'
           - '12'
           - '13'
           - '14'
       - name: UP_PG_TO
         default: '12'
         allowed-values:
           - '12'
           - '13'
           - '14'
           - '15'   
    - upgrade_postgres:
      - step:
        name: "some saving of old version"
        script:
          - ./build_${UP_PG_FROM}.sh
      - step: 
        name: "Some steps for raising up new version"
        script:
          - ./build_${UP_PG_TO}.sh


Enter fullscreen mode Exit fullscreen mode

Notice that in this snipped we set up variables with a set of possible values using keyword allowed-values and default values with keyword default

And we use those values with the syntax ${variable name}

This example could lead you to this once you click on Run Pipeline:

Image description

💖 💪 🙅 🚩
montells
Michel Sánchez Montells

Posted on December 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related