Reputation: 4620
I have the following terraform scripts structure
environments
|- dev
|- staging
modules
Each environment has its own state stored in S3 and they create resources from the modules
directory.
How can I deploy all terraform environments at once by using GitHub Workflows without having to duplicate the init, plan, apply
steps for each env directory?
This is my current yml
file for deploying the dev
environment:
name: Terraform Deployment after push
on:
push:
branches:
- main
permissions:
id-token: write
contents: write
env:
AWS_REGION: us-east-1
jobs:
merged:
name: deploy
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.2.6
- name: Run terraform fmt check
id: fmt
run: terraform fmt -check -diff -recursive
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.ROLE }}
role-session-name: InfraDeployOnMerge
- name: Initialize Terraform
id: init
run: |
cd environments/dev
rm -rf .terraform.*
terraform init -input=false
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
id: plan
run: |
cd environments/dev
terraform plan -var="aws_region=${{ env.AWS_REGION }}" -var="aws_profile=" -input=false -no-color -out=tfplan \
&& terraform show -no-color tfplan
continue-on-error: true
- name: Apply Terraform
if: steps.plan.outcome == 'success'
id: apply
continue-on-error: true
run: |
cd environments/dev
terraform apply -input=false -no-color \
tfplan
Upvotes: 1
Views: 1494
Reputation: 44
You can use github matrix syntax to define multiple environments and use it to cd
to respective folder and apply.
Or if possible adapt terragrunt which makes your life easier to deploy to multiple accounts and environments.
Upvotes: 1