Skip to content

Commit 529465a

Browse files
committed
self-hosted runner: automatically shut down the VM after a job ran
We register the new runner as "ephemereal", i.e. it runs one job and gets automatically removed from the self-hosted runner group. To avoid incurring unnecessary Azure cost, let's shut down the VM after running the job. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 954f83b commit 529465a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ if((Get-FileHash -Path ${GitHubActionsRunnerPath}\actions-runner-win-${GithubAct
113113
Write-Output "Installing GitHub Actions runner ${GitHubActionsRunnerVersion} as a Windows service with labels ${GithubActionsRunnerLabels}..."
114114

115115
Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("${GitHubActionsRunnerPath}\actions-runner-win-${GithubActionsRunnerArch}-${GitHubActionsRunnerVersion}.zip", $GitHubActionsRunnerPath)
116+
117+
Write-Output "Configuring the runner to shut down automatically after running"
118+
Set-Content -Path "${GitHubActionsRunnerPath}\shut-down.ps1" -Value "shutdown -s -t 60 -d p:4:0 -c `"workflow job is done`""
119+
[System.Environment]::SetEnvironmentVariable("ACTIONS_RUNNER_HOOK_JOB_COMPLETED", "${GitHubActionsRunnerPath}\shut-down.ps1", [System.EnvironmentVariableTarget]::Machine)
120+
121+
Write-Output "Configuring the runner"
116122
cmd.exe /c "${GitHubActionsRunnerPath}\config.cmd" --unattended --ephemeral --name ${GithubActionsRunnerName} --runasservice --labels ${GithubActionsRunnerLabels} --url ${GithubActionsRunnerRegistrationUrl} --token ${GitHubActionsRunnerToken}
117123

118124
# Ensure that the service was created. If not, exit with error code.

0 commit comments

Comments
 (0)