How to use pipes across steps with just one variable change

nagarro_bhavya July 21, 2021

I have a use case wherein I would like to use pipes in after scripts of my each step but with one variable change.

- step:
<<: *build-function
name: function1
script:
- *pre-build-steps
- cd abc/function1
- *build-steps
condition:
changesets:
includePaths:
- abc/function1/**
- abc/function3
after-script:
- echo $BITBUCKET_EXIT_CODE
- if [ $BITBUCKET_EXIT_CODE != 0 ]; then echo "Step failed";
- pipe: atlassian/email-notify:0.4.4
variables:
USERNAME: $AWS_SES_USERNAME
PASSWORD: $AWS_SES_PASSWORD
FROM: $FROM_EMAIL
TO: $TO_EMAIL
HOST: $HOST
PORT: $PORT
SUBJECT: 'Build Failed: function1'
ATTACHMENTS: '/opt/atlassian/pipelines/agent/build/abc/function1/coverage/index.html'
- fi;



- step:
<<: *build-function
name: function2
script:
- *pre-build-steps
- cd abc/function2
- *build-steps
condition:
changesets:
includePaths:
- abc/function2/**
- abc/function3
after-script:
- echo $BITBUCKET_EXIT_CODE
- if [ $BITBUCKET_EXIT_CODE != 0 ]; then echo "Step failed";
- pipe: atlassian/email-notify:0.4.4
variables:
USERNAME: $AWS_SES_USERNAME
PASSWORD: $AWS_SES_PASSWORD
FROM: $FROM_EMAIL
TO: $TO_EMAIL
HOST: $HOST
PORT: $PORT
SUBJECT: 'Build Failed: function2'
ATTACHMENTS: '/opt/atlassian/pipelines/agent/build/abc/function2/coverage/index.html'
- fi;

 

I have two questions:
1. Is there any way I can keep the pipe or the entire after-script as common (in definitions or something like that) and just change 1-2 variables depending on each step? So that I dont have to repeat 7-8 lines again and again. I have 20 steps and it will be excellent for me.

2. Is there a way, I can provide two variables in each step either through script (function name and function url) which can then be used in rest of the script, conditions and after-script. This will again help me keep the entire step in definition anchor (currently build-function is defined as anchor and has other common things such as cache, artifacts etc but couldnt make these common due to this small step specific change) and just use that variable.

 

2 answers

0 votes
Deleted user March 10, 2023

I was struggling with same stuff and got solution you can basically create definitions for pipes as well. Thanks to THIS ISSUE.

definitions:
  services:
    docker:
      memory: 1536

  pipes:
    - pipe: &triggerpipeline
        pipe: atlassian/trigger-pipeline:5.1.0
        variables:
          BITBUCKET_USERNAME: $BB_INTEGRATION_USER
          BITBUCKET_APP_PASSWORD: $BB_INTEGRATION_TOKEN
          REPOSITORY: ${SERVICE_TO_DEPLOY}
          REF_TYPE: "branch"
          REF_NAME: "pipelines"
          CUSTOM_PIPELINE_NAME: "main-to-feature-deploy"
          PIPELINE_VARIABLES: >
            [{
              "key": "BRANCH_TO_DEPLOY",
              "value": "${BITBUCKET_BRANCH}"
            },
            {
              "key": "SOURCE_REPO_NAME",
              "value": "$BITBUCKET_REPO_SLUG"
            }]
          WAIT: "true"

 And parallel steps

      - parallel:
          - step:
              name: Deploy Identity Service Main to Feature
              script:
                - echo "deploy identity service"
                - export SERVICE_TO_DEPLOY=identityservice
                - echo "Deploy service ${SERVICE_TO_DEPLOY}"
                - *triggerpipeline

          - step:
              name: Deploy Admin Service Main to Feature
              script:
                - echo "deploy admin service"
                - export SERVICE_TO_DEPLOY=adminservice
                - echo "Deploy service ${SERVICE_TO_DEPLOY}"
                - *triggerpipeline
0 votes
Mark C
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2021

Hi @nagarro_bhavya

Thank you for reaching out to the community.

AFAIK, I'm afraid it is not possible to use dynamic variables in Pipelines YAML at the moment.
You will have to define your variables before running your builds.
We do have an existing feature request for it that can be located through this link. - https://jira.atlassian.com/browse/BCLOUD-15849
You can upvote and watch it for now so that you'll be notified of any updates from our team when the feature becomes available on Bitbucket Cloud.

For the after-script to be used as YAML anchors, it's not also possible at the moment as you can only use step as an anchor.

You can, however, use custom variables in a custom step.
However, I'm not sure if this is applicable to you.

Regards,
Mark C

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events