How can you expand context that is passed in via an output

When passing data via the env as a string, the context is expanded, but if the env variable is populated via a file output, the context is not expanded.

this example below works fine

- name: Send Message
      env:
        MSG_TEMPLATE: '
        {
          "text": "*${{ github.workflow }}*\n`${{ job.status }}`"
        }
     run: echo ${{env.MSG_TEMPLATE}}

but if changed to the one below, it doesn’t work

id: gettemplate
run: echo ::set-output name=msg::$(cat template.json)
....
run: echo ${{ steps.gettemplate.outputs.msg }}

the first scenario described above outputs

{ text : “Workflow Name\nsucceeds” }

the second scenario outputs

{ “text”: “*{{ github.workflow }}*\n`{{ job.status }}`” }

How can I achieve the same results by passing the string as a “parameter”?

According to the logs , when you set the json content with context directly as the output variable value, it could be evaluated.


But when you use $(cat template.json) , it could only evaluate this cat command. It will not continue evaluate the json file content.

I would recommend you using sed to replace the value in json file, the context could expand in bash scripts. And then you cat json file content. Please see my code snippet :

      - id: gettemplate
        run: |
           sed -i '/text/c\   \"text\" : \"*${{ github.workflow }}*\n`${{ job.status }}`",' template.json
           message=$(cat template.json)
           echo ::set-output name=msg::$message
2 Likes

Thanks @yanjingzhu for the detailed response, it was really helpful. Seeing that the expansion is essentially a format() call, I used that idea to substitute in the placeholders.

env:
        MSG_TEMPLATE: ${{ format(steps.gettemplate.outputs.msg, github.workflow, job.status) }}

The json needed to be escaped in order for format() to work as per the docs, so it ended up looking like this

{{“text”:“Workflow Results for : {0} {1}”}}

I think it might be better using sed to escape the curly braces in a json automatically and using format() for the substitution

How were you able to see the debug info?, it is quite handy for knowing what is happening under the hood.

@Ajibola You could also enable step debug logging for your repo by adding ACTIONS_STEP_DEBUG to true in secrets.


Please refer to this document: https://help.github.com/en/actions/configuring-and-managing-workflows/managing-a-workflow-run#enabling-step-debug-logging

1 Like