Skip to content

Commit 781a66e

Browse files
authored
Merge pull request #1011 from oceanprotocol/feature/docker
Create Docker multi-platform build workflow
2 parents e492c35 + aad0445 commit 781a66e

1 file changed

Lines changed: 200 additions & 0 deletions

File tree

.github/workflows/docker.yml

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
name: Docker Multi Platform Builds
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- 'main'
8+
tags:
9+
- 'v*.*.*'
10+
pull_request:
11+
branches:
12+
- 'main'
13+
14+
env:
15+
DOCKERHUB_IMAGE: ${{ 'oceanprotocol/ocean-contracts' }}
16+
GHCR_IMAGE: ${{ 'ghcr.io/oceanprotocol/ocean-contracts' }}
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
if: ${{ github.actor != 'dependabot[bot]' }}
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
# we keep this just in case we need to change
26+
platform: ${{ github.event_name == 'pull_request' && fromJSON('["linux/amd64"]') || fromJSON('["linux/amd64"]') }}
27+
steps:
28+
- name: Prepare
29+
run: |
30+
platform=${{ matrix.platform }}
31+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
32+
- name: Checkout
33+
uses: actions/checkout@v6
34+
- name: Set up QEMU
35+
uses: docker/setup-qemu-action@v3
36+
with:
37+
platforms: ${{ matrix.platform }}
38+
#image: tonistiigi/binfmt:qemu-v8.0.4
39+
- name: Set up Docker Buildx
40+
id: buildx
41+
uses: docker/setup-buildx-action@v3
42+
with:
43+
platforms: ${{ matrix.platform }}
44+
- name: Login to Docker Hub
45+
id: dockerhub_login
46+
env:
47+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
48+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
49+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
50+
uses: docker/login-action@v3
51+
with:
52+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
53+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
54+
- name: Login to GitHub Container Registry
55+
id: ghcr_login
56+
env:
57+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
58+
if: env.GHCR_PUSH_TOKEN != ''
59+
uses: docker/login-action@v3
60+
with:
61+
registry: ghcr.io
62+
username: ${{ github.repository_owner }}
63+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
64+
- name: Set Docker metadata
65+
id: ocean_node_meta
66+
uses: docker/metadata-action@v5
67+
with:
68+
images: |
69+
${{ env.DOCKERHUB_IMAGE }}
70+
${{ env.GHCR_IMAGE }}
71+
# generate Docker tags based on the following events/attributes
72+
tags: |
73+
type=ref,event=branch
74+
type=semver,pattern={{version}}
75+
type=ref,event=pr
76+
# type=semver,pattern={{major}}.{{minor}}
77+
# type=semver,pattern={{major}}
78+
# type=sha
79+
- name: Build and push to Docker Hub
80+
if: steps.dockerhub_login.outcome == 'success'
81+
id: build_dockerhub
82+
uses: docker/build-push-action@v5
83+
with:
84+
builder: ${{ steps.buildx.outputs.name }}
85+
context: .
86+
platforms: ${{ matrix.platform }}
87+
push: true
88+
# tags: ${{ steps.ocean_node_meta.outputs.tags }}
89+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
90+
outputs: type=image,name=${{ env.DOCKERHUB_IMAGE }},push-by-digest=true,name-canonical=true,push=true
91+
- name: Build and push to GHCR
92+
if: steps.ghcr_login.outcome == 'success'
93+
id: build_ghcr
94+
uses: docker/build-push-action@v5
95+
with:
96+
builder: ${{ steps.buildx.outputs.name }}
97+
context: .
98+
platforms: ${{ matrix.platform }}
99+
push: true
100+
labels: ${{ steps.ocean_node_meta.outputs.labels }}
101+
outputs: type=image,name=${{ env.GHCR_IMAGE }},push-by-digest=true,name-canonical=true,push=true
102+
- name: Export digests
103+
run: |
104+
mkdir -p /tmp/digests
105+
if [ -n "${{ steps.build_dockerhub.outputs.digest }}" ]; then
106+
dockerhub_digest="${{ steps.build_dockerhub.outputs.digest }}"
107+
touch "/tmp/digests/dockerhub-${dockerhub_digest#sha256:}"
108+
fi
109+
if [ -n "${{ steps.build_ghcr.outputs.digest }}" ]; then
110+
ghcr_digest="${{ steps.build_ghcr.outputs.digest }}"
111+
touch "/tmp/digests/ghcr-${ghcr_digest#sha256:}"
112+
fi
113+
- name: Upload digest
114+
uses: actions/upload-artifact@v4
115+
with:
116+
name: digests-${{ env.PLATFORM_PAIR }}
117+
path: /tmp/digests/*
118+
if-no-files-found: error
119+
retention-days: 1
120+
121+
122+
merge:
123+
runs-on: ubuntu-latest
124+
if: ${{ github.actor != 'dependabot[bot]' }}
125+
needs:
126+
- build
127+
steps:
128+
- name: Download digests
129+
uses: actions/download-artifact@v4
130+
with:
131+
path: /tmp/digests
132+
pattern: digests-*
133+
merge-multiple: true
134+
- name: Set up Docker Buildx
135+
uses: docker/setup-buildx-action@v3
136+
- name: Set Docker metadata
137+
id: ocean_node_meta
138+
uses: docker/metadata-action@v5
139+
with:
140+
images: |
141+
${{ env.DOCKERHUB_IMAGE }}
142+
${{ env.GHCR_IMAGE }}
143+
# generate Docker tags based on the following events/attributes
144+
tags: |
145+
type=ref,event=branch
146+
type=semver,pattern={{version}}
147+
type=ref,event=pr
148+
# type=semver,pattern={{major}}.{{minor}}
149+
# type=semver,pattern={{major}}
150+
# type=sha
151+
- name: Login to Docker Hub
152+
id: dockerhub_login
153+
env:
154+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
155+
DOCKERHUB_TOKEN: ${{ secrets.DOCKER_PUSH_TOKEN }}
156+
if: env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != ''
157+
uses: docker/login-action@v3
158+
with:
159+
username: ${{ secrets.DOCKERHUB_PUSH_USERNAME }}
160+
password: ${{ secrets.DOCKER_PUSH_TOKEN }}
161+
- name: Login to GitHub Container Registry
162+
id: ghcr_login
163+
env:
164+
GHCR_PUSH_TOKEN: ${{ secrets.GHCR_PUSH_TOKEN }}
165+
if: env.GHCR_PUSH_TOKEN != ''
166+
uses: docker/login-action@v3
167+
with:
168+
registry: ghcr.io
169+
username: ${{ github.repository_owner }}
170+
password: ${{ secrets.GHCR_PUSH_TOKEN }}
171+
- name: Create manifest list and push to Docker Hub
172+
if: steps.dockerhub_login.outcome == 'success'
173+
working-directory: /tmp/digests
174+
env:
175+
DOCKERHUB_IMAGE: ${{ env.DOCKERHUB_IMAGE }}
176+
run: |
177+
if ls dockerhub-* 1> /dev/null 2>&1; then
178+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${DOCKERHUB_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
179+
# Strip dockerhub-arm64- and dockerhub- so digest is always just the sha256 hex
180+
DIGESTS=$(ls dockerhub-* | sed -e "s|dockerhub-arm64-|${DOCKERHUB_IMAGE}@sha256:|" -e "s|dockerhub-|${DOCKERHUB_IMAGE}@sha256:|" | tr '\n' ' ')
181+
docker buildx imagetools create $TAGS $DIGESTS
182+
fi
183+
- name: Create manifest list and push to GHCR
184+
if: steps.ghcr_login.outcome == 'success'
185+
working-directory: /tmp/digests
186+
env:
187+
GHCR_IMAGE: ${{ env.GHCR_IMAGE }}
188+
run: |
189+
TAGS=$(echo "${{ steps.ocean_node_meta.outputs.tags }}" | grep -E "^${GHCR_IMAGE}" | sed 's|^|-t |' | tr '\n' ' ')
190+
# Strip ghcr-arm64- and ghcr- so digest is always just the sha256 hex
191+
DIGESTS=$(ls ghcr-* | sed -e "s|ghcr-arm64-|${GHCR_IMAGE}@sha256:|" -e "s|ghcr-|${GHCR_IMAGE}@sha256:|" | tr '\n' ' ')
192+
docker buildx imagetools create $TAGS $DIGESTS
193+
- name: Inspect Docker Hub image
194+
if: steps.dockerhub_login.outcome == 'success'
195+
run: |
196+
docker buildx imagetools inspect ${{ env.DOCKERHUB_IMAGE }}:${{ steps.ocean_node_meta.outputs.version }}
197+
- name: Inspect GHCR image
198+
if: steps.ghcr_login.outcome == 'success'
199+
run: |
200+
docker buildx imagetools inspect ${{ env.GHCR_IMAGE }}:${{ steps.ocean_node_meta.outputs.version }}

0 commit comments

Comments
 (0)