skip to Main Content

I need to read file zip file name in GitHub actions. But in following code, both Artifact and Artifact1 are returning blank values. What is wrong with the code?

name: SCR Initial

on:
  push:
    branches:
      - main
    paths:
      - 'test/**zip'

jobs:
  SCR:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Artifact zip name
        run: |
          echo "Commit text = ${{ github.event.head_commit.message }}"
          echo "Artifact name= ${{ github.event.head_commit.added }}"
          echo "Artifact1 name = ${{ github.event.head_commit.added[0] }}"

I have checked in workflow logs and able to see blank.

2

Answers


  1. In a push event triggering a github actions workflow, the head_commit object payload doesn’t contain those message and added fields.

    This head_commit object is filled as follow (example from a personal repository):

    {
    ...
        "head_commit": {
          "author": {
            "email": "[email protected]",
            "name": "GuillaumeFalourd",
            "url": "https://api.github.com/users/GuillaumeFalourd"
          },
          "private": false,
          "pulls_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/pulls{/number}",
          "pushed_at": 1624727244,
          "releases_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/releases{/id}",
          "size": 230,
          "ssh_url": "[email protected]:GuillaumeFalourd/poc-github-actions.git",
          "stargazers": 2,
          "stargazers_count": 2,
          "stargazers_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/stargazers",
          "statuses_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/statuses/{sha}",
          "subscribers_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/subscribers",
          "subscription_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/subscription",
          "svn_url": "https://github.com/GuillaumeFalourd/poc-github-actions",
          "tags_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/tags",
          "teams_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/teams",
          "trees_url": "https://api.github.com/repos/GuillaumeFalourd/poc-github-actions/git/trees{/sha}",
          "updated_at": "2021-06-26T17:05:58Z",
          "url": "https://github.com/GuillaumeFalourd/poc-github-actions",
          "watchers": 2,
          "watchers_count": 2
        },
    ...
    }
    

    Here is an example of a full push event JSON if you want to check its structure.

    To access the last push commit message for example, you would need to use ${{ github.event.commits.[0].message }}

    Login or Signup to reply.
  2. ${{ github.event.head_commit.added }} does not exist any more, see this blog post for more information.

    In addition to what GuiFalourd already said, you can do something like this:

    git diff --diff-filter=A --name-only HEAD~1
    

    This will list only the files that were added (=A) in the latest commit.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search