I have a use case wherein my repository works on multiple AWS Lambda functions. I am using bitbucket pipelines to test each of those python functions (based on the changeset condition) with pytest and pylint and send an email in case any of those fail.
All the steps are exactly same for all the functions except some things such as
1. the path where to run pytest
2. the message to send in email etc.
My pipeline looks something like this:
- step:
name: function 1
caches:
- pip
script:
- pip install --upgrade pip
- other common steps
- cd root_folder/function_1
- pytest
- other common steps
condition:
changesets:
includePaths:
- root_folder/function_1/**
- utility/scripts/**
after-script:
- echo $BITBUCKET_EXIT_CODE
- if [ $BITBUCKET_EXIT_CODE != 0 ]; then echo "Step failed";
- pipe: atlassian/email-notify:0.4.4
variables:
USERNAME: some username
PASSWORD: some password
FROM: $FROM_EMAIL
Other common variables
SUBJECT: 'function 1 Failed'
ATTACHMENTS:
'/opt/atlassian/pipelines/agent/build/root_folder/function_1/coverage/index.html'
- fi;
artifacts:
- test-reports/*
- step:
name: function 2
caches:
- pip
script:
- pip install --upgrade pip
- other common steps
- cd root_folder/function_2
- pytest
- other common steps
condition:
changesets:
includePaths:
- root_folder/function_2/**
- utility/scripts/**
after-script:
- echo $BITBUCKET_EXIT_CODE
- if [ $BITBUCKET_EXIT_CODE != 0 ]; then echo "Step failed";
- pipe: atlassian/email-notify:0.4.4
variables:
USERNAME: some username
PASSWORD: some password
FROM: $FROM_EMAIL
Other common variables
SUBJECT: 'function 2 Failed'
ATTACHMENTS:
'/opt/atlassian/pipelines/agent/build/root_folder/function_2/coverage/index.html'
- fi;
artifacts:
- test-reports/*
All the bold text is common to each step. Whereas only the italic part varies.
I have 10 such functions. I am looking a way out so that I dont have to repeat all these lines again and again. As you can see keeping two arguments which change with each step will solve the issue.
I cant use YAML anchors as there are a couple of changes here and there which are specific to each step.