Skip to content

Commit 6df80aa

Browse files
committed
refactor: move reusable GHA code into reusable action
1 parent 9fd841f commit 6df80aa

File tree

4 files changed

+135
-69
lines changed

4 files changed

+135
-69
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Build and Push
2+
description: Build and push Docker images
3+
inputs:
4+
platforms:
5+
description: "Platforms to build"
6+
required: true
7+
default: "linux/amd64,linux/arm64"
8+
builder_type:
9+
description: "Builder type"
10+
required: true
11+
default: "cloud"
12+
cloud_builder:
13+
description: "The cloud builder endpoint"
14+
required: false
15+
default: "docker/collab-bu"
16+
docker_username:
17+
description: "Docker Hub username"
18+
required: true
19+
docker_password:
20+
description: "Docker Hub password"
21+
required: true
22+
docker_hub_account:
23+
description: "Docker Hub account to push the image to"
24+
required: true
25+
cache:
26+
description: "Cache the build"
27+
required: false
28+
default: "true"
29+
runs:
30+
using: "composite"
31+
steps:
32+
- name: Login to Docker
33+
uses: docker/login-action@v3
34+
with:
35+
username: ${{ inputs.docker_username }}
36+
password: ${{ inputs.docker_password }}
37+
38+
- name: Set up QEMU
39+
if: ${{ inputs.builder_type == 'local' }}
40+
uses: docker/setup-qemu-action@v3
41+
42+
- name: Set up Docker Buildx (local)
43+
if: ${{ inputs.builder_type == 'local' }}
44+
uses: docker/setup-buildx-action@v3
45+
46+
- name: Set up Docker Buildx (cloud)
47+
if: ${{ inputs.builder_type != 'local' }}
48+
uses: docker/setup-buildx-action@v3
49+
with:
50+
version: lab:latest
51+
driver: cloud
52+
endpoint: ${{ inputs.cloud_builder }}
53+
54+
- name: Build and push (local)
55+
if: ${{ inputs.builder_type == 'local' }}
56+
uses: docker/build-push-action@v5
57+
with:
58+
context: .
59+
platforms: ${{ inputs.platforms || 'linux/amd64,linux/arm64' }}
60+
push: true
61+
tags: ${{ inputs.docker_hub_account }}/dbc-demo:latest-local
62+
cache-from: type=gha
63+
cache-to: type=gha,mode=max
64+
65+
- name: Build and push (cloud)
66+
if: ${{ inputs.builder_type != 'local' }}
67+
uses: docker/build-push-action@v5
68+
with:
69+
context: .
70+
platforms: ${{ inputs.platforms || 'linux/amd64,linux/arm64' }}
71+
push: true
72+
tags: ${{ inputs.docker_hub_account }}/dbc-demo:latest-cloud
73+
cache: ${{ inputs.cache }}

.github/workflows/build.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Build and Push
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
builder_type:
7+
required: true
8+
type: string
9+
platforms:
10+
required: true
11+
type: string
12+
workflow_dispatch:
13+
inputs:
14+
builder_type:
15+
type: choice
16+
description: Builder type
17+
default: cloud
18+
options:
19+
- local
20+
- cloud
21+
platforms:
22+
type: choice
23+
description: Platforms to build
24+
default: multiplatform
25+
options:
26+
- linux/amd64,linux/arm64
27+
- linux/amd64
28+
- linux/arm64
29+
cache:
30+
type: boolean
31+
description: Cache the build
32+
default: true
33+
jobs:
34+
build:
35+
runs-on: ubuntu-latest
36+
environment: ${{ inputs.builder_type }}
37+
steps:
38+
- name: Checkout
39+
uses: actions/checkout@v4
40+
41+
- name: Build and push
42+
uses: ./.github/actions/buildpush
43+
with:
44+
builder_type: ${{ inputs.builder_type }}
45+
platforms: ${{ inputs.platforms }}
46+
docker_username: ${{ vars.DOCKER_USERNAME }}
47+
docker_password: ${{ secrets.DOCKER_PASSWORD }}
48+
docker_hub_account: ${{ vars.DOCKER_HUB_ACCOUNT }}
49+
cache: ${{ inputs.cache }}

.github/workflows/ci.yaml

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
name: Build and Push
1+
name: CI
22

33
on:
4-
workflow_call:
5-
inputs:
6-
builder_type:
7-
required: true
8-
type: string
9-
platforms:
10-
required: true
11-
type: string
12-
workflow_dispatch:
13-
inputs:
14-
builder_type:
15-
type: choice
16-
description: Builder type
17-
default: cloud
18-
options:
19-
- local
20-
- cloud
21-
platforms:
22-
type: choice
23-
description: Platforms to build
24-
default: multiplatform
25-
options:
26-
- linux/amd64,linux/arm64
27-
- linux/amd64
28-
- linux/arm64
294
pull_request:
305
branches:
316
- main
@@ -35,50 +10,19 @@ on:
3510

3611
jobs:
3712
build:
13+
strategy:
14+
matrix:
15+
builder_type: [local, cloud]
3816
runs-on: ubuntu-latest
39-
environment: ${{ inputs.builder_type }}
4017
steps:
4118
- name: Checkout
4219
uses: actions/checkout@v4
4320

44-
- name: Login to Docker
45-
uses: docker/login-action@v3
21+
- name: Build and push
22+
uses: ./.github/actions/buildpush
4623
with:
47-
username: ${{ vars.DOCKER_USERNAME }}
48-
password: ${{ secrets.DOCKER_PASSWORD }}
49-
50-
- name: Set up QEMU
51-
if: ${{ inputs.builder_type == 'local' }}
52-
uses: docker/setup-qemu-action@v3
53-
54-
- name: Set up Docker Buildx (local)
55-
if: ${{ inputs.builder_type == 'local' }}
56-
uses: docker/setup-buildx-action@v3
57-
58-
- name: Set up Docker Buildx (cloud)
59-
if: ${{ inputs.builder_type != 'local' }}
60-
uses: docker/setup-buildx-action@v3
61-
with:
62-
version: lab:latest
63-
driver: cloud
64-
endpoint: docker/collab-bu
65-
66-
- name: Build and push (local)
67-
if: ${{ inputs.builder_type == 'local' }}
68-
uses: docker/build-push-action@v5
69-
with:
70-
context: .
71-
platforms: ${{ inputs.platforms || 'linux/amd64,linux/arm64' }}
72-
push: true
73-
tags: ${{ vars.DOCKER_HUB_ACCOUNT }}/dbc-demo:latest
74-
cache-from: type=gha
75-
cache-to: type=gha,mode=max
76-
77-
- name: Build and push (cloud)
78-
if: ${{ inputs.builder_type != 'local' }}
79-
uses: docker/build-push-action@v5
80-
with:
81-
context: .
82-
platforms: ${{ inputs.platforms || 'linux/amd64,linux/arm64' }}
83-
push: true
84-
tags: ${{ vars.DOCKER_HUB_ACCOUNT }}/dbc-demo:latest
24+
builder_type: ${{ matrix.builder_type }}
25+
platforms: linux/amd64,linux/arm64
26+
docker_username: ${{ vars.DOCKER_USERNAME }}
27+
docker_password: ${{ secrets.DOCKER_PASSWORD }}
28+
docker_hub_account: ${{ vars.DOCKER_HUB_ACCOUNT }}

.github/workflows/compare.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ on:
44

55
jobs:
66
build_local:
7-
uses: ./.github/workflows/ci.yaml
7+
uses: ./.github/workflows/build.yaml
88
secrets: inherit
99
with:
1010
builder_type: local
1111
platforms: linux/amd64,linux/arm64
1212

1313
build_cloud:
14-
uses: ./.github/workflows/ci.yaml
14+
uses: ./.github/workflows/build.yaml
1515
secrets: inherit
1616
with:
1717
builder_type: cloud

0 commit comments

Comments
 (0)