@@ -62,7 +62,7 @@ if [ -n "$GITHUB_REPOSITORY" ] && [ -n "$GITHUB_TOKEN" ]; then
6262 ./run.sh
6363 echo " 🚀 Executing GitHub Runner for $GITHUB_REPOSITORY "
6464
65- else
65+ elif [ -n " $GITHUB_PAT " ] ; then
6666
6767 # Retrieve a short lived runner registration token using the PAT
6868 REGISTRATION_TOKEN=" $( curl -X POST -fsSL \
8686 export GITHUB_PAT=_REDACTED_
8787 export REGISTRATION_TOKEN=_REDACTED_
8888
89+ elif [ -n " $GITHUB_APP_ID " ] && [ -n " $GITHUB_APP_KEY " ] && [ -n " $GITHUB_APP_INSTALLATION_ID " ] && [ -n " $REGISTRATION_TOKEN_API_URL " ] && [ -n " $REPO_URL " ]; then
90+
91+ app_id=" $GITHUB_APP_ID "
92+ pem_path=" $( mktemp /tmp/github-app-key.XXXXXX.pem) "
93+ chmod 600 " $pem_path "
94+ trap ' rm -f "$pem_path"' EXIT INT TERM HUP
95+ printf ' %b\n' " $GITHUB_APP_KEY " > " $pem_path "
96+
97+ now=$( date +%s)
98+ iat=$(( ${now} - 60 )) # Issues 60 seconds in the past
99+ exp=$(( ${now} + 600 )) # Expires 10 minutes in the future
100+
101+ b64enc () { openssl base64 | tr -d ' =' | tr ' /+' ' _-' | tr -d ' \n' ; }
102+
103+ header_json=' {
104+ "typ":"JWT",
105+ "alg":"RS256"
106+ }'
107+ # Header encode
108+ header=$( echo -n " ${header_json} " | b64enc )
109+
110+ payload_json=" {
111+ \" iat\" :${iat} ,
112+ \" exp\" :${exp} ,
113+ \" iss\" :\" ${app_id} \"
114+ }"
115+ # Payload encode
116+ payload=$( echo -n " ${payload_json} " | b64enc )
117+
118+ # Signature
119+ header_payload=" ${header} " ." ${payload} "
120+ signature=$(
121+ openssl dgst -sha256 -sign " ${pem_path} " \
122+ <( echo -n " ${header_payload} " ) | b64enc
123+ )
124+
125+ # Create JWT
126+ JWT=" ${header_payload} " ." ${signature} "
127+
128+ ACCESS_TOKEN=" $( curl -fsSL --request POST \
129+ --header ' Accept: application/vnd.github+json' \
130+ --header " Authorization: Bearer $JWT " \
131+ --header ' X-GitHub-Api-Version: 2022-11-28' \
132+ " https://api.github.com/app/installations/$GITHUB_APP_INSTALLATION_ID /access_tokens" \
133+ | jq -r ' .token' ) "
134+
135+ if [ -z " $ACCESS_TOKEN " ] || [ " $ACCESS_TOKEN " = " null" ]; then
136+ echo " ❌ Failed to retrieve GitHub App access token"
137+ exit 1
138+ fi
139+
140+ # Retrieve a short lived runner registration token using the ACCESS_TOKEN
141+ REGISTRATION_TOKEN=" $( curl -X POST -fsSL \
142+ -H ' Accept: application/vnd.github.v3+json' \
143+ -H " Authorization: Bearer $ACCESS_TOKEN " \
144+ -H ' X-GitHub-Api-Version: 2022-11-28' \
145+ " $REGISTRATION_TOKEN_API_URL " \
146+ | jq -r ' .token' ) "
147+
148+ # <https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners>
149+ ./config.sh \
150+ --url " ${REPO_URL} " \
151+ --token " ${REGISTRATION_TOKEN} " \
152+ --unattended \
153+ --disableupdate \
154+ --ephemeral \
155+ --replace \
156+ --labels " $LABELS " \
157+ && ./run.sh
158+
159+ export signature=_REDACTED_
160+ export JWT=_REDACTED_
161+ export GITHUB_APP_KEY=_REDACTED_
162+ export ACCESS_TOKEN=_REDACTED_
163+ export REGISTRATION_TOKEN=_REDACTED_
164+
165+ else
166+
167+ echo " ❌ No valid authentication method configured."
168+ echo " Please set one of the following:"
169+ echo " - GITHUB_REPOSITORY and GITHUB_TOKEN (legacy)"
170+ echo " - GITHUB_PAT, REGISTRATION_TOKEN_API_URL, and REPO_URL"
171+ echo " - GITHUB_APP_ID, GITHUB_APP_KEY, GITHUB_APP_INSTALLATION_ID, REGISTRATION_TOKEN_API_URL, and REPO_URL"
172+ exit 1
173+
89174fi
0 commit comments