Search code examples
githubcontinuous-integrationgithub-actions

Simple echo of github.event fails in Github Actions


I just want to print out the github.event context in a GitHub Actions step, so I am doing the following

      - name: check context
        shell: bash
        run: echo ${{ toJSON(github.event) }}

However this fails as follows:

/home/runner/work/_temp/hd73999-5309-44cf-9218-9e2e3805d525.sh: line 2: after:: command not found
Error: Process completed with exit code 127.

(although the github.event DOES get printed before the error.

Why is that?

I am using the toJSON function because if I don't all that gets printed is:

Run echo Object
Object

Solution

  • Updated 10/23

    A better method avoid escaping issues is to dump the context to an environment variable and then just echo that out in bash.

    name: Context testing
    on: push
    
    jobs:
      dump_contexts_to_log:
        runs-on: ubuntu-latest
        steps:
          - name: Dump GitHub context
            env:
              GITHUB_CONTEXT: ${{ toJson(github) }}
            run: echo "$GITHUB_CONTEXT"
    
    

    You need to add quotes around the expression so it doesn't evaluate it as JS.

    From: https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log

    name: Context testing
    on: push
    
    jobs:
      dump_contexts_to_log:
        runs-on: ubuntu-latest
        steps:
          - name: Dump GitHub context
            id: github_context_step
            run: echo '${{ toJSON(github) }}'