-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.ps1
130 lines (111 loc) · 3.43 KB
/
entrypoint.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Set the runner name
if ($null -ne $env:RUNNER_NAME) {
$RUNNER_NAME = $env:RUNNER_NAME
} else {
if ($null -ne $env:RUNNER_NAME_PREFIX) {
$RUNNER_NAME = $env:RUNNER_NAME_PREFIX
} else {
$RUNNER_NAME = "windows-runner"
}
$RUNNER_NAME += "-" + (((New-Guid).Guid).replace("-", "")).substring(0, 8)
}
# Set GitHub host if not set
if ($null -ne $env:GITHUB_HOST) {
$GITHUB_HOST = $env:GITHUB_HOST
} else {
$GITHUB_HOST = 'github.com'
}
# Set the api url
if ('github.com' -eq $GITHUB_HOST) {
$URI = "https://api.$GITHUB_HOST"
} else {
$URI = "https://$GITHUB_HOST/api/v3"
}
# Set the api to get the access token from
switch ($env:RUNNER_SCOPE) {
org {
if ($null -eq $env:ORG_NAME) {
Write-Error "ORG_NAME required for organisational runners"
exit 1
}
Write-Host "Setting up GitHub Self Hosted Runner for organisation: $env:ORG_NAME"
$TOKEN_URL = "$URI/orgs/$env:ORG_NAME/actions/runners/registration-token"
$CONFIG_URL = "https://$GITHUB_HOST/$env:ORG_NAME"
}
enterprise {
if ($null -eq $env:ENTERPRISE_NAME) {
Write-Error "ENTERPRISE_NAME required for enterprise runners"
exit
}
Write-Host "Setting up GitHub Self Hosted Runner for enterprise: $env:ENTERPRISE_NAME"
$TOKEN_URL = "$URI/enterprises/$env:ENTERPRISE_NAME/actions/runners/registration-token"
$CONFIG_URL = "https://$GITHUB_HOST/enterprises/$env:ENTERPRISE_NAME"
}
default {
if ($null -eq $env:REPO_URL) {
Write-Error "REPO_URL required for repository runners"
exit
}
if ($null -ne $env:RUNNER_TOKEN) {
$RUNNER_TOKEN = $env:RUNNER_TOKEN
} elseif ($null -ne $env:ACCESS_TOKEN) {
$PATTERN = "https://(?:[^/]+/)?([^/]+)/([^/]+)"
if ($env:REPO_URL -match $PATTERN) {
$OWNER = $Matches[1]
$REPO = $Matches[2]
$TOKEN_URL = "$URI/repos/$OWNER/$REPO/actions/runners/registration-token"
} else {
Write-Error "URL format not recognized: $env:REPO_URL"
}
} else {
Write-Error "ACCESS_TOKEN or RUNNER_TOKEN required for repository runners"
exit
}
Write-Host "Setting up GitHub Self Hosted Runner for repository: $env:REPO_URL"
$CONFIG_URL = $env:REPO_URL
}
}
if ($null -ne $TOKEN_URL) {
$HEADERS = @{
'Accept' = 'application/vnd.github.v3+json';
'Authorization' = "token $env:ACCESS_TOKEN";
'Content-Length' = '0';
}
try {
Write-Host "Obtaining the token for the runner"
$RUNNER_TOKEN = ((Invoke-WebRequest -Uri $TOKEN_URL -Method "POST" -Headers $HEADERS).Content | ConvertFrom-Json).token
}
catch {
Write-Error "Cannot obtain the token => $_"
exit
}
}
# Set the labels if given
if ($null -ne $env:LABELS) {
$LABELS = $env:LABELS
} else {
$LABELS = 'default'
}
# Set the labels if given
if ($null -ne $env:RUNNER_GROUP) {
$RUNNER_GROUP = $env:RUNNER_GROUP
} else {
$RUNNER_GROUP = 'Default'
}
$EXTRA_ARGS=''
# Disable auto update if set
if ($null -ne $env:DISABLE_AUTO_UPDATE) {
Write-Host "Auto updating is disabled"
$EXTRA_ARGS += " --disableupdate"
}
try {
Write-Host "Configuring runner: $RUNNER_NAME"
./config.cmd --unattended --replace --url $CONFIG_URL --token $RUNNER_TOKEN --name $RUNNER_NAME --labels $LABELS --runnergroup $RUNNER_GROUP $EXTRA_ARGS
# Remove access token for security reasons
$env:ACCESS_TOKEN=$null
./run.cmd
} catch {
Write-Error $_.Exception.Message
} finally {
./config.cmd remove --unattended --token $RUNNER_TOKEN
}