Skip to content

Commit

Permalink
Added az login
Browse files Browse the repository at this point in the history
  • Loading branch information
Abby Artagame committed Nov 18, 2024
1 parent a4c1717 commit 8829cfd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/tf-plan-apply-destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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="clientId=${{ secrets.AZURE_CLIENT_ID }}" -var="tenantId=${{ secrets.AZURE_TENANT_ID }}" -detailed-exitcode -no-color -out terraform.tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -164,6 +164,13 @@ jobs:
path: tf
name: tfplan

# - name: Azure CLI Login
# uses: azure/login@v2
# with:
# client-id: ${{ secrets.AZURE_CLIENT_ID }}
# tenant-id: ${{ secrets.AZURE_TENANT_ID }}
# subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

# Terraform Apply
- name: Terraform Apply
run: |
Expand Down
24 changes: 24 additions & 0 deletions tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ resource "null_resource" "build_image_template" {
}
provisioner "local-exec" {
command = <<EOT
# Variables for Service Principal authentication
CLIENT_ID=${var.clientId}
TENANT_ID=${var.tenantId}
# Function for Azure login
perform_login() {
echo "Performing Azure login..."
az login --service-principal -u "$CLIENT_ID" --tenant "$TENANT_ID" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Azure login successful at $(date)"
else
echo "Azure login failed at $(date). Check credentials."
exit 1
fi
}
# Loop to perform login every 30 minutes
while true; do
perform_login
echo "Waiting for 30 minutes before the next login..."
sleep 1800 # 1800 seconds = 30 minutes
done
# Run the main image builder commands
az image builder run -n ${var.imageTemplateName} -g ${data.azurerm_resource_group.rg.name} --debug
EOT
Expand Down
8 changes: 8 additions & 0 deletions tf/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ variable "imageTemplateName" {

variable "devBoxPoolName" {
type = string
}

variable "clientId" {
type = string
}

variable "tenantId" {
type = string
}

0 comments on commit 8829cfd

Please sign in to comment.