Terraform Plan/Apply #25
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 Plan/Apply' | |
on: [workflow_dispatch] | |
#Special permissions required for OIDC authentication | |
permissions: | |
id-token: write | |
contents: read | |
pull-requests: write | |
#These environment variables are used by the terraform azure provider to setup OIDD authenticate. | |
env: | |
ARM_CLIENT_ID: "${{ secrets.AZURE_CLIENT_ID }}" | |
ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" | |
ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" | |
GIT_SSH_COMMAND: ${{ secrets.GIT_SSH_COMMAND }} | |
ARM_USE_OIDC: true | |
jobs: | |
terraform-plan: | |
name: 'Terraform Plan' | |
runs-on: ubuntu-latest | |
environment: plan | |
env: | |
#this is needed since we are running terraform with read-only permissions | |
ARM_SKIP_PROVIDER_REGISTRATION: true | |
outputs: | |
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }} | |
steps: | |
# Checkout the repository to the GitHub Actions runner | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# Install Azure CLI | |
- name: Setup Azure CLI | |
run: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash | |
# Login in to Azure using OpenID | |
- name: Log in to Azure using OIDC | |
uses: azure/login@v1 | |
with: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
# 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@v2 | |
with: | |
terraform_wrapper: false | |
# Install SSH | |
- name: Setup SSH | |
run: sudo apt-get install openssh-client -y | |
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | |
- name: Terraform Init | |
run: | | |
eval `ssh-agent -s` | |
ssh-add - <<< '${{ secrets.SSH_KEY_GITHUB_ACTIONS }}' | |
terraform init | |
# Generates an execution plan for Terraform | |
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes. | |
- name: Terraform Plan | |
id: tf-plan | |
run: | | |
export exitcode=0 | |
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$? | |
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT | |
if [ $exitcode -eq 1 ]; then | |
echo Terraform Plan Failed! | |
exit 1 | |
else | |
exit 0 | |
fi | |
# Save plan to artifacts | |
- name: Publish Terraform Plan | |
uses: actions/upload-artifact@v3 | |
with: | |
name: tfplan | |
path: tfplan | |
terraform-apply: | |
name: 'Terraform Apply' | |
if: needs.terraform-plan.outputs.tfplanExitCode == 2 | |
runs-on: ubuntu-latest | |
environment: apply | |
env: | |
#this is needed since we are running terraform with read-only permissions | |
ARM_SKIP_PROVIDER_REGISTRATION: true | |
needs: [terraform-plan] | |
steps: | |
# Checkout the repository to the GitHub Actions runner | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# Install Azure CLI | |
- name: Setup Azure CLI | |
run: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash | |
# Login in to Azure using OpenID | |
- name: Log in to Azure using OIDC | |
uses: azure/login@v1 | |
with: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
# 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@v2 | |
# Install SSH | |
- name: Setup SSH | |
run: sudo apt-get install openssh-client -y | |
# Install latest Node | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 'latest' | |
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | |
- name: Terraform Init | |
run: | | |
eval `ssh-agent -s` | |
ssh-add - <<< '${{ secrets.SSH_KEY_GITHUB_ACTIONS }}' | |
terraform init | |
# Download saved plan from artifacts | |
- name: Download Terraform Plan | |
uses: actions/download-artifact@v3 | |
with: | |
name: tfplan | |
# Terraform Apply | |
- name: Terraform Apply | |
run: terraform apply -auto-approve tfplan |