Skip to content

Commit

Permalink
Fixed error in null resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Abby Artagame committed Nov 13, 2024
1 parent 7ba1938 commit f7ff868
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
48 changes: 27 additions & 21 deletions .github/workflows/tf-plan-apply-destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,30 @@ jobs:
id: tf-plan
run: |
export exitcode=0
terraform plan -var-file='variables/dev.tfvars' -detailed-exitcode -no-color -out terraform.tfplan || export exitcode=$?
terraform plan -var-file='variables/dev.tfvars' -var "client_id=$ARM_CLIENT_ID" -var "tenant_id=$ARM_TENANT_ID" -var "subscription_id=$ARM_SUBSCRIPTION_ID" -detailed-exitcode -no-color -out terraform.tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
else
exit 0
fi
# Save plan to artifacts
# Save plan to artifacts
- name: Publish Terraform Plan
uses: actions/upload-artifact@v4
with:
name: tfplan
path: tf/terraform.tfplan

# Create string output of Terraform Plan
- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform show -no-color terraform.tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Terraform Plan Output" >> $GITHUB_OUTPUT
Expand All @@ -108,14 +108,14 @@ jobs:
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
# Publish Terraform Plan as task summary
- name: Publish Terraform Plan to Task Summary
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
# If this is a PR post the changes
- name: Push Terraform Output to PR
if: github.ref != 'refs/heads/main'
Expand Down Expand Up @@ -167,7 +167,10 @@ jobs:
# Terraform Apply
- name: Terraform Apply
run: |
terraform apply -auto-approve terraform.tfplan
terraform apply -auto-approve terraform.tfplan \
-var "client_id=$ARM_CLIENT_ID" \
-var "tenant_id=$ARM_TENANT_ID" \
-var "subscription_id=$ARM_SUBSCRIPTION_ID"
terraform-plan-destroy:
name: 'Terraform Plan Destroy'
Expand Down Expand Up @@ -209,30 +212,30 @@ jobs:
id: tf-plan
run: |
export exitcode=0
terraform plan -var-file='variables/dev.tfvars' -destroy -detailed-exitcode -no-color -out terraform.tfplan || export exitcode=$?
terraform plan -destroy -var-file='variables/dev.tfvars' -var "client_id=$ARM_CLIENT_ID" -var "tenant_id=$ARM_TENANT_ID" -var "subscription_id=$ARM_SUBSCRIPTION_ID" -detailed-exitcode -no-color -out terraform.tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
else
exit 0
fi
# Save plan to artifacts
# Save plan to artifacts
- name: Publish Terraform Plan
uses: actions/upload-artifact@v4
with:
name: tfplan
path: tf/terraform.tfplan

# Create string output of Terraform Plan
- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform show -no-color terraform.tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Terraform Plan Output" >> $GITHUB_OUTPUT
Expand All @@ -243,14 +246,14 @@ jobs:
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
# Publish Terraform Plan as task summary
- name: Publish Terraform Plan to Task Summary
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
# If this is a PR post the changes
- name: Push Terraform Output to PR
if: github.ref != 'refs/heads/main'
Expand Down Expand Up @@ -278,7 +281,7 @@ jobs:
working-directory: ./tf
environment: DEV-Destroy
needs: [terraform-plan-destroy]

steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
Expand All @@ -302,4 +305,7 @@ jobs:
# Terraform Apply
- name: Terraform Apply
run: |
terraform apply -auto-approve terraform.tfplan
terraform apply -auto-approve terraform.tfplan \
-var "client_id=$ARM_CLIENT_ID" \
-var "tenant_id=$ARM_TENANT_ID" \
-var "subscription_id=$ARM_SUBSCRIPTION_ID"
7 changes: 7 additions & 0 deletions tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ provider "azapi" {
use_oidc = true
}


# User Managed Identity
resource "azurerm_user_assigned_identity" "userIdentity" {
location = data.azurerm_resource_group.rg.location
Expand Down Expand Up @@ -174,10 +175,16 @@ resource "azapi_resource" "imageTemplate" {

# Example Azure CLI command to build the template and wait
resource "null_resource" "build_image_template" {

# triggers = {
# imageTemplateName = var.imageTemplateName
# }
provisioner "local-exec" {
environment = {
ARM_CLIENT_ID = var.client_id
ARM_TENANT_ID = var.tenant_id
ARM_SUBSCRIPTION_ID = var.subscription_id
}
command = <<EOT
az login
az image builder run -n ${var.imageTemplateName} -g ${data.azurerm_resource_group.rg.name} --no-wait
Expand Down
12 changes: 12 additions & 0 deletions tf/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ variable "imageTemplateName" {

variable "devBoxPoolName" {
type = string
}

variable "client_id" {
type = string
}

variable "tenant_id" {
type = string
}

variable "subscription_id" {
type = string
}

0 comments on commit f7ff868

Please sign in to comment.