DEV Community

Michel Sánchez Montells
Michel Sánchez Montells

Posted on • Edited on

4

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay