Send argument to yml anchor for a step in bitbucket-pipelines.yml
Asked Answered
R

1

14

I would like to send arguments when I call an anchor with bitbucket pipelines

Here is the file I am using, I have to call after-script because I need to push to a certain S3 bucket

definitions:
  steps:
    - step: &node-build
        name: Build React app
        image: node:lts-alpine
        script:
          - npm install --no-optional
          - npm run build
        artifacts:
          - build/**
    - step: &aws-ecr-s3
        name: AWS S3 deployment
        image: amazon/aws-cli
        script:
          - aws configure set aws_access_key_id "${AWS_KEY}"
          - aws configure set aws_secret_access_key "${AWS_SECRET}"

pipelines:
  branches:
    master:
      - step: *node-build
      - step:
          <<: *aws-ecr-s3
          after-script:
            - aws s3 cp ./build s3://my-app-site-dev --recursive
    staging:
      - step: *node-build
      - step:
          <<: *aws-ecr-s3
          after-script:
            - aws s3 cp ./build s3://my-app-site-uat --recursive

I am trying to do something like the following to not have to use that after-script part

definitions:
  steps:
    - step: &node-build
        name: Build React app
        image: node:lts-alpine
        script:
          - npm install --no-optional
          - npm run build
        artifacts:
          - build/**
    - step: &aws-ecr-s3 $FIRST-ARGUMENT
        name: AWS S3 deployment
        image: amazon/aws-cli
        script:
          - aws configure set aws_access_key_id "${AWS_KEY}"
          - aws configure set aws_secret_access_key "${AWS_SECRET}"
          - aws s3 cp ./build s3://${FIRST-ARGUMENT} --recursive

pipelines:
  branches:
    master:
      - step: *node-build
      - step: *aws-ecr-s3 my-app-site-dev
    staging:
      - step: *node-build
      - step: *aws-ecr-s3 my-app-site-uat
Reidreidar answered 29/8, 2021 at 20:13 Comment(0)
A
0

To the best of my knowledge, you can only override particular values of YAML anchors. Attempts to 'pass arguments' won't work.

Instead, Bitbucket Pipelines provide Deployments - an ad-hoc way to assign different values to your variables depending on the environment. You'll need to create two deployments (say, dev and uat), and use them when referring to a step:

pipelines:
  branches:
    master:
      - step: *node-build
          <<: *pushImage
          deployment: uat
    staging:
      - step: *node-build
          <<: *pushImage
          deployment: dev

More on Bitbucket Deployments:

https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Deployment-variables https://support.atlassian.com/bitbucket-cloud/docs/set-up-and-monitor-deployments/

Alephnull answered 21/1, 2022 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.