Skip to content

Commit f43550c

Browse files
authored
Add Docker multi-platform build workflow (#843)
* Add Docker multi-platform build workflow * Add GHCR cleanup workflow * Update docker.yml
1 parent 697809e commit f43550c

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

.github/workflows/docker.yml

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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/df-py' }}
15+
GHCR_IMAGE: ${{ 'ghcr.io/oceanprotocol/df-py' }}
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+
merge:
121+
runs-on: ubuntu-latest
122+
if: ${{ github.actor != 'dependabot[bot]' }}
123+
needs:
124+
- build
125+
steps:
126+
- name: Download digests
127+
uses: actions/download-artifact@v4
128+
with:
129+
path: /tmp/digests
130+
pattern: digests-*
131+
merge-multiple: true
132+
- name: Set up Docker Buildx
133+
uses: docker/setup-buildx-action@v3
134+
- name: Set Docker metadata
135+
id: ocean_node_meta
136+
uses: docker/metadata-action@v5
137+
with:
138+
images: |
139+
${{ env.DOCKERHUB_IMAGE }}
140+
${{ env.GHCR_IMAGE }}
141+
# generate Docker tags based on the following events/attributes
142+
tags: |
143+
type=ref,event=branch
144+
type=semver,pattern={{version}}
145+
type=ref,event=pr
146+
# type=semver,pattern={{major}}.{{minor}}
147+
# type=semver,pattern={{major}}
148+
# type=sha
149+
- name: Login to Docker Hub
150+
id: dockerhub_login
151+
env:
152+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
153+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
154+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
155+
uses: docker/login-action@v3
156+
with:
157+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
158+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
159+
- name: Login to GitHub Container Registry
160+
id: ghcr_login
161+
env:
162+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
163+
if: env.GHCR_PUSH_TOKEN != ''
164+
uses: docker/login-action@v3
165+
with:
166+
registry: ghcr.io
167+
username: ${{ github.repository_owner }}
168+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
169+
- name: Create manifest list and push to Docker Hub
170+
if: steps.dockerhub_login.outcome == 'success'
171+
working-directory: /tmp/digests
172+
env:
173+
DOCKERHUB_IMAGE: ${{ env.DOCKERHUB_IMAGE }}
174+
run: |
175+
if ls dockerhub-* 1> /dev/null 2>&1; then
176+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${DOCKERHUB_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
177+
# Strip dockerhub-arm64- and dockerhub- so digest is always just the sha256 hex
178+
DIGESTS=$(ls dockerhub-* | sed -e "s|dockerhub-arm64-|${DOCKERHUB_IMAGE}@sha256:|" -e "s|dockerhub-|${DOCKERHUB_IMAGE}@sha256:|" | tr '\n' ' ')
179+
docker buildx imagetools create $TAGS $DIGESTS
180+
fi
181+
- name: Create manifest list and push to GHCR
182+
if: steps.ghcr_login.outcome == 'success'
183+
working-directory: /tmp/digests
184+
env:
185+
GHCR_IMAGE: ${{ env.GHCR_IMAGE }}
186+
run: |
187+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${GHCR_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
188+
# Strip ghcr-arm64- and ghcr- so digest is always just the sha256 hex
189+
DIGESTS=$(ls ghcr-* | sed -e "s|ghcr-arm64-|${GHCR_IMAGE}@sha256:|" -e "s|ghcr-|${GHCR_IMAGE}@sha256:|" | tr '\n' ' ')
190+
docker buildx imagetools create $TAGS $DIGESTS
191+
- name: Inspect Docker Hub image
192+
if: steps.dockerhub_login.outcome == 'success'
193+
run: |
194+
docker buildx imagetools inspect ${{ env.DOCKERHUB_IMAGE }}:${{ steps.ocean_node_meta.outputs.version }}
195+
- name: Inspect GHCR image
196+
if: steps.ghcr_login.outcome == 'success'
197+
run: |
198+
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)