Obtaining Step Names as Environment Variables in Bitbucket Cloud Pipelines
Platform Notice: Cloud Only - This article only applies to Atlassian apps 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 $ATLASSIAN_ACCOUNT_EMAIL:$API_TOKEN | jq -r '.name')In the CURL command above, you are required to define the variables ATLASSIAN_ACCOUNT_EMAIL and API_TOKEN as repository or workspace variables for API basic authentication. The variable ATLASSIAN_ACCOUNT_EMAIL has as value the Atlassian account email address of a user with at least read access to the repo. The variable API_TOKEN has as value an API token for this account, with at least the scope read:pipeline:bitbucket.
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 $ATLASSIAN_ACCOUNT_EMAIL:$API_TOKEN | jq -r '.name')
- echo $BB_STEP_NAMEWas this helpful?