I am trying to implement a github actions workflow with a job which will plan and apply my terraform code changes only for directory where changes were made. The problem I am currently facing is that I can’t figure out how to switch directories so that terraform plan is executed from a directory where code has been updated/changed.
I have a monorepo setup which is as follow:
repo
tf-folder-1
tf-folder-2
tf-folder-3
Each folder contains an independent terraform configuration. So, for example I would like run a workflow only when files change inside tf-folder-1. Such workflow needs to switch to working directory which is tf-folder-1 and then run terraform plan/apply.
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./tf-folder-1
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials from Test account
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::000000000000000:role/deploy-role
aws-region: eu-west-2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
...
So far, I have the above terraform job but it only runs for statically defined working-directory. It doesn’t work with a use case where it should run the workflow when changes happen within specific folder. Can someone advise how to fix this pipeline?
Thanks
3
Answers
Here is the solution to run multiple jobs based on number of directories that have been update.
In the below snippet you can see
directories
job which will check which directories have been updated, later it output an array or switch which is then used inmatrix
strategy for terraform job.GitHub Actions has path filtering you can take advantage of when you are working with workflows that are triggered off a
push
orpush_request
event.For example say you have a monorepo with the directories,
tf_1
,tf_2
, andtf_3
. You can do something like below for when changes occur to the directorytf_1
.For more details on path filtering, please refer to the GitHub Actions syntax documentation.
You can use a GitHub action that outputs the directories where the files have changed/modified, for example, this one: Changed-files or even perform the calculation with a shell step using
git diff
.If you use the GHA suggested you can set the input
dir_names
totrue
, which would output unique changed directories instead of filenames, based on the results of that you can change the directory to run your Terraform operations.