Terraform oicd #5
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 oicd | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: 'Deployment environment' | |
required: true | |
default: 'qa' | |
type: choice | |
options: | |
- dev | |
- qa | |
pipeline_action: | |
description: 'Pipeline action (create or destroy)' | |
required: true | |
default: 'create' | |
type: choice | |
options: | |
- create | |
- destroy | |
jobs: | |
terraform: | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Az CLI login | |
uses: azure/login@v1 | |
with: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
- name: Terraform Init | |
env: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
run: terraform init -backend-config=azurerm.tfbackend -upgrade | |
- name: Terraform workspace setup | |
env: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
run: | | |
terraform workspace select ${{ inputs.environment }} || (terraform workspace new ${{ inputs.environment }} && terraform workspace select ${{ inputs.environment }}) | |
- name: Terraform Plan | |
env: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
run: terraform plan -out=tfplan -var-file="./env/${{ inputs.environment }}.tfvars" | |
- name: Terraform Apply | |
env: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
if: github.event.inputs.pipeline_action == 'create' | |
run: terraform apply -auto-approve -var-file="./env/${{ inputs.environment }}.tfvars" | |
- name: Terraform Destroy | |
env: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
if: github.event.inputs.pipeline_action == 'destroy' | |
run: terraform destroy -auto-approve -var-file="./env/${{ inputs.environment }}.tfvars" |