testing automation flow #107
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: Deploy Azure Resources-Updated | |
on: | |
push: | |
branches: | |
- PSL-Automation-Flow-Draft | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Setup Azure CLI | |
run: | | |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash | |
az --version # Verify installation | |
- name: Login to Azure | |
run: | | |
az login --service-principal -u ${{ secrets.AUTO_AZURE_CLIENT_ID }} -p ${{ secrets.AUTO_AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AUTO_AZURE_TENANT_ID }} | |
# az account set --subscription ${{ secrets.AUTO_AZURE_SUBSCRIPTION_ID }} | |
- name: Install Bicep CLI | |
run: az bicep install | |
- name: Get Access Token | |
id: get_token | |
run: | | |
response=$(curl -X POST \ | |
https://login.microsoftonline.com/${{ secrets.AUTO_AZURE_TENANT_ID }}/oauth2/v2.0/token \ | |
-d "grant_type=client_credentials" \ | |
-d "client_id=${{ secrets.AUTO_AZURE_CLIENT_ID }}" \ | |
-d "client_secret=${{ secrets.AUTO_AZURE_CLIENT_SECRET }}" \ | |
-d "scope=https://graph.microsoft.com/.default") | |
echo "Response: $response" | |
echo "ACCESS_TOKEN=$(echo $response | jq -r .access_token)" >> $GITHUB_ENV | |
- name: Debug Access Token | |
run: echo "ACCESS_TOKEN=${{ env.ACCESS_TOKEN }}" | |
- name: Deploy Bicep Template | |
id: deploy | |
run: | | |
set -e | |
# Temporary files for capturing output and errors | |
output_file=$(mktemp) | |
error_file=$(mktemp) | |
# Execute the deployment and redirect output and errors | |
az deployment sub create \ | |
--name autoDemo \ | |
--location eastus \ | |
--template-file infra/main.bicep \ | |
--parameters environmentName=pslautomation2 location=eastus2 \ | |
> "$output_file" 2> "$error_file" || true | |
# Check the exit status | |
exit_code=$? | |
# Capture output and errors | |
output=$(cat "$output_file") | |
error=$(cat "$error_file") | |
# Clean up temporary files | |
rm "$output_file" "$error_file" | |
# Custom message to print | |
custom_message="Deployment status is:" | |
# Print the custom message and raw output | |
echo "$custom_message $output | |
# Handle errors | |
if [ $exit_code -ne 0 ]; then | |
# Extract and print error details | |
error_message=$(echo "$error" | jq -r '.error.message // "No detailed error message provided"') | |
echo "Deployment failed with the following error:" | |
echo "$error_message" | |
exit 1 | |
else | |
# Check for typical success indicators in output | |
if echo "$output" | grep -q 'Provisioning state: Succeeded'; then | |
echo "Deployment succeeded." | |
else | |
echo "Deployment output indicates a problem, but no explicit error was detected." | |
echo "$output" | |
exit 1 | |
fi | |
fi | |
- name: Send Notification on Failure | |
if: failure() | |
run: | | |
curl -X POST https://graph.microsoft.com/v1.0/users/prashant_malusare@persistent.com/sendMail \ | |
-H "Authorization: Bearer ${{ env.ACCESS_TOKEN }}" \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"message": { | |
"subject": "Deployment Failure!!", | |
"body": { | |
"contentType": "Text", | |
"content": "The deployment failed(cwyd)." | |
}, | |
"toRecipients": [ | |
{ | |
"emailAddress": { | |
"address": "v-pmalusare@microsoft.com" | |
} | |
} | |
] | |
} | |
}' |