Skip to content

Commit 21ce830

Browse files
authored
ci: Create action for docker builds (#261)
1 parent d2419bf commit 21ce830

3 files changed

Lines changed: 82 additions & 37 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Action that wraps together all the necessary actions to build an image and publish it.
2+
# It does not include login actions.
3+
# This is a convenience, so that we know that everything related to the build lies in this file,
4+
# and if it gets updated we can automatically run our `test-docker-build` from `build-test.yml.`
5+
6+
name: "Docker Build"
7+
description: "Builds Docker images with optional caching and multi-platform support"
8+
9+
inputs:
10+
push-image:
11+
description: "Whether to push the built image"
12+
required: false
13+
default: "false"
14+
platforms:
15+
description: "Target platforms for the build"
16+
required: false
17+
default: "linux/amd64"
18+
dockerfile:
19+
description: "Path to the Dockerfile"
20+
required: false
21+
default: "./Dockerfile"
22+
context:
23+
description: "Build context path"
24+
required: false
25+
default: "."
26+
image-name:
27+
description: "Docker image name"
28+
required: true
29+
cache-from:
30+
description: "Cache source for Docker build"
31+
required: false
32+
default: ""
33+
cache-to:
34+
description: "Cache destination for Docker build"
35+
required: false
36+
default: ""
37+
38+
runs:
39+
using: "composite"
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v4
43+
44+
- name: Set up Docker Buildx
45+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
46+
47+
- name: Extract Docker metadata
48+
id: meta
49+
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0
50+
with:
51+
images: ${{ inputs['image-name'] }}
52+
53+
- name: Build Docker image
54+
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
55+
with:
56+
context: ${{ inputs.context }}
57+
file: ${{ inputs.dockerfile }}
58+
push: ${{ inputs['push-image'] }}
59+
platforms: ${{ inputs.platforms }}
60+
tags: ${{ steps.meta.outputs.tags }}
61+
labels: ${{ steps.meta.outputs.labels }}
62+
cache-from: ${{ inputs['cache-from'] }}
63+
cache-to: ${{ inputs['cache-to'] }}

.github/workflows/build-test.yml

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: ["main"]
88
pull_request:
99
branches: ["main"]
10-
10+
1111
jobs:
1212
test-build-package:
1313
name: Test package build
@@ -53,20 +53,23 @@ jobs:
5353

5454
- name: Check if Dockerfile changed
5555
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
56-
id: dockerfile-changes
56+
id: docker-changes
5757
with:
5858
filters: |
59-
dockerfile:
59+
docker:
6060
- 'Dockerfile'
6161
- '.dockerignore'
62+
workflow:
63+
- ./.github/actions/docker-build/action.yml
6264
outputs:
63-
docker: ${{ steps.dockerfile-changes.outputs.dockerfile }}
65+
docker: ${{ steps.docker-changes.outputs.docker }}
66+
workflow: ${{ steps.docker-changes.outputs.workflow }}
6467

6568
test-docker-build:
6669
needs: [should-test-docker-build]
6770
name: Test Docker build
6871
runs-on: ubuntu-latest
69-
if: needs.should-test-docker-build.outputs.docker == 'true' && !startsWith(github.event.head_commit.message, 'bump:')
72+
if: ((needs.should-test-docker-build.outputs.workflow == 'true' && ${{ github.actor == 'dependabot[bot]' }} ) || needs.should-test-docker-build.outputs.docker == 'true') && !startsWith(github.event.head_commit.message, 'bump:')
7073
permissions:
7174
contents: read
7275
packages: read
@@ -81,22 +84,12 @@ jobs:
8184
username: ${{ github.actor }}
8285
password: ${{ secrets.GITHUB_TOKEN }}
8386

84-
- name: Set up Docker Buildx
85-
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
86-
87-
- name: Extract metadata (tags, labels) for Docker
88-
id: meta
89-
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0
90-
with:
91-
images: elementsinteractive/twyn
92-
93-
- name: Build Docker image
94-
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
87+
- name: Build image
88+
uses: ./.github/actions/docker-build
9589
with:
9690
context: .
9791
file: ./Dockerfile
9892
push: false
9993
platforms: linux/amd64,linux/arm64
100-
tags: ${{ steps.meta.outputs.tags }}
101-
labels: ${{ steps.meta.outputs.labels }}
10294
cache-from: type=registry,ref=ghcr.io/elementsinteractive/twyn:buildcache
95+

.github/workflows/publish.yml

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ jobs:
3737
- name: Check out the repo
3838
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3939

40-
- name: Set up Docker Buildx
41-
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
42-
4340
- name: Log in to Docker Hub
4441
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
4542
with:
@@ -53,25 +50,17 @@ jobs:
5350
username: ${{ github.actor }}
5451
password: ${{ secrets.GITHUB_TOKEN }}
5552

56-
- name: Extract metadata (tags, labels) for Docker
57-
id: meta
58-
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0
59-
with:
60-
images: elementsinteractive/twyn
61-
6253
- name: Build and push Docker image
63-
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
54+
uses: ./.github/actions/docker-build
6455
with:
65-
context: .
66-
file: ./Dockerfile
67-
push: true
56+
push-image: "true"
6857
platforms: linux/amd64,linux/arm64
69-
tags: ${{ steps.meta.outputs.tags }}
70-
labels: ${{ steps.meta.outputs.labels }}
71-
cache-from: |
72-
type=registry,ref=ghcr.io/elementsinteractive/twyn:buildcache
73-
cache-to: |
74-
type=registry,ref=ghcr.io/elementsinteractive/twyn:buildcache,mode=max,compression=zstd,force-compression=true,oci-mediatypes=true
58+
dockerfile: ./Dockerfile
59+
context: .
60+
image-name: elementsinteractive/twyn
61+
cache-from: type=registry,ref=ghcr.io/elementsinteractive/twyn:buildcache
62+
cache-to: type=registry,ref=ghcr.io/elementsinteractive/twyn:buildcache,mode=max,compression=zstd,force-compression=true,oci-mediatypes=true
63+
7564
- name: Delete old cache entries
7665
env:
7766
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)