-
Notifications
You must be signed in to change notification settings - Fork 3
148 lines (128 loc) · 4.88 KB
/
docker.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
name: Build and test docker
on:
pull_request:
paths-ignore:
- "**.md"
push:
branches:
- main
jobs:
get_dockerfiles:
name: Get list of Docker files for different containers
runs-on: ubuntu-latest
outputs:
docker_toml: ${{ steps.filter.outputs.docker_toml }}
docker_tasks: ${{ steps.set-matrix.outputs.docker_tasks }}
steps:
- name: Checkout mrpro repo
uses: actions/checkout@v4
- name: Check if files in docker or the toml file has been modified
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
docker_toml:
- 'docker/*'
- '.github/workflows/docker.yml'
- name: Do we need to do something?
run: |
echo "Push to main? ${{ github.event_name }} "
echo "Rebuild containers? ${{ steps.filter.outputs.docker_toml }}"
- name: Define docker image names
id: set-matrix
if: steps.filter.outputs.docker_toml == 'true' || github.event_name == 'push'
run: |
# docker_tasks is a list of pairs (dictionaries) with keys 'filepath' and 'image_name' like:
# [{"filepath": "docker/Dockerfile_x1", "image_name": "ghcr.io/ptb-mr/mrpro_x1"}, ...]
docker_tasks=$(find docker -type f -name 'Dockerfile*' | jq -R -s -c 'split("\n")[:-1]' | \
jq -r -c 'map({filepath: ., image_name: . | sub("docker/Dockerfile"; "ghcr.io\/ptb-mr\/mrpro")})')
echo "docker_tasks: $docker_tasks"
echo "docker_tasks=$docker_tasks" >> $GITHUB_OUTPUT
- name: Dockerfile overview
if: steps.filter.outputs.docker_toml == 'true' || github.event_name == 'push'
run: |
echo "final list of docker_tasks: ${{ steps.set-matrix.outputs.docker_tasks }}"
push_test:
name: Create test images and push to GCR
needs: get_dockerfiles
if: needs.get_dockerfiles.outputs.docker_toml == 'true' || github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
packages: write
strategy:
fail-fast: false
matrix:
docker_task: ${{ fromJson(needs.get_dockerfiles.outputs.docker_tasks) }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get image basename
id: image_name
run: |
echo "dockerfile_basename=$(basename ${{ matrix.docker_task.filepath }})" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Packages
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ptb-mr
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
cache-from: type=gha,scope=$${{ steps.image_name.outputs.dockerfile_basename }})
cache-to: type=gha,mode=max,scope=${{ steps.image_name.outputs.dockerfile_basename }}
file: ${{ matrix.docker_task.filepath }}
push: true
tags: ${{ matrix.docker_task.image_name }}:test
test:
name: Test docker containers
needs: [get_dockerfiles, push_test]
if: needs.get_dockerfiles.outputs.docker_toml == 'true' || github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
strategy:
matrix:
docker_task: ${{ fromJson(needs.get_dockerfiles.outputs.docker_tasks) }}
# runs within Docker container
container:
image: ${{ matrix.docker_task.image_name }}:test
options: --user runner
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install mrpro and dependencies
run: pip install --upgrade --upgrade-strategy "eager" .[test]
- name: Install pytest-github-actions-annotate-failures plugin
run: pip install pytest-github-actions-annotate-failures
- name: Run PyTest
run: |
pytest -n 4 -m "not cuda"
push_latest:
name: Pull latest images and push to GCR with new tag
needs: [get_dockerfiles, test]
if: needs.get_dockerfiles.outputs.docker_toml == 'true' || github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
packages: write
strategy:
matrix:
docker_task: ${{ fromJson(needs.get_dockerfiles.outputs.docker_tasks) }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Login to GitHub Packages
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ptb-mr
password: ${{ secrets.GITHUB_TOKEN }}
- name: Pull and push Docker image with new tag
run: |
docker pull ${{ matrix.docker_task.image_name }}:test
docker tag ${{ matrix.docker_task.image_name }}:test ${{ matrix.docker_task.image_name }}:latest
docker push ${{ matrix.docker_task.image_name}}:latest