CI/CD Pipeline Automation
DevOps Terraform Sprint β Day 7
Automate terraform plan & apply in CI/CD pipelines with pull-request exit code validation gates and self-healing deployment checks
Congratulations on reaching Day 7! In this capstone lab, you will tie together everything you learned over the past 6 days into a continuous integration pipeline. You will construct a complete GitHub Actions Workflow (or local CI pipeline script) that automatically runs terraform fmt, terraform validate, tflint, checkov, and terraform plan on every Pull Request.
An interactive 20-minute video walkthrough for Day 7 is currently in production. Follow the step-by-step interactive playbook below!
The verified Day 7 sample code is live in the public GitHub repository. Clone it to get the complete working solution immediately.
git clone https://github.com/letstrnsfrm-ai/devops-terraform-roadmap.git
cd devops-terraform-roadmap/day-7-cicd-pipelineConstructing the CI Quality Gate Workflow
.github/workflows/terraform-ci.yml.name: "Terraform Infrastructure Quality Gate"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
terraform-ci:
name: "Lint, Scan & Plan"
runs-on: ubuntu-latest
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
- name: Setup Terraform CLI
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.7.5
- name: 1. Enforce Code Formatting Check
run: terraform fmt -check
- name: 2. Validate HCL Syntax
run: |
terraform init -backend=false
terraform validate
- name: 3. Static Security Scan
uses: bridgecrewio/checkov-action@master
with:
framework: terraform
output_format: cli
- name: 4. Generate Execution Plan
run: terraform plan -no-color -out=tfplanLocal Pipeline Runner Simulation Script
ci-gate-check.sh:#!/usr/bin/env bash
set -e
echo "=== 1. Checking Code Formatting ==="
terraform fmt -check || { echo "β Code formatting error! Run 'terraform fmt'"; exit 1; }
echo "=== 2. Validating HCL Syntax ==="
terraform init -backend=false
terraform validate || { echo "β HCL syntax validation failed!"; exit 1; }
echo "=== 3. Generating Execution Plan ==="
terraform plan -no-color -out=tfplan || { echo "β Terraform plan failed!"; exit 1; }
echo "β
ALL QUALITY GATES PASSED! Ready for deployment."Executing & Testing the Pipeline Gate
chmod +x ci-gate-check.sh
./ci-gate-check.sh=== 1. Checking Code Formatting ====== 2. Validating HCL Syntax ===Success! The configuration is valid.β
ALL QUALITY GATES PASSED! Ready for deployment.Course Completion & Certification
# Delete plan files and workspace
rm -f tfplan ci-gate-check.sh
terraform destroy -auto-approveTake your DevOps & IaC skills to the next level with Part 2: Remote State & DynamoDB Locking, Kind/EKS Cluster Provisioning, Terraform Kubernetes & Helm Providers, Multi-Environment Architecture, and Automated GitOps with ArgoCD.