Skip to content

Commit ac00b08

Browse files
authored
Support for multi-platform builds (#32)
Support for multi-platform builds
1 parent 0a4b450 commit ac00b08

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Test docker multi-platform
2+
on:
3+
# # Uncomment when test added first time to register workflow and comment it back after workflow would be registered
4+
# #
5+
# # Added pull_request to register workflow from the PR.
6+
# # Read more https://stackoverflow.com/questions/63362126/github-actions-how-to-run-a-workflow-created-on-a-non-master-branch-from-the-wo
7+
# pull_request: {}
8+
workflow_dispatch: {}
9+
10+
jobs:
11+
setup:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup
15+
run: echo "Do setup"
16+
17+
test:
18+
runs-on: ubuntu-latest
19+
needs: [setup]
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v3
23+
24+
- uses: ./
25+
id: current
26+
with:
27+
workdir: ./test/custom
28+
file: Dockerfile_multi_platform
29+
organization: ${{ github.event.repository.owner.login }}
30+
repository: ${{ github.event.repository.name }}
31+
registry: registry.hub.docker.com
32+
login: ${{ secrets.DOCKERHUB_USERNAME }}
33+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
34+
platforms: linux/amd64,linux/arm64
35+
36+
- uses: nick-fields/assert-action@v1
37+
with:
38+
expected: 'registry.hub.docker.com/cloudposse/github-action-docker-build-push'
39+
actual: ${{ steps.current.outputs.image }}
40+
41+
- uses: nick-fields/assert-action@v1
42+
with:
43+
expected: sha-${{ github.sha }}
44+
actual: ${{ steps.current.outputs.tag }}
45+
46+
- uses: nick-fields/assert-action@v1
47+
with:
48+
expected: 'containerimage.buildinfo/linux/amd64'
49+
actual: ${{ steps.current.outputs.metadata }}
50+
comparison: contains
51+
52+
- uses: nick-fields/assert-action@v1
53+
with:
54+
expected: 'containerimage.buildinfo/linux/arm64'
55+
actual: ${{ steps.current.outputs.metadata }}
56+
comparison: contains
57+
58+
teardown:
59+
runs-on: ubuntu-latest
60+
needs: [test]
61+
if: ${{ always() }}
62+
steps:
63+
- name: Tear down
64+
run: echo "Do Tear down"

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Build Docker image and push it.
9090
repository: "${{ github.event.repository.name }}"
9191
login: "${{ secrets.DOCKERHUB_USERNAME }}"
9292
password: "${{ secrets.DOCKERHUB_PASSWORD }}"
93+
platforms: linux/amd64,linux/arm64
9394

9495
outputs:
9596
image: ${{ steps.build.outputs.image }}
@@ -102,25 +103,34 @@ Build Docker image and push it.
102103
103104
104105
<!-- markdownlint-disable -->
106+
105107
## Inputs
106108
107109
| Name | Description | Default | Required |
108110
|------|-------------|---------|----------|
109111
| build-args | List of build-time variables | N/A | false |
112+
| cache-from | List of external cache sources for buildx (e.g., user/app:cache, type=local,src=path/to/dir) | type=gha | false |
113+
| cache-to | List of cache export destinations for buildx (e.g., user/app:cache, type=local,dest=path/to/dir) | type=gha,mode=max | false |
110114
| file | Dockerfile name | Dockerfile | false |
115+
| image\_name | Image name (excluding registry). Defaults to {{$organization/$repository}}. | | false |
111116
| login | Docker login | | false |
112117
| organization | Organization | N/A | true |
113118
| password | Docker password | | false |
119+
| platforms | List of target platforms for build (e.g. linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,etc) | linux/amd64 | false |
114120
| registry | Docker registry | N/A | true |
115121
| repository | Repository | N/A | true |
122+
| ssh | List of SSH agent socket or keys to expose to the build | N/A | false |
116123
| tags | List of tags (supports https://github.com/docker/metadata-action#tags-input) | N/A | false |
124+
| target | Sets the target stage to build | | false |
117125
| workdir | Working directory | ./ | false |
118126
127+
119128
## Outputs
120129
121130
| Name | Description |
122131
|------|-------------|
123132
| image | Docker image name |
133+
| metadata | Docker image metadata |
124134
| tag | Docker image tag |
125135
<!-- markdownlint-restore -->
126136
@@ -219,7 +229,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
219229
220230
## Copyright
221231
222-
Copyright © 2017-2022 [Cloud Posse, LLC](https://cpco.io/copyright)
232+
Copyright © 2017-2023 [Cloud Posse, LLC](https://cpco.io/copyright)
223233
224234
225235

README.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ usage: |-
6969
repository: "${{ github.event.repository.name }}"
7070
login: "${{ secrets.DOCKERHUB_USERNAME }}"
7171
password: "${{ secrets.DOCKERHUB_PASSWORD }}"
72+
platforms: linux/amd64,linux/arm64
7273
7374
outputs:
7475
image: ${{ steps.build.outputs.image }}

action.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ inputs:
2929
description: "Sets the target stage to build"
3030
required: false
3131
default: ''
32+
platforms:
33+
description: "List of target platforms for build (e.g. linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,etc)"
34+
required: false
35+
default: 'linux/amd64'
3236
image_name:
3337
description: "Image name (excluding registry). Defaults to {{$organization/$repository}}."
3438
required: false
@@ -62,6 +66,9 @@ outputs:
6266
tag:
6367
description: "Docker image tag"
6468
value: ${{ steps.tag.outputs.output }}
69+
metadata:
70+
description: "Docker image metadata"
71+
value: ${{ steps.get-metadata.outputs.metadata }}
6572

6673
runs:
6774
using: "composite"
@@ -113,6 +120,9 @@ runs:
113120
run: |
114121
docker context create buildx-context || true
115122
123+
- name: Set up QEMU
124+
uses: docker/setup-qemu-action@v2
125+
116126
- name: Set up Docker Buildx
117127
uses: docker/setup-buildx-action@v2
118128
with:
@@ -128,6 +138,7 @@ runs:
128138

129139
- name: Build and push Docker images
130140
uses: docker/build-push-action@v3
141+
id: docker-build-push-action
131142
with:
132143
context: ${{ inputs.workdir }}
133144
file: ${{ inputs.workdir }}/${{ inputs.file }}
@@ -140,3 +151,12 @@ runs:
140151
tags: ${{ steps.meta.outputs.tags }}
141152
target: ${{ inputs.target }}
142153
labels: ${{ steps.meta.outputs.labels }}
154+
platforms: ${{ inputs.platforms }}
155+
156+
- name: Get Metadata
157+
id: get-metadata
158+
shell: bash
159+
run: |
160+
metadata=$(echo '${{ steps.docker-build-push-action.outputs.metadata }}' | jq -c)
161+
echo "metadata=$metadata" >> $GITHUB_OUTPUT
162+
echo "Docker image metadata: $metadata"

docs/github-action.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
<!-- markdownlint-disable -->
2+
23
## Inputs
34

45
| Name | Description | Default | Required |
56
|------|-------------|---------|----------|
67
| build-args | List of build-time variables | N/A | false |
8+
| cache-from | List of external cache sources for buildx (e.g., user/app:cache, type=local,src=path/to/dir) | type=gha | false |
9+
| cache-to | List of cache export destinations for buildx (e.g., user/app:cache, type=local,dest=path/to/dir) | type=gha,mode=max | false |
710
| file | Dockerfile name | Dockerfile | false |
11+
| image\_name | Image name (excluding registry). Defaults to {{$organization/$repository}}. | | false |
812
| login | Docker login | | false |
913
| organization | Organization | N/A | true |
1014
| password | Docker password | | false |
15+
| platforms | List of target platforms for build (e.g. linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,etc) | linux/amd64 | false |
1116
| registry | Docker registry | N/A | true |
1217
| repository | Repository | N/A | true |
18+
| ssh | List of SSH agent socket or keys to expose to the build | N/A | false |
1319
| tags | List of tags (supports https://github.com/docker/metadata-action#tags-input) | N/A | false |
20+
| target | Sets the target stage to build | | false |
1421
| workdir | Working directory | ./ | false |
1522

23+
1624
## Outputs
1725

1826
| Name | Description |
1927
|------|-------------|
2028
| image | Docker image name |
29+
| metadata | Docker image metadata |
2130
| tag | Docker image tag |
2231
<!-- markdownlint-restore -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM nginx:1.23.0-alpine
2+
3+
ARG TARGETPLATFORM
4+
5+
# Run commands based on the platform
6+
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
7+
echo "This command only runs on ARM64-based platforms"; \
8+
else \
9+
echo "This command runs on all platforms"; \
10+
fi

0 commit comments

Comments
 (0)