Merge pull request #81 from nafiuishaaq/feat/sync #70
This file contains hidden or 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
| # ============================================================================ | |
| # GitHub Actions Terraform Workflow | |
| # ============================================================================ | |
| # This workflow automates Terraform operations including: | |
| # - Plan on pull requests | |
| # - Apply on merges to main | |
| # - Security compliance checks | |
| # - Infrastructure testing | |
| name: Terraform Infrastructure | |
| on: | |
| push: | |
| branches: [main, develop] | |
| pull_request: | |
| branches: [main, develop] | |
| workflow_dispatch: | |
| env: | |
| TF_VERSION: '1.6.0' | |
| AWS_REGION: 'us-east-1' | |
| jobs: | |
| # ============================================================================= | |
| # Terraform Validate | |
| # ============================================================================= | |
| terraform-validate: | |
| name: Terraform Validate | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ env.TF_VERSION }} | |
| - name: Terraform Format Check | |
| run: | | |
| terraform fmt -check -recursive -diff | |
| working-directory: terraform | |
| - name: Terraform Init | |
| run: | | |
| terraform init -backend=false | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| env: | |
| TF_WORKSPACE: ${{ matrix.environment }} | |
| - name: Terraform Validate | |
| run: | | |
| terraform validate | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| - name: Terraform Docs | |
| uses: terraform-docs/gh-actions@v1.0.0 | |
| with: | |
| working-dir: terraform | |
| output-file: README.md | |
| output-method: inject | |
| strategy: | |
| matrix: | |
| environment: [dev, staging, prod] | |
| # ============================================================================= | |
| # Terraform Plan | |
| # ============================================================================= | |
| terraform-plan: | |
| name: Terraform Plan (${{ matrix.environment }}) | |
| runs-on: ubuntu-latest | |
| needs: terraform-validate | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ env.TF_VERSION }} | |
| - name: Terraform Init | |
| run: terraform init | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| env: | |
| TF_WORKSPACE: ${{ matrix.environment }} | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| - name: Terraform Plan | |
| id: plan | |
| run: terraform plan -no-color -out=tfplan | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| env: | |
| TF_WORKSPACE: ${{ matrix.environment }} | |
| - name: Post Plan to PR | |
| if: github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const output = `#### Terraform Format and Style 🖌\`${{ steps.plan.outcome }}\` | |
| #### Terraform Initialization ⚙️ | |
| \`\`\` | |
| ${{ steps.plan.outputs.stdout }} | |
| \`\`\` | |
| #### Terraform Plan 📖 | |
| <details><summary>Show Plan</summary> | |
| \`\`\` | |
| ${{ steps.plan.outputs.stdout }} | |
| \`\`` | |
| </details> | |
| *Pushed by: @${{ github.actor }}, Action: `${{ github.event_name }}`*`; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: output | |
| }) | |
| - name: Plan Status | |
| if: steps.plan.outcome == 'failure' | |
| run: exit 1 | |
| strategy: | |
| matrix: | |
| environment: [dev, staging, prod] | |
| # ============================================================================= | |
| # Terraform Apply | |
| # ============================================================================= | |
| terraform-apply: | |
| name: Terraform Apply (${{ matrix.environment }}) | |
| runs-on: ubuntu-latest | |
| needs: terraform-plan | |
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ env.TF_VERSION }} | |
| - name: Terraform Init | |
| run: terraform init | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| env: | |
| TF_WORKSPACE: ${{ matrix.environment }} | |
| - name: Terraform Apply | |
| run: terraform apply -auto-approve | |
| working-directory: terraform/environments/${{ matrix.environment }} | |
| env: | |
| TF_WORKSPACE: ${{ matrix.environment }} | |
| strategy: | |
| matrix: | |
| environment: [dev, staging, prod] | |
| # ============================================================================= | |
| # Security Compliance Check | |
| # ============================================================================= | |
| security-check: | |
| name: Security Check | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Run tfsec | |
| uses: aquasecurity/tfsec-action@v1.0.0 | |
| with: | |
| soft_fail: true | |
| - name: Run checkov | |
| uses: bridgecrewio/checkov-action@master | |
| with: | |
| directory: terraform | |
| framework: terraform | |
| output_format: sarif | |
| - name: Upload SARIF file | |
| uses: github/codeql-action/upload-sarif@v3 | |
| with: | |
| sarif_file: results.sarif | |
| # ============================================================================= | |
| # Infrastructure Testing | |
| # ============================================================================= | |
| infrastructure-test: | |
| name: Infrastructure Test | |
| runs-on: ubuntu-latest | |
| needs: terraform-validate | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Setup Terratest | |
| run: | | |
| go mod init tests | |
| go get github.com/gruntwork-io/terratest/modules/terraform | |
| go get github.com/stretchr/testify/assert | |
| - name: Run Terratest | |
| run: go test -v ./tests/... | |
| working-directory: terraform | |
| # ============================================================================= | |
| # Cost Estimation | |
| # ============================================================================= | |
| cost-estimation: | |
| name: Cost Estimation | |
| runs-on: ubuntu-latest | |
| needs: terraform-plan | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Run Infracost | |
| uses: infracost/infracost-action@v2 | |
| with: | |
| api_key: ${{ secrets.INFRACOST_API_KEY }} | |
| config_file: infracost.yml |