Home > Software design >  Execute github actions workflow/job in directory where code was changed
Execute github actions workflow/job in directory where code was changed

Time:08-19

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

CodePudding user response:

GitHub Actions has path filtering you can take advantage of when you are working with workflows that are triggered off a push or push_request event.

For example say you have a monorepo with the directories, tf_1, tf_2, and tf_3. You can do something like below for when changes occur to the directory tf_1.

name: Demonstrate GitHub Actions on Monorepo 
on:
  push:
    branches:
      - master
    paths:
      - 'tf_1/**'

defaults:
  run:
    working-directory: tf_1 

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

For more details on path filtering, please refer to the GitHub Actions syntax documentation.

CodePudding user response:

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 to true, 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.

  • Related