Update main.yml #53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Terraform CI/CD | |
on: | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: [ main ] | |
workflow_dispatch: # Allows manual trigger for the entire workflow | |
jobs: | |
validate: | |
name: Terraform Validate | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Initialize Terraform | |
run: terraform init | |
- name: Terraform Validate | |
run: terraform validate | |
test: | |
name: Terraform Test | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Terraform Test (Linting and Static Analysis) | |
run: terraform fmt -check | |
plan: | |
name: Terraform Plan | |
runs-on: ubuntu-latest | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} | |
AWS_DEFAULT_REGION: "us-east-1" | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Use Terraform Cache | |
uses: actions/cache@v3 | |
with: | |
path: ~/.terraform | |
key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock') }} | |
restore-keys: | | |
${{ runner.os }}-terraform- | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Initialize Terraform | |
run: terraform init | |
- name: Terraform Plan | |
run: terraform plan -out=planfile | |
- name: Upload Plan | |
uses: actions/upload-artifact@v3 | |
with: | |
name: planfile | |
path: planfile | |
apply: | |
name: Terraform Apply | |
runs-on: ubuntu-latest | |
needs: plan | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} | |
AWS_DEFAULT_REGION: "us-east-1" | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Download Plan | |
uses: actions/download-artifact@v3 | |
with: | |
name: planfile | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Apply Terraform | |
run: terraform apply -input=false planfile | |
destroy: | |
name: Terraform Destroy | |
runs-on: ubuntu-latest | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} | |
AWS_DEFAULT_REGION: "us-east-1" | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Initialize Terraform | |
run: terraform init | |
- name: Destroy Terraform Resources | |
run: terraform destroy -auto-approve |