MartijnvdB
MartijnvdB

Reputation: 1002

How to share environment variables in parallel Bitbucket pipeline steps?

So, I am using Bitbucket pipelines to deploy my application. The app consists of two components: 1 and 2. They are deployed in two parallel steps in the Bitbucket pipeline:

pipelines:
  custom:
    1-deploy-to-test:
      - parallel:
        - step:
            name: Deploying 1
            image: google/cloud-sdk:latest
            script:
              - SERVICE_ENV=test
              - GCLOUD_PROJECT="some-project"
              - MEMORY_LIMIT="256Mi"
              - ./deploy.sh
        - step:
            name: Deploying 2
            image: google/cloud-sdk:latest
            script:
              - SERVICE_ENV=test
              - GCLOUD_PROJECT="some-project"
              - MEMORY_LIMIT="256Mi"
              - ./deploy2.sh

The environment variables SERVICE_ENV, GCLOUD_PROJECT and MEMORY_LIMIT are always the same for deployments 1 and 2.

Is there any way to define these variables once for both parallel steps?

Upvotes: 2

Views: 5939

Answers (3)

davidwebca
davidwebca

Reputation: 441

To add to the other answers, here's a glimpse of how we currently handle this, inspired from all the other solutions found in Bitbucket's forums.

To allow parallel tasks to re-use deployment variables (currently cannot be passed between steps), we use the bash Docker image first to set environment variables in an artifact. The pure bash image is very fast (runs under 8 seconds usually).

Then all other tasks can run in parallel benefiting of the deployment and repository variables that we usually set, all of that bypassing the current Bitbucket Pipelines limitations.

definitions:
  steps:
    - step: &set_env
        name: Set multi-steps env variables
        image: bash:5.2.12
        artifacts:
          - set_env.sh
        script:
          ## Pass env
          - echo "Passing all env variables to next steps"
          - >-
            echo "
            export USERNAME=$USERNAME;
            export HOST=$HOST;
            export PORT_LIVE=$PORT_LIVE;
            export THEME=$THEME;
            " >> set_env.sh
    - step: &git_ftp
        name: Git-Ftp
        image: davidwebca/docker-php-deployer:8.0.25
        script: 
          # check if env file exists
          - if [ -e set_env.sh ]; then
          -   cat set_env.sh
          -   source set_env.sh
          - fi
          # ...

branches:
    staging:
      - parallel: 
        - step: 
            <<: *set_env
            deployment: staging
      - parallel:
        - step: 
            <<: *git_ftp
        - step: 
            <<: *root_composer
        - step: 
            <<: *theme_assets

According to the forums, Bitbucket staff is working on a solution to allow more flexibility on this as we speak (2022-12-01) but we shouldn't expect immediate release of this as it seems complicated to implement safely on their end.

Upvotes: 3

Gigi_men
Gigi_men

Reputation: 11

as was explained in this link, you can define the Environment Variables and copy them to a file.

After that you can share that file between steps as an Artifact.

Upvotes: 0

Alexander Zhukov
Alexander Zhukov

Reputation: 4547

You can use User-defined variables in Pipelines. For example, you can configure your SERVICE_ENV, GCLOUD_PROJECT and MEMORY_LIMIT as a Repository variables and they will be available to all steps in your pipeline.

Upvotes: 2

Related Questions