Skip to content

Commit fc468c4

Browse files
committed
Merge branch 'main' of https://github.com/NHSDigital/eps-assist-me into AEA-5507-code-tidy-up
2 parents df21258 + 93b47ec commit fc468c4

30 files changed

+1963
-1
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
66
curl git build-essential libssl-dev zlib1g-dev \
77
libbz2-dev libreadline-dev libsqlite3-dev wget llvm \
88
libncurses5-dev libncursesw5-dev xz-utils tk-dev \
9-
liblzma-dev python3-pip libffi-dev
9+
liblzma-dev python3-pip libffi-dev libyaml-dev
1010

1111
# Set user to vscode
1212
USER vscode
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
cat <<EOF > payload.json
4+
{
5+
"currentTag": "$CURRENT_DEPLOYED_TAG",
6+
"targetTag": "$DEV_TAG",
7+
"repoName": "eps-assist-me",
8+
"targetEnvironment": "$ENV",
9+
"productName": "EPS Assist Me",
10+
"releaseNotesPageId": "$PAGE_ID",
11+
"releaseNotesPageTitle": "Current EPS Assist Me release notes - $ENV"
12+
}
13+
EOF
14+
cat payload.json
15+
16+
function_arn=$(aws cloudformation list-exports --query "Exports[?Name=='release-notes:CreateReleaseNotesLambdaArn'].Value" --output text)
17+
aws lambda invoke --function-name "${function_arn}" --cli-binary-format raw-in-base64-out --payload file://payload.json out.txt
18+
cat out.txt

.github/scripts/delete_stacks.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
3+
# generic script for removing cloudformation stacks and 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=eps-assist-me
7+
8+
# this should be a regex used in jq command that parses the output from aws cloudformation list-stacks and just captures stacks we are interested in
9+
CAPTURE_REGEX="^epsam-pr-(\\d+)(-sandbox)?$"
10+
11+
# this should be a regex that is used to get the pull request id from the cloud formation stack name
12+
# this is used in a replace command to replace the stack name so what is left is just the pull request id
13+
PULL_REQUEST_STACK_REGEX=epsam-pr-
14+
15+
CNAME_QUERY=epsam-pr-
16+
17+
main() {
18+
delete_cloudformation_stacks
19+
delete_cname_records
20+
}
21+
22+
delete_cloudformation_stacks() {
23+
echo "checking cloudformation stacks"
24+
echo
25+
ACTIVE_STACKS=$(aws cloudformation list-stacks | jq -r --arg CAPTURE_REGEX "${CAPTURE_REGEX}" '.StackSummaries[] | select ( .StackStatus != "DELETE_COMPLETE" ) | select( .StackName | capture($CAPTURE_REGEX) ) | .StackName ')
26+
27+
mapfile -t ACTIVE_STACKS_ARRAY <<< "$ACTIVE_STACKS"
28+
29+
for i in "${ACTIVE_STACKS_ARRAY[@]}"
30+
do
31+
echo "Checking if stack $i has open pull request"
32+
PULL_REQUEST=${i//${PULL_REQUEST_STACK_REGEX}/}
33+
PULL_REQUEST=${PULL_REQUEST//-sandbox/}
34+
echo "Checking pull request id ${PULL_REQUEST}"
35+
URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}"
36+
RESPONSE=$(curl "${URL}" 2>/dev/null)
37+
STATE=$(echo "${RESPONSE}" | jq -r .state)
38+
if [ "$STATE" == "closed" ]; then
39+
echo "** going to delete stack $i as state is ${STATE} **"
40+
aws cloudformation delete-stack --stack-name "${i}"
41+
echo "** Sleeping for 60 seconds to avoid 429 on delete stack **"
42+
sleep 60
43+
else
44+
echo "not going to delete stack $i as state is ${STATE}"
45+
fi
46+
done
47+
}
48+
49+
delete_cname_records() {
50+
HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name dev.eps.national.nhs.uk. | jq -r ".HostedZones[0] | .Id")
51+
CNAME_RECORDS=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \
52+
--query "ResourceRecordSets[?Type == 'CNAME' && contains(Name, '${CNAME_QUERY}')]" \
53+
| jq -r " .[] | .Name")
54+
55+
mapfile -t CNAME_RECORDS_ARRAY <<< "$CNAME_RECORDS"
56+
57+
for i in "${CNAME_RECORDS_ARRAY[@]}"
58+
do
59+
echo "Checking if CNAME record $i has open pull request"
60+
61+
PULL_REQUEST=$(echo "$i" | grep -Po '(?<=-pr-)\d+')
62+
echo "Checking pull request id ${PULL_REQUEST}"
63+
URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}"
64+
RESPONSE=$(curl --url "${URL}" --header "Authorization: Bearer ${GITHUB_TOKEN}" 2>/dev/null)
65+
STATE=$(echo "${RESPONSE}" | jq -r .state)
66+
if [ "$STATE" == "closed" ]; then
67+
echo "** going to delete CNAME record $i as state is ${STATE} **"
68+
record_set=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \
69+
--query "ResourceRecordSets[?Name == '$i']" --output json | jq .[0])
70+
71+
jq -n --argjson record_set "${record_set}" \
72+
'{Changes: [{Action: "DELETE", ResourceRecordSet: $record_set}]}' > /tmp/payload.json
73+
74+
aws route53 change-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" --change-batch file:///tmp/payload.json
75+
76+
echo "CNAME record $i deleted"
77+
else
78+
echo "not going to delete CNAME record $i as state is ${STATE} **"
79+
fi
80+
done
81+
}
82+
83+
main

.github/scripts/fix_cdk_json.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# script used to set context key values in cdk.json pre deployment from environment variables
5+
6+
# helper function to set string values
7+
fix_string_key() {
8+
KEY_NAME=$1
9+
KEY_VALUE=$2
10+
if [ -z "${KEY_VALUE}" ]; then
11+
echo "${KEY_NAME} value is unset or set to the empty string"
12+
exit 1
13+
fi
14+
echo "Setting ${KEY_NAME}"
15+
jq \
16+
--arg key_value "${KEY_VALUE}" \
17+
--arg key_name "${KEY_NAME}" \
18+
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
19+
mv .build/cdk.new.json .build/cdk.json
20+
}
21+
22+
# helper function to set boolean and number values (without quotes)
23+
fix_boolean_number_key() {
24+
KEY_NAME=$1
25+
KEY_VALUE=$2
26+
if [ -z "${KEY_VALUE}" ]; then
27+
echo "${KEY_NAME} value is unset or set to the empty string"
28+
exit 1
29+
fi
30+
echo "Setting ${KEY_NAME}"
31+
jq \
32+
--argjson key_value "${KEY_VALUE}" \
33+
--arg key_name "${KEY_NAME}" \
34+
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
35+
mv .build/cdk.new.json .build/cdk.json
36+
}
37+
38+
# go through all the key values we need to set
39+
fix_string_key accountId "${ACCOUNT_ID}"
40+
fix_string_key stackName "${STACK_NAME}"
41+
fix_string_key versionNumber "${VERSION_NUMBER}"
42+
fix_string_key commitId "${COMMIT_ID}"
43+
fix_string_key logRetentionInDays "${LOG_RETENTION_IN_DAYS}"
44+
fix_string_key logLevel "${LOG_LEVEL}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
dev_tag=$(aws cloudformation describe-stacks --stack-name cpt --query "Stacks[0].Tags[?Key=='version'].Value" --output text)
4+
5+
echo "DEV_TAG=${dev_tag}" >> "$GITHUB_ENV"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
current_deployed_tag=$(aws cloudformation describe-stacks --stack-name epsam --query "Stacks[0].Tags[?Key=='version'].Value" --output text)
4+
5+
echo "CURRENT_DEPLOYED_TAG=${current_deployed_tag}" >> "$GITHUB_ENV"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: cdk package code
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
VERSION_NUMBER:
7+
required: true
8+
type: string
9+
COMMIT_ID:
10+
required: true
11+
type: string
12+
13+
jobs:
14+
package_code:
15+
runs-on: ubuntu-22.04
16+
permissions:
17+
id-token: write
18+
contents: read
19+
packages: read
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
with:
24+
ref: ${{ env.BRANCH_NAME }}
25+
26+
# using git commit sha for version of action to ensure we have stable version
27+
- name: Install asdf
28+
uses: asdf-vm/actions/setup@1902764435ca0dd2f3388eea723a4f92a4eb8302
29+
with:
30+
asdf_branch: v0.14.1
31+
32+
- name: Cache asdf
33+
uses: actions/cache@v4
34+
with:
35+
path: |
36+
~/.asdf
37+
key: ${{ runner.os }}-asdf-${{ hashFiles('**/.tool-versions') }}
38+
restore-keys: |
39+
${{ runner.os }}-asdf-
40+
41+
- name: Install asdf dependencies in .tool-versions
42+
uses: asdf-vm/actions/install@1902764435ca0dd2f3388eea723a4f92a4eb8302
43+
with:
44+
asdf_branch: v0.14.1
45+
env:
46+
PYTHON_CONFIGURE_OPTS: --enable-shared
47+
48+
- name: Setting up .npmrc
49+
env:
50+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
run: |
52+
echo "//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}" >> ~/.npmrc
53+
echo "@nhsdigital:registry=https://npm.pkg.github.com" >> ~/.npmrc
54+
55+
- name: make install
56+
run: |
57+
make install
58+
59+
- name: 'Tar files'
60+
run: |
61+
tar -rf artifact.tar \
62+
.tool-versions \
63+
packages \
64+
node_modules \
65+
package.json \
66+
package-lock.json \
67+
tsconfig.defaults.json \
68+
cdk.json
69+
70+
- uses: actions/upload-artifact@v4
71+
name: upload build artifact
72+
with:
73+
name: build_artifact
74+
path: artifact.tar

0 commit comments

Comments
 (0)