Skip to content

Commit 92a4fc7

Browse files
authored
CCM-12759 use GitHub App token (#80)
Signed-off-by: Tim Ireland <[email protected]>
1 parent 27b3c6a commit 92a4fc7

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
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/cicd-1-pull-request.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ jobs:
134134
- uses: actions/[email protected]
135135
- name: Trigger dynamic environment creation
136136
env:
137-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
137+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
138+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
138139
shell: bash
139140
run: |
140141
.github/scripts/dispatch_internal_repo_workflow.sh \

.github/workflows/pr_closed.yaml

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

5353
- name: Updating Main Environment
5454
env:
55-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
55+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
56+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
5657
run: |
5758
bash .github/scripts/dispatch_internal_repo_workflow.sh \
5859
--releaseVersion "main" \

.github/workflows/pr_destroy_dynamic_env.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ jobs:
1717
- uses: actions/[email protected]
1818
- name: Trigger dynamic environment creation
1919
env:
20-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
20+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
21+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
2122
shell: bash
2223
run: |
2324
.github/scripts/dispatch_internal_repo_workflow.sh \

.github/workflows/release_created.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ jobs:
2828

2929
- name: Deploy Nonprod Environment
3030
env:
31-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
31+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
32+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
3233
run: |
3334
bash .github/scripts/dispatch_internal_repo_workflow.sh \
3435
--releaseVersion "${{ github.event.release.tag_name }}" \
@@ -43,7 +44,8 @@ jobs:
4344
needs: [deploy-main]
4445
uses: ./.github/workflows/stage-4-acceptance.yaml
4546
secrets:
46-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
47+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
48+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
4749
with:
4850
target_environment: "main"
4951
target_account_group: nhs-notify-digital-letters-nonprod

0 commit comments

Comments
 (0)