forked from navikt/github-app-token-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-installation-access-token.sh
executable file
·42 lines (32 loc) · 1.22 KB
/
get-installation-access-token.sh
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
#!/bin/sh -e
export PRIVATE_KEY="$1"
export APP_ID="$2"
if [ -z "$PRIVATE_KEY" ]; then
echo "Private key not specified."
echo "Usage: ${0} <private-key> <app-id>"
exit 1
fi
if [ -z "$APP_ID" ]; then
echo "Application ID not specified."
echo "Usage: ${0} <private-key> <app-id>"
exit 1
fi
repo=${GITHUB_REPOSITORY:?Missing required GITHUB_REPOSITORY environment variable}
[ -n "$INPUT_REPO" ] && repo="$INPUT_REPO"
jwt=$(ruby "$(dirname "$0")"/generate_jwt.rb)
response=$(curl -s -H "Authorization: Bearer ${jwt}" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/${repo}/installation")
installation_id=$(echo "$response" | jq -r .id)
if [ "$installation_id" = "null" ]; then
echo "Unable to get installation ID. Is the GitHub App installed on ${repo}?"
echo "$response" | jq -r .message
exit 1
fi
token=$(curl -s -X POST \
-H "Authorization: Bearer ${jwt}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/"${installation_id}"/access_tokens | jq -r .token)
if [ "$token" = "null" ]; then
echo "Unable to generate installation access token"
exit 1
fi
echo "token=${token}" >> "$GITHUB_OUTPUT"