Skip to content

Commit 15ecd92

Browse files
committed
self-hosted-runner: pass the post-deployment script without a URL
When running this workflow in a private repository, providing a public URL to the post-deployment script simply would not work. It is not even possible to use the `GITHUB_TOKEN` to construct an `Invoke-WebRequest` call: The `GITHUB_TOKEN` lacks the permission to access the resource. So let's just pass this post-deployment script as a parameter. Since it is somewhat large-ish, weighing 14kB, let's compress it. And since the compressed file is binary and cannot easily be passed around, let's Base64-encode it. The result is still somewhat large (5.6kB) but at least this works and still leaves some room for additional stuff to be put into the post-deployment script. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7a87c8f commit 15ecd92

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ env:
4242
ACTIONS_RUNNER_REPO: "${{ github.event.inputs.runner_repo || github.event.repository.name }}"
4343
DEALLOCATE_IMMEDIATELY: ${{ github.event.inputs.deallocate_immediately }}
4444
EPHEMERAL_RUNNER: ${{ github.event.inputs.ephemeral }}
45-
# This has to be a public URL that the VM can access after creation
46-
POST_DEPLOYMENT_SCRIPT_URL: https://raw.githubusercontent.com/${{ github.repository }}/${{ github.ref }}/azure-self-hosted-runners/post-deployment-script.ps1
4745
# Note that you'll need "p" (arm64 processor) and ideally "d" (local temp disk). The number 4 stands for 4 CPU-cores.
4846
# For a convenient overview of all arm64 VM types, see e.g. https://azureprice.net/?_cpuArchitecture=Arm64
4947
AZURE_VM_TYPE: Standard_D4plds_v5
@@ -161,10 +159,21 @@ jobs:
161159
ACTIONS_RUNNER_PATH="D:\a"
162160
fi
163161
162+
# Zip up and Base64-encode the post-deployment script; We used to provide a public URL
163+
# for that script instead, but that does not work in private repositories (and we could
164+
# not even use the `GITHUB_TOKEN` to access the file because it lacks the necessary
165+
# scope to read repository contents).
166+
POST_DEPLOYMENT_SCRIPT_ZIP_BASE64="$(
167+
cd azure-self-hosted-runners &&
168+
zip -9 tmp.zip post-deployment-script.ps1 >&2 &&
169+
base64 -w 0 tmp.zip
170+
)"
171+
164172
AZURE_ARM_PARAMETERS=$(tr '\n' ' ' <<-END
165173
githubActionsRunnerRegistrationUrl="$ACTIONS_RUNNER_REGISTRATION_URL"
166174
githubActionsRunnerToken="$ACTIONS_RUNNER_TOKEN"
167-
postDeploymentPsScriptUrl="$POST_DEPLOYMENT_SCRIPT_URL"
175+
postDeploymentScriptZipBase64="$POST_DEPLOYMENT_SCRIPT_ZIP_BASE64"
176+
postDeploymentScriptFileName="post-deployment-script.ps1"
168177
virtualMachineImage="$AZURE_VM_IMAGE"
169178
virtualMachineName="${{ steps.generate-vm-name.outputs.vm_name }}"
170179
virtualMachineSize="$AZURE_VM_TYPE"
@@ -216,7 +225,7 @@ jobs:
216225
if: always()
217226
env:
218227
CUSTOM_SCRIPT_OUTPUT: ${{ steps.deploy-arm-template.outputs.customScriptInstanceView }}
219-
run: echo "$CUSTOM_SCRIPT_OUTPUT" | jq -r '.substatuses[0].message'
228+
run: echo "$CUSTOM_SCRIPT_OUTPUT" | jq -r '.substatuses[0].message'
220229

221230
- name: Deallocate the VM for later use
222231
if: env.DEALLOCATE_IMMEDIATELY == 'true'

azure-self-hosted-runners/azure-arm-template.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
"description": "Path to the Actions Runner. Keep this path short to prevent Long Path issues, e.g. D:\\a"
2323
}
2424
},
25-
"postDeploymentPsScriptUrl": {
25+
"postDeploymentScriptZipBase64": {
2626
"type": "string",
2727
"minLength": 6,
2828
"metadata": {
29-
"description": "URL to the post-deployment PowerShell script. E.g. https://raw.githubusercontent.com/git-for-windows/git-for-windows-automation/main/azure-self-hosted-runners/post-deployment-script.ps1"
29+
"description": "Base64-encoded .zip file containing the post-deployment script"
30+
}
31+
},
32+
"postDeploymentScriptFileName": {
33+
"type": "string",
34+
"minLength": 6,
35+
"metadata": {
36+
"description": "File name of the post-deployment script"
3037
}
3138
},
3239
"computerName": {
@@ -118,10 +125,6 @@
118125
"vnetName": "[concat(parameters('virtualMachineName'), '-vnet')]",
119126
"vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', concat(parameters('virtualMachineName'), '-vnet'))]",
120127
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]",
121-
"UriFileNamePieces": "[split(parameters('postDeploymentPsScriptUrl'), '/')]",
122-
"firstFileNameString": "[variables('UriFileNamePieces')[sub(length(variables('UriFileNamePieces')), 1)]]",
123-
"firstFileNameBreakString": "[split(variables('firstFileNameString'), '?')]",
124-
"firstFileName": "[variables('firstFileNameBreakString')[0]]",
125128
"postDeploymentScriptArguments": "[concat('-GitHubActionsRunnerToken ', parameters('githubActionsRunnerToken'), ' -GithubActionsRunnerRegistrationUrl ', parameters('githubActionsRunnerRegistrationUrl'), ' -GithubActionsRunnerName ', parameters('virtualMachineName'), ' -Ephemeral ', parameters('ephemeral'), ' -StopService ', parameters('stopService'), ' -GitHubActionsRunnerPath ', parameters('githubActionsRunnerPath'))]"
126129
},
127130
"resources": [
@@ -269,11 +272,8 @@
269272
"type": "CustomScriptExtension",
270273
"typeHandlerVersion": "1.9",
271274
"autoUpgradeMinorVersion": true,
272-
"settings": {
273-
"fileUris": "[split(parameters('postDeploymentPsScriptUrl'), ' ')]"
274-
},
275275
"protectedSettings": {
276-
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('firstFileName'), ' ', variables('postDeploymentScriptArguments'))]"
276+
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -Command \"[System.IO.File]::WriteAllBytes(\\\"tmp.zip\\\", [System.Convert]::FromBase64String(\\\"', parameters('postDeploymentScriptZipBase64'), '\\\")); Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory(\\\"tmp.zip\\\", \\\".\\\"); & .\\', parameters('postDeploymentScriptFileName'), ' ', variables('postDeploymentScriptArguments'), '\"')]"
277277
}
278278
}
279279
}

0 commit comments

Comments
 (0)