Skip to content

Commit df83428

Browse files
authored
Merge pull request #98 from git-for-windows/logging-on-vm-creation-failure
Azure self-hosted runners: show logs on failure
2 parents 2ad3c61 + ab3fad1 commit df83428

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Azure Login
2+
description: Logs into Azure using a service principal
3+
inputs:
4+
credentials:
5+
description: Your credentials in JSON format
6+
required: true
7+
8+
runs:
9+
using: "composite"
10+
steps:
11+
- name: Process Azure credentials
12+
uses: actions/github-script@v7
13+
env:
14+
AZURE_CREDENTIALS: ${{ inputs.credentials }}
15+
with:
16+
script: |
17+
if (!process.env.AZURE_CREDENTIALS) {
18+
core.setFailed('The AZURE_CREDENTIALS secret is required.')
19+
process.exit(1)
20+
}
21+
22+
const azureCredentials = JSON.parse(process.env.AZURE_CREDENTIALS)
23+
const {clientId, clientSecret, tenantId, subscriptionId} = azureCredentials
24+
25+
core.setSecret(clientId)
26+
core.exportVariable('AZURE_CLIENT_ID', clientId)
27+
28+
core.setSecret(clientSecret)
29+
core.exportVariable('AZURE_CLIENT_SECRET', clientSecret)
30+
31+
core.setSecret(tenantId)
32+
core.exportVariable('AZURE_TENANT_ID', tenantId)
33+
34+
core.setSecret(subscriptionId)
35+
core.exportVariable('AZURE_SUBSCRIPTION_ID', subscriptionId)
36+
37+
- name: Azure Login
38+
shell: bash
39+
run: |
40+
echo "Logging into Azure..."
41+
az login --service-principal -u ${{ env.AZURE_CLIENT_ID }} -p ${{ env.AZURE_CLIENT_SECRET }} --tenant ${{ env.AZURE_TENANT_ID }}
42+
echo "Setting subscription..."
43+
az account set --subscription ${{ env.AZURE_SUBSCRIPTION_ID }} --output none

.github/workflows/cleanup-self-hosted-runners.yml

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,16 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- uses: actions/checkout@v4
24-
- name: Process Azure credentials
25-
uses: actions/github-script@v7
26-
env:
27-
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
24+
- name: Azure Login
25+
uses: ./.github/workflows/azure-login
2826
with:
29-
script: |
30-
if (!process.env.AZURE_CREDENTIALS) {
31-
core.setFailed('The AZURE_CREDENTIALS secret is required.')
32-
process.exit(1)
33-
}
34-
35-
const azureCredentials = JSON.parse(process.env.AZURE_CREDENTIALS)
36-
const {clientId, clientSecret, tenantId, subscriptionId} = azureCredentials
37-
38-
core.setSecret(clientId)
39-
core.exportVariable('AZURE_CLIENT_ID', clientId)
40-
41-
core.setSecret(clientSecret)
42-
core.exportVariable('AZURE_CLIENT_SECRET', clientSecret)
43-
44-
core.setSecret(tenantId)
45-
core.exportVariable('AZURE_TENANT_ID', tenantId)
27+
credentials: ${{ secrets.AZURE_CREDENTIALS }}
4628

47-
core.setSecret(subscriptionId)
48-
core.exportVariable('AZURE_SUBSCRIPTION_ID', subscriptionId)
4929
- name: Discover VMs to delete
5030
env:
5131
GH_APP_ID: ${{ secrets.GH_APP_ID }}
5232
GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }}
5333
run: |
54-
az login --service-principal -u ${{ env.AZURE_CLIENT_ID }} -p ${{ env.AZURE_CLIENT_SECRET }} --tenant ${{ env.AZURE_TENANT_ID }}
55-
az account set --subscription ${{ env.AZURE_SUBSCRIPTION_ID }}
5634
active_vms=$(az vm list -g ${{ secrets.AZURE_RESOURCE_GROUP }} | jq -c '.[] | {name,timeCreated}')
5735
current_time=$(date +%s)
5836
one_hour_ago=$(($current_time - 3600))

.github/workflows/create-azure-self-hosted-runners.yml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ jobs:
144144
)
145145
146146
echo "AZURE_ARM_PARAMETERS=$AZURE_ARM_PARAMETERS" >> $GITHUB_ENV
147-
147+
148148
- name: Azure Login
149-
uses: azure/login@v2
149+
uses: ./.github/workflows/azure-login
150150
with:
151-
creds: ${{ secrets.AZURE_CREDENTIALS }}
151+
credentials: ${{ secrets.AZURE_CREDENTIALS }}
152152

153153
- uses: azure/arm-deploy@v2
154154
id: deploy-arm-template
@@ -158,16 +158,29 @@ jobs:
158158
template: ./azure-self-hosted-runners/azure-arm-template.json
159159
parameters: ./azure-self-hosted-runners/azure-arm-template-example-parameters.json ${{ env.AZURE_ARM_PARAMETERS }}
160160
scope: resourcegroup
161+
162+
- name: Show some more information on failure
163+
if: failure()
164+
run: |
165+
echo "::group::VM status"
166+
az vm get-instance-view --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --name ${{ steps.generate-vm-name.outputs.vm_name }} --query "instanceView.statuses"
167+
az vm get-instance-view --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --name ${{ steps.generate-vm-name.outputs.vm_name }} --query "statuses"
168+
echo "::endgroup::"
169+
170+
echo "::group::Deployment logs"
171+
az group deployment show --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --name deploy-${{ steps.generate-vm-name.outputs.vm_name }}
172+
echo "::endgroup::"
173+
174+
echo "::group::Extension logs"
175+
az vm extension show --resource-group ${{ secrets.AZURE_RESOURCE_GROUP }} --vm-name ${{ steps.generate-vm-name.outputs.vm_name }} --name CustomScriptExtension
176+
echo "::endgroup::"
161177
162178
- name: Show post-deployment script output
179+
if: always()
163180
env:
164181
CUSTOM_SCRIPT_OUTPUT: ${{ steps.deploy-arm-template.outputs.customScriptInstanceView }}
165182
run: echo "$CUSTOM_SCRIPT_OUTPUT" | jq -r '.substatuses[0].message'
166183

167184
- name: Deallocate the VM for later use
168185
if: env.DEALLOCATE_IMMEDIATELY == 'true'
169-
uses: azure/CLI@v2
170-
with:
171-
azcliversion: 2.64.0
172-
inlineScript: |
173-
az vm deallocate -n ${{ steps.generate-vm-name.outputs.vm_name }} -g ${{ secrets.AZURE_RESOURCE_GROUP }} --verbose
186+
run: az vm deallocate -n ${{ steps.generate-vm-name.outputs.vm_name }} -g ${{ secrets.AZURE_RESOURCE_GROUP }} --verbose

0 commit comments

Comments
 (0)