Search code examples
github-actions

Github action generationg json payload with env variables


I am having problem generating some basic json that I use to call api for processing. Eg I have:

env:
  API: .....
  BRANCH_NAME: ${GITHUB_REF##*/}

- name: call api
    uses: privateaction
    with:
      API: ${{ env.AWS_LAMBDA_NAME }}
      Payload: '{"Name": "${env.BRANCH_NAME}" }'

further down I use the same variable as:

${{ env.BRANCH_NAME }}

And there it gets the right branch name and works. But when I use it in the API call in different ways I get: ${env.BRANCH_NAME} as input to the api and ${GITHUB_REF##*/}

I feel like I keep going in circles. The idea is I am going to need a bigger json structure with more fields. But I can't really get it to work with just 1.

I feel like I have tried all combinations of what could be valid eg. ${{env.BRANCH_NAME}} ${env.BRANCH_NAME} $env.BRANCH_NAME etc.

Most likely just something stupid from my side. Would love some help :)

I know the action works. Tried with hardcoded payload eg: '{"Name": "Joshua"}'. So it is something with the variable.


Solution

  • So what I ended up doing to make it work per @GuiFalourd comment was to move payload generation into it's own step like so:

    - name: Create JSON Payload
        run: echo "JSON_PAYLOAD={\"Name\":\"${{ env.BRANCH_NAME }}\"}" >> $GITHUB_ENV
    

    and use it further down like this:

    Payload: ${{ env.JSON_PAYLOAD }}