Scrum 52 shiun terraform module #3
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: "Email Service Infrastructure Change Management Pipeline with GitHub Actions" | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened, closed] | |
push: | |
branches: | |
- main | |
- dev | |
paths: | |
- src/email_service/** | |
env: | |
TF_LOG: INFO | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
TF_DIR: ./src/email_service/terraform | |
permissions: | |
contents: write | |
issues: write | |
pull-requests: write | |
jobs: | |
preview: | |
name: "Deploy Preview Environment" | |
if: github.event_name == 'pull_request' && github.event.action != 'closed' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.8.3 | |
- name: Terraform Init | |
id: init | |
run: terraform init -backend-config="preview.tfbackend" -reconfigure | |
working-directory: ${{ env.TF_DIR }} | |
- name: Terraform Destroy (Cleanup) | |
id: destroy | |
run: terraform destroy -auto-approve -var-file='preview.tfvars' | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true | |
- name: Terraform Plan | |
id: plan | |
run: terraform plan -var-file='preview.tfvars' | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true | |
- name: Terraform Apply | |
id: apply | |
run: terraform apply -var-file='preview.tfvars' -auto-approve | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true | |
- uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
env: | |
APPLY: "terraform\n${{ steps.apply.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { data: comments } = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
}); | |
const botComment = comments.find(comment => { | |
return comment.user.type === 'Bot' && comment.body.includes('Terraform Apply Results'); | |
}); | |
const output = `#### Terraform Apply Results 🌟 | |
Preview environment has been deployed. You can now test your changes. | |
<details><summary>Show Actual Apply</summary> | |
\`\`\`\n | |
${{ steps.apply.outputs.stdout }} | |
\`\`\` | |
</details> | |
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
if (botComment) { | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: output, | |
}); | |
} else { | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output, | |
}); | |
} | |
cleanup_preview: | |
name: "Cleanup Preview Environment" | |
if: github.event_name == 'pull_request' && github.event.action == 'closed' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.8.3 | |
- name: Terraform Init | |
run: terraform init -backend-config="preview.tfbackend" -reconfigure | |
working-directory: ${{ env.TF_DIR }} | |
- name: Terraform Destroy | |
run: terraform destroy -auto-approve -var-file='preview.tfvars' | |
working-directory: ${{ env.TF_DIR }} | |
deploy: | |
name: "Deploy to Target Environment" | |
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true) | |
runs-on: ubuntu-latest | |
needs: [cleanup_preview] | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.8.3 | |
- name: Set Environment Variables | |
run: | | |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then | |
echo "ENVIRONMENT=prod" >> $GITHUB_ENV | |
echo "TF_VARS_FILE=prod.tfvars" >> $GITHUB_ENV | |
elif [[ "${{ github.ref }}" == "refs/heads/dev" ]]; then | |
echo "ENVIRONMENT=dev" >> $GITHUB_ENV | |
echo "TF_VARS_FILE=dev.tfvars" >> $GITHUB_ENV | |
else | |
echo "Unsupported branch: ${{ github.ref }}" | |
exit 1 | |
fi | |
- name: Terraform Init | |
id: init | |
run: terraform init -backend-config="${{ env.ENVIRONMENT }}.tfbackend" -reconfigure | |
working-directory: ${{ env.TF_DIR }} | |
- name: Terraform Destroy (Cleanup) | |
id: destroy | |
run: terraform destroy -auto-approve -var-file='${{ env.TF_VARS_FILE }}' | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true | |
- name: Terraform Plan | |
id: plan | |
run: terraform plan -var-file='${{ env.TF_VARS_FILE }}' -out=tfplan | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true | |
- name: Terraform Apply | |
id: apply | |
run: terraform apply -var-file='${{ env.TF_VARS_FILE }}' -auto-approve | |
working-directory: ${{ env.TF_DIR }} | |
continue-on-error: true |