How to get the changed directory and use it with terraform action?

Hi all~

We have a folder that contains all of our domains, and under each domain is the terraform configuration of the domain, like:

dns/
  example1.com/
    cdn.tf
    records.tf
  example2.com/
    mx.tf
    records.tf
  example3.com/
    txt.tf
    records.tf

We want to get the path of each changed folder and set it to the strategy matrix, and let Terraform use the path in the matrix to run.

If the following file changed:

dns/example1.com/cdn.tf
dns/example2.com/mx.tf
dns/example3.com/records.tf

How we get the path to the file like this:

dns/example1.com
dns/example2.com
dns/example3.com

and how do we set the paths into the matrix?

Is this possible? please help me, thanks.

@Teakowa ,

The are few points may be helpful:

  1. To get the parent directory of a file, you can do like as this.

    file=dns/example1.com/cdn.tf
    echo "$(dirname -- "$file")"
    

    output:

    dns/example1.com
    
  2. To get the list of the changed files, you can use the following command.

    git diff --name-only HEAD^ HEAD
    
  3. You can reference here to set dynamical matrix.

In your case, here is an example workflow as reference:

jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: get parent directory and set matrix
        id: set-matrix
        run: |
          git diff --name-only HEAD^ HEAD > files.txt
          tf_config=''

          while IFS= read -r file
          do
            echo "file = $file"
            parent_dir=$(dirname -- "$file")
            echo "parent_dir = $parent_dir"
            
            if [[ -z $tf_config ]]; then
              tf_config="{\"tf_config\":\"$parent_dir\"}"
            else
              tf_config="$tf_config, {\"tf_config\":\"$parent_dir\"}"
            fi
          done < files.txt

          tf_config="{\"include\":[$tf_config]}"
          echo "::set-output name=matrix::$tf_config"

  job2:
    needs: [job1]
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.job1.outputs.matrix)}}
    steps:
      . . .
1 Like

@Teakowa ,

How are things going?
Have you try as my above suggestion? Is it helpful to you?
Please try it, any update, feel free to tell me.

@Teakowa ,

As my above suggestion:

  • Using the command (git diff --name-only HEAD^ HEAD) to get the list of the changed files.
  • Using the command (dirname – “<file_path>”) to get the parent directory of the files.
  • Using a loop to set the dynamical matrix with the parent directories.

You can try as the steps in the suggestion, and please give the feedback if you have any update.

I’m sorry for the late reply.

Thanks for your help it works :relaxed:, but But I’ve got another problem.

If multiple files are changed in a directory, like this:

dns/example1.com/cdn.tf
dns/example1.com/records.tf
dns/example2.com/cdn.tf
dns/example2.com/records.tf

I’ll get an output like this:

dns/example1.com
dns/example1.com
dns/example2.com
dns/example2.com

There is duplication, how to remove duplicates?

You can pipe that through uniq, e.g. if you have the list of directories in a file:

cat dirs.txt | uniq