Skip to content

Commit 6918b58

Browse files
authored
CCM-13485 use GH app creds for CD pipeline (#115)
Signed-off-by: Tim Ireland <[email protected]>
1 parent 16a8fdc commit 6918b58

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,59 @@ while [[ $# -gt 0 ]]; do
8686
;;
8787
esac
8888
done
89+
# Validate required parameters
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')
89142

90143
# Set default values if not provided
91144
if [[ -z "$PR_TRIGGER_PAT" ]]; then

.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_create_dynamic_env.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jobs:
2323
- uses: actions/[email protected]
2424
- name: Trigger dynamic environment creation
2525
env:
26-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
26+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
27+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
2728
shell: bash
2829
run: |
2930
.github/scripts/dispatch_internal_repo_workflow.sh \

.github/workflows/pr_destroy_dynamic_env.yaml

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

2323
- name: Trigger dynamic environment destruction
2424
env:
25-
PR_TRIGGER_PAT: ${{ secrets.PR_TRIGGER_PAT }}
25+
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
26+
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
2627
shell: bash
2728
run: |
2829
.github/scripts/dispatch_internal_repo_workflow.sh \

.github/workflows/release_created.yaml

Lines changed: 2 additions & 1 deletion
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 }}" \

0 commit comments

Comments
 (0)