|
| 1 | +name: Deploy Resources |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main # Adjust this to the branch you want to trigger the deployment on |
| 7 | + - dev |
| 8 | + - demo |
| 9 | + schedule: |
| 10 | + - cron: '0 6,18 * * *' # Runs at 6:00 AM and 6:00 PM GMT |
| 11 | + |
| 12 | + |
| 13 | +jobs: |
| 14 | + deploy: |
| 15 | + runs-on: windows-latest # Use a Windows runner for PowerShell scripts |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout Code |
| 19 | + uses: actions/checkout@v3 # Checks out your repository |
| 20 | + # Install Azure CLI |
| 21 | + - name: Install Azure CLI |
| 22 | + shell: pwsh |
| 23 | + run: | |
| 24 | + Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile AzureCLI.msi |
| 25 | + Start-Process msiexec.exe -ArgumentList '/I AzureCLI.msi /quiet' -Wait |
| 26 | +
|
| 27 | + # Install kubectl (Windows method) |
| 28 | + - name: Install kubectl |
| 29 | + shell: pwsh |
| 30 | + run: | |
| 31 | + Invoke-WebRequest -Uri https://dl.k8s.io/release/v1.28.0/bin/windows/amd64/kubectl.exe -OutFile kubectl.exe |
| 32 | + Move-Item -Path ./kubectl.exe -Destination "C:\kubectl.exe" |
| 33 | + [Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';C:\', [System.EnvironmentVariableTarget]::Machine) |
| 34 | +
|
| 35 | +
|
| 36 | + # Install Helm (Windows method) |
| 37 | + - name: Install Helm |
| 38 | + shell: pwsh |
| 39 | + run: | |
| 40 | + Invoke-WebRequest -Uri https://get.helm.sh/helm-v3.13.0-windows-amd64.zip -OutFile helm.zip |
| 41 | + Expand-Archive helm.zip -DestinationPath helm |
| 42 | + Move-Item -Path ./helm/windows-amd64/helm.exe -Destination "C:\helm.exe" |
| 43 | + [Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';C:\', [System.EnvironmentVariableTarget]::Machine) |
| 44 | +
|
| 45 | +
|
| 46 | + - name: Set Docker environment variables |
| 47 | + run: echo "DOCKER_BUILDKIT=0" >> $GITHUB_ENV |
| 48 | + |
| 49 | + # Set up Docker |
| 50 | + - name: Set up Docker |
| 51 | + uses: docker/setup-buildx-action@v2 |
| 52 | + with: |
| 53 | + driver: docker |
| 54 | + |
| 55 | + - name: Setup PowerShell |
| 56 | + shell: pwsh |
| 57 | + run: | |
| 58 | + $PSVersionTable.PSVersion |
| 59 | +
|
| 60 | + - name: Run Deployment Script with Input |
| 61 | + shell: pwsh |
| 62 | + run: | |
| 63 | + cd Deployment |
| 64 | + $input = @" |
| 65 | + ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 66 | + CanadaCentral |
| 67 | + WestUS3 |
| 68 | + ${{ secrets.EMAIL }} |
| 69 | + yes |
| 70 | + "@ |
| 71 | + $input | pwsh ./resourcedeployment.ps1 |
| 72 | + echo "Resource Group Name is ${{ env.rg_name }}" |
| 73 | + echo "Kubernetes resource group are ${{ env.krg_name }}" |
| 74 | + env: |
| 75 | + AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 76 | + AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} |
| 77 | + AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} |
| 78 | + AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} |
| 79 | + - name: Cleanup Resource Group |
| 80 | + if: always() # Ensures this step runs even if the deployment fails |
| 81 | + shell: pwsh |
| 82 | + run: | |
| 83 | + az login --service-principal --username ${{ secrets.AZURE_CLIENT_ID }} --password ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} |
| 84 | + az group delete --name ${{ env.rg_name }} --yes --no-wait |
| 85 | + az group delete --name ${{ env.krg_name }} --yes --no-wait |
| 86 | + env: |
| 87 | + AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 88 | + AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} |
| 89 | + AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} |
| 90 | + AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} |
| 91 | + |
| 92 | + - name: Wait for Resource Deletion to Complete |
| 93 | + shell: pwsh |
| 94 | + run: | |
| 95 | + $retries = 0 |
| 96 | + $maxRetries = 3 |
| 97 | + $sleepIntervals = @(700, 200, 200) |
| 98 | + |
| 99 | + while ($retries -lt $maxRetries) { |
| 100 | + $rgStatus = az group exists --name ${{ env.rg_name }} |
| 101 | + $krgStatus = az group exists --name ${{ env.krg_name }} |
| 102 | + |
| 103 | + |
| 104 | + # if (-not $rgStatus -and -not $krgStatus) { |
| 105 | + # Write-Host "Both resource groups deleted successfully." |
| 106 | + # break |
| 107 | + # } |
| 108 | + if ($rgStatus -eq "false" -and $krgStatus -eq "false") { |
| 109 | + Write-Host "Both resource groups deleted successfully." |
| 110 | + break |
| 111 | + } |
| 112 | + |
| 113 | + $retries++ |
| 114 | + if ($retries -eq $maxRetries) { |
| 115 | + Write-Host "Resource groups deletion not confirmed after $maxRetries attempts. Exiting." |
| 116 | + exit 1 |
| 117 | + } |
| 118 | + |
| 119 | + Write-Host "Resource groups still exist. Retrying in $($sleepIntervals[$retries - 1]) seconds..." |
| 120 | + Start-Sleep -Seconds $sleepIntervals[$retries - 1] |
| 121 | + } |
| 122 | + |
| 123 | + - name: Purging the Resources |
| 124 | + if: success() |
| 125 | + shell: pwsh |
| 126 | + run: | |
| 127 | + # Set variables using GitHub Actions environment values |
| 128 | + $solutionPrefix = "${{ env.SOLUTION_PREFIX }}" |
| 129 | + $subscriptionId = "${{ secrets.AZURE_SUBSCRIPTION_ID }}" |
| 130 | + $resourceGroupName = "${{ env.rg_name }}" |
| 131 | + |
| 132 | + $openai_name = "openaiservice-$solutionPrefix" |
| 133 | + $cognitiveservice_name = "cognitiveservice-$solutionPrefix" |
| 134 | + |
| 135 | + # Debug: Print resource names |
| 136 | + Write-Host "Purging OpenAI resource: $openai_name" |
| 137 | + Write-Host "Purging CognitiveService Account: $cognitiveservice_name" |
| 138 | + |
| 139 | + # Construct resource IDs |
| 140 | + $openaiResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/westus3/resourceGroups/$resourceGroupName/deletedAccounts/$openai_name" |
| 141 | + $cognitiveResourceId = "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/$resourceGroupName/deletedAccounts/$cognitiveservice_name" |
| 142 | + |
| 143 | + # Debug: Print constructed resource IDs |
| 144 | + Write-Host "Command to purge OpenAI resource: az resource delete --ids `"$openaiResourceId`" --verbose" |
| 145 | + Write-Host "Command to purge CognitiveService Account: az resource delete --ids `"$cognitiveResourceId`" --verbose" |
| 146 | + # Purge OpenAI Resource |
| 147 | + az resource delete --ids $openaiResourceId --verbose |
| 148 | + if (-not $?) { |
| 149 | + Write-Host "Failed to purge OpenAI resource: $openaiResourceId" |
| 150 | + } |
| 151 | +
|
| 152 | + # Purge CognitiveService Account |
| 153 | + |
| 154 | + |
| 155 | + az resource delete --ids $cognitiveResourceId --verbose |
| 156 | + if (-not $?) { |
| 157 | + Write-Host "Failed to purge CognitiveService Account." |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + - name: Send Notification on Failure |
| 162 | + if: failure() |
| 163 | + shell: pwsh |
| 164 | + run: | |
| 165 | + # Define the RUN_URL variable |
| 166 | + $RUN_URL = "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 167 | + |
| 168 | + # Construct the email body using a Here-String |
| 169 | + $EMAIL_BODY = @" |
| 170 | + { |
| 171 | + "body": "<p>Dear Team,</p><p>The Document Knowledge Mining Automation process encountered an issue.</p><p><strong>Build URL:</strong> <a href='$RUN_URL'>$RUN_URL</a></p><p>Please investigate promptly.</p><p>Best regards,<br>Your Automation Team</p>" |
| 172 | + } |
| 173 | + "@ |
| 174 | + |
| 175 | + # Send the notification with error handling |
| 176 | + try { |
| 177 | + curl -X POST "${{ secrets.LOGIC_APP_URL }}" ` |
| 178 | + -H "Content-Type: application/json" ` |
| 179 | + -d "$EMAIL_BODY" |
| 180 | + } catch { |
| 181 | + Write-Output "Failed to send notification." |
| 182 | + } |
| 183 | + |
0 commit comments