Skip to content

Commit 1c29f6b

Browse files
authored
ci: Create docker-build action (#116)
1 parent ac8b645 commit 1c29f6b

File tree

3 files changed

+88
-45
lines changed

3 files changed

+88
-45
lines changed
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@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.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: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
name: Test package build
1414
runs-on: ubuntu-latest
1515
if: "!startsWith(github.event.head_commit.message, 'bump:')"
16-
1716
steps:
1817
- name: Check out the repo
1918
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -49,55 +48,48 @@ jobs:
4948
if: "!startsWith(github.event.head_commit.message, 'bump:')"
5049
steps:
5150
- name: Check out the repo
52-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
51+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
5352

5453
- name: Check if Dockerfile changed
5554
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
56-
id: dockerfile-changes
55+
id: docker-changes
5756
with:
5857
filters: |
59-
dockerfile:
58+
docker:
6059
- 'Dockerfile'
6160
- '.dockerignore'
61+
workflow:
62+
- ./.github/actions/docker-build/action.yml
6263
outputs:
63-
docker: ${{ steps.dockerfile-changes.outputs.dockerfile }}
64+
docker: ${{ steps.docker-changes.outputs.docker }}
65+
workflow: ${{ steps.docker-changes.outputs.workflow }}
66+
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' || needs.should-test-docker-build.outputs.docker == 'true') && !startsWith(github.event.head_commit.message, 'bump:')
7073
permissions:
7174
contents: read
7275
packages: read
7376
steps:
7477
- name: Check out the repo
75-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
78+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
7679

7780
- name: Log in to GitHub Container Registry
78-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
81+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
7982
with:
8083
registry: ghcr.io
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@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0
90-
with:
91-
images: elementsinteractive/lightman-ai
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:
96-
images: docker.io/elementsinteractive/lightman-ai
9790
context: .
9891
file: ./Dockerfile
9992
push: false
10093
platforms: linux/amd64,linux/arm64
101-
tags: ${{ steps.meta.outputs.tags }}
102-
labels: ${{ steps.meta.outputs.labels }}
10394
cache-from: type=registry,ref=ghcr.io/elementsinteractive/lightman-ai:buildcache
95+

.github/workflows/publish.yml

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,32 @@ jobs:
3535
packages: write
3636
steps:
3737
- name: Check out the repo
38-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
39-
40-
- name: Set up Docker Buildx
41-
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
38+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
4239

4340
- name: Log in to Docker Hub
44-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
41+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
4542
with:
4643
username: ${{ secrets.DOCKER_USERNAME }}
4744
password: ${{ secrets.DOCKER_PASSWORD }}
4845

4946
- name: Log in to GitHub Container Registry
50-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
47+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
5148
with:
5249
registry: ghcr.io
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@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0
59-
with:
60-
images: elementsinteractive/lightman-ai
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-
images: docker.io/elementsinteractive/lightman-ai
66-
context: .
67-
file: ./Dockerfile
68-
push: true
56+
push-image: "true"
6957
platforms: linux/amd64,linux/arm64
70-
tags: ${{ steps.meta.outputs.tags }}
71-
labels: ${{ steps.meta.outputs.labels }}
72-
cache-from: |
73-
type=registry,ref=ghcr.io/elementsinteractive/lightman-ai:buildcache
74-
cache-to: |
75-
type=registry,ref=ghcr.io/elementsinteractive/lightman-ai:buildcache,mode=max,compression=zstd,force-compression=true,oci-mediatypes=true
58+
dockerfile: ./Dockerfile
59+
context: .
60+
image-name: elementsinteractive/lightman-ai
61+
cache-from: type=registry,ref=ghcr.io/elementsinteractive/lightman-ai:buildcache
62+
cache-to: type=registry,ref=ghcr.io/elementsinteractive/lightman-ai:buildcache,mode=max,compression=zstd,force-compression=true,oci-mediatypes=true
63+
7664
- name: Delete old cache entries
7765
env:
7866
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)