yung peso
yung peso

Reputation: 1766

How to access this JSON object field in Github actions?

I'm trying to access github.event context which is in a json object format below:

{
  event: {
    after: 
    base_ref: 
    before: 
    commits: [
      {
        author: {
          email: 
          name: 
          username: 
        },
        committer: {
          email: 
          name: 
          username: 
        },
        message:
     ] ,
   },
}

I'm struggling trying to access and retrieve the author +committer email, name, and username fields.

I'm able to retrieve the message of the commit like so: github.event.commits[0].message, but cannot access the author and committer objects.

How can I access the embedded json objects within the array inside github.event ?

EDIT: I've tried the following: github.event.commits[0].author.name

and other similar actions, but no luck.

Upvotes: 2

Views: 4583

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 23050

To achieve what you want, you would need to convert the author and committer objects to JSON, using env variables.

Here is an example using jq to get the values:

      - name: Other variables with ENV
        run: |
          echo "Commits:" $COMMITS
          echo "Message:" $MESSAGE
          echo "Author Email:" $(echo $AUTHOR | jq -r '.email')
          echo "Author Name:" $(echo $AUTHOR | jq -r '.name')
          echo "Author Username:" $(echo $AUTHOR | jq -r '.username')
          echo "Committer Email:" $(echo $COMMITTER | jq -r '.email')
          echo "Committer Name:" $(echo $COMMITTER | jq -r '.name')
          echo "Committer Username:" $(echo $COMMITTER | jq -r '.username')
        env:
          COMMITS: ${{ toJSON(github.event.commits[0]) }}
          MESSAGE: ${{ toJSON(github.event.commits[0].message) }}
          AUTHOR: ${{ toJSON(github.event.commits[0].author) }}
          COMMITTER: ${{ toJSON(github.event.commits[0].committer) }}

You can check the tests I made here:

Upvotes: 1

Related Questions