github action triggered with files not in paths

I’m using the terraform actions workflow on pull_requests but my repo has a mix of terraform files and non terraform files.

All my terraform is under the directory

./terraform

---
name: 'Terraform fmt'
on:
  pull_request:
    paths:
      - 'terraform/**.tf'
jobs:
  terraform:
    name: 'Terraform'
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@master

      - name: 'Terraform Format'
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'fmt'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Terraform Init'
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'init'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The only thing different is the block under pull_requests where I’m specifying only when files in the terraform directory change that have a .tf extension. 

but the problem is that the workflow is always getting triggered

It works fine on my end with your code, workflow is triggered only when file change includes the tf file in directory ‘terraform’.

Please check the file changes tab in your pull request, make sure it doesn’t contain the tf files from directory ‘terraform’ indeed.

Ok so I think I see what I was misinterpreting.

If the entire PR itself has terraform files modified it will always run the terraform workflow. I was misintepreting the intended functionality. I’m pushing files that aren’t terraform files to the PR and the workflow is being triggered due to how github diffs the PR. It does something like git diff develop…feature-branch.

How would I only trigger the terraform workflow when changes to only terraform files are present. I’ve seen something like this

---
name: 'Terraform fmt'
on:
  push:
    paths:
      - '**.tf'
  pull_request:
    paths:
      - 'terraform/**.tf'
jobs:
  terraform:
    name: 'Terraform'
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@master

      - name: 'Terraform Format'
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'fmt'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Terraform Init'
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'init'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 Like

Yes, if you submit other file changes, they will be added to the pull request file change list, if the pull request already contains the terraform/**.tf file change,  the workflow will be triggered.

It’s not supported to only monitor ‘terraform/**.tf’ file change if pull request exists, you need to close the pull request firstly.