-
Notifications
You must be signed in to change notification settings - Fork 1
244 lines (217 loc) · 8.47 KB
/
Copy pathci_cd_docker.yml
File metadata and controls
244 lines (217 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
name: CI/CD - Docker
on:
# Trigger the workflow for all PRs
pull_request:
# Make the workflow callable (from cd_release.yml workflow)
workflow_call:
inputs:
source_ref:
description: 'The source branch to build from.'
required: true
default: 'master'
type: 'string'
image_name:
description: 'The name of the Docker image to build.'
required: false
default: 'oteapi'
type: 'string'
image_owner:
description: 'The owner of the Docker image to build.'
required: false
default: 'emmc-asbl'
type: 'string'
registry_url:
description: 'The URL for the Container Registry.'
required: false
default: 'ghcr.io'
type: 'string'
secrets:
REGISTRY_USER:
description: 'The username for the Container Registry.'
required: true
REGISTRY_PASSWORD:
description: 'The password (or Personal Access Token) for the Container Registry.'
required: true
PAT:
description: 'The Personal Access Token to read contents from the GitHub organization.'
required: true
jobs:
docker:
name: Docker
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
services:
redis:
image: redis:latest
ports:
- 6379:6379
steps:
- name: Checkout ${{ github.repository }}
uses: actions/checkout@v6
with:
ref: ${{ inputs.source_ref || github.ref }}
fetch-depth: 0
- name: Retrieve metadata information
id: metadata
run: |
# Get current OTEAPI-Services version from app/__init__.py
regex="^__version__ = (\"|')(.*)(\"|')$"
while IFS="" read -r line || [ -n "${line}" ]; do
if [[ "${line}" =~ $regex ]]; then
VERSION="${BASH_REMATCH[2]}"
fi
done < app/__init__.py
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "time_now=$(date --utc +%FT%TZ)" >> $GITHUB_OUTPUT
- name: Docker metadata (development)
id: docker_metadata_dev
uses: docker/metadata-action@v6
with:
images: |
${{ inputs.registry_url || 'ghcr.io' }}/${{ inputs.image_owner || 'emmc-asbl' }}/${{ inputs.image_name || 'oteapi' }}
flavor: |
latest=false
prefix=
suffix=
tags: |
type=raw,value=${{ steps.metadata.outputs.version }}-dev
type=raw,value=latest-dev
labels: |
org.opencontainers.image.created=${{ steps.metadata.outputs.time_now }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.title=OTEAPI Services
org.opencontainers.image.vendor=SINTEF; EMMC-ASBL
org.opencontainers.image.authors=SINTEF (Team4.0@SINTEF.no)
org.opencontainers.image.licenses=MIT
gh_actions_runnumber=${{ github.run_id }}
oteapi=development
- name: Docker metadata (production)
id: docker_metadata_prod
uses: docker/metadata-action@v6
with:
images: |
${{ inputs.registry_url || 'ghcr.io' }}/${{ inputs.image_owner || 'emmc-asbl' }}/${{ inputs.image_name || 'oteapi' }}
tags: |
type=raw,value=${{ steps.metadata.outputs.version }}
type=raw,value=latest
labels: |
org.opencontainers.image.created=${{ steps.metadata.outputs.time_now }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.title=OTEAPI Services
org.opencontainers.image.vendor=SINTEF; EMMC-ASBL
org.opencontainers.image.authors=SINTEF (Team4.0@SINTEF.no)
org.opencontainers.image.licenses=MIT
gh_actions_runnumber=${{ github.run_id }}
oteapi=production
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to Container Registry
uses: docker/login-action@v4
with:
registry: ${{ inputs.registry_url || 'ghcr.io' }}
username: ${{ secrets.REGISTRY_USER || github.actor }}
password: ${{ secrets.REGISTRY_PASSWORD || secrets.GITHUB_TOKEN }}
- name: Build and push (development)
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
target: development
build-contexts: |
oteapi-core=https://github.com/EMMC-ASBL/oteapi-core.git
# Push only if called from `cd_release.yml` workflow (push to 'master' branch)
push: ${{ github.event_name == 'push' && inputs.source_ref == 'master' }}
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ steps.docker_metadata_dev.outputs.tags }}
labels: ${{ steps.docker_metadata_dev.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
secrets: |
GIT_AUTH_TOKEN=${{ secrets.PAT || secrets.RELEASE_PAT }}
- name: Build and push (production)
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
target: production
# Push only if called from `cd_release.yml` workflow (push to 'master' branch)
push: ${{ github.event_name == 'push' && inputs.source_ref == 'master' }}
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ steps.docker_metadata_prod.outputs.tags }}
labels: ${{ steps.docker_metadata_prod.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
- name: Test run Docker development image
if: github.event_name == 'pull_request'
run: |
CONTAINER_NAME=${{ github.run_id }}-${{ github.action }}-devel
# Run Docker development image
docker run -d \
--name ${CONTAINER_NAME} \
-e "OTEAPI_REDIS_TYPE=redis" \
-e "OTEAPI_REDIS_HOST=localhost" \
-e "OTEAPI_REDIS_PORT=6379" \
-e "OTEAPI_PREFIX=/api/v1" \
--network host \
${{ fromJSON(steps.docker_metadata_dev.outputs.json).tags[0] }}
# Health check
tmp_file=/tmp/.dockerPs
SECONDS=0
TIMEOUT_LIMIT=240
until curl -sf http://localhost:8080/openapi.json > /dev/null || [ "$SECONDS" -gt "$TIMEOUT_LIMIT" ] ; do
docker ps -a > $tmp_file
grep -E "^.*${CONTAINER_NAME} .* Exited .*$" $tmp_file && exited=yes && break
sleep 1
done
rm -f $tmp_file
if [ "$SECONDS" -gt "$TIMEOUT_LIMIT" ]; then
echo "OTEAPI-Services (development) failed to start within ${TIMEOUT_LIMIT} seconds"
exited=yes
fi
# Write logs
docker logs ${CONTAINER_NAME}
if [ -n "$exited" ]; then
exit 1
fi
# Cleanup
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}
- name: Test run Docker production image
if: github.event_name == 'pull_request'
run: |
CONTAINER_NAME=${{ github.run_id }}-${{ github.action }}-prod
# Run Docker production image
docker run -d \
--name ${CONTAINER_NAME} \
-e "OTEAPI_REDIS_TYPE=redis" \
-e "OTEAPI_REDIS_HOST=localhost" \
-e "OTEAPI_REDIS_PORT=6379" \
-e "OTEAPI_PREFIX=/api/v1" \
--network host \
${{ fromJSON(steps.docker_metadata_prod.outputs.json).tags[0] }}
# Health check
tmp_file=/tmp/.dockerPs
SECONDS=0
TIMEOUT_LIMIT=240
until curl -sf http://localhost:8080/openapi.json > /dev/null || [ "$SECONDS" -gt "$TIMEOUT_LIMIT" ] ; do
docker ps -a > $tmp_file
grep -E "^.*${CONTAINER_NAME} .* Exited .*$" $tmp_file && exited=yes && break
sleep 1
done
rm -f $tmp_file
if [ "$SECONDS" -gt "$TIMEOUT_LIMIT" ]; then
echo "OTEAPI-Services (production) failed to start within ${TIMEOUT_LIMIT} seconds"
exited=yes
fi
# Write logs
docker logs ${CONTAINER_NAME}
if [ -n "$exited" ]; then
exit 1
fi
# Cleanup
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}