Skip to content

Commit 5b184d0

Browse files
committed
Invoke test from internal repo, so secrets are not in public repo
1 parent 855e83b commit 5b184d0

6 files changed

Lines changed: 99 additions & 47 deletions

File tree

.github/actions/acceptance-tests/action.yml

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ runs:
2424

2525
steps:
2626
- name: Fetch terraform output
27+
if: ${{ inputs.testType != 'e2e' }}
2728
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5
2829
with:
2930
name: terraform-output-${{ inputs.targetComponent }}
3031

3132
- name: Get Node version
33+
if: ${{ inputs.testType != 'e2e' }}
3234
id: nodejs_version
3335
shell: bash
3436
run: |
@@ -40,11 +42,75 @@ runs:
4042
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
4143

4244
- name: "Set PR NUMBER"
45+
if: ${{ inputs.testType == 'e2e' }}
46+
id: set_pr_number
4347
shell: bash
4448
run: |
45-
echo "PR_NUMBER=${{ inputs.targetEnvironment }}" >> $GITHUB_ENV
49+
env="${{ inputs.targetEnvironment }}"
50+
if [[ "$env" == main ]]; then
51+
echo "pr_number=" >> $GITHUB_OUTPUT
52+
elif [[ "$env" == pr* ]]; then
53+
echo "pr_number=${env#pr}" >> $GITHUB_OUTPUT
54+
else
55+
echo "pr_number=$env" >> $GITHUB_OUTPUT
56+
fi
57+
58+
- name: Install poetry and e2e test dependencies
59+
if: ${{ inputs.testType == 'e2e' }}
60+
shell: bash
61+
run: |
62+
pipx install poetry
63+
cd tests/e2e-tests && poetry install
64+
65+
- name: "Determine if proxy has been deployed"
66+
if: ${{ inputs.testType == 'e2e' }}
67+
id: check_proxy_deployed
68+
env:
69+
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
70+
PR_NUMBER: ${{ steps.set_pr_number.outputs.pr_number }}
71+
shell: bash
72+
run: |
73+
if [[ -z "$PR_NUMBER" ]]; then
74+
echo "No pull request detected; proxy was deployed."
75+
echo "proxy_deployed=true" >> $GITHUB_OUTPUT
76+
exit 0
77+
fi
78+
79+
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
80+
81+
labels=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name')
82+
echo "Labels on PR #$PR_NUMBER: $labels"
83+
84+
if echo "$labels" | grep -Fxq 'deploy-proxy'; then
85+
echo "proxy_deployed=true" >> $GITHUB_OUTPUT
86+
else
87+
echo "proxy_deployed=false" >> $GITHUB_OUTPUT
88+
fi
4689
4790
- name: Run test - ${{ inputs.testType }}
91+
if: ${{ inputs.testType != 'e2e'}}
4892
shell: bash
93+
env:
94+
TARGET_ENVIRONMENT: ${{ inputs.targetEnvironment }}
4995
run: |
5096
make test-${{ inputs.testType }}
97+
98+
- name: Run test - e2e
99+
if: ${{ inputs.testType == 'e2e' && steps.check_proxy_deployed.outputs.proxy_deployed == 'true'}}
100+
shell: bash
101+
env:
102+
TARGET_ENVIRONMENT: ${{ inputs.targetEnvironment }}
103+
run: |
104+
echo "$SUPPLIER_API_PRIVATE_KEY" > "${GITHUB_WORKSPACE}/internal-dev-test-1.pem"
105+
chmod 600 "${GITHUB_WORKSPACE}/internal-dev-test-1.pem"
106+
BASE_PROXY_NAME=nhs-notify-supplier--internal-dev--nhs-notify-supplier
107+
if [[ "${{ inputs.targetEnvironment }}" == "main" ]]; then
108+
export PROXY_NAME="${BASE_PROXY_NAME}"
109+
else
110+
export PROXY_NAME="${BASE_PROXY_NAME}-${{ inputs.targetEnvironment }}"
111+
fi
112+
export API_ENVIRONMENT=internal-dev
113+
export NON_PROD_API_KEY="${APIM_API_KEY}"
114+
export STATUS_ENDPOINT_API_KEY="${APIM_STATUS_API_KEY}"
115+
export NON_PROD_PRIVATE_KEY="${GITHUB_WORKSPACE}/internal-dev-test-1.pem"
116+
make .internal-dev-test

.github/actions/e2e-tests/action.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/actions/test-types.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
22
"component",
3+
"e2e",
34
"sandbox"
45
]

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# ./dispatch_internal_repo_workflow.sh \
77
# --infraRepoName <repo> \
88
# --releaseVersion <version> \
9-
# --targetWorkflow <workflow.yaml> \
9+
# --targetWorkflow "deploy.yaml" \
1010
# --targetEnvironment <env> \
1111
# --targetComponent <component> \
1212
# --targetAccountGroup <group> \
@@ -17,7 +17,11 @@
1717
# --overrideRoleName <name>
1818

1919
#
20-
# All arguments are required except terraformAction, and internalRef.
20+
# Required arguments are:
21+
# infraRepoName, releaseVersion, targetWorkflow, targetEnvironment, targetComponent, targetAccountGroup.
22+
#
23+
# All other arguments are optional.
24+
#
2125
# Example:
2226
# ./dispatch_internal_repo_workflow.sh \
2327
# --infraRepoName "nhs-notify-web-template-management" \
@@ -30,7 +34,9 @@
3034
# --internalRef "main" \
3135
# --overrides "tf_var=someString" \
3236
# --overrideProjectName nhs \
33-
# --overrideRoleName nhs-service-iam-role
37+
# --overrideRoleName nhs-service-iam-role \
38+
# --extraSecretNames '["MY_API_KEY"]'
39+
3440

3541
set -e
3642

@@ -104,6 +110,10 @@ while [[ $# -gt 0 ]]; do
104110
version="$2"
105111
shift 2
106112
;;
113+
--extraSecretNames) # JSON array of secret names to fetch in the internal repo (optional)
114+
extraSecretNames="$2"
115+
shift 2
116+
;;
107117
*)
108118
echo "[ERROR] Unknown argument: $1"
109119
exit 1
@@ -202,6 +212,10 @@ if [[ -z "$version" ]]; then
202212
version=""
203213
fi
204214

215+
if [[ -z "$extraSecretNames" ]]; then
216+
extraSecretNames=""
217+
fi
218+
205219
echo "==================== Workflow Dispatch Parameters ===================="
206220
echo " infraRepoName: $infraRepoName"
207221
echo " releaseVersion: $releaseVersion"
@@ -240,6 +254,7 @@ DISPATCH_EVENT=$(jq -ncM \
240254
--arg boundedContext "$boundedContext" \
241255
--arg targetDomain "$targetDomain" \
242256
--arg version "$version" \
257+
--argjson extraSecretNames "${extraSecretNames:-null}" \
243258
'{
244259
"ref": "'"$internalRef"'",
245260
"inputs": (
@@ -255,6 +270,7 @@ DISPATCH_EVENT=$(jq -ncM \
255270
(if $boundedContext != "" then { "boundedContext": $boundedContext } else {} end) +
256271
(if $targetDomain != "" then { "targetDomain": $targetDomain } else {} end) +
257272
(if $version != "" then { "version": $version } else {} end) +
273+
(if $extraSecretNames != null then { "extraSecretNames": ($extraSecretNames | tojson) } else {} end) +
258274
(if $targetAccountGroup != "" then { "targetAccountGroup": $targetAccountGroup } else {} end) +
259275
{
260276
"releaseVersion": $releaseVersion,
@@ -269,16 +285,22 @@ echo "[INFO] Triggering workflow '$targetWorkflow' in nhs-notify-internal..."
269285
echo "[DEBUG] Dispatch event payload: $DISPATCH_EVENT"
270286

271287
trigger_response=$(curl -s -L \
272-
--fail \
288+
-w "\nHTTP_STATUS:%{http_code}" \
273289
-X POST \
274290
-H "Accept: application/vnd.github+json" \
275291
-H "Authorization: Bearer ${PR_TRIGGER_PAT}" \
276292
-H "X-GitHub-Api-Version: 2022-11-28" \
277293
"https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \
278294
-d "$DISPATCH_EVENT" 2>&1)
279295

280-
if [[ $? -ne 0 ]]; then
281-
echo "[ERROR] Failed to trigger workflow. Response: $trigger_response"
296+
http_status=$(echo "$trigger_response" | grep "HTTP_STATUS:" | cut -d: -f2)
297+
body=$(echo "$trigger_response" | grep -v "HTTP_STATUS:")
298+
299+
echo "[DEBUG] HTTP status: $http_status"
300+
echo "[DEBUG] Response body: $body"
301+
302+
if [[ "$http_status" -lt 200 || "$http_status" -ge 300 ]]; then
303+
echo "[ERROR] Failed to trigger workflow. HTTP $http_status. Response: $body"
282304
exit 1
283305
fi
284306

.github/workflows/stage-4-acceptance.yaml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,5 @@ jobs:
8080
--overrideProjectName "nhs" \
8181
--targetEnvironment "$ENVIRONMENT" \
8282
--targetAccountGroup "nhs-notify-supplier-api-dev" \
83-
--targetComponent "api"
84-
85-
run-e2e-tests:
86-
name: Run End-to-End Tests
87-
runs-on: ubuntu-latest
88-
if: inputs.proxy_deployed == 'true'
89-
steps:
90-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
91-
92-
- name: "Run e2e tests"
93-
uses: ./.github/actions/e2e-tests
94-
env:
95-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96-
NON_PROD_API_KEY: ${{ secrets.NON_PROD_API_KEY }}
97-
INTERNAL_DEV_TEST_PEM: ${{ secrets.INTERNAL_DEV_TEST_PEM }}
98-
STATUS_ENDPOINT_API_KEY: ${{ secrets.STATUS_ENDPOINT_API_KEY }}
83+
--targetComponent "api" \
84+
--extraSecretNames '["SUPPLIER_API_PRIVATE_KEY","APIM_API_KEY","APIM_STATUS_API_KEY"]'

tests/constants/api-constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const SUPPLIER_LETTERS = "letters";
22
export const SUPPLIER_API_URL_SANDBOX =
33
"https://internal-dev-sandbox.api.service.nhs.uk/nhs-notify-supplier";
44
export const AWS_REGION = "eu-west-2";
5-
export const envName = process.env.PR_NUMBER ?? "main";
5+
export const envName = process.env.TARGET_ENVIRONMENT ?? "main";
66
export const API_NAME = `nhs-${envName}-supapi`;
77
export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
88
export const SUPPLIERID = "TestSupplier1";

0 commit comments

Comments
 (0)