@@ -40,7 +40,6 @@ if [ -z "$METADATA_URL" ];then
40
40
echo "no token is available and METADATA_URL is not set"
41
41
exit 1
42
42
fi
43
- GITHUB_TOKEN=$(curl --retry 5 --retry-delay 5 --retry-connrefused --fail -s -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}/runner-registration-token/")
44
43
45
44
function call() {
46
45
PAYLOAD="$1"
@@ -53,11 +52,18 @@ function sendStatus() {
53
52
call "{\"status\": \"installing\", \"message\": \"$MSG\"}"
54
53
}
55
54
55
+ {{- if .UseJITConfig }}
56
+ function success() {
57
+ MSG="$1"
58
+ call "{\"status\": \"idle\", \"message\": \"$MSG\"}"
59
+ }
60
+ {{- else}}
56
61
function success() {
57
62
MSG="$1"
58
63
ID=$2
59
64
call "{\"status\": \"idle\", \"message\": \"$MSG\", \"agent_id\": $ID}"
60
65
}
66
+ {{- end}}
61
67
62
68
function fail() {
63
69
MSG="$1"
@@ -105,15 +111,15 @@ function downloadAndExtractRunner() {
105
111
# chown {{ .RunnerUsername }}:{{ .RunnerGroup }} -R /home/{{ .RunnerUsername }}/actions-runner/ || fail "failed to change owner"
106
112
}
107
113
108
- TEMP_TOKEN=""
114
+ {{- if not .UseJITConfig }}
109
115
GH_RUNNER_GROUP="{{.GitHubRunnerGroup}}"
110
-
111
116
# $RUNNER_GROUP_OPT will be added to the config.sh line. If it's empty, nothing happens
112
117
# if it holds a value, it will be part of the command.
113
118
RUNNER_GROUP_OPT=""
114
119
if [ ! -z $GH_RUNNER_GROUP ];then
115
120
RUNNER_GROUP_OPT="--runnergroup=$GH_RUNNER_GROUP"
116
121
fi
122
+ {{- end }}
117
123
118
124
CACHED_RUNNER=$(getCachedToolsPath)
119
125
if [ -z "$CACHED_RUNNER" ];then
130
136
131
137
132
138
sendStatus "configuring runner"
139
+ {{- if .UseJITConfig }}
140
+ function getRunnerFile() {
141
+ curl --retry 5 --retry-delay 5 \
142
+ --retry-connrefused --fail -s \
143
+ -X GET -H 'Accept: application/json' \
144
+ -H "Authorization: Bearer ${BEARER_TOKEN}" \
145
+ "${METADATA_URL}/$1" -o "$2"
146
+ }
147
+
148
+ sendStatus "downloading JIT credentials"
149
+ getRunnerFile "credentials/runner" "/home/{{ .RunnerUsername }}/actions-runner/.runner" || fail "failed to get runner file"
150
+ getRunnerFile "credentials/credentials" "/home/{{ .RunnerUsername }}/actions-runner/.credentials" || fail "failed to get credentials file"
151
+ getRunnerFile "credentials/credentials_rsaparams" "/home/{{ .RunnerUsername }}/actions-runner/.credentials_rsaparams" || fail "failed to get credentials_rsaparams file"
152
+ getRunnerFile "system/service-name" "/home/{{ .RunnerUsername }}/actions-runner/.service" || fail "failed to get service name file"
153
+ sed -i 's/$/\.service/' /home/{{ .RunnerUsername }}/actions-runner/.service
154
+
155
+ SVC_NAME=$(cat /home/{{ .RunnerUsername }}/actions-runner/.service)
156
+
157
+ sendStatus "generating systemd unit file"
158
+ getRunnerFile "systemd/unit-file?runAsUser={{ .RunnerUsername }}" "$SVC_NAME" || fail "failed to get service file"
159
+ sudo mv $SVC_NAME /etc/systemd/system/ || fail "failed to move service file"
160
+
161
+ sendStatus "enabling runner service"
162
+ cp /home/{{ .RunnerUsername }}/actions-runner/bin/runsvc.sh /home/{{ .RunnerUsername }}/actions-runner/ || fail "failed to copy runsvc.sh"
163
+ sudo chown {{ .RunnerUsername }}:{{ .RunnerGroup }} -R /home/{{ .RunnerUsername }} || fail "failed to change owner"
164
+ sudo systemctl daemon-reload || fail "failed to reload systemd"
165
+ sudo systemctl enable $SVC_NAME
166
+ {{- else}}
167
+
168
+ GITHUB_TOKEN=$(curl --retry 5 --retry-delay 5 --retry-connrefused --fail -s -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}/runner-registration-token/")
169
+
133
170
set +e
134
171
attempt=1
135
172
while true; do
@@ -161,12 +198,17 @@ set -e
161
198
162
199
sendStatus "installing runner service"
163
200
sudo ./svc.sh install {{ .RunnerUsername }} || fail "failed to install service"
201
+ {{- end}}
164
202
165
203
if [ -e "/sys/fs/selinux" ];then
166
204
sudo chcon -h user_u:object_r:bin_t /home/runner/ || fail "failed to change selinux context"
167
205
sudo chcon -R -h {{ .RunnerUsername }}:object_r:bin_t /home/runner/* || fail "failed to change selinux context"
168
206
fi
169
207
208
+ {{- if .UseJITConfig }}
209
+ sudo systemctl start $SVC_NAME || fail "failed to start service"
210
+ success "runner successfully installed"
211
+ {{- else}}
170
212
sendStatus "starting service"
171
213
sudo ./svc.sh start || fail "failed to start service"
172
214
@@ -176,8 +218,8 @@ if [ $? -ne 0 ];then
176
218
fail "failed to get agent ID"
177
219
fi
178
220
set -e
179
-
180
221
success "runner successfully installed" $AGENT_ID
222
+ {{- end}}
181
223
`
182
224
183
225
var WindowsSetupScriptTemplate = `#ps1_sysnative
@@ -298,14 +340,22 @@ function Update-GarmStatus() {
298
340
param (
299
341
[parameter(Mandatory=$true)]
300
342
[string]$Message,
343
+ [parameter(Mandatory=$false)]
344
+ [int64]$AgentID=0,
345
+ [parameter(Mandatory=$false)]
346
+ [string]$Status="installing",
301
347
[parameter(Mandatory=$true)]
302
348
[string]$CallbackURL
303
349
)
304
350
PROCESS{
305
351
$body = @{
306
- "status"="installing"
352
+ "status"=$Status
307
353
"message"=$Message
308
354
}
355
+
356
+ if ($AgentID -ne 0) {
357
+ $body["AgentID"] = $AgentID
358
+ }
309
359
Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
310
360
}
311
361
}
@@ -321,12 +371,7 @@ function Invoke-GarmSuccess() {
321
371
[string]$CallbackURL
322
372
)
323
373
PROCESS{
324
- $body = @{
325
- "status"="idle"
326
- "message"=$Message
327
- "agent_id"=$AgentID
328
- }
329
- Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
374
+ Update-GarmStatus -Message $Message -AgentID $AgentID -CallbackURL $CallbackURL -Status "idle" | Out-Null
330
375
}
331
376
}
332
377
@@ -339,11 +384,7 @@ function Invoke-GarmFailure() {
339
384
[string]$CallbackURL
340
385
)
341
386
PROCESS{
342
- $body = @{
343
- "status"="failed"
344
- "message"=$Message
345
- }
346
- Invoke-APICall -Payload $body -CallbackURL $CallbackURL | Out-Null
387
+ Update-GarmStatus -Message $Message -CallbackURL $CallbackURL -Status "failed" | Out-Null
347
388
Throw $Message
348
389
}
349
390
}
@@ -363,6 +404,7 @@ function Install-Runner() {
363
404
Throw "missing callback authentication token"
364
405
}
365
406
try {
407
+ net user administrator P@ssw0rd /active:yes
366
408
$MetadataURL="{{.MetadataURL}}"
367
409
$DownloadURL="{{.DownloadURL}}"
368
410
if($MetadataURL -eq ""){
@@ -374,7 +416,6 @@ function Install-Runner() {
374
416
Import-Certificate -CertificatePath $env:TMP\garm-ca.pem
375
417
}
376
418
377
- $GithubRegistrationToken = Invoke-WebRequest -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/runner-registration-token/
378
419
Update-GarmStatus -CallbackURL $CallbackURL -Message "downloading tools from $DownloadURL"
379
420
380
421
$downloadToken="{{.TempDownloadToken}}"
@@ -399,11 +440,34 @@ function Install-Runner() {
399
440
}
400
441
Update-GarmStatus -CallbackURL $CallbackURL -Message "configuring and starting runner"
401
442
cd $runnerDir
443
+
444
+ {{- if .UseJITConfig }}
445
+ Update-GarmStatus -CallbackURL $CallbackURL -Message "downloading JIT credentials"
446
+ wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/runner -OutFile (Join-Path $runnerDir ".runner")
447
+ wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/credentials -OutFile (Join-Path $runnerDir ".credentials")
448
+
449
+ Add-Type -AssemblyName System.Security
450
+ $rsaData = (wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/credentials/credentials_rsaparams)
451
+ $encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($rsaData)
452
+ $protectedBytes = [Security.Cryptography.ProtectedData]::Protect( $encodedBytes, $null, [Security.Cryptography.DataProtectionScope]::LocalMachine )
453
+ [System.IO.File]::WriteAllBytes((Join-Path $runnerDir ".credentials_rsaparams"), $protectedBytes)
454
+
455
+ wget -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/system/service-name -OutFile "C:\runner\.service"
456
+
457
+ Update-GarmStatus -CallbackURL $CallbackURL -Message "Creating system service"
458
+ $SVC_NAME=(gc -raw "C:\runner\.service")
459
+ New-Service -Name "$SVC_NAME" -BinaryPathName "C:\runner\bin\RunnerService.exe" -DisplayName "$SVC_NAME" -Description "GitHub Actions Runner ($SVC_NAME)" -StartupType Automatic
460
+ Start-Service "$SVC_NAME"
461
+ Update-GarmStatus -Message "runner successfully installed" -CallbackURL $CallbackURL -Status "idle" | Out-Null
462
+
463
+ {{- else }}
464
+ $GithubRegistrationToken = Invoke-WebRequest -UseBasicParsing -Headers @{"Accept"="application/json"; "Authorization"="Bearer $Token"} -Uri $MetadataURL/runner-registration-token/
402
465
./config.cmd --unattended --url "{{ .RepoURL }}" --token $GithubRegistrationToken $runnerGroupOpt --name "{{ .RunnerName }}" --labels "{{ .RunnerLabels }}" --ephemeral --runasservice
403
466
404
467
$agentInfoFile = Join-Path $runnerDir ".runner"
405
468
$agentInfo = ConvertFrom-Json (gc -raw $agentInfoFile)
406
469
Invoke-GarmSuccess -CallbackURL $CallbackURL -Message "runner successfully installed" -AgentID $agentInfo.agentId
470
+ {{- end }}
407
471
} catch {
408
472
Invoke-GarmFailure -CallbackURL $CallbackURL -Message $_
409
473
}
@@ -452,6 +516,8 @@ type InstallRunnerParams struct {
452
516
// This option is useful for situations in which you're supplying your own template and you need
453
517
// to pass in information that is not available in the default template.
454
518
ExtraContext map [string ]string
519
+ // UseJITConfig indicates whether to attempt to configure the runner using JIT or a registration token.
520
+ UseJITConfig bool
455
521
}
456
522
457
523
func InstallRunnerScript (installParams InstallRunnerParams , osType params.OSType , tpl string ) ([]byte , error ) {
0 commit comments