Skip to content

Commit 7b7c8a3

Browse files
ci: use docker bake
1 parent 54648ea commit 7b7c8a3

File tree

3 files changed

+120
-24
lines changed

3 files changed

+120
-24
lines changed

.github/workflows/docker.yml

+22-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ on:
1717
# Docker and environment configs
1818
- 'Dockerfile*'
1919
- 'docker-compose.yml'
20+
- 'docker-bake.hcl'
2021
- '.dockerignore'
2122
- '.env.example'
2223
# Dependencies and python configs
@@ -38,18 +39,12 @@ on:
3839
env:
3940
REGISTRY_URL: ghcr.io
4041
REGISTRY_USER: ${{ github.repository_owner }}
41-
IMAGE: ${{ vars.IMAGE }}
42+
IMAGE_NAME: ${{ vars.IMAGE || 'meetup_bot' }}
4243

4344
jobs:
44-
push_to_registry:
45-
name: Push Docker image to container registry
46-
# if: |
47-
# (github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/'))) ||
48-
# (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
45+
build-and-push:
46+
name: Build and push Docker image
4947
runs-on: ubuntu-latest
50-
strategy:
51-
matrix:
52-
dockerfile: [Dockerfile.web]
5348
concurrency:
5449
group: ${{ github.workflow }}-${{ github.ref }}
5550
cancel-in-progress: true
@@ -72,7 +67,7 @@ jobs:
7267
uses: docker/metadata-action@v5
7368
with:
7469
images: |
75-
name=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ env.IMAGE }}
70+
${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ env.IMAGE_NAME }}
7671
tags: |
7772
type=raw,value=latest,enable=${{ endsWith(github.ref, 'main') }}
7873
type=ref,event=branch,enable=${{ !endsWith(github.ref, 'main') }}
@@ -88,21 +83,24 @@ jobs:
8883
- name: Set up Docker Buildx
8984
uses: docker/setup-buildx-action@v3
9085
with:
91-
# Use native builders if available
9286
platforms: linux/amd64,linux/arm64
9387

94-
- name: Build and push Docker image
95-
uses: docker/build-push-action@v6
88+
- name: Build and push with Docker Bake
89+
uses: docker/bake-action@v4
9690
with:
97-
context: .
98-
file: ${{ matrix.dockerfile }}
91+
files: |
92+
./docker-bake.hcl
93+
${{ steps.meta.outputs.bake-file }}
94+
targets: multi-platform
9995
push: ${{ github.event_name != 'pull_request' }}
100-
tags: ${{ steps.meta.outputs.tags }}
101-
labels: ${{ steps.meta.outputs.labels }}
102-
platforms: linux/amd64,linux/arm64/v8
103-
cache-from: |
104-
type=gha,scope=${{ github.workflow }}
105-
type=registry,ref=ghcr.io/${{ env.REGISTRY_USER }}/${{ env.IMAGE }}:buildcache
106-
cache-to: |
107-
type=gha,scope=${{ github.workflow }},mode=max
108-
type=registry,ref=ghcr.io/${{ env.REGISTRY_USER }}/${{ env.IMAGE }}:buildcache,mode=max
96+
set: |
97+
*.cache-from=type=gha,scope=${{ github.workflow }}
98+
*.cache-to=type=gha,mode=max,scope=${{ github.workflow }}
99+
*.platform=linux/amd64,linux/arm64
100+
*.args.PYTHON_VERSION=3.11
101+
*.args.PORT=3100
102+
REGISTRY_URL=${{ env.REGISTRY_URL }}
103+
REGISTRY_USER=${{ env.REGISTRY_USER }}
104+
IMAGE_NAME=${{ env.IMAGE_NAME }}
105+
TAG=${{ steps.meta.outputs.version }}
106+
DOCKERFILE=Dockerfile.web

docker-bake.hcl

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Variables with defaults that can be overridden
2+
variable "REGISTRY_URL" {
3+
default = "ghcr.io"
4+
}
5+
6+
variable "REGISTRY_USER" {
7+
default = "" // Set from GitHub Actions
8+
}
9+
10+
variable "IMAGE_NAME" {
11+
default = "meetup_bot" // Matches the label in Dockerfile.web
12+
}
13+
14+
variable "TAG" {
15+
default = "latest"
16+
}
17+
18+
variable "DOCKERFILE" {
19+
default = "Dockerfile.web"
20+
}
21+
22+
// Base target with shared configuration
23+
target "docker-metadata-action" {
24+
tags = [
25+
"${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:${TAG}",
26+
"${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:latest",
27+
]
28+
}
29+
30+
// Default target that extends the base
31+
target "build" {
32+
inherits = ["docker-metadata-action"]
33+
context = "."
34+
dockerfile = "${DOCKERFILE}"
35+
args = {
36+
PYTHON_VERSION = "3.11"
37+
}
38+
}
39+
40+
// Group target to build both platforms
41+
group "default" {
42+
targets = ["build"]
43+
}
44+
45+
// Optional specific targets for each platform
46+
target "amd64" {
47+
inherits = ["build"]
48+
platforms = ["linux/amd64"]
49+
cache-from = [
50+
"type=gha,scope=linux/amd64",
51+
"type=registry,ref=${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:buildcache"
52+
]
53+
cache-to = [
54+
"type=gha,mode=max,scope=linux/amd64"
55+
]
56+
}
57+
58+
target "arm64" {
59+
inherits = ["build"]
60+
platforms = ["linux/arm64"]
61+
cache-from = [
62+
"type=gha,scope=linux/arm64",
63+
"type=registry,ref=${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:buildcache"
64+
]
65+
cache-to = [
66+
"type=gha,mode=max,scope=linux/arm64"
67+
]
68+
}
69+
70+
// Matrix build target for multi-platform builds
71+
target "multi-platform" {
72+
inherits = ["build"]
73+
platforms = ["linux/amd64", "linux/arm64"]
74+
cache-from = [
75+
"type=gha,scope=build",
76+
"type=registry,ref=${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:buildcache"
77+
]
78+
cache-to = [
79+
"type=gha,mode=max,scope=build",
80+
"type=registry,ref=${REGISTRY_URL}/${REGISTRY_USER}/${IMAGE_NAME}:buildcache,mode=max"
81+
]
82+
}

taskfiles/docker.yml

+16
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,19 @@ tasks:
111111
cmds:
112112
- docker system prune --all --force
113113
- docker builder prune --all --force
114+
115+
validate:
116+
desc: Validate the docker-bake.hcl file
117+
vars:
118+
BAKE_OUTPUT:
119+
sh: docker buildx bake --file docker-bake.hcl --print 2>&1 || true
120+
VALIDATION_ERROR:
121+
sh: echo "{{.BAKE_OUTPUT}}" | grep -q "ERROR:" && echo "true" || echo "false"
122+
preconditions:
123+
- sh: "test {{.VALIDATION_ERROR}} = false"
124+
msg: |
125+
Docker bake file is invalid. Error details:
126+
{{.BAKE_OUTPUT}}
127+
cmds:
128+
- cmd: echo "Docker bake file is valid"
129+
silent: true

0 commit comments

Comments
 (0)