Skip to content

Commit d897327

Browse files
committed
Use native arm runner to build arm64 images
1 parent e3ecd76 commit d897327

File tree

2 files changed

+153
-25
lines changed

2 files changed

+153
-25
lines changed

.github/workflows/docker-build-simple.libsonnet

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
// Docker manifest #{name}/Dockerfile
44
// ECR repository: #{name}
55

6+
local runnersMap = {
7+
'linux/amd64': 'ubuntu-24.04',
8+
'linux/arm64': 'ubuntu-24.04-arm',
9+
};
10+
11+
local setupSteps = function(region) [
12+
{ uses: 'docker/setup-buildx-action@v2' },
13+
{
14+
uses: 'aws-actions/configure-aws-credentials@v1',
15+
with: {
16+
'aws-region': region,
17+
'role-to-assume': 'arn:aws:iam::005216166247:role/GhaDockerPush',
18+
'role-skip-session-tagging': true,
19+
},
20+
},
21+
{
22+
uses: 'aws-actions/amazon-ecr-login@v1',
23+
id: 'login-ecr',
24+
},
25+
];
26+
627
function(name, region='ap-northeast-1', platforms=['linux/arm64']) {
728
name: std.format('docker-%s', name),
829
on: {
@@ -17,33 +38,72 @@ function(name, region='ap-northeast-1', platforms=['linux/arm64']) {
1738
jobs: {
1839
build: {
1940
name: 'build',
20-
'runs-on': 'ubuntu-latest',
41+
strategy: {
42+
matrix: {
43+
include: std.map(function(platform) {
44+
key: std.strReplace(platform, '/', '-'), // for artifact name
45+
platform: platform,
46+
runner: runnersMap[platform],
47+
}, platforms),
48+
},
49+
},
50+
'runs-on': '${{ matrix.runner }}',
2151
permissions: { 'id-token': 'write', contents: 'read' },
22-
steps: [] +
23-
(if std.member(platforms, 'linux/arm64') then [{ uses: 'docker/setup-qemu-action@v2' }] else []) + [
24-
{ uses: 'docker/setup-buildx-action@v2' },
52+
steps: setupSteps(region) + [
2553
{
26-
uses: 'aws-actions/configure-aws-credentials@v1',
54+
uses: 'docker/build-push-action@v3',
55+
id: 'build-push',
2756
with: {
28-
'aws-region': region,
29-
'role-to-assume': 'arn:aws:iam::005216166247:role/GhaDockerPush',
30-
'role-skip-session-tagging': true,
57+
context: std.format('{{defaultContext}}:%s', name),
58+
platforms: '${{ matrix.platform }}',
59+
outputs: std.format('type=image,"name=${{ steps.login-ecr.outputs.registry }}/%s",push-by-digest=true,name-canonical=true,push=true', name),
3160
},
3261
},
3362
{
34-
uses: 'aws-actions/amazon-ecr-login@v1',
35-
id: 'login-ecr',
63+
name: 'Export digest',
64+
run: |||
65+
mkdir -p "${RUNNER_TEMP}/digests"
66+
printenv DIGEST > "${RUNNER_TEMP}/digests/${PLATFORM}"
67+
|||,
68+
env: {
69+
RUNNER_TEMP: '${{ runner.temp }}',
70+
DIGEST: '${{ steps.build-push.outputs.digest }}',
71+
PLATFORM: '${{ matrix.key }}',
72+
},
3673
},
3774
{
38-
uses: 'docker/build-push-action@v3',
75+
uses: 'actions/upload-artifact@v4',
3976
with: {
40-
context: std.format('{{defaultContext}}:%s', name),
41-
platforms: std.join(',', platforms),
42-
tags: std.join(',', [
43-
std.format('${{ steps.login-ecr.outputs.registry }}/%s:${{ github.sha }}', name),
44-
std.format('${{ steps.login-ecr.outputs.registry }}/%s:latest', name),
45-
]),
46-
push: true,
77+
name: 'digests-${{ matrix.key }}',
78+
path: '${{ runner.temp }}/digests/*',
79+
'if-no-files-found': 'error',
80+
'retention-days': 1,
81+
},
82+
},
83+
],
84+
},
85+
merge: {
86+
'runs-on': 'ubuntu-latest',
87+
needs: ['build'],
88+
permissions: { 'id-token': 'write' },
89+
steps: setupSteps(region) + [
90+
{
91+
uses: 'actions/download-artifact@v4',
92+
with: {
93+
path: '${{ runner.temp }}/digests',
94+
pattern: 'digests-*',
95+
'merge-multiple': true,
96+
},
97+
},
98+
{
99+
name: 'Push manifest',
100+
run: |||
101+
cat * | xargs -I{} printf "%s@%s" "${REPO}" {} | docker buildx imagetools create -f /dev/stdin -t "${REPO}:latest" -t "${REPO}:${SHA}"
102+
docker buildx imagetools inspect "${REPO}:${SHA}"
103+
|||,
104+
env: {
105+
REPO: std.format('${{ steps.login-ecr.outputs.registry }}/%s', name),
106+
SHA: '${{ github.sha }}',
47107
},
48108
},
49109
],

.github/workflows/docker-unbound.yml

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
"contents": "read",
77
"id-token": "write"
88
},
9-
"runs-on": "ubuntu-latest",
9+
"runs-on": "${{ matrix.runner }}",
1010
"steps": [
11-
{
12-
"uses": "docker/setup-qemu-action@v2"
13-
},
1411
{
1512
"uses": "docker/setup-buildx-action@v2"
1613
},
@@ -27,13 +24,84 @@
2724
"uses": "aws-actions/amazon-ecr-login@v1"
2825
},
2926
{
27+
"id": "build-push",
3028
"uses": "docker/build-push-action@v3",
3129
"with": {
3230
"context": "{{defaultContext}}:unbound",
33-
"platforms": "linux/arm64",
34-
"push": true,
35-
"tags": "${{ steps.login-ecr.outputs.registry }}/unbound:${{ github.sha }},${{ steps.login-ecr.outputs.registry }}/unbound:latest"
31+
"outputs": "type=image,\"name=${{ steps.login-ecr.outputs.registry }}/unbound\",push-by-digest=true,name-canonical=true,push=true",
32+
"platforms": "${{ matrix.platform }}"
33+
}
34+
},
35+
{
36+
"env": {
37+
"DIGEST": "${{ steps.build-push.outputs.digest }}",
38+
"PLATFORM": "${{ matrix.key }}",
39+
"RUNNER_TEMP": "${{ runner.temp }}"
40+
},
41+
"name": "Export digest",
42+
"run": "mkdir -p \"${RUNNER_TEMP}/digests\"\nprintenv DIGEST > \"${RUNNER_TEMP}/digests/${PLATFORM}\"\n"
43+
},
44+
{
45+
"uses": "actions/upload-artifact@v4",
46+
"with": {
47+
"if-no-files-found": "error",
48+
"name": "digests-${{ matrix.key }}",
49+
"path": "${{ runner.temp }}/digests/*",
50+
"retention-days": 1
51+
}
52+
}
53+
],
54+
"strategy": {
55+
"matrix": {
56+
"include": [
57+
{
58+
"key": "linux-arm64",
59+
"platform": "linux/arm64",
60+
"runner": "ubuntu-24.04-arm"
61+
}
62+
]
63+
}
64+
}
65+
},
66+
"merge": {
67+
"needs": [
68+
"build"
69+
],
70+
"permissions": {
71+
"id-token": "write"
72+
},
73+
"runs-on": "ubuntu-latest",
74+
"steps": [
75+
{
76+
"uses": "docker/setup-buildx-action@v2"
77+
},
78+
{
79+
"uses": "aws-actions/configure-aws-credentials@v1",
80+
"with": {
81+
"aws-region": "ap-northeast-1",
82+
"role-skip-session-tagging": true,
83+
"role-to-assume": "arn:aws:iam::005216166247:role/GhaDockerPush"
3684
}
85+
},
86+
{
87+
"id": "login-ecr",
88+
"uses": "aws-actions/amazon-ecr-login@v1"
89+
},
90+
{
91+
"uses": "actions/download-artifact@v4",
92+
"with": {
93+
"merge-multiple": true,
94+
"path": "${{ runner.temp }}/digests",
95+
"pattern": "digests-*"
96+
}
97+
},
98+
{
99+
"env": {
100+
"REPO": "${{ steps.login-ecr.outputs.registry }}/unbound",
101+
"SHA": "${{ github.sha }}"
102+
},
103+
"name": "Push manifest",
104+
"run": "cat * | xargs -I{} printf \"%s@%s\" \"${REPO}\" {} | docker buildx imagetools create -f /dev/stdin -t \"${REPO}:latest\" -t \"${REPO}:${SHA}\"\ndocker buildx imagetools inspect \"${REPO}:${SHA}\"\n"
37105
}
38106
]
39107
}

0 commit comments

Comments
 (0)