Skip to content

Commit 994a052

Browse files
NIAD-3192: Move from Jenkins to GitHub Actions (#285)
* NIAD-3192: Move from Jenkins to GitHub Actions * Add initial build action. * Add a unit test job in test.yml. * * Change branch name from main to develop as per NHAIS Adaptor primary branch * * Add component tests to `test.yml` * Remove unneeded name parameter from `test.yml` * Remove unneeded quotation marks around named values * * Update the test path for collecting artifacts for component tests. * * Add integration tests to test workflow * * Update working directory for docker. * * use docker with compose subcommand rather than deprecated `docker-compose`. * * Add detach flag to docker compose * * Update test containers mongo db issue due to `DEPRECATION NOTICE] Docker Image Format v1 and Docker Image manifest version 2, schema 1 support is disabled by default and will be removed in an upcoming release. Suggest the author of docker.io/library/mongo:3.2.4 to upgrade the image to the OCI Format or Docker Image manifest v2, schema 2`. * * Remove unnecessary parameter `path` from test workflow. * Remove unnecessary passing of secrets to test workflow. * Remove unnecessary quotation marks around named valued. * * Update job name to shorten it. * * Add `publish.yml` to allow the images to be pushed to Amazon ECR. * Add a job to `build.yml` to generate a build tag. * Add `create_build_id.sh` to create the build tag. * Add a job to `build.yml` to call publish workflow. * * Fix an incorrect path in `publish.yml`. * * Correct pluralization typos.
1 parent bec8b4e commit 994a052

File tree

5 files changed

+303
-1
lines changed

5 files changed

+303
-1
lines changed

.github/workflows/build.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: "Build"
2+
on:
3+
pull_request:
4+
types: [opened, synchronize, reopened]
5+
branches:
6+
- develop
7+
push:
8+
branches:
9+
- develop
10+
11+
jobs:
12+
tests:
13+
name: Tests
14+
uses: ./.github/workflows/test.yml
15+
16+
generate-build-id:
17+
name: "Generate Build Id"
18+
needs: [ tests ]
19+
runs-on: ubuntu-latest
20+
outputs:
21+
build-id: ${{ steps.generate.outputs.buildId }}
22+
steps:
23+
- name: Checkout Repository
24+
uses: actions/checkout@v4
25+
26+
- id: generate
27+
working-directory: ./scripts
28+
shell: bash
29+
env:
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: |
32+
chmod +x ./create_build_id.sh
33+
34+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
35+
GIT_BRANCH=PR
36+
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
37+
GIT_BRANCH=main
38+
fi
39+
40+
BUILD_ID=$(./create_build_id.sh $GIT_BRANCH ${{ github.run_number }} ${{ github.sha }})
41+
echo "Generated the build tag: $BUILD_ID"
42+
echo "buildId=$BUILD_ID" >> "$GITHUB_OUTPUT"
43+
44+
publish-docker-image:
45+
name: "Publish docker image to ECR"
46+
needs: [ generate-build-id ]
47+
48+
uses: ./.github/workflows/publish.yml
49+
with:
50+
directory: .
51+
repository: nhais
52+
build-context: .
53+
build-id: ${{ needs.generate-build-id.outputs.build-id }}
54+
secrets: inherit
55+
56+
comment:
57+
if: github.event_name == 'pull_request'
58+
name: "Create Build ID Comment"
59+
needs: [ generate-build-id, publish-docker-image]
60+
continue-on-error: true
61+
permissions:
62+
pull-requests: write
63+
runs-on: ubuntu-latest
64+
steps:
65+
- name: Comment PR
66+
uses: thollander/actions-comment-pull-request@v3
67+
with:
68+
message: |
69+
Images built and published to ECR using a Build Id of ${{ needs.generate-build-id.outputs.build-id }}
70+
comment-tag: images-built
71+
mode: upsert
72+

.github/workflows/publish.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Publish Workflow
2+
on:
3+
workflow_call:
4+
inputs:
5+
directory:
6+
required: true
7+
type: string
8+
repository:
9+
required: true
10+
type: string
11+
build-context:
12+
required: true
13+
type: string
14+
build-id:
15+
required: true
16+
type: string
17+
18+
jobs:
19+
build-and-publish-docker-image:
20+
name: Build & Publish Docker Image
21+
runs-on: ubuntu-latest
22+
permissions:
23+
id-token: write
24+
contents: read
25+
26+
steps:
27+
- name: Checkout Repository
28+
uses: actions/checkout@v4
29+
30+
- name: Configure AWS Credentials
31+
uses: aws-actions/configure-aws-credentials@v4
32+
with:
33+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_ROLE_TO_ASSUME }}
34+
role-session-name: gp2gp_github_action_build_workflow
35+
aws-region: ${{ secrets.AWS_REGION }}
36+
37+
- name: Build Docker Image
38+
run: |
39+
DOCKER_REGISTRY="${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com"
40+
DOCKER_TAG="$DOCKER_REGISTRY/${{ inputs.repository }}:${{ inputs.build-id }}"
41+
echo "DOCKER_TAG=$DOCKER_TAG" >> $GITHUB_ENV
42+
43+
# Build Image
44+
docker build -f ./${{ inputs.directory }}/Dockerfile -t $DOCKER_TAG ${{ inputs.build-context }}
45+
46+
- name: Login to AWS ECR
47+
run: |
48+
DOCKER_REGISTRY="https://${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com"
49+
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin $DOCKER_REGISTRY
50+
51+
- name: Publish image to ECR
52+
run: docker push $DOCKER_TAG
53+
54+
- name: Logout of AWS ECR (Clean up Credentials)
55+
if: always()
56+
run: |
57+
DOCKER_REGISTRY="https://${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com"
58+
docker logout $DOCKER_REGISTRY

.github/workflows/test.yml

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
name: Test Workflow
2+
on:
3+
workflow_call:
4+
5+
jobs:
6+
unit-tests:
7+
name: Unit Tests
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout Repository
11+
uses: actions/checkout@v4
12+
13+
- name: Setup Java 11
14+
uses: actions/setup-java@v4
15+
with:
16+
java-version: 11
17+
distribution: temurin
18+
19+
- name: Execute Unit Tests
20+
run: ./gradlew test --parallel --build-cache
21+
22+
- name: Collect Artifacts
23+
if: always()
24+
run: |
25+
mkdir -p artifacts
26+
cp -r ./build/reports ./artifacts
27+
28+
- name: Upload Artifacts
29+
uses: actions/upload-artifact@v4
30+
if: always()
31+
with:
32+
name: Unit Test Report
33+
path: ./artifacts/**
34+
compression-level: 9
35+
36+
- name: Test Job Summary
37+
if: always()
38+
uses: test-summary/action@v2
39+
with:
40+
paths: ./build/test-results/test/TEST-*.xml
41+
42+
- name: Temporary Artifacts Cleanup
43+
run: rm -rf ./artifacts
44+
45+
component-tests:
46+
name: Component Tests
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout Repository
50+
uses: actions/checkout@v4
51+
52+
- name: Setup Java 11
53+
uses: actions/setup-java@v4
54+
with:
55+
java-version: 11
56+
distribution: temurin
57+
58+
- name: Execute Component Tests
59+
run: ./gradlew componentTest --build-cache
60+
61+
- name: Collect Artifacts
62+
if: always()
63+
run: |
64+
mkdir -p artifacts
65+
cp -r ./build/reports ./artifacts
66+
67+
- name: Upload Artifacts
68+
uses: actions/upload-artifact@v4
69+
if: always()
70+
with:
71+
name: Component Test Report
72+
path: ./artifacts/**
73+
compression-level: 9
74+
75+
- name: Test Job Summary
76+
if: always()
77+
uses: test-summary/action@v2
78+
with:
79+
paths: ./build/test-results/componentTest/TEST-*.xml
80+
81+
- name: Temporary Artifacts Cleanup
82+
run: rm -rf ./artifacts
83+
84+
integration_tests:
85+
name: Integration Tests
86+
permissions:
87+
id-token: write
88+
contents: read
89+
runs-on: ubuntu-latest
90+
steps:
91+
- name: Checkout Repository
92+
uses: actions/checkout@v4
93+
94+
- name: Setup Java
95+
uses: actions/setup-java@v4
96+
with:
97+
java-version: 11
98+
distribution: temurin
99+
100+
- name: Setup Required Docker Images
101+
run: docker compose up mongodb activemq fake-mesh -d
102+
103+
- name: Execute Integration Tests
104+
run: ./gradlew integrationTest
105+
106+
- name: Dump Docker Logs
107+
if: always()
108+
run: |
109+
mkdir -p ./logs
110+
container_names=$(docker ps -a --format '{{.Names}}')
111+
for container in $container_names; do
112+
docker logs "$container" > ./logs/"$container".log
113+
echo "Logs saved for container: $container"
114+
done
115+
shell: bash
116+
117+
- name: Collect Artifacts
118+
if: always()
119+
run: |
120+
mkdir -p artifacts
121+
cp -r ./build/reports ./artifacts
122+
cp -r ./logs ./artifacts
123+
124+
- name: Upload Artifacts
125+
uses: actions/upload-artifact@v4
126+
if: always()
127+
with:
128+
name: Integration Test Report & Docker Logs
129+
path: ./artifacts/**
130+
compression-level: 9
131+
132+
- name: Test Job Summary
133+
if: always()
134+
uses: test-summary/action@v2
135+
with:
136+
paths: ./build/test-results/integrationTest/TEST-*.xml
137+
138+
- name: Stop Docker Dependencies
139+
if: always()
140+
run: docker compose down --rmi=local --remove-orphans
141+
142+
- name: Temporary Artifacts Cleanup
143+
run: rm -rf ./artifacts

scripts/create_build_id.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
clean_tag_element() {
4+
local tag_element="$1"
5+
echo "${tag_element//\//-}"
6+
}
7+
8+
generate_tag() {
9+
local clean_branch_name
10+
clean_branch_name=$(clean_tag_element "$1")
11+
local clean_build_id
12+
clean_build_id=$(clean_tag_element "$2")
13+
local git_hash="$3"
14+
15+
local tag="${clean_branch_name}-${clean_build_id}-${git_hash:0:7}"
16+
17+
echo "$tag"
18+
}
19+
20+
if [[ $# -ne 3 ]]; then
21+
echo "Usage: $0 branch_name build_id git_hash"
22+
exit 1
23+
fi
24+
25+
branch_name="$1"
26+
build_id="$2"
27+
git_hash="$3"
28+
29+
generate_tag "$branch_name" "$build_id" "$git_hash"

src/intTest/java/uk/nhs/digital/nhsconnect/nhais/container/MongoDbContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class MongoDbContainer extends GenericContainer<MongoDbContainer> {
88

99
public static final int MONGODB_PORT = 27017;
10-
public static final String DEFAULT_IMAGE_AND_TAG = "mongo:3.2.4";
10+
public static final String DEFAULT_IMAGE_AND_TAG = "mongo:8.0";
1111
private static MongoDbContainer container;
1212

1313
private MongoDbContainer() {

0 commit comments

Comments
 (0)