Obtaining Step Names as Environment Variables in Bitbucket Cloud Pipelines
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
This article outlines how to obtain the step name as an environment variable in a Bitbucket Cloud Pipelines build.
The step name is not included in the default environment variables for Bitbucket Cloud Pipelines. We have a feature request for this:
Environment
Bitbucket Cloud Pipelines
Solution
One can utilize a REST API endpoint to retrieve the current step name within their specific step and then store it as an environment variable.
The specific endpoint to use:
GET /repositories/{workspace}/{repo_slug}/pipelines/{pipeline_uuid}/steps/{step_uuid}
API Documentation: Get a step of a pipeline
To store it as an environment variable, the API endpoint can be utilized in a CURL command and then the result can be filtered using the JQ command.
BB_STEP_NAME=$(curl --request GET --url "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_FULL_NAME}/pipelines/%7B${BITBUCKET_PIPELINE_UUID}%7D/steps/%7B${BITBUCKET_STEP_UUID}%7D?fields=name" -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD | jq -r '.name')
In the CURL command above, you are required to define the variables BITBUCKET_USERNAME and BITBUCKET_APP_PASSWORD as repository or workspace variables for API basic authentication.
Here's an example of YAML configuration:
image: atlassian/default-image:4
pipelines:
default:
- step:
name: "Some Step Name"
script:
- BB_STEP_NAME=$(curl --request GET --url "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_FULL_NAME}/pipelines/%7B${BITBUCKET_PIPELINE_UUID}%7D/steps/%7B${BITBUCKET_STEP_UUID}%7D?fields=name" -u $BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD | jq -r '.name')
- echo $BB_STEP_NAME