PR
This is the core workflow that orchestrates other reusable workflows to ensure all required checks run in each PR. This is a combination of:
- OpenTofu Format Checks (
tofu fmt
) - OpenTofu Native Testing Checks (using
tofu test
) - OpenTofu Validation Checks (
tofu validate
) - OpenTofu Plan (
tofu plan
) - Infracost
- SAST (Static Application Security Testing)
- Linters
- terraform-docs
Note
You may notice OpenTofu Apply (tofu apply
) hasn't been mentioned above. This is because this is done following a merge to the base branch using GitHub Environments!
The workflow is presented below:
Configuration
As this is the first Action we're seeing. Lets breakdown some commonality you'll see amongst all workflows:
Name
Quite self explanatory - name of the GitHub Action.
On
Without starting a fight on the right way of defining a list in yaml
so I did both , this workflow only runs under the following conditions:
- Is a pull request
- Whether that pull request is
opened
,reopened
or synchronised (synchronize
) - If the target branch is either
dev
ormain
- Only if the pull request has changes to anything inside the
tofu/
directory
Info
All of these conditions must be true before the Action will execute!
Permissions
permissions
can be set at the workflow level and job level. With the job level taking precedence. This is a basic permissions, which gives the workflow permission to, say, use the checkout
GitHub Action to pull the code down.
Defaults
defaults
is used to set any default for all jobs and steps. In this case, it will ensure each step/command runs in the bash
shell.
Jobs
I won't go into too much detail about what jobs are and all the available arguments. You can find all that information out here: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobs.
I will cover this first one, and then the rest of the document will follow the order of the PR workflow jobs covering each workflow and its intended purpose.
Job ID
tofu-checks
is the name of the specific job.
Job Permissions
This reusable action also requires additional permissions, so like I mentioned before I have specified permissions at the job
level - which takes precedence.
contents
is set atread
so the PR can be checked outid-token
is set aswrite
so the JWT (JSON Web Token using OIDC (OpenID Connect)) can be requested to authenticate with AWS (this action requirestofu init
to be ran)pull-requests
is set aswrite
as it will post a comment to the PR with status checks
Warning
I could technically place this at the root of the workflow, but that would mean ALL jobs would have these permissions. To adhere to the principal of least privilege, I'm only specifying additional permissions for each job that actually needs them!
Uses
The uses
keyword points to another workflow (reusable) to run. It is defined at the following path: ./.github/workflows/tofu_checks.yml
Info
You might notice that a commit sha, or version is not specified. In this case, the workflow in GitHub Actions shows the following: kieran-lowe/gitops-2024/.github/workflows/tofu_plan.yml@refs/pull/57/merge
- as this workflow is part of my repo anyway, I haven't specified anything:
With
The with
keywords allows you to pass in inputs to the reusable workflow, in this case tf_var_file
which the ./.github/workflows/tofu_checks.yml
workflow expects.
As there are two environments: dev
and prod
we use some conditional logic using the github
context. The conditional works like this: ${{ condition && is_truthy || is_false }}
Now lets go into the jobs! We have 6 in total for this workflow:
Note
Each additional job/workflow has been documented in its own area to avoid making this page huge. The links for each are below!
tofu-checks
- Please visit: Tofu Checks
tofu-plan
- Please visit: Tofu Plan
sast-checks
- Please visit: SAST Checks
linters
- Please visit: Linter Checks
terraform-docs
- Please visit: Terraform Docs Check
infracost
- Please visit: Infracost
Info
You may notice additional workflows in the navigation menu. These are not called by this main orchestrator and run on their own cadences!