hashFiles in github action displays incorrect hash

Viewed 1934

I am running the following code in github actions

- name: Verify Workflow integrity
  run: |
        echo "some text" > test/apps/test/test.yml
        cat test/apps/test/test.yml
        
        echo "${{ hashFiles('test/apps/test/*.yml') }}"

        echo "alter text" > test/apps/test/test.yml
        cat test/apps/test/test.yml
        echo "${{ hashFiles('test/apps/test/*.yml') }}"

It produces the following output

| some text
| e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
| alter text
| e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Since the text in the file has changed, I am expecting a different hash. However, it produces the same hash. Am I missing something?

basically, I want to find the hash of all the files in test/apps/test directory

2 Answers

The whole "script" in your run section is executed at once. Before execution, the "script" is being evaluated and all variables and/or function calls are resolved.

So before the "script" is being executed, hashFiles('test/apps/test/*.yml') is being evaluated (twice) with the result being e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. Then the following resolved version of your "script" is being executed:

            echo "some text" > test/apps/test/test.yml
            cat test/apps/test/test.yml
            
            echo "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    
            echo "alter text" > test/apps/test/test.yml
            cat test/apps/test/test.yml
            echo "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

So everything that happens in that script is not being considered, because all hashFiles() calls are resolved before everything is being executed.

A solution would be to split things up in multiple runs:

    - name: Verify Workflow integrity
      run: |
            echo "some text" > test/apps/test/test.yml
            cat test/apps/test/test.yml
    - run: echo "${{ hashFiles('test/apps/test/*.yml') }}"
    - run: |
            echo "alter text" > test/apps/test/test.yml
            cat test/apps/test/test.yml
    - run: echo "${{ hashFiles('test/apps/test/*.yml') }}"

First, try the same test with the exact name file instead of a wildcard:

 echo "${{ hashFiles('test/apps/test/test.yml') }}"

(There was an issue on hashfiles with actions/runner, but it is supposed to be fixed)

Second, try with double-quotes instead of single quotes (strong quotes) in order to make sure the '*' wildcard is interpreted by the shell.

 echo "${{ hashFiles("test/apps/test/*.yml") }}"
Related