Skip to content

Commit 22c583b

Browse files
Speedup docker build in CI (#723)
* Speedup docker build in CI * Set github token readonly permissions * Del trivy-setup * Revert permissions block
1 parent 33966cb commit 22c583b

1 file changed

Lines changed: 84 additions & 9 deletions

File tree

.github/workflows/docker.yaml

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ on: [push]
44

55
jobs:
66
docker:
7-
name: Build Docker Image
8-
runs-on: ubuntu-latest
7+
name: Build Docker Image (${{ matrix.platform }})
8+
runs-on: ${{ matrix.runner }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
include:
13+
- platform: linux/amd64
14+
runner: ubuntu-latest
15+
- platform: linux/arm64
16+
runner: ubuntu-24.04-arm
917
steps:
1018
- name: Checkout code
1119
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
@@ -35,20 +43,87 @@ jobs:
3543
with:
3644
username: ${{ secrets.DOCKERHUB_USERNAME }}
3745
password: ${{ secrets.DOCKERHUB_TOKEN }}
38-
- name: Build and push
46+
- name: Build and push by digest
47+
id: build
3948
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5
4049
with:
4150
context: .
42-
push: ${{ github.event_name != 'pull_request' }}
43-
tags: ${{ steps.meta.outputs.tags }}
51+
platforms: ${{ matrix.platform }}
4452
labels: ${{ steps.meta.outputs.labels }}
45-
platforms: linux/amd64,linux/arm64
46-
cache-from: type=gha
47-
cache-to: type=gha,mode=max
53+
outputs: type=image,"name=europe-west4-docker.pkg.dev/stakewiselabs/public/v3-operator,stakewiselabs/v3-operator",push-by-digest=true,name-canonical=true,push=true
54+
cache-from: type=gha,scope=${{ matrix.platform }}
55+
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
56+
- name: Export digest
57+
run: |
58+
mkdir -p /tmp/digests
59+
digest="${{ steps.build.outputs.digest }}"
60+
touch "/tmp/digests/${digest#sha256:}"
61+
- name: Upload digest
62+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
63+
with:
64+
name: digests-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }}
65+
path: /tmp/digests/*
66+
if-no-files-found: error
67+
retention-days: 1
68+
69+
docker-merge:
70+
name: Merge Docker Manifests
71+
needs: docker
72+
runs-on: ubuntu-latest
73+
steps:
74+
- name: Download digests
75+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
76+
with:
77+
path: /tmp/digests
78+
pattern: digests-*
79+
merge-multiple: true
80+
- name: Docker meta
81+
id: meta
82+
uses: docker/metadata-action@b2391d37b4157fa4aa2e118d643f417910ff3242 # v3
83+
with:
84+
images: |
85+
europe-west4-docker.pkg.dev/stakewiselabs/public/v3-operator
86+
stakewiselabs/v3-operator
87+
flavor: |
88+
latest=auto
89+
tags: |
90+
type=ref,event=branch
91+
type=ref,event=tag
92+
type=sha
93+
- name: Set up Docker Buildx
94+
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
95+
- name: Login to GAR
96+
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
97+
with:
98+
registry: europe-west4-docker.pkg.dev
99+
username: _json_key
100+
password: ${{ secrets.GAR_JSON_KEY }}
101+
- name: Login to Docker Hub
102+
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
103+
with:
104+
username: ${{ secrets.DOCKERHUB_USERNAME }}
105+
password: ${{ secrets.DOCKERHUB_TOKEN }}
106+
- name: Create and push manifest (GAR)
107+
working-directory: /tmp/digests
108+
env:
109+
DOCKER_METADATA_OUTPUT_JSON: ${{ steps.meta.outputs.json }}
110+
run: |
111+
docker buildx imagetools create \
112+
$(jq -cr '.tags | map(select(startswith("europe-west4-docker.pkg.dev/"))) | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
113+
$(printf 'europe-west4-docker.pkg.dev/stakewiselabs/public/v3-operator@sha256:%s ' *)
114+
- name: Create and push manifest (Docker Hub)
115+
working-directory: /tmp/digests
116+
env:
117+
DOCKER_METADATA_OUTPUT_JSON: ${{ steps.meta.outputs.json }}
118+
run: |
119+
docker buildx imagetools create \
120+
$(jq -cr '.tags | map(select(startswith("stakewiselabs/"))) | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
121+
$(printf 'stakewiselabs/v3-operator@sha256:%s ' *)
122+
48123
scanner:
49124
name: Trivy scanner
50125
runs-on: ubuntu-latest
51-
needs: docker
126+
needs: docker-merge
52127
steps:
53128
- id: commit-hash
54129
uses: prompt/actions-commit-hash@01d19a83c242e1851c9aa6cf9625092ecd095d09 # v2

0 commit comments

Comments
 (0)