-
Notifications
You must be signed in to change notification settings - Fork 5
421 lines (379 loc) · 17 KB
/
build_and_publish.yml
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# .github/workflows/build_and_publish.yml
name: Build and Publish Containers
on:
workflow_dispatch:
pull_request:
branches: [main]
paths-ignore:
- '.devcontainer/**'
- '.github/**'
- '.vscode/**'
push:
branches: [main]
paths-ignore:
- '.devcontainer/**'
- '.github/**'
- '.vscode/**'
release:
types: [published]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-publish:
runs-on: ubuntu-latest
strategy:
matrix:
version_major: [16, 17, 18]
version_minor: [0]
permissions:
id-token: write
packages: write
contents: read
attestations: write
steps:
# Checkout the repository
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history for versioning and labels
# Set up QEMU for multi-platform builds
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
# Set up Docker Buildx for multi-platform builds
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true # Ensures Buildx is set up
# Get the current timestamp for build identification
- name: Get Current Timestamp
id: get_timestamp
run: echo "build_timestamp=$(date +%s)" >> $GITHUB_OUTPUT
# Log in to the GitHub Container Registry
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Set up build variables and determine tags
- name: Set up Build Variables
id: vars
shell: bash
run: |
set -e
echo "Setting up build variables..."
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
COMMIT_SHA="${{ github.sha }}"
REPO_URL="${{ github.repositoryUrl }}"
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV
echo "COMMIT_SHA=${COMMIT_SHA}" >> $GITHUB_ENV
echo "REPO_URL=${REPO_URL}" >> $GITHUB_ENV
ODOO_VERSION="${{ matrix.version_major }}.${{ matrix.version_minor }}"
ODOO_VERSION_PATTERN='^[0-9]+\.[0-9]+'
echo "Determining version and tags..."
TAG_LIST=()
if [[ "${GITHUB_EVENT_NAME}" == "release" ]]; then
echo "Event is a release"
RELEASE_VERSION="${{ github.event.release.tag_name }}"
# Extract semver components from RELEASE_VERSION
IFS='.' read -ra VER_COMPONENTS <<< "$RELEASE_VERSION"
REL_MAJOR="${VER_COMPONENTS[0]}"
REL_MINOR="${VER_COMPONENTS[1]}"
REL_PATCH="${VER_COMPONENTS[2]}"
# Build tag list
TAG_LIST+=("${ODOO_VERSION}")
if [[ -n "${REL_MAJOR}" ]]; then
TAG_LIST+=("${ODOO_VERSION}-${REL_MAJOR}")
fi
if [[ -n "${REL_MAJOR}" && -n "${REL_MINOR}" ]]; then
TAG_LIST+=("${ODOO_VERSION}-${REL_MAJOR}.${REL_MINOR}")
fi
if [[ -n "${REL_MAJOR}" && -n "${REL_MINOR}" && -n "${REL_PATCH}" ]]; then
TAG_LIST+=("${ODOO_VERSION}-${REL_MAJOR}.${REL_MINOR}.${REL_PATCH}")
fi
# For latest Odoo version, add "latest" tag
if [[ "${ODOO_VERSION}" == "18.0" ]]; then
TAG_LIST+=("latest")
fi
VERSION="${REL_MAJOR}.${REL_MINOR}.${REL_PATCH}"
elif [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
echo "On main branch"
VERSION="edge"
TAG_LIST+=("${ODOO_VERSION}-edge")
elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
PR_BRANCH="${GITHUB_HEAD_REF}"
echo "Pull request from branch ${PR_BRANCH}"
# Handle branch names that start with ODOO_VERSION
if [[ "${PR_BRANCH}" =~ ^(${ODOO_VERSION_PATTERN})-(.+)$ ]]; then
BRANCH_ODOO_VERSION="${BASH_REMATCH[1]}"
BRANCH_SUFFIX="${BASH_REMATCH[2]}"
if [[ "${BRANCH_ODOO_VERSION}" == "${ODOO_VERSION}" ]]; then
VERSION="${BRANCH_SUFFIX}"
TAG_LIST+=("${ODOO_VERSION}-${VERSION}")
else
VERSION="${PR_BRANCH}"
TAG_LIST+=("${VERSION}")
fi
else
VERSION="${PR_BRANCH}"
TAG_LIST+=("${ODOO_VERSION}-${VERSION}")
fi
elif [[ "${GITHUB_REF_TYPE}" == "branch" ]]; then
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
echo "On branch ${BRANCH_NAME}"
# Handle branch names that start with ODOO_VERSION
if [[ "${BRANCH_NAME}" =~ ^(${ODOO_VERSION_PATTERN})-(.+)$ ]]; then
BRANCH_ODOO_VERSION="${BASH_REMATCH[1]}"
BRANCH_SUFFIX="${BASH_REMATCH[2]}"
if [[ "${BRANCH_ODOO_VERSION}" == "${ODOO_VERSION}" ]]; then
VERSION="${BRANCH_SUFFIX}"
TAG_LIST+=("${ODOO_VERSION}-${VERSION}")
else
VERSION="${BRANCH_NAME}"
TAG_LIST+=("${VERSION}")
fi
else
VERSION="${BRANCH_NAME}"
TAG_LIST+=("${ODOO_VERSION}-${VERSION}")
fi
else
echo "Event not matched, defaulting to 'dev'"
VERSION="dev"
TAG_LIST+=("${ODOO_VERSION}-dev")
fi
echo "VERSION=${VERSION}" >> $GITHUB_ENV
# Remove duplicates from TAG_LIST
TAG_LIST=($(printf "%s\n" "${TAG_LIST[@]}" | sort -u))
ODOO_IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/odoo-base"
NGINX_IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/odoo-nginx-base"
echo "ODOO_IMAGE_NAME=${ODOO_IMAGE_NAME}" >> $GITHUB_ENV
echo "NGINX_IMAGE_NAME=${NGINX_IMAGE_NAME}" >> $GITHUB_ENV
# Generate tag lists for community and enterprise images
ODOO_COMMUNITY_IMAGE_TAGS=""
ODOO_ENTERPRISE_IMAGE_TAGS=""
NGINX_COMMUNITY_IMAGE_TAGS=""
NGINX_ENTERPRISE_IMAGE_TAGS=""
for TAG in "${TAG_LIST[@]}"; do
ODOO_COMMUNITY_IMAGE_TAGS+="${ODOO_IMAGE_NAME}:${TAG}\n"
ENTERPRISE_TAG=$(echo "$TAG" | sed "s#^${ODOO_VERSION}#${ODOO_VERSION}e#")
ODOO_ENTERPRISE_IMAGE_TAGS+="${ODOO_IMAGE_NAME}:${ENTERPRISE_TAG}\n"
NGINX_COMMUNITY_IMAGE_TAGS+="${NGINX_IMAGE_NAME}:${TAG}\n"
NGINX_ENTERPRISE_IMAGE_TAGS+="${NGINX_IMAGE_NAME}:${ENTERPRISE_TAG}\n"
done
echo "ODOO_COMMUNITY_IMAGE_TAGS<<EOF" >> $GITHUB_OUTPUT
echo -e "${ODOO_COMMUNITY_IMAGE_TAGS}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "ODOO_ENTERPRISE_IMAGE_TAGS<<EOF" >> $GITHUB_OUTPUT
echo -e "${ODOO_ENTERPRISE_IMAGE_TAGS}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "NGINX_COMMUNITY_IMAGE_TAGS<<EOF" >> $GITHUB_OUTPUT
echo -e "${NGINX_COMMUNITY_IMAGE_TAGS}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "NGINX_ENTERPRISE_IMAGE_TAGS<<EOF" >> $GITHUB_OUTPUT
echo -e "${NGINX_ENTERPRISE_IMAGE_TAGS}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# Adjust SOURCE_IMAGE variables to match the correct tags
SOURCE_TAG="${TAG_LIST[0]}"
SOURCE_IMAGE_ODOO="${ODOO_IMAGE_NAME}:${SOURCE_TAG}"
SOURCE_IMAGE_NGINX="${NGINX_IMAGE_NAME}:${SOURCE_TAG}"
echo "SOURCE_IMAGE_ODOO=${SOURCE_IMAGE_ODOO}" >> $GITHUB_ENV
echo "SOURCE_IMAGE_NGINX=${SOURCE_IMAGE_NGINX}" >> $GITHUB_ENV
# Cache Python dependencies to speed up builds
- name: Cache Python dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
# Install Python dependencies
- name: Install Python dependencies
run: |
pip install -r requirements.txt
# Run Python unit tests
- name: Run Python tests
shell: bash
continue-on-error: ${{ github.event_name != 'release' && github.ref != 'refs/heads/main' }}
run: |
echo "Running tests..."
if [[ "${GITHUB_REF}" == "refs/heads/main" ]] || [[ "${GITHUB_EVENT_NAME}" == "release" ]]; then
echo "Strict mode for tests; failing on test failures."
set -e
python3 -m unittest discover tests
else
echo "Non-strict mode for tests; not failing on test failures."
set +e
python3 -m unittest discover tests
fi
# Run builder preparation script
- name: Run builder prep script
env:
ODOO_MAJOR_VERSION: ${{ matrix.version_major }}
ODOO_MINOR_VERSION: ${{ matrix.version_minor }}
ODOO_COMMUNITY_REPOSITORY: "github.com/odoo/odoo"
ODOO_ENTERPRISE_REPOSITORY: "github.com/odoo/enterprise"
GITHUB_TOKEN: ${{ secrets.APERIM_GITHUB_CI_PAT }}
GEOIPUPDATE_ACCOUNT_ID: ${{ secrets.GEOIPUPDATE_ACCOUNT_ID }}
GEOIPUPDATE_LICENSE_KEY: ${{ secrets.GEOIPUPDATE_LICENSE_KEY }}
run: python3 builder/src/main.py
# Build and Push Community Odoo Container
- name: Build and Push Community Odoo Container
id: odoo_community_push
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.c.odoo
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ steps.vars.outputs.ODOO_COMMUNITY_IMAGE_TAGS }}
build-args: |
ODOO_MAJOR_VERSION=${{ matrix.version_major }}
ODOO_MINOR_VERSION=${{ matrix.version_minor }}
BUILD_DATE=${{ env.BUILD_DATE }}
VCS_REF=${{ env.COMMIT_SHA }}
VERSION=${{ env.VERSION }}
REPO_URL=${{ env.REPO_URL }}
BUILD_TIMESTAMP=${{ steps.get_timestamp.outputs.build_timestamp }}
labels: |
org.opencontainers.image.created=${{ env.BUILD_DATE }}
org.opencontainers.image.url=${{ env.REPO_URL }}
org.opencontainers.image.source=${{ env.REPO_URL }}
org.opencontainers.image.version=${{ env.VERSION }}
org.opencontainers.image.revision=${{ env.COMMIT_SHA }}
org.opencontainers.image.vendor="${{ github.repository_owner }}"
org.opencontainers.image.title="Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }}"
org.opencontainers.image.description="Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }} Community Container"
cache-from: type=gha
cache-to: type=gha,mode=max
# Attest Community Odoo Container build provenance
- name: Attest Odoo Container
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.ODOO_IMAGE_NAME }}
subject-digest: ${{ steps.odoo_community_push.outputs.digest }}
push-to-registry: true
# Build and Push Community Nginx Container
- name: Build and Push Community Nginx Container
id: odoo_community_nginx_push
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.c.nginx
platforms: linux/amd64,linux/arm64,linux/ppc64le
push: true
tags: |
${{ steps.vars.outputs.NGINX_COMMUNITY_IMAGE_TAGS }}
build-args: |
ODOO_MAJOR_VERSION=${{ matrix.version_major }}
ODOO_MINOR_VERSION=${{ matrix.version_minor }}
BUILD_DATE=${{ env.BUILD_DATE }}
VCS_REF=${{ env.COMMIT_SHA }}
VERSION=${{ env.VERSION }}
REPO_URL=${{ env.REPO_URL }}
labels: |
org.opencontainers.image.created=${{ env.BUILD_DATE }}
org.opencontainers.image.url=${{ env.REPO_URL }}
org.opencontainers.image.source=${{ env.REPO_URL }}
org.opencontainers.image.version=${{ env.VERSION }}
org.opencontainers.image.revision=${{ env.COMMIT_SHA }}
org.opencontainers.image.vendor="${{ github.repository_owner }}"
org.opencontainers.image.title="Odoo Nginx ${{ matrix.version_major }}.${{ matrix.version_minor }}"
org.opencontainers.image.description="Nginx Container for Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }} Community"
cache-from: type=gha
cache-to: type=gha,mode=max
# Attest Community Nginx Container build provenance
- name: Attest Nginx Container
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.NGINX_IMAGE_NAME }}
subject-digest: ${{ steps.odoo_community_nginx_push.outputs.digest }}
push-to-registry: true
# Build and Push Enterprise Odoo Container
- name: Build and Push Enterprise Odoo Container
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.e.odoo
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ steps.vars.outputs.ODOO_ENTERPRISE_IMAGE_TAGS }}
build-args: |
SOURCE_IMAGE=${{ env.SOURCE_IMAGE_ODOO }}
BUILD_DATE=${{ env.BUILD_DATE }}
VCS_REF=${{ env.COMMIT_SHA }}
VERSION=${{ env.VERSION }}
REPO_URL=${{ env.REPO_URL }}
BUILD_TIMESTAMP=${{ steps.get_timestamp.outputs.build_timestamp }}
labels: |
org.opencontainers.image.created=${{ env.BUILD_DATE }}
org.opencontainers.image.url=${{ env.REPO_URL }}
org.opencontainers.image.source=${{ env.REPO_URL }}
org.opencontainers.image.version=${{ env.VERSION }}
org.opencontainers.image.revision=${{ env.COMMIT_SHA }}
org.opencontainers.image.vendor="${{ github.repository_owner }}"
org.opencontainers.image.title="Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }}e"
org.opencontainers.image.description="Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }} Enterprise Container"
cache-from: type=gha
cache-to: type=gha,mode=max
# Build and Push Enterprise Nginx Container
- name: Build and Push Enterprise Nginx Container
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.e.nginx
platforms: linux/amd64,linux/arm64,linux/ppc64le
push: true
tags: |
${{ steps.vars.outputs.NGINX_ENTERPRISE_IMAGE_TAGS }}
build-args: |
SOURCE_IMAGE=${{ env.SOURCE_IMAGE_NGINX }}
BUILD_DATE=${{ env.BUILD_DATE }}
VCS_REF=${{ env.COMMIT_SHA }}
VERSION=${{ env.VERSION }}
REPO_URL=${{ env.REPO_URL }}
labels: |
org.opencontainers.image.created=${{ env.BUILD_DATE }}
org.opencontainers.image.url=${{ env.REPO_URL }}
org.opencontainers.image.source=${{ env.REPO_URL }}
org.opencontainers.image.version=${{ env.VERSION }}
org.opencontainers.image.revision=${{ env.COMMIT_SHA }}
org.opencontainers.image.vendor="${{ github.repository_owner }}"
org.opencontainers.image.title="Odoo Nginx ${{ matrix.version_major }}.${{ matrix.version_minor }}e"
org.opencontainers.image.description="Nginx Container for Odoo ${{ matrix.version_major }}.${{ matrix.version_minor }} Enterprise"
cache-from: type=gha
cache-to: type=gha,mode=max
notify-repos:
needs: build-and-publish
runs-on: ubuntu-latest
if: ${{ github.event_name == 'release' && needs.build-and-publish.result == 'success' }}
steps:
- name: Send repository_dispatch events to list of repos
env:
REPOSITORIES_TO_PING: ${{ secrets.REPOSITORIES_TO_PING }}
PAT_FOR_PINGING: ${{ secrets.APERIM_GITHUB_CI_PAT }}
CURRENT_REPO: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
run: |
if [ -z "${REPOSITORIES_TO_PING}" ]; then
echo "No repositories to ping."
exit 0
fi
echo "Repositories to ping: ${REPOSITORIES_TO_PING}"
# Replace commas and/or spaces with newlines, then loop over lines
echo "${REPOSITORIES_TO_PING}" | tr ',\n' ' ' | tr ' ' '\n' | while read -r REPO; do
REPO=$(echo "$REPO" | xargs) # Trim whitespace
if [ ! -z "$REPO" ]; then
echo "Pinging repository: $REPO"
# Prepare the payload
PAYLOAD="{\"event_type\": \"update_base_image\", \"client_payload\": { \"repository\": \"${CURRENT_REPO}\", \"release_tag\": \"${RELEASE_TAG}\" }}"
# Send repository_dispatch event
curl -X POST \
-H "Authorization: token ${PAT_FOR_PINGING}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${REPO}/dispatches \
-d "${PAYLOAD}"
fi
done