How can I use Environment Variables in Script steps?

I’m trying to execute a curl command in a script step. I’m basically trying to post a build status to our Bitbucket Server. As part of the data I send I want to pass the bitrise build URL and build number.

However it seems the environment variables are not substituted where I put them, instead the variable names are passed. Is there a way to get environment variable values injected in the script?

I also have a custom environment variable $PRCommit that I pass in when triggering the build, and replacing that in the script does work as I expect

Stripped down sample script:

curl -H "Content-Type: application/json" -X POST https://our.build.server/rest/build-status/1.0/commits/$PRCommit --data '{"state": "INPROGRESS", "key": "$BITRISE_BUILD_NUMBER", "url": "$BITRISE_BUILD_URL"}'

2 Likes

You’re doing it almost good :wink:

What you have to know about Bash (it seems you use a Bash script here) is that env var (or any other variable, expansion, etc.) are not handled in single quoted strings. E.g.

echo '$HOME'

will print $HOME, while

echo "$HOME"

will print the value of $HOME (e.g. /Users/myuser).

So, in the case you posted:

curl -H "Content-Type: application/json" -X POST "https://our.build.server/rest/build-status/1.0/commits/$PRCommit" --data "{\"state\": \"INPROGRESS\", \"key\": \"$BITRISE_BUILD_NUMBER\", \"url\": \"$BITRISE_BUILD_URL\"}"

should work.

Note: you have to escape double quotes inside the double quoted string!

4 Likes