Skip to content

Commit d2158b4

Browse files
committed
feat: initial versions of the new workflows, to be able to test iterations from branch
1 parent c9fc452 commit d2158b4

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: CI/CD pull request - devtest
2+
3+
on:
4+
push:
5+
tags:
6+
- 'devtest'
7+
8+
jobs:
9+
print-debug-info:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Display Information
13+
run: |
14+
echo "Workflow triggered by tag!"
15+
echo "--------------------------------------"
16+
echo "Tag/Ref Name : $GITHUB_REF"
17+
echo "Commit Hash : $GITHUB_SHA"
18+
echo "Triggered By : $GITHUB_ACTOR"
19+
echo "Event Name : $GITHUB_EVENT_NAME"
20+
echo "--------------------------------------"
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
name: Docker Image CI
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment_tag:
7+
description: Environment of the deployment
8+
required: true
9+
type: string
10+
default: development
11+
docker_compose_file:
12+
description: The path of the compose.yaml file needed to build docker images
13+
required: true
14+
type: string
15+
function_app_source_code_path:
16+
description: The source path of the function app source code for the docker builds
17+
required: true
18+
type: string
19+
project_name:
20+
description: The name of the project
21+
required: true
22+
type: string
23+
excluded_containers_csv_list:
24+
description: Excluded containers in a comma separated list
25+
required: true
26+
type: string
27+
build_all_images:
28+
description: Build all images (true) or only changed ones (false)
29+
required: false
30+
type: boolean
31+
default: false
32+
33+
secrets:
34+
client_id:
35+
description: 'The Azure Client ID.'
36+
required: true
37+
tenant_id:
38+
description: 'The Azure Tenant ID.'
39+
required: true
40+
subscription_id:
41+
description: 'The Azure Subscription ID.'
42+
required: true
43+
acr_name:
44+
description: 'The name of the Azure Container Registry.'
45+
required: true
46+
47+
jobs:
48+
get-functions:
49+
runs-on: ubuntu-latest
50+
permissions:
51+
contents: read
52+
pull-requests: read
53+
id-token: write
54+
outputs:
55+
FUNC_NAMES: ${{ steps.get-function-names.outputs.FUNC_NAMES }}
56+
DOCKER_COMPOSE_DIR: ${{ steps.get-function-names.outputs.DOCKER_COMPOSE_DIR }}
57+
steps:
58+
- uses: actions/checkout@v4
59+
with:
60+
fetch-depth: 2
61+
token: ${{ secrets.GITHUB_TOKEN }}
62+
63+
- name: Checkout dtos-devops-templates repository
64+
uses: actions/checkout@v4
65+
with:
66+
repository: NHSDigital/dtos-devops-templates
67+
path: templates
68+
ref: main
69+
70+
- name: Determine which Docker container(s) to build
71+
72+
build-and-push:
73+
runs-on: ubuntu-latest
74+
permissions:
75+
id-token: write
76+
contents: read
77+
pull-requests: read
78+
needs: get-functions
79+
strategy:
80+
matrix:
81+
function: ${{ fromJSON(needs.get-functions.outputs.FUNC_NAMES) }}
82+
if: needs.get-functions.outputs.FUNC_NAMES != '[]'
83+
outputs:
84+
pr_num_tag: ${{ env.PR_NUM_TAG }}
85+
short_commit_hash: ${{ env.COMMIT_HASH_TAG }}
86+
steps:
87+
- uses: actions/checkout@v4
88+
with:
89+
token: ${{ secrets.GITHUB_TOKEN }}
90+
fetch-depth: 1
91+
submodules: 'true'
92+
93+
- name: Checkout dtos-devops-templates repository
94+
uses: actions/checkout@v4
95+
with:
96+
repository: NHSDigital/dtos-devops-templates
97+
path: templates
98+
ref: main
99+
100+
- name: Az CLI login
101+
if: github.ref == 'refs/heads/main'
102+
uses: azure/login@v2
103+
with:
104+
client-id: ${{ secrets.client_id }}
105+
tenant-id: ${{ secrets.tenant_id }}
106+
subscription-id: ${{ secrets.subscription_id }}
107+
108+
- name: Azure Container Registry login
109+
if: github.ref == 'refs/heads/main'
110+
env:
111+
ACR_NAME: ${{ secrets.acr_name }}
112+
run: az acr login --name ${ACR_DEVTEST_NAME}
113+
114+
- name: Create Tags
115+
env:
116+
GH_TOKEN: ${{ github.token }}
117+
ENVIRONMENT_TAG: ${{ inputs.environment_tag }}
118+
continue-on-error: false
119+
run: |
120+
echo "The branch is: ${GITHUB_REF}"
121+
122+
if [[ "${GITHUB_REF}" == refs/pull/*/merge ]]; then
123+
PR_NUM_TAG=$(echo "${GITHUB_REF}" | sed 's/refs\/pull\/\([0-9]*\)\/merge/\1/')
124+
else
125+
PULLS_JSON=$(gh api /repos/{owner}/{repo}/commits/${GITHUB_SHA}/pulls)
126+
ORIGINATING_BRANCH=$(echo ${PULLS_JSON} | jq -r '.[].head.ref' | python3 -c "import sys, urllib.parse; print(urllib.parse.quote_plus(sys.stdin.read().strip()))")
127+
echo "ORIGINATING_BRANCH: ${ORIGINATING_BRANCH}"
128+
PR_NUM_TAG=$(echo ${PULLS_JSON} | jq -r '.[].number')
129+
fi
130+
131+
echo "PR_NUM_TAG: pr${PR_NUM_TAG}"
132+
echo "PR_NUM_TAG=pr${PR_NUM_TAG}" >> ${GITHUB_ENV}
133+
134+
SHORT_COMMIT_HASH=$(git rev-parse --short ${GITHUB_SHA})
135+
echo "Commit hash tag: ${SHORT_COMMIT_HASH}"
136+
echo "COMMIT_HASH_TAG=${SHORT_COMMIT_HASH}" >> ${GITHUB_ENV}
137+
138+
echo "ENVIRONMENT_TAG=${ENVIRONMENT_TAG}" >> ${GITHUB_ENV}
139+
140+
- name: Build and Push Image
141+
working-directory: ${{ steps.get-function-names.outputs.DOCKER_COMPOSE_DIR }}
142+
continue-on-error: false
143+
env:
144+
COMPOSE_FILE: ${{ inputs.docker_compose_file }}
145+
PROJECT_NAME: ${{ inputs.project_name }}
146+
ACR_NAME: ${{ secrets.acr_name }}
147+
run: |
148+
function=${{ matrix.function }}
149+
150+
echo PROJECT_NAME: ${PROJECT_NAME}
151+
152+
if [ -z "${function}" ]; then
153+
echo "Function variable is empty. Skipping Docker build."
154+
exit 0
155+
fi
156+
157+
# Build the image
158+
docker compose -f ${COMPOSE_FILE//,/ -f } -p ${PROJECT_NAME} --profile "*" build --no-cache --pull ${function}
159+
160+
repo_name="${ACR_NAME}.azurecr.io/${PROJECT_NAME}-${function}"
161+
echo $(repo_name)
162+
163+
# Tag the image
164+
echo "Tag the image:"
165+
docker tag ${PROJECT_NAME}-$ {function}:latest "$repo_name:${COMMIT_HASH_TAG}"
166+
docker tag ${PROJECT_NAME}-${function}:latest "$repo_name:${PR_NUM_TAG}"
167+
docker tag ${PROJECT_NAME}-${function}:latest "$repo_name:${ENVIRONMENT_TAG}"
168+
169+
# If this variable is set, the create-sbom-report.sh script will scan this docker image instead.
170+
export CHECK_DOCKER_IMAGE=${PROJECT_NAME}-${function}:latest
171+
export FORCE_USE_DOCKER=true
172+
173+
export PR_NUM_TAG=${PR_NUM_TAG}
174+
echo "PR_NUM_TAG=${PR_NUM_TAG}" >> ${GITHUB_ENV}
175+
176+
# Push the image to the repository
177+
docker push "${repo_name}:${COMMIT_HASH_TAG}"
178+
if [ "${PR_NUM_TAG}" != 'pr' ]; then
179+
docker push "${repo_name}:${PR_NUM_TAG}"
180+
fi
181+
docker push "${repo_name}:${ENVIRONMENT_TAG}"
182+
183+
- name: Cleanup the docker images
184+
env:
185+
PROJECT_NAME: ${{ inputs.project_name }}
186+
ACR_NAME: ${{ secrets.acr_name }}
187+
run: |
188+
function=${{ matrix.function }}
189+
repo_name="${ACR_NAME}.azurecr.io/${PROJECT_NAME}-${function}"
190+
191+
# Remove the images
192+
docker rmi "${repo_name}:${COMMIT_HASH_TAG}"
193+
docker rmi "${repo_name}:${PR_NUM_TAG}"
194+
docker rmi "${repo_name}:${ENVIRONMENT_TAG}"
195+
docker rmi ${PROJECT_NAME}-${function}:latest

0 commit comments

Comments
 (0)