Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,830
Community Members
 
Community Events
184
Community Groups

How to use pipes across steps with just one variable change

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

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.
Sep 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