-
Notifications
You must be signed in to change notification settings - Fork 11
159 lines (141 loc) · 6.88 KB
/
build-test-and-publish-dependencies-image.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
# Copyright 2024 The MathWorks, Inc.
name: Build and Publish the Container Image
# images build with this workflow are created with the following naming conventions:
# ${base_image_name}:${matlab_release}-${os}
# built images are pushed to DockerHub. Note: Both camel & Pascal case versions of tags are generated and published
# This workflow is only be triggered when called from another workflow.
on:
workflow_call:
inputs:
docker_build_context:
description: "Relative path to folder with Dockerfile. Ex: ./matlab-deps/r2023a/ubuntu20.04"
required: true
type: string
base_image_name:
description: "Name of base image. Example: mathworks/matlab-deps"
required: true
type: string
matlab_release_tag:
description: "Name of the MATLAB release. Example: r2023a"
required: true
type: string
os_info_tag:
description: "Allowed values: aws-batch, ubi8, ubi9, ubuntu20.04 ubuntu22.04"
required: true
type: string
is_default_os:
description: "Specify whether the OS being tagged is desired default OS. For example, This field is used to configure that matlab-deps/r2023a should point to matlab-deps/r2023a-ubuntu20.04"
required: true
type: boolean
should_add_latest_tag:
description: "Specify if this image should also be tagged as latest"
required: true
type: boolean
test_file_path:
description: "Relative path to the root directory. Ex: ./tests/matlab-deps/test_ubuntu2204.py"
required: false
type: string
jobs:
build-push-image:
runs-on: ubuntu-latest
steps:
# Invalid combination would error out.
- name: Invalid combination of should_add_latest_tag set to true and is_default_os set to false
if: ${{ inputs.should_add_latest_tag == true && inputs.is_default_os == false }}
run: |
echo "Invalid situation detected. A workflow marked as latest must also set the default os to be true. "
exit 1
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Image Tags
id: setup_image_tags
run: |
RAW_MATLAB_RELEASE=${{ inputs.matlab_release_tag }} \
&& LOWER_CASE_MATLAB_RELEASE=${RAW_MATLAB_RELEASE,,} \
&& echo "TAG_RELEASE_ONLY_CAMEL_CASE=${LOWER_CASE_MATLAB_RELEASE}" >> "${GITHUB_OUTPUT}" \
&& echo "TAG_RELEASE_ONLY_PASCAL_CASE=${LOWER_CASE_MATLAB_RELEASE^}" >> "${GITHUB_OUTPUT}" \
&& echo "TAG_RELEASE_AND_OS_CAMEL_CASE=${LOWER_CASE_MATLAB_RELEASE}-${{ inputs.os_info_tag }}" >> "${GITHUB_OUTPUT}" \
&& echo "TAG_RELEASE_AND_OS_PASCAL_CASE=${LOWER_CASE_MATLAB_RELEASE^}-${{ inputs.os_info_tag }}" >> "${GITHUB_OUTPUT}"
# See here for example: https://docs.docker.com/build/ci/github-actions/push-multi-registries/
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04
- name: Build Image
if: ${{ inputs.should_add_latest_tag == false && inputs.is_default_os == false }}
uses: docker/build-push-action@v6
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
load: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04, r2023a, R2023a
- name: Build Image for latest OS
if: ${{ inputs.should_add_latest_tag == false && inputs.is_default_os == true }}
uses: docker/build-push-action@v6
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
load: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_PASCAL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04, r2023a, R2023a, latest
- name: Build Image with latest Tag for latest OS
if: ${{ inputs.should_add_latest_tag == true && inputs.is_default_os == true }}
uses: docker/build-push-action@v6
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
load: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_PASCAL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
${{ inputs.base_image_name }}:latest
- name: Should Run Tests
id: run_tests
run: |
if [ -n "${{ inputs.test_file_path }}" ]; then
echo "should_run=true" >> $GITHUB_OUTPUT
else
echo "should_run=false" >> $GITHUB_OUTPUT
fi
- name: Set up Python 3
if: ${{ steps.run_tests.outputs.should_run == 'true' }}
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install test dependencies
if: ${{ steps.run_tests.outputs.should_run == 'true' }}
shell: bash
working-directory: tests
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Get test relative path
id: get_test_path
if: ${{ steps.run_tests.outputs.should_run == 'true' }}
run: |
TESTDIR=tests
echo "testdir=${TESTDIR}" >> $GITHUB_OUTPUT
echo "relpath=$(realpath --relative-to=${TESTDIR} ${{ inputs.test_file_path }})" >> $GITHUB_OUTPUT
- name: Test container
if: ${{ steps.run_tests.outputs.should_run == 'true' }}
shell: bash
env:
IMAGE_NAME: "${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}"
working-directory: ${{ steps.get_test_path.outputs.testdir }}
run: python -m unittest -v ${{ steps.get_test_path.outputs.relpath }}
- name: Push Image
shell: bash
run: docker push --all-tags ${{ inputs.base_image_name }}