Skip to content

Commit 3503d53

Browse files
authored
CCM-12755 use GitHub App creds for WF dispatch (#84)
Signed-off-by: Tim Ireland <[email protected]>
1 parent e1be408 commit 3503d53

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,59 @@ while [[ $# -gt 0 ]]; do
8787
esac
8888
done
8989

90+
if [[ -z "$APP_PEM_FILE" ]]; then
91+
echo "[ERROR] PEM_FILE environment variable is not set or is empty."
92+
exit 1
93+
fi
94+
95+
if [[ -z "$APP_CLIENT_ID" ]]; then
96+
echo "[ERROR] CLIENT_ID environment variable is not set or is empty."
97+
exit 1
98+
fi
99+
100+
now=$(date +%s)
101+
iat=$((${now} - 60)) # Issues 60 seconds in the past
102+
exp=$((${now} + 600)) # Expires 10 minutes in the future
103+
104+
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
105+
106+
header_json='{
107+
"typ":"JWT",
108+
"alg":"RS256"
109+
}'
110+
# Header encode
111+
header=$( echo -n "${header_json}" | b64enc )
112+
113+
payload_json="{
114+
\"iat\":${iat},
115+
\"exp\":${exp},
116+
\"iss\":\"${APP_CLIENT_ID}\"
117+
}"
118+
# Payload encode
119+
payload=$( echo -n "${payload_json}" | b64enc )
120+
121+
# Signature
122+
header_payload="${header}"."${payload}"
123+
signature=$(
124+
openssl dgst -sha256 -sign <(echo -n "${APP_PEM_FILE}") \
125+
<(echo -n "${header_payload}") | b64enc
126+
)
127+
128+
# Create JWT
129+
JWT="${header_payload}"."${signature}"
130+
131+
INSTALLATION_ID=$(curl -X GET \
132+
-H "Accept: application/vnd.github+json" \
133+
-H "Authorization: Bearer ${JWT}" \
134+
-H "X-GitHub-Api-Version: 2022-11-28" \
135+
--url "https://api.github.com/app/installations" | jq -r '.[0].id')
136+
137+
PR_TRIGGER_PAT=$(curl --request POST \
138+
--url "https://api.github.com/app/installations/${INSTALLATION_ID}/access_tokens" \
139+
-H "Accept: application/vnd.github+json" \
140+
-H "Authorization: Bearer ${JWT}" \
141+
-H "X-GitHub-Api-Version: 2022-11-28" | jq -r '.token')
142+
90143
# Set default values if not provided
91144
if [[ -z "$PR_TRIGGER_PAT" ]]; then
92145
echo "[ERROR] PR_TRIGGER_PAT environment variable is not set or is empty."

.github/workflows/pr_closed.disabled

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ jobs:
5454

5555
- name: Updating Main Environment
5656
env:
57-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
57+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
58+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
5859
run: |
5960
bash .github/scripts/dispatch_internal_repo_workflow.sh \
6061
--releaseVersion "main" \

.github/workflows/release_created.disabled

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ jobs:
3030

3131
- name: Updating Main Environment
3232
env:
33-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
33+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
34+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
3435
run: |
3536
bash .github/scripts/dispatch_internal_repo_workflow.sh \
3637
--releaseVersion "${{ github.event.release.tag_name }}" \

0 commit comments

Comments
 (0)