Skip to content

Commit 253de50

Browse files
authored
Chore: [AEA-0000] - add Proxygen cleanup (#2445)
## Summary - 🤖 Operational or Infrastructure Change ### Details Add Proxygen cleanup of PRs.
1 parent 0ce3ab6 commit 253de50

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
3+
# generic script for removing proxygen deployed apis where the pull request is closed
4+
5+
# set the repo name to be the name of the repo this is running in
6+
REPO_NAME=prescriptionsforpatients
7+
8+
# this should be customised to delete relevant proxygen deployments if they are used
9+
main() {
10+
echo "Checking prescriptions for patients deployments"
11+
PULL_REQUEST_PROXYGEN_REGEX="prescriptions-for-patients-v2-pr-"
12+
delete_apigee_deployments "internal-dev" "prescriptions-for-patients-v2" "PrescriptionsForPatientsProxygenPrivateKey" "2026-01-22-PROD-prescriptions-for-patients-v2"
13+
delete_apigee_deployments "internal-dev-sandbox" "prescriptions-for-patients-v2" "PrescriptionsForPatientsProxygenPrivateKey" "2026-01-22-PROD-prescriptions-for-patients-v2"
14+
}
15+
16+
delete_apigee_deployments() {
17+
APIGEE_ENVIRONMENT=$1
18+
APIGEE_API=$2
19+
PROXYGEN_PRIVATE_KEY_NAME=$3
20+
PROXYGEN_KID=$4
21+
proxygen_private_key_arn=$(aws cloudformation list-exports --query "Exports[?Name=='secrets:${PROXYGEN_PRIVATE_KEY_NAME}'].Value" --output text)
22+
23+
echo
24+
echo "checking apigee deployments on ${APIGEE_ENVIRONMENT}"
25+
echo
26+
27+
jq -n --arg apiName "${APIGEE_API}" \
28+
--arg environment "${APIGEE_ENVIRONMENT}" \
29+
--arg kid "${PROXYGEN_KID}" \
30+
--arg proxygenSecretName "${proxygen_private_key_arn}" \
31+
'{apiName: $apiName, environment: $environment, kid, $kid, proxygenSecretName: $proxygenSecretName}' > payload.json
32+
33+
aws lambda invoke --function-name "lambda-resources-ProxygenPTLInstanceGet" --cli-binary-format raw-in-base64-out --payload file://payload.json out.json > response.json
34+
35+
if eval "cat response.json | jq -e '.FunctionError' >/dev/null"; then
36+
echo 'Error calling lambda'
37+
cat out.json
38+
exit 1
39+
fi
40+
41+
jq -r '.[].name' "out.json" | while read -r i; do
42+
echo "Checking if apigee deployment $i has open pull request"
43+
PULL_REQUEST=${i//${PULL_REQUEST_PROXYGEN_REGEX}/}
44+
echo "Checking pull request id ${PULL_REQUEST}"
45+
URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}"
46+
RESPONSE=$(curl "${URL}" -H "Authorization: token ${GITHUB_TOKEN}" 2>/dev/null)
47+
STATE=$(echo "${RESPONSE}" | jq -r .state)
48+
if [ "$STATE" == "closed" ]; then
49+
echo "** going to delete apigee deployment $i as state is ${STATE} **"
50+
jq -n --arg apiName "${APIGEE_API}" \
51+
--arg environment "${APIGEE_ENVIRONMENT}" \
52+
--arg instance "${i}" \
53+
--arg kid "${PROXYGEN_KID}" \
54+
--arg proxygenSecretName "${proxygen_private_key_arn}" \
55+
'{apiName: $apiName, environment: $environment, kid, $kid, proxygenSecretName: $proxygenSecretName, instance: $instance}' > payload.json
56+
57+
aws lambda invoke --function-name "lambda-resources-ProxygenPTLInstanceDelete" --cli-binary-format raw-in-base64-out --payload file://payload.json out.txt > response.json
58+
if eval "cat response.json | jq -e '.FunctionError' >/dev/null"; then
59+
echo 'Error calling lambda'
60+
cat out.txt
61+
exit 1
62+
fi
63+
64+
65+
else
66+
echo "not going to delete apigee deployment $i as state is ${STATE}"
67+
fi
68+
done
69+
}
70+
71+
main

.github/workflows/delete_old_cloudformation_stacks.yml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
name: "Delete old cloudformation stacks"
22

3-
# Controls when the action will run - in this case triggered manually
43
on:
54
workflow_dispatch:
5+
inputs:
6+
branch_name:
7+
description: "Branch to run against, defaults to main"
8+
required: false
9+
default: "main"
610
schedule:
711
- cron: "0 0,12 * * *"
812
push:
913
branches: [main]
1014

11-
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
1215
jobs:
13-
# This workflow contains a single job called "combine-prs"
1416
delete-old-cloudformation-stacks:
15-
# The type of runner that the job will run on
1617
runs-on: ubuntu-22.04
1718
permissions:
1819
id-token: write
1920
contents: read
2021

21-
# Steps represent a sequence of tasks that will be executed as part of the job
2222
steps:
2323
- name: Checkout local github scripts
2424
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
2525
with:
26-
ref: ${{ env.BRANCH_NAME }}
26+
ref: ${{ github.event.inputs.branch_name || github.ref_name }}
2727
sparse-checkout: |
2828
.github/scripts
2929
@@ -40,3 +40,30 @@ jobs:
4040
run: ./delete_stacks.sh
4141
env:
4242
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
44+
delete-old-proxygen-deployments:
45+
runs-on: ubuntu-22.04
46+
permissions:
47+
id-token: write
48+
contents: read
49+
50+
steps:
51+
- name: Checkout local code
52+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
53+
with:
54+
ref: ${{ github.event.inputs.branch_name || github.ref_name }}
55+
fetch-depth: 0
56+
57+
- name: Configure AWS Credentials
58+
uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7
59+
with:
60+
aws-region: eu-west-2
61+
role-to-assume: ${{ secrets.PROXYGEN_PTL_ROLE }}
62+
role-session-name: pfp-delete-old-proxygen
63+
64+
- name: delete proxygen deployments
65+
shell: bash
66+
working-directory: .github/scripts
67+
run: ./delete_proxygen_deployments.sh
68+
env:
69+
GITHUB_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)