Skip to content

Commit 30f3d40

Browse files
authored
Create Docker multi-platform build workflow and native ARM64 (#1761)
* Create Docker multi-platform build workflow Add GitHub Actions workflow for Docker multi-platform builds, including steps for building and pushing images to Docker Hub and GitHub Container Registry. * Add GHCR cleanup workflow * Remove workflow_dispatch from docker.yml Removed workflow_dispatch trigger from Docker build.
1 parent 6e56ccf commit 30f3d40

2 files changed

Lines changed: 333 additions & 0 deletions

File tree

.github/workflows/docker.yml

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
name: Docker Multi Platform Builds
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags:
8+
- 'v*.*.*'
9+
pull_request:
10+
branches:
11+
- 'main'
12+
13+
env:
14+
DOCKERHUB_IMAGE: ${{ 'oceanprotocol/pdr-backend' }}
15+
GHCR_IMAGE: ${{ 'ghcr.io/oceanprotocol/pdr-backend' }}
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
if: ${{ github.actor != 'dependabot[bot]' }}
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
# we keep this just in case we need to change
25+
platform: ${{ github.event_name == 'pull_request' && fromJSON('["linux/amd64"]') || fromJSON('["linux/amd64"]') }}
26+
steps:
27+
- name: Prepare
28+
run: |
29+
platform=${{ matrix.platform }}
30+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
31+
- name: Checkout
32+
uses: actions/checkout@v6
33+
- name: Set up QEMU
34+
uses: docker/setup-qemu-action@v3
35+
with:
36+
platforms: ${{ matrix.platform }}
37+
#image: tonistiigi/binfmt:qemu-v8.0.4
38+
- name: Set up Docker Buildx
39+
id: buildx
40+
uses: docker/setup-buildx-action@v3
41+
with:
42+
platforms: ${{ matrix.platform }}
43+
- name: Login to Docker Hub
44+
id: dockerhub_login
45+
env:
46+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
47+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
48+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
49+
uses: docker/login-action@v3
50+
with:
51+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
52+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
53+
- name: Login to GitHub Container Registry
54+
id: ghcr_login
55+
env:
56+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
57+
if: env.GHCR_PUSH_TOKEN != ''
58+
uses: docker/login-action@v3
59+
with:
60+
registry: ghcr.io
61+
username: ${{ github.repository_owner }}
62+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
63+
- name: Set Docker metadata
64+
id: ocean_node_meta
65+
uses: docker/metadata-action@v5
66+
with:
67+
images: |
68+
${{ env.DOCKERHUB_IMAGE }}
69+
${{ env.GHCR_IMAGE }}
70+
# generate Docker tags based on the following events/attributes
71+
tags: |
72+
type=ref,event=branch
73+
type=semver,pattern={{version}}
74+
type=ref,event=pr
75+
# type=semver,pattern={{major}}.{{minor}}
76+
# type=semver,pattern={{major}}
77+
# type=sha
78+
- name: Build and push to Docker Hub
79+
if: steps.dockerhub_login.outcome == 'success'
80+
id: build_dockerhub
81+
uses: docker/build-push-action@v5
82+
with:
83+
builder: ${{ steps.buildx.outputs.name }}
84+
context: .
85+
platforms: ${{ matrix.platform }}
86+
push: true
87+
# tags: ${{ steps.ocean_node_meta.outputs.tags }}
88+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
89+
outputs: type=image,name=${{ env.DOCKERHUB_IMAGE }},push-by-digest=true,name-canonical=true,push=true
90+
- name: Build and push to GHCR
91+
if: steps.ghcr_login.outcome == 'success'
92+
id: build_ghcr
93+
uses: docker/build-push-action@v5
94+
with:
95+
builder: ${{ steps.buildx.outputs.name }}
96+
context: .
97+
platforms: ${{ matrix.platform }}
98+
push: true
99+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
100+
outputs: type=image,name=${{ env.GHCR_IMAGE }},push-by-digest=true,name-canonical=true,push=true
101+
- name: Export digests
102+
run: |
103+
mkdir -p /tmp/digests
104+
if [ -n "${{ steps.build_dockerhub.outputs.digest }}" ]; then
105+
dockerhub_digest="${{ steps.build_dockerhub.outputs.digest }}"
106+
touch "/tmp/digests/dockerhub-${dockerhub_digest#sha256:}"
107+
fi
108+
if [ -n "${{ steps.build_ghcr.outputs.digest }}" ]; then
109+
ghcr_digest="${{ steps.build_ghcr.outputs.digest }}"
110+
touch "/tmp/digests/ghcr-${ghcr_digest#sha256:}"
111+
fi
112+
- name: Upload digest
113+
uses: actions/upload-artifact@v4
114+
with:
115+
name: digests-${{ env.PLATFORM_PAIR }}
116+
path: /tmp/digests/*
117+
if-no-files-found: error
118+
retention-days: 1
119+
120+
build-arm:
121+
runs-on: ubuntu-24.04-arm
122+
if: ${{ github.actor != 'dependabot[bot]' }}
123+
strategy:
124+
fail-fast: false
125+
matrix:
126+
# we keep this just in case we need to change
127+
platform: ${{ github.event_name == 'pull_request' && fromJSON('["linux/arm64"]') || fromJSON('["linux/arm64"]') }}
128+
steps:
129+
- name: Prepare
130+
run: |
131+
platform=${{ matrix.platform }}
132+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
133+
- name: Checkout
134+
uses: actions/checkout@v6
135+
- name: Set up QEMU
136+
uses: docker/setup-qemu-action@v3
137+
with:
138+
platforms: ${{ matrix.platform }}
139+
#image: tonistiigi/binfmt:qemu-v8.0.4
140+
- name: Set up Docker Buildx
141+
id: buildx
142+
uses: docker/setup-buildx-action@v3
143+
with:
144+
platforms: ${{ matrix.platform }}
145+
- name: Login to Docker Hub
146+
id: dockerhub_login
147+
env:
148+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
149+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
150+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
151+
uses: docker/login-action@v3
152+
with:
153+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
154+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
155+
- name: Login to GitHub Container Registry
156+
id: ghcr_login
157+
env:
158+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
159+
if: env.GHCR_PUSH_TOKEN != ''
160+
uses: docker/login-action@v3
161+
with:
162+
registry: ghcr.io
163+
username: ${{ github.repository_owner }}
164+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
165+
- name: Set Docker metadata
166+
id: ocean_node_meta
167+
uses: docker/metadata-action@v5
168+
with:
169+
images: |
170+
${{ env.DOCKERHUB_IMAGE }}
171+
${{ env.GHCR_IMAGE }}
172+
# generate Docker tags based on the following events/attributes
173+
tags: |
174+
type=ref,event=branch
175+
type=semver,pattern={{version}}
176+
type=ref,event=pr
177+
# type=semver,pattern={{major}}.{{minor}}
178+
# type=semver,pattern={{major}}
179+
# type=sha
180+
- name: Build and push to Docker Hub
181+
if: steps.dockerhub_login.outcome == 'success'
182+
id: build_dockerhub
183+
uses: docker/build-push-action@v5
184+
with:
185+
builder: ${{ steps.buildx.outputs.name }}
186+
context: .
187+
platforms: ${{ matrix.platform }}
188+
push: true
189+
# tags: ${{ steps.ocean_node_meta.outputs.tags }}
190+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
191+
outputs: type=image,name=${{ env.DOCKERHUB_IMAGE }},push-by-digest=true,name-canonical=true,push=true
192+
- name: Build and push to GHCR
193+
if: steps.ghcr_login.outcome == 'success'
194+
id: build_ghcr
195+
uses: docker/build-push-action@v5
196+
with:
197+
builder: ${{ steps.buildx.outputs.name }}
198+
context: .
199+
platforms: ${{ matrix.platform }}
200+
push: true
201+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
202+
outputs: type=image,name=${{ env.GHCR_IMAGE }},push-by-digest=true,name-canonical=true,push=true
203+
- name: Export digests
204+
run: |
205+
mkdir -p /tmp/digests
206+
if [ -n "${{ steps.build_dockerhub.outputs.digest }}" ]; then
207+
dockerhub_digest="${{ steps.build_dockerhub.outputs.digest }}"
208+
touch "/tmp/digests/dockerhub-arm64-${dockerhub_digest#sha256:}"
209+
fi
210+
if [ -n "${{ steps.build_ghcr.outputs.digest }}" ]; then
211+
ghcr_digest="${{ steps.build_ghcr.outputs.digest }}"
212+
touch "/tmp/digests/ghcr-arm64-${ghcr_digest#sha256:}"
213+
fi
214+
- name: Upload digest
215+
uses: actions/upload-artifact@v4
216+
with:
217+
name: digests-${{ env.PLATFORM_PAIR }}
218+
path: /tmp/digests/*
219+
if-no-files-found: error
220+
retention-days: 1
221+
222+
merge:
223+
runs-on: ubuntu-latest
224+
if: ${{ github.actor != 'dependabot[bot]' }}
225+
needs:
226+
- build
227+
- build-arm
228+
steps:
229+
- name: Download digests
230+
uses: actions/download-artifact@v4
231+
with:
232+
path: /tmp/digests
233+
pattern: digests-*
234+
merge-multiple: true
235+
- name: Set up Docker Buildx
236+
uses: docker/setup-buildx-action@v3
237+
- name: Set Docker metadata
238+
id: ocean_node_meta
239+
uses: docker/metadata-action@v5
240+
with:
241+
images: |
242+
${{ env.DOCKERHUB_IMAGE }}
243+
${{ env.GHCR_IMAGE }}
244+
# generate Docker tags based on the following events/attributes
245+
tags: |
246+
type=ref,event=branch
247+
type=semver,pattern={{version}}
248+
type=ref,event=pr
249+
# type=semver,pattern={{major}}.{{minor}}
250+
# type=semver,pattern={{major}}
251+
# type=sha
252+
- name: Login to Docker Hub
253+
id: dockerhub_login
254+
env:
255+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
256+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
257+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
258+
uses: docker/login-action@v3
259+
with:
260+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
261+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
262+
- name: Login to GitHub Container Registry
263+
id: ghcr_login
264+
env:
265+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
266+
if: env.GHCR_PUSH_TOKEN != ''
267+
uses: docker/login-action@v3
268+
with:
269+
registry: ghcr.io
270+
username: ${{ github.repository_owner }}
271+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
272+
- name: Create manifest list and push to Docker Hub
273+
if: steps.dockerhub_login.outcome == 'success'
274+
working-directory: /tmp/digests
275+
env:
276+
DOCKERHUB_IMAGE: ${{ env.DOCKERHUB_IMAGE }}
277+
run: |
278+
if ls dockerhub-* 1> /dev/null 2>&1; then
279+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${DOCKERHUB_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
280+
# Strip dockerhub-arm64- and dockerhub- so digest is always just the sha256 hex
281+
DIGESTS=$(ls dockerhub-* | sed -e "s|dockerhub-arm64-|${DOCKERHUB_IMAGE}@sha256:|" -e "s|dockerhub-|${DOCKERHUB_IMAGE}@sha256:|" | tr '\n' ' ')
282+
docker buildx imagetools create $TAGS $DIGESTS
283+
fi
284+
- name: Create manifest list and push to GHCR
285+
if: steps.ghcr_login.outcome == 'success'
286+
working-directory: /tmp/digests
287+
env:
288+
GHCR_IMAGE: ${{ env.GHCR_IMAGE }}
289+
run: |
290+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${GHCR_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
291+
# Strip ghcr-arm64- and ghcr- so digest is always just the sha256 hex
292+
DIGESTS=$(ls ghcr-* | sed -e "s|ghcr-arm64-|${GHCR_IMAGE}@sha256:|" -e "s|ghcr-|${GHCR_IMAGE}@sha256:|" | tr '\n' ' ')
293+
docker buildx imagetools create $TAGS $DIGESTS
294+
- name: Inspect Docker Hub image
295+
if: steps.dockerhub_login.outcome == 'success'
296+
run: |
297+
docker buildx imagetools inspect ${{ env.DOCKERHUB_IMAGE }}:${{ steps.ocean_node_meta.outputs.version }}
298+
- name: Inspect GHCR image
299+
if: steps.ghcr_login.outcome == 'success'
300+
run: |
301+
docker buildx imagetools inspect ${{ env.GHCR_IMAGE }}:${{ steps.ocean_node_meta.outputs.version }}

.github/workflows/ghcr_cleanup.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: GHCR cleanup
2+
3+
on:
4+
workflow_dispatch:
5+
6+
env:
7+
DOCKERHUB_IMAGE: ${{ 'oceanprotocol/ocean-node' }}
8+
GHCR_IMAGE: ${{ 'ghcr.io/oceanprotocol/ocean-node' }}
9+
10+
jobs:
11+
ghcr_cleanup:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Login to GitHub Container Registry
15+
id: ghcr_login
16+
env:
17+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
18+
if: env.GHCR_PUSH_TOKEN != ''
19+
uses: docker/login-action@v3
20+
with:
21+
registry: ghcr.io
22+
username: ${{ github.repository_owner }}
23+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
24+
- name: 'Clean up docker images'
25+
if: steps.ghcr_login.outcome == 'success'
26+
uses: dataaxiom/ghcr-cleanup-action@v1
27+
with:
28+
token: ${{ secrets.GHCR_PUSH_TOKEN }}
29+
exclude-tags: latest,main,v*
30+
older-than: 1 month
31+
delete-untagged: true
32+
delete-partial-images: true

0 commit comments

Comments
 (0)