Problem with pass CIRCLE_BRANCH variable to POST body

hi, I have the code in my config file:

run:
command: |
if [ "$CIRCLE_BRANCH" = "test_branch" ] || [ "$CIRCLE_BRANCH" = "master" ]; then

               curl -X POST \
               --header "Circle-Token: $CIRCLECI_API_TOKEN" \
               --header 'Accept: application/json' \
               --header 'Content-Type: application/json' \
               --url https://circleci.com/api/v2/project/bitbucket/......... \
               --data '{ "branch": "$CIRCLE_BRANCH" }'     
 fi  

When I pass test_branch or master directly like this --data '{ "branch": "master" }'
It works fine. But I need to pass a parameter like above. When I run the config with parameter, I see info: Branch not found
I have tried "${CIRCLE_BRANCH}" also but does not work

Could you tell me how to pass a generic value there ?

The problem was solved.

curl -X POST \
               --header "Circle-Token: $CIRCLECI_API_TOKEN" \
               --header "Content-Type: application/x-www-form-urlencoded" \
               --url https://circleci.com/api/v2/project/bitbucket/......... \
               -d "branch: $CIRCLE_BRANCH"  
2 Likes

Unfortunately, the problem is not solved. It worked fine but now when I call this from branch_name = feature/B1

if [ "$CIRCLE_BRANCH" = "develop" ] || [ "$CIRCLE_BRANCH" = "feature/B1" ]; then

    curl -X POST \
    --header "Circle-Token: $CIRCLECI_API_TOKEN" \
    --header "Content-Type: application/x-www-form-urlencoded" \
    --url https://circleci.com/api/v2/project/bitbucket/......... \
    -d "branch: $CIRCLE_BRANCH"    <----- **develop branch is called here, i do not have develop now**

fi

echo $CIRCLE_BRANCH returns feature/B1 , but why in curl I see develop ??

Hi @problemator, and welcome to the CircleCI community!

Here are 2 options to pass the environment variable.

  1. Add a single quote and a double quote around the variable
curl -X POST \
               --header "Circle-Token: $CIRCLECI_API_TOKEN" \
               --header 'Content-Type: application/json' \
               --url https://circleci.com/api/v2/project/bitbucket/......... \
               --data '{ "branch": "'"$CIRCLE_BRANCH"'" }'
  1. Escape the double quotes
curl -X POST \
               --header "Circle-Token: $CIRCLECI_API_TOKEN" \
               --header 'Content-Type: application/json' \
               --url https://circleci.com/api/v2/project/bitbucket/......... \
               --data "{ \"branch\": \"$CIRCLE_BRANCH\" }"
2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.