Skip to content

edited gh action

edited gh action #22

name: 'Terraform'
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
id-token: write
jobs:
terraform:
name: 'Terraform'
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
directory: [300_load_balancer_vm, 301_load_balancer_vmss, 302_load_balancer_internal_vmss_natgateway, 400_private_dns_zone, 410_private_endpoint_mssql, 420_private_endpoint_pls, 430_service_endpoint_storage_account, 431_service_endpoint_app_service, 500_application_gateway_vmss, 520_application_gateway_vmss_tls_cert_keyvault, 600_public_ip_vm, 610_public_ip_prefix, 620_nat_gateway, 621_nat_gateway_lb, 700_api_management_public, 705_api_management_external, 710_api_management_internal, 715_api_management_internal_app_gateway]
# directory: [004_vnet_peering_tf, 003_vm_bastion_nsg, 200_hub_spoke_firewall, 300_load_balancer_vm, 301_load_balancer_vmss, 302_load_balancer_internal_vmss_natgateway, 400_private_dns_zone, 410_private_endpoint_mssql, 420_private_endpoint_pls]
# node: [14, 16,]
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
defaults:
run:
shell: bash
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v3
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
# Login to Azure using OIDC
- name: Azure 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: Azure CLI script
uses: azure/CLI@v1
with:
azcliversion: latest
inlineScript: |
az account show
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform -chdir=${{ matrix.directory }} init
# Checks that all Terraform configuration files adhere to a canonical format
- name: Terraform Format
run: terraform fmt -check
working-directory: ${{ matrix.directory }}
continue-on-error: true
# Generates an execution plan for Terraform
- name: Terraform Plan
run: terraform plan -out tfplan
working-directory: ${{ matrix.directory }}
# On push to "main", build or change infrastructure according to Terraform configuration files
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
- name: Terraform Apply
run: terraform apply tfplan
working-directory: ${{ matrix.directory }}
continue-on-error: true
# if: github.ref == 'refs/heads/"main"' && github.event_name == 'push'
# # Terraform Destroy
# - name: Terraform Destroy
# run: terraform destroy -auto-approve -input=false
# working-directory: ${{ matrix.directory }}
# Delete Resource Groups
- name: Delete Resource Groups
run: |
RG=$(jq -r '.resources[] | select(.type=="azurerm_resource_group") | .name' terraform.tfstate)
RG_ARRAY=($RG)
for name in "${RG_ARRAY[@]}"; do
echo "Resource group: $name"
az group delete -g $name --no-wait --yes
done
working-directory: ${{ matrix.directory }}