Skip to content

Commit 92ee10f

Browse files
authored
Merge pull request #1068 from NHSDigital/add-mock-careplus-service
Add a mock careplus service
2 parents 0b9c6c6 + 0c7d512 commit 92ee10f

8 files changed

Lines changed: 600 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Build and push image for mock careplus service
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
git-sha:
7+
description: The FULL git commit sha to build the image from (optional).
8+
type: string
9+
workflow_call:
10+
inputs:
11+
git-sha:
12+
description: The git commit sha to build the image from.
13+
type: string
14+
15+
concurrency:
16+
group: build-and-push-image-${{ inputs.git-sha || github.sha }}
17+
18+
permissions: {}
19+
20+
jobs:
21+
check-image-presence:
22+
name: Check if images already exist
23+
runs-on: ubuntu-latest
24+
permissions:
25+
id-token: write
26+
outputs:
27+
build-needed: >-
28+
${{ steps.check-image.outputs.build-needed }}
29+
steps:
30+
- name: Configure AWS Credentials
31+
uses: aws-actions/configure-aws-credentials@v6
32+
with:
33+
role-to-assume: arn:aws:iam::393416225559:role/GithubDeployECSService
34+
aws-region: eu-west-2
35+
- name: Check if image exists
36+
id: check-image
37+
run: |
38+
if aws ecr describe-images --repository-name mavis/mock-careplus \
39+
--image-ids imageTag=${{ inputs.git-sha || github.sha }} > /dev/null 2>&1; then
40+
echo "Image with given tag already exists"
41+
else
42+
echo "Image does not exist. Build needed"
43+
echo "build-needed=true" >> "$GITHUB_OUTPUT"
44+
fi
45+
46+
build:
47+
needs: check-image-presence
48+
if: needs.check-image-presence.outputs.build-needed == 'true'
49+
runs-on: ubuntu-latest
50+
steps:
51+
- name: Checkout code
52+
uses: actions/checkout@v6
53+
with:
54+
ref: ${{ inputs.git-sha || github.sha }}
55+
- name: Make sure public dir is present
56+
run: mkdir -p public
57+
- name: Write build SHA
58+
run: git rev-parse HEAD > public/sha
59+
- name: Build Docker image
60+
run: docker build -f mavis/test/mocks/careplus/Dockerfile -t "mavis-mock-careplus:latest" .
61+
- name: Save Docker image
62+
run: docker save -o image.tar mavis-mock-careplus:latest
63+
- name: Upload Docker image
64+
uses: actions/upload-artifact@v7
65+
with:
66+
name: image
67+
path: image.tar
68+
push:
69+
runs-on: ubuntu-latest
70+
needs: build
71+
permissions:
72+
id-token: write
73+
steps:
74+
- name: Download Docker image
75+
uses: actions/download-artifact@v8
76+
with:
77+
name: image
78+
- name: Configure AWS Credentials
79+
uses: aws-actions/configure-aws-credentials@v6
80+
with:
81+
role-to-assume: arn:aws:iam::393416225559:role/GithubDeployECSService
82+
aws-region: eu-west-2
83+
- name: Login to ECR
84+
id: login-ecr
85+
uses: aws-actions/amazon-ecr-login@v2
86+
- name: Load Docker image
87+
run: docker load -i image.tar
88+
# yamllint disable rule:line-length
89+
- name: Tag Docker image
90+
run: >-
91+
docker tag mavis-mock-careplus:latest
92+
"${{ steps.login-ecr.outputs.registry }}/mavis/mock-careplus":"${{ inputs.git-sha || github.sha }}"
93+
- name: Push Docker image
94+
run: >-
95+
docker push
96+
"${{ steps.login-ecr.outputs.registry }}/mavis/mock-careplus":"${{ inputs.git-sha || github.sha }}"
97+
# yamllint enable rule:line-length
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
name: Deploy mock careplus service to ECS
2+
run-name: Deploy mock careplus service to ${{ inputs.environment }}
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
environment:
8+
description: Deployment environment
9+
required: true
10+
type: choice
11+
options:
12+
- qa
13+
- test
14+
- sandbox-alpha
15+
- sandbox-beta
16+
git_ref_to_deploy:
17+
description: The git ref to deploy.
18+
required: false
19+
type: string
20+
workflow_call:
21+
inputs:
22+
environment:
23+
required: true
24+
type: string
25+
git_ref_to_deploy:
26+
description: The git ref to deploy.
27+
required: true
28+
type: string
29+
30+
permissions: {}
31+
32+
concurrency:
33+
group: deploy-mavis-${{ inputs.environment }}
34+
35+
env:
36+
aws_role: arn:aws:iam::393416225559:role/GithubDeployECSService
37+
aws_account_id: '393416225559'
38+
cluster_name: mavis-${{ inputs.environment }}
39+
family_name: mavis-mock-careplus-task-definition-${{ inputs.environment }}
40+
41+
jobs:
42+
determine-git-sha:
43+
runs-on: ubuntu-latest
44+
permissions: {}
45+
outputs:
46+
git-sha: ${{ steps.get-git-sha.outputs.git-sha }}
47+
steps:
48+
- name: Checkout code
49+
uses: actions/checkout@v6
50+
with:
51+
ref: ${{ inputs.git_ref_to_deploy || github.sha }}
52+
- name: Get git sha
53+
id: get-git-sha
54+
run: echo "git-sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
55+
56+
build-and-push-image:
57+
permissions:
58+
id-token: write
59+
needs: determine-git-sha
60+
uses: ./.github/workflows/build-and-push-mock-careplus-image.yaml
61+
with:
62+
git-sha: ${{ needs.determine-git-sha.outputs.git-sha }}
63+
64+
prepare-deployment:
65+
name: Prepare deployment
66+
runs-on: ubuntu-latest
67+
needs: [determine-git-sha, build-and-push-image]
68+
permissions:
69+
id-token: write
70+
steps:
71+
- name: Checkout code
72+
uses: actions/checkout@v6
73+
with:
74+
ref: ${{ needs.determine-git-sha.outputs.git-sha }}
75+
- name: Configure AWS Credentials
76+
uses: aws-actions/configure-aws-credentials@v6
77+
with:
78+
role-to-assume: ${{ env.aws_role }}
79+
aws-region: eu-west-2
80+
- name: Get image digest
81+
id: get-image-digest
82+
run: |
83+
digest=$(aws ecr describe-images \
84+
--repository-name mavis/mock-careplus \
85+
--image-ids imageTag=${{ needs.determine-git-sha.outputs.git-sha }} \
86+
--query 'imageDetails[0].imageDigest' \
87+
--output text)
88+
echo "digest=$digest" >> "$GITHUB_OUTPUT"
89+
- name: Populate mock-careplus task definition
90+
id: create-task-definition
91+
uses: aws-actions/amazon-ecs-render-task-definition@v1
92+
with:
93+
# yamllint disable-line rule:line-length
94+
task-definition-family: mavis-mock-careplus-task-definition-${{ inputs.environment }}-template
95+
container-name: application
96+
# yamllint disable-line rule:line-length
97+
image: ${{ env.aws_account_id }}.dkr.ecr.eu-west-2.amazonaws.com/mavis/mock-careplus@${{ steps.get-image-digest.outputs.digest }}
98+
- name: Rename task definition file
99+
run: >-
100+
mv ${{ steps.create-task-definition.outputs.task-definition }}
101+
${{ runner.temp }}/mock-careplus-task-definition.json
102+
- name: Upload artifact for mock-careplus task definition
103+
uses: actions/upload-artifact@v7
104+
with:
105+
name: ${{ inputs.environment }}-mock-careplus-task-definition
106+
path: ${{ runner.temp }}/mock-careplus-task-definition.json
107+
108+
deploy:
109+
name: Deploy mock-careplus service
110+
runs-on: ubuntu-latest
111+
needs: prepare-deployment
112+
environment: ${{ inputs.environment }}
113+
permissions:
114+
id-token: write
115+
steps:
116+
- name: Configure AWS Credentials
117+
uses: aws-actions/configure-aws-credentials@v6
118+
with:
119+
role-to-assume: ${{ env.aws_role }}
120+
aws-region: eu-west-2
121+
- name: Download mock-careplus task definition artifact
122+
uses: actions/download-artifact@v8
123+
with:
124+
path: ${{ runner.temp }}
125+
name: ${{ inputs.environment }}-mock-careplus-task-definition
126+
- name: Change family of task definition
127+
run: |
128+
file_path="${{ runner.temp }}/mock-careplus-task-definition.json"
129+
family_name="mavis-mock-careplus-task-definition-${{ inputs.environment }}"
130+
jq --arg f "$family_name" '.family = $f' "$file_path" > tmpfile && mv tmpfile "$file_path"
131+
- name: Deploy mock-careplus service
132+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
133+
with:
134+
task-definition: ${{ runner.temp }}/mock-careplus-task-definition.json
135+
cluster: ${{ env.cluster_name }}
136+
service: mavis-${{ inputs.environment }}-mock-careplus
137+
force-new-deployment: true
138+
wait-for-service-stability: true

mavis/test/mocks/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.13-slim
2+
3+
WORKDIR /app
4+
5+
COPY mavis/ mavis/
6+
7+
RUN pip install --no-cache-dir defusedxml
8+
9+
EXPOSE 8080
10+
11+
CMD ["python", "-m", "mavis.test.mocks.careplus", "--host", "0.0.0.0", "--port", "8080", "--site-namespace", "MOCK"]

0 commit comments

Comments
 (0)