Is the `hashFiles` function _actually_ available in the `if` context?

I manage an organization with many repositories. Some of them have a Dockerfile, and other’s don’t. We use our own Github Actions job to build Docker images for repositories that have a Dockerfile.

We’d like to be able to use identical GitHub Actions workflow file across all repositories, to keep things easier to manage. However we only want to run the job to build Docker images in repositories that have a Dockerfile.

I looked at using the hashFiles function in the job’s if conditional, so that the job won’t run if there isn’t a Dockerfile.

Our workflow file ends up looking something like this:

  build-docker-images:
    if: ${{ hashFiles('Dockerfile') }}
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Build Docker images
        run: #our-code-to-build-images

However, this results in a startup error: Unrecognized function: 'hashFiles'.

The context documentation says that hashFiles should be available in the if conditional of a job, but is it actually?

1 Like

@orangejulius No luck here with hashFiles either, ever get this to work? I have the exact same use case.

1 Like

@randori-jay funny you should just mention this a few days ago.

I finally got things working and trick is to add the always() function call to the if: check as well.

So the example above would be

build-docker-images:
    if: ${{ always() && hashFiles('Dockerfile') }}
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - name: Build Docker images
        run: #our-code-to-build-images

You can see it in action here: docker-elasticsearch/push.yml at 599cd66629452e99b3109d99ab28945dad822e4e · pelias/docker-elasticsearch · GitHub