Skip to content

Commit d031469

Browse files
committed
migrate GH app token retrieval to script
1 parent cb20236 commit d031469

File tree

3 files changed

+31
-49
lines changed

3 files changed

+31
-49
lines changed

.drone.yml

+1-20
Original file line numberDiff line numberDiff line change
@@ -432,25 +432,6 @@ steps:
432432
image: grafana/grafana-plugin-ci:1.9.0
433433
name: package-alpine-x64-no-chromium
434434
- commands:
435-
- apk add --no-cache openssl curl jq
436-
- echo "$GITHUB_APP_PRIVATE_KEY" > private-key.pem
437-
- chmod 600 private-key.pem
438-
- NOW=$(date +%s)
439-
- EXPIRATION=$(($NOW + 600))
440-
- HEADER=$(printf '{"alg":"RS256","typ":"JWT"}' | openssl base64 -A | tr '+/' '-_'
441-
| tr -d '=')
442-
- PAYLOAD=$(printf '{"iat":%d,"exp":%d,"iss":"%s"}' $NOW $EXPIRATION $GITHUB_APP_ID
443-
| openssl base64 -A | tr '+/' '-_' | tr -d '=')
444-
- HEADER_PAYLOAD="$HEADER.$PAYLOAD"
445-
- SIGNATURE=$(echo -n "$HEADER_PAYLOAD" | openssl dgst -sha256 -sign private-key.pem
446-
| openssl base64 -A | tr '+/' '-_' | tr -d '=')
447-
- JWT="$HEADER_PAYLOAD.$SIGNATURE"
448-
- RESPONSE=$(curl -s -X POST \
449-
- ' -H "Authorization: Bearer $JWT" \'
450-
- ' -H "Accept: application/vnd.github+json" \'
451-
- ' https://api.github.com/app/installations/$GITHUB_INSTALLATION_ID/access_tokens)'
452-
- GITHUB_TOKEN=$(echo $RESPONSE | jq -r '.token')
453-
- export GITHUB_TOKEN
454435
- ./scripts/generate_md5sum.sh
455436
- ./scripts/publish_github_release.sh
456437
depends_on:
@@ -530,6 +511,6 @@ kind: secret
530511
name: gar
531512
---
532513
kind: signature
533-
hmac: e7f5ecbd0a068575bbbca6380fa74ac0cfee3a7fff8f58e473e217e9a54e7ab1
514+
hmac: 252ca4f941a5574982e31359f036c1d7ff7a6aedf98c6f38029e86bb2696fec8
534515

535516
...

scripts/drone/promotion.star

-29
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,6 @@ def publish_gh_release():
66
'name': 'publish_to_github',
77
'image': 'cibuilds/github:0.13.0',
88
'commands': [
9-
# Ensure necessary tools are installed
10-
'apk add --no-cache openssl curl jq',
11-
12-
# Write the private key to a file
13-
'echo "$GITHUB_APP_PRIVATE_KEY" > private-key.pem',
14-
'chmod 600 private-key.pem',
15-
16-
# Generate the JWT
17-
'NOW=$(date +%s)',
18-
'EXPIRATION=$(($NOW + 600))',
19-
'HEADER=$(printf \'{"alg":"RS256","typ":"JWT"}\' | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')',
20-
'PAYLOAD=$(printf \'{"iat":%d,"exp":%d,"iss":"%s"}\' $NOW $EXPIRATION $GITHUB_APP_ID | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')',
21-
'HEADER_PAYLOAD="$HEADER.$PAYLOAD"',
22-
'SIGNATURE=$(echo -n "$HEADER_PAYLOAD" | openssl dgst -sha256 -sign private-key.pem | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')',
23-
'JWT="$HEADER_PAYLOAD.$SIGNATURE"',
24-
25-
# Request the installation access token
26-
'RESPONSE=$(curl -s -X POST \\',
27-
' -H "Authorization: Bearer $JWT" \\',
28-
' -H "Accept: application/vnd.github+json" \\',
29-
' https://api.github.com/app/installations/$GITHUB_INSTALLATION_ID/access_tokens)',
30-
31-
# Extract the token from the response
32-
'GITHUB_TOKEN=$(echo $RESPONSE | jq -r \'.token\')',
33-
34-
# Export the token for use in subsequent commands
35-
'export GITHUB_TOKEN',
36-
37-
# Run your scripts
389
'./scripts/generate_md5sum.sh',
3910
'./scripts/publish_github_release.sh',
4011
],

scripts/push-to-gcom.sh

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
#!/bin/zsh
22

3+
## Get GitHub Token
4+
# Ensure necessary tools are installed
5+
apk add --no-cache openssl curl jq
6+
7+
# Write the private key to a file
8+
echo "$GITHUB_APP_PRIVATE_KEY" > private-key.pem
9+
chmod 600 private-key.pem
10+
11+
# Generate the JWT
12+
NOW=$(date +%s)
13+
EXPIRATION=$(($NOW + 600))
14+
HEADER=$(printf \'{"alg":"RS256","typ":"JWT"}\' | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')
15+
PAYLOAD=$(printf \'{"iat":%d,"exp":%d,"iss":"%s"}\' $NOW $EXPIRATION $GITHUB_APP_ID | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')
16+
HEADER_PAYLOAD="$HEADER.$PAYLOAD"
17+
SIGNATURE=$(echo -n "$HEADER_PAYLOAD" | openssl dgst -sha256 -sign private-key.pem | openssl base64 -A | tr \'+/\' \'-_\' | tr -d \'=\')
18+
JWT="$HEADER_PAYLOAD.$SIGNATURE"
19+
20+
# Request the installation access token
21+
RESPONSE=$(curl -s -X POST \
22+
-H "Authorization: Bearer $JWT" \
23+
-H "Accept: application/vnd.github+json" \
24+
https://api.github.com/app/installations/$GITHUB_INSTALLATION_ID/access_tokens)
25+
26+
# Extract the token from the response
27+
GITHUB_TOKEN=$(echo $RESPONSE | jq -r '.token')
28+
29+
# Export the token for use in subsequent commands
30+
export GITHUB_TOKEN
31+
32+
## Push release
333
JSON=$(cat ./scripts/tmp/plugin.json)
434

535
echo $JSON

0 commit comments

Comments
 (0)