skip to Main Content

Observe the following, noting that inputs.root-path is set to env.

- name: DEBUG
  run: |
    set -x
    ls -l repo/${{ inputs.root-path }}/package-lock.json
    echo "HASH: ${{ hashFiles('repo/${{ inputs.root-path }}/package-lock.json') }}"
    echo "HASH: ${{ hashFiles('repo/env/package-lock.json') }}"

Output when run via GHA ubuntu-22.04 runner:

-rw-r--r-- 1 runner docker 474757 Nov 15 16:37 repo/env/package-lock.json
HASH: 
HASH: faedfd4f973187a14d71ddedb368bb5d6b76c22cf03838fe3e5e5a0335b7ab35

Note that the first call to hashFiles does not seem to correctly "intepret" the argument 'repo/${{ inputs.root-path }}/package-lock.json'. Is this "just how it works"? How can I call hashFiles with a variable input?

2

Answers


  1. You can use the format function.

    - name: DEBUG
      run: |
        set -x
        ls -l repo/${{ inputs.root-path }}/package-lock.json
        echo "HASH: ${{ hashFiles(format('repo/{0}/package-lock.json', inputs['root-path'])) }}"
    

    See also:

    Note: The solution is not tested

    Login or Signup to reply.
  2. Currently, such nesting of expressions is not supported.

    However, hashFiles function can be provided its input in different ways e.g.:

    • By setting an environment variable:
    - env:
        FILE: repo/${{ inputs.root-path }}/package-lock.json
      run: echo "${{ hashFiles(env.FILE) }}"
    
    • By using * glob pattern if there’s only one intermediate directory and only one such file exists depending on your use case:
    - run: echo "${{ hashFiles('repo/*/package-lock.json') }}"
    
    • By using ** glob pattern for multiple intermediate directories:
    - run: echo "${{ hashFiles('repo/**/package-lock.json') }}"
    
    - run: echo "${{ hashFiles(format('repo/{0}/package-lock.json', inputs.root-path) }}"
    

    or, using an intermediate env var:

    - env:
        FILE: ${{ format('repo/{0}/package-lock.json', inputs.root-path) }}
      run: echo "${{ hashFiles(env.FILE) }}"
    
    - uses: actions/github-script@v7
      id: hash
      with:
        result-encoding: string
        script: return glob.hashFiles('repo/${{ inputs.root-path }}/package-lock.json');
    
    - name: Check
      run: echo "${{ steps.hash.outputs.result }}"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search