Skip to content

Commit 11e20e0

Browse files
committed
self-hosted-runner: optionally make 'em non-ephemeral
In private repositories, we can often get away with keeping a runner around, at least as a deallocated VM for most of the time. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent f887d39 commit 11e20e0

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ on:
2727
required: true
2828
description: Deallocate the runner immediately after creating it (useful for spinning up runners preemptively)
2929
default: "false"
30+
ephemeral:
31+
type: choice
32+
options:
33+
- false
34+
- true
35+
required: true
36+
description: Start the runner in ephemeral mode (i.e. unregister after running one job)
37+
default: "true"
3038

3139
env:
3240
ACTIONS_RUNNER_SCOPE: ${{ github.event.inputs.runner_scope }}
3341
ACTIONS_RUNNER_ORG: "${{ github.event.inputs.runner_org || github.repository_owner }}"
3442
ACTIONS_RUNNER_REPO: "${{ github.event.inputs.runner_repo || github.event.repository.name }}"
3543
DEALLOCATE_IMMEDIATELY: ${{ github.event.inputs.deallocate_immediately }}
44+
EPHEMERAL_RUNNER: ${{ github.event.inputs.ephemeral }}
3645
# This has to be a public URL that the VM can access after creation
3746
POST_DEPLOYMENT_SCRIPT_URL: https://raw.githubusercontent.com/${{ github.repository }}/${{ github.ref }}/azure-self-hosted-runners/post-deployment-script.ps1
3847
# Note that you'll need "p" (arm64 processor) and ideally "d" (local temp disk). The number 4 stands for 4 CPU-cores.
@@ -142,6 +151,7 @@ jobs:
142151
publicIpAddressName1="${{ steps.generate-vm-name.outputs.vm_name }}-ip"
143152
adminUsername="${{ secrets.AZURE_VM_USERNAME }}"
144153
adminPassword="${{ secrets.AZURE_VM_PASSWORD }}"
154+
ephemeral="$EPHEMERAL_RUNNER"
145155
stopService="$DEALLOCATE_IMMEDIATELY"
146156
githubActionsRunnerPath="$ACTIONS_RUNNER_PATH"
147157
location="$AZURE_VM_REGION"

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"description": "Windows Computer Name. Can be maximum 15 characters."
3737
}
3838
},
39+
"ephemeral": {
40+
"type": "string",
41+
"metadata": {
42+
"description": "(optional) Whether to spin up an ephemeral runner or not."
43+
}
44+
},
3945
"stopService": {
4046
"type": "string",
4147
"metadata": {
@@ -116,7 +122,7 @@
116122
"firstFileNameString": "[variables('UriFileNamePieces')[sub(length(variables('UriFileNamePieces')), 1)]]",
117123
"firstFileNameBreakString": "[split(variables('firstFileNameString'), '?')]",
118124
"firstFileName": "[variables('firstFileNameBreakString')[0]]",
119-
"postDeploymentScriptArguments": "[concat('-GitHubActionsRunnerToken ', parameters('githubActionsRunnerToken'), ' -GithubActionsRunnerRegistrationUrl ', parameters('githubActionsRunnerRegistrationUrl'), ' -GithubActionsRunnerName ', parameters('virtualMachineName'), ' -StopService ', parameters('stopService'), ' -GitHubActionsRunnerPath ', parameters('githubActionsRunnerPath'))]"
125+
"postDeploymentScriptArguments": "[concat('-GitHubActionsRunnerToken ', parameters('githubActionsRunnerToken'), ' -GithubActionsRunnerRegistrationUrl ', parameters('githubActionsRunnerRegistrationUrl'), ' -GithubActionsRunnerName ', parameters('virtualMachineName'), ' -Ephemeral ', parameters('ephemeral'), ' -StopService ', parameters('stopService'), ' -GitHubActionsRunnerPath ', parameters('githubActionsRunnerPath'))]"
120126
},
121127
"resources": [
122128
{

azure-self-hosted-runners/post-deployment-script.ps1

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ param (
1515
[ValidateNotNullOrEmpty()]
1616
[string]$GithubActionsRunnerName,
1717

18+
[Parameter(Mandatory = $false, HelpMessage = "Start an ephemeral runner (this is the default)")]
19+
[ValidateSet('true', 'false')]
20+
[string]$Ephemeral = 'true',
21+
1822
[Parameter(Mandatory = $false, HelpMessage = "Stop Service immediately (useful for spinning up runners preemptively)")]
1923
[ValidateSet('true', 'false')]
2024
[string]$StopService = 'true',
@@ -261,12 +265,17 @@ Write-Output "Installing GitHub Actions runner $($GitHubAction.Tag) as a Windows
261265

262266
Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory($GitHubAction.OutFile, $GitHubActionsRunnerPath)
263267

264-
Write-Output "Configuring the runner to shut down automatically after running"
265-
Set-Content -Path "${GitHubActionsRunnerPath}\shut-down.ps1" -Value "shutdown -s -t 60 -d p:4:0 -c `"workflow job is done`""
266-
[System.Environment]::SetEnvironmentVariable("ACTIONS_RUNNER_HOOK_JOB_COMPLETED", "${GitHubActionsRunnerPath}\shut-down.ps1", [System.EnvironmentVariableTarget]::Machine)
268+
If ($Ephemeral -ne 'true') {
269+
$EphemeralOption = ""
270+
} Else {
271+
$EphemeralOption = "--ephemeral"
272+
Write-Output "Configuring the runner to shut down automatically after running"
273+
Set-Content -Path "${GitHubActionsRunnerPath}\shut-down.ps1" -Value "shutdown -s -t 60 -d p:4:0 -c `"workflow job is done`""
274+
[System.Environment]::SetEnvironmentVariable("ACTIONS_RUNNER_HOOK_JOB_COMPLETED", "${GitHubActionsRunnerPath}\shut-down.ps1", [System.EnvironmentVariableTarget]::Machine)
275+
}
267276

268277
Write-Output "Configuring the runner"
269-
cmd.exe /c "${GitHubActionsRunnerPath}\config.cmd" --unattended --ephemeral --name ${GithubActionsRunnerName} --runasservice --labels $($GitHubAction.RunnerLabels) --url ${GithubActionsRunnerRegistrationUrl} --token ${GitHubActionsRunnerToken}
278+
cmd.exe /c "${GitHubActionsRunnerPath}\config.cmd" --unattended $EphemeralOption --name ${GithubActionsRunnerName} --runasservice --labels $($GitHubAction.RunnerLabels) --url ${GithubActionsRunnerRegistrationUrl} --token ${GitHubActionsRunnerToken}
270279

271280
# Ensure that the service was created. If not, exit with error code.
272281
if ($null -eq (Get-Service -Name "actions.runner.*")) {

0 commit comments

Comments
 (0)