Skip to content

Commit 84c6880

Browse files
committed
use strategy matrix to deduplicate jobs
1 parent 39569b9 commit 84c6880

File tree

1 file changed

+33
-72
lines changed

1 file changed

+33
-72
lines changed

.github/workflows/build_images.yml

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ on:
88
env:
99
IMAGE_NAME: "${{ secrets.DOCKER_ORG }}/nansat_base"
1010
jobs:
11-
build_standard_docker_image:
11+
build_docker_images:
1212
runs-on: 'ubuntu-20.04'
1313
strategy:
1414
matrix:
15-
versions:
16-
- {'python': '3.7', 'extra_tags': ['latest']}
17-
- {'python': '3.8', 'extra_tags': []}
18-
- {'python': '3.9', 'extra_tags': []}
19-
- {'python': '3.10', 'extra_tags': []}
15+
image:
16+
- dockerfile: Dockerfile
17+
tag_suffix: ''
18+
multi_stage: false
19+
- dockerfile: Dockerfile_slim
20+
tag_suffix: '-slim'
21+
multi_stage: true
22+
version:
23+
- {'python': '3.7', 'latest': true}
24+
- {'python': '3.8', 'latest': false}
25+
- {'python': '3.9', 'latest': false}
26+
- {'python': '3.10', 'latest': false}
2027
steps:
2128
- name: 'Checkout repository'
2229
uses: actions/checkout@v4
@@ -25,11 +32,11 @@ jobs:
2532
id: get_version
2633
run: |
2734
if [[ $GITHUB_REF == refs/tags/* ]];then
28-
TAG="${GITHUB_REF#refs/tags/}-python${{ matrix.versions.python }}"
35+
TAG="${GITHUB_REF#refs/tags/}"
2936
else
30-
TAG='tmp-python${{ matrix.versions.python }}'
37+
TAG='tmp'
3138
fi
32-
echo "::set-output name=VERSION::${TAG}"
39+
echo "VERSION=${TAG}" >> $GITHUB_OUTPUT
3340
3441
- name: Set up Docker Buildx
3542
uses: docker/setup-buildx-action@v3
@@ -38,90 +45,44 @@ jobs:
3845
uses: actions/cache@v3
3946
with:
4047
path: /tmp/.buildx-cache
41-
key: ${{ runner.os }}-buildx-standard-python${{ matrix.versions.python }}-${{ github.sha }}
48+
key: ${{ runner.os }}-buildx${{ matrix.image.tag_suffix }}-python${{ matrix.version.python }}-${{ github.sha }}
4249
restore-keys: |
43-
${{ runner.os }}-buildx-standard-python${{ matrix.versions.python }}-
50+
${{ runner.os }}-buildx${{ matrix.image.tag_suffix }}-python${{ matrix.version.python }}-
4451
4552
- name: Login to DockerHub
4653
uses: docker/login-action@v3
4754
with:
4855
username: ${{ secrets.DOCKER_USER }}
4956
password: ${{ secrets.DOCKER_PASS }}
5057

51-
- name: Build docker image
58+
- name: Build builder image for multi-stage builds
59+
if: ${{ matrix.image.multi_stage }}
5260
uses: docker/build-push-action@v5
5361
with:
5462
context: .
55-
file: Dockerfile
56-
build-args: 'PYTHON_VERSION=${{ matrix.versions.python }}'
57-
push: ${{ startsWith(github.ref, 'refs/tags/') }}
63+
file: ${{ matrix.image.dockerfile }}
64+
build-args: 'PYTHON_VERSION=${{ matrix.version.python }}'
65+
target: builder
66+
push: false
5867
tags: |
59-
${{ env.IMAGE_NAME }}:latest-python${{ matrix.versions.python }}
60-
${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}
61-
${{ join(matrix.versions.extra_tags, '\n') }}
68+
${{ env.IMAGE_NAME }}:builder-${{ steps.get_version.outputs.VERSION }}${{ matrix.image.tag_suffix }}-python${{ matrix.version.python }}
6269
cache-from: type=local,src=/tmp/.buildx-cache
6370
cache-to: type=local,dest=/tmp/.buildx-cache-new
6471

65-
# Temp fix
66-
# https://github.com/docker/build-push-action/issues/252
67-
# https://github.com/moby/buildkit/issues/1896
68-
- name: Move cache
69-
run: |
70-
rm -rf /tmp/.buildx-cache
71-
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
72-
73-
build_slim_docker_image:
74-
runs-on: 'ubuntu-20.04'
75-
strategy:
76-
matrix:
77-
versions:
78-
- {'python': '3.7', 'extra_tags': ['latest-slim']}
79-
- {'python': '3.8', 'extra_tags': []}
80-
- {'python': '3.9', 'extra_tags': []}
81-
- {'python': '3.10', 'extra_tags': []}
82-
steps:
83-
- name: 'Checkout repository'
84-
uses: actions/checkout@v4
85-
86-
- name: "Extract tag name"
87-
id: get_version
88-
run: |
89-
if [[ $GITHUB_REF == refs/tags/* ]];then
90-
TAG="${GITHUB_REF#refs/tags/}-python${{ matrix.versions.python }}"
91-
else
92-
TAG='tmp-python${{ matrix.versions.python }}'
93-
fi
94-
echo "::set-output name=VERSION::${TAG}"
95-
96-
- name: Set up Docker Buildx
97-
uses: docker/setup-buildx-action@v3
98-
99-
- name: Cache Docker layers
100-
uses: actions/cache@v3
101-
with:
102-
path: /tmp/.buildx-cache
103-
key: ${{ runner.os }}-buildx-slim-python${{ matrix.versions.python }}-${{ github.sha }}
104-
restore-keys: |
105-
${{ runner.os }}-buildx-slim-python${{ matrix.versions.python }}-
106-
107-
- name: Login to DockerHub
108-
uses: docker/login-action@v3
109-
with:
110-
username: ${{ secrets.DOCKER_USER }}
111-
password: ${{ secrets.DOCKER_PASS }}
112-
11372
- name: Build docker image
11473
uses: docker/build-push-action@v5
11574
with:
11675
context: .
117-
file: Dockerfile_slim
118-
build-args: 'PYTHON_VERSION=${{ matrix.versions.python }}'
76+
file: ${{ matrix.image.dockerfile }}
77+
build-args: 'PYTHON_VERSION=${{ matrix.version.python }}'
11978
push: ${{ startsWith(github.ref, 'refs/tags/') }}
12079
tags: |
121-
${{ env.IMAGE_NAME }}:latest-slim-python${{ matrix.versions.python }}
122-
${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}-slim
123-
${{ join(matrix.versions.extra_tags, '\n') }}
124-
cache-from: type=local,src=/tmp/.buildx-cache
80+
${{ env.IMAGE_NAME }}:latest${{ matrix.image.tag_suffix }}-python${{ matrix.version.python }}
81+
${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}${{ matrix.image.tag_suffix }}-python${{ matrix.version.python }}
82+
${{ matrix.version.latest && format('latest{0}', matrix.image.tag_suffix) || '' }}
83+
cache-from: |
84+
type=local,src=/tmp/.buildx-cache
85+
${{ matrix.image.multi_stage && format('{0}:builder-{1}{2}-python{3}', env.IMAGE_NAME, steps.get_version.outputs.VERSION, matrix.image.tag_suffix, matrix.version.python) || '' }}
12586
cache-to: type=local,dest=/tmp/.buildx-cache-new
12687

12788
# Temp fix

0 commit comments

Comments
 (0)