Search code examples
bitbucket-pipelines

How to pass parameter to Bitbucket pipeline pipe included in a step definition?


I have the following Bitbucket pipeline

# This image is used in both "Zip and Upload to S3 steps"
image: atlassian/default-image:3

pipelines:
  default:
    - step:
        script:
          # Include this step so that we can get a successful build when any
          # commit is made to a branch. This will allow us to merge to the
          # staging branch which requires a successful commit previously
          - echo "The current branch is $BITBUCKET_BRANCH"

  branches:
    staging:
      - step:
          # We have decided to include the Zip and upload to S3 sections of the
          # pipeline in the same step because of a bug in the pipeline within
          # artifacts. See https://jira.atlassian.com/browse/BCLOUD-21666 for
          # details.
          name: Zip & Upload to S3
          services:
            - docker
          oidc: true
          script:
            # Build the zip file containing the contents of the applications
            # folder
            - zip -r $BITBUCKET_REPO_SLUG.zip "applications"
            # Upload the zip file to S3
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                COMMAND: "upload"
                APPLICATION_NAME: $APPLICATION_NAME
                ZIP_FILE: $BITBUCKET_REPO_SLUG.zip
                S3_BUCKET: $S3_BUCKET_STAGING
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

      - step:
          name: Deploy code
          deployment: staging
          oidc: true
          script:
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                APPLICATION_NAME: $APPLICATION_NAME
                COMMAND: "deploy"
                DEPLOYMENT_GROUP: $DEPLOYMENT_GROUP_STAGING
                S3_BUCKET: $S3_BUCKET_STAGING
                WAIT: "true"
                FILE_EXISTS_BEHAVIOR: OVERWRITE
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

    production:
      - step:
          # We have decided to include the Zip and upload to S3 sections of the
          # pipeline in the same step because of a bug in the pipeline within
          # artifacts. See https://jira.atlassian.com/browse/BCLOUD-21666 for
          # details.
          name: Zip & Upload to S3
          services:
            - docker
          oidc: true
          script:
            # Build the zip file
            - zip -r $BITBUCKET_REPO_SLUG.zip "applications"
            # Upload the zip file to S3
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                COMMAND: "upload"
                APPLICATION_NAME: $APPLICATION_NAME
                ZIP_FILE: $BITBUCKET_REPO_SLUG.zip
                S3_BUCKET: $S3_BUCKET_PROD
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

      - step:
          name: Deploy code
          deployment: production
          oidc: true
          script:
            - pipe: atlassian/aws-code-deploy:1.1.1
              variables:
                AWS_DEFAULT_REGION: $AWS_REGION
                AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
                APPLICATION_NAME: $APPLICATION_NAME
                COMMAND: "deploy"
                DEPLOYMENT_GROUP: $DEPLOYMENT_GROUP_PROD
                S3_BUCKET: $S3_BUCKET_PROD
                WAIT: "true"
                FILE_EXISTS_BEHAVIOR: OVERWRITE
                VERSION_LABEL: $BITBUCKET_REPO_SLUG

As you can see, the staging and production branches are almost identical. The only differences are:

  1. The use of "staging" or "production" in the "deployment" variable
  2. The S3_BUCKET variable that is different depending on whether we are deploying to staging or production.

To avoid duplicate code, I want to create a step definition that I can reuse. So for example, the definition for the "Zip and Upload to S3" step would look something like below

definitions:
  steps:
    - step: &EchoCurrentBranch
        name: Echo current branch
        script:
          - echo "The current branch is $BITBUCKET_BRANCH"

    - step: &ZipAndUploadToS3
        name: ⬆️ Zip & Upload to S3
        services:
          - docker
        oidc: true
        script:
          # Build the zip file containing the contents of the applications
          # folder
          - zip -r $BITBUCKET_REPO_SLUG.zip "applications"
          # Upload the zip file to S3
          - pipe: atlassian/aws-code-deploy:1.1.1
            variables:
              AWS_DEFAULT_REGION: $AWS_REGION
              AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN
              COMMAND: "upload"
              APPLICATION_NAME: $APPLICATION_NAME
              ZIP_FILE: $BITBUCKET_REPO_SLUG.zip
              S3_BUCKET: <I want this variable to be changeable>
              VERSION_LABEL: $BITBUCKET_REPO_SLUG

But the problem is that I can't find a way to pass a different value for the S3 bucket into the pipeline for different calls to the defined step. How do I pass in a different value for the S3 bucket into the pipe depending on the branch that I am deploying to?

I know that it is possible to pass different values into step definitions (as shown in Reusable Bitbucket pipelines configuration with YAML anchors for example) but I can't find anywhere that shows how to pass a pipe variable into a defined step.


Solution

  • I see your definition mentions Deployments, but did you try to actually use it?

    It's a feature that allows you to define different values for the same variable depending on the environment.

    Populate your deployments with variables and adjust your pipeline definition. The result should look similar to

    pipelines:
      branches:
        staging:
          - step:
              <<: *&ZipAndUploadToS3
              deployment: upload-staging
          - step:
              <<: *&Deploy
              deployment: deploy-staging
    
        production:
          - step: 
              <<: *&ZipAndUploadToS3
              deployment: upload-production
          - step:
              <<: *&Deploy
              deployment: deploy-production
    

    More on Deployments: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Deployment-variables