Skip to content

Commit 2b3f818

Browse files
authored
Merge pull request #102 from 3for/feat/arm64
feat(docker): add multi-platform Docker manifest support
2 parents e0db346 + 452db5d commit 2b3f818

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

tools/docker/build.gradle

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ task sourceDocker {
8080

8181
if (targetingPlatform.contains("aarch64") || targetingPlatform.contains("arm64")) {
8282
dockerfileName = "${dockerfileName}.arm64"
83+
image = "${image}-arm64"
84+
} else {
85+
image = "${image}-amd64"
8386
}
8487

8588
doLast {
@@ -100,7 +103,11 @@ task sourceDocker {
100103
def gitDetails = getGitCommitDetails(7)
101104
executable shell
102105
workingDir dockerBuildDir
103-
args "-c", "docker build --no-cache ${dockerPlatform} --build-arg BUILD_DATE=${buildTime()} --build-arg VERSION=${dockerBuildVersion} --build-arg VCS_REF=${gitDetails.hash} -t ${image} ."
106+
/* if (targetingPlatform.contains("aarch64") || targetingPlatform.contains("arm64")) {
107+
environment 'DOCKER_BUILDKIT', '0'
108+
} */
109+
// Add `--provenance=false --sbom=false` to fix unknown dirty manifests: https://github.com/orgs/community/discussions/45969
110+
args "-c", "docker build --provenance=false --sbom=false --no-cache ${dockerPlatform} --build-arg BUILD_DATE=${buildTime()} --build-arg VERSION=${dockerBuildVersion} --build-arg VCS_REF=${gitDetails.hash} -t ${image} ."
104111
}
105112
}
106113
}
@@ -158,6 +165,11 @@ tasks.register("downloadGossBinaries") {
158165
task testDocker {
159166
def dockerReportsDir = "${rootDir}/../docker/reports/"
160167
def image = "${dockerImageName}:${dockerBuildVersion}"
168+
if (targetingPlatform.contains("aarch64") || targetingPlatform.contains("arm64")) {
169+
image = "${image}-arm64"
170+
} else {
171+
image = "${image}-amd64"
172+
}
161173
imageArch = ["sh", "-c", "docker inspect ${image} --format='{{.Architecture}}'"]
162174
.execute()
163175
.text
@@ -183,15 +195,75 @@ task testDocker {
183195
// Make sure to `docker login` first
184196
task dockerUpload {
185197
def image = "${dockerImageName}:${dockerBuildVersion}"
198+
def archs = [
199+
"arm64",
200+
"amd64"]
186201

187202
doLast {
188-
exec {
189-
def cmd = "docker push '${image}'"
190-
println "Executing '${cmd}'"
191-
executable shell
192-
args "-c", cmd
203+
archs.forEach { arch ->
204+
println "Uploading for ${image}-${arch}..."
205+
exec {
206+
def cmd = "docker push '${image}-${arch}'"
207+
println "Executing '${cmd}'"
208+
executable shell
209+
args "-c", cmd
210+
}
193211
}
212+
213+
}
214+
}
215+
216+
task manifestDocker {
217+
def image = "${dockerImageName}:${dockerBuildVersion}"
218+
def archs = [
219+
"arm64",
220+
"amd64"] //TODO: this assumes dockerUpload task has already been run on 2 different archs!
221+
doLast {
222+
exec {
223+
def targets = ""
224+
archs.forEach { arch -> targets += "'${image}-${arch}' " }
225+
def cmd = "docker manifest create '${image}' ${targets}"
226+
println "Executing '${cmd}'"
227+
executable shell
228+
args "-c", cmd
229+
}
230+
exec {
231+
def cmd = "docker manifest push '${image}'"
232+
println "Executing '${cmd}'"
233+
executable shell
234+
args "-c", cmd
235+
}
236+
}
237+
}
238+
239+
task manifestDockerAsLatest {
240+
def image = "${dockerImageName}:${dockerBuildVersion}"
241+
def targetImage = "${dockerImageName}:latest"
242+
def archs = [
243+
"arm64",
244+
"amd64"] //TODO: this assumes dockerUpload task has already been run on 2 different archs!
245+
doLast {
246+
exec {
247+
def cmd = "docker manifest rm '${targetImage}' "
248+
println "Executing '${cmd}'"
249+
executable shell
250+
args "-c", cmd
251+
}
252+
exec {
253+
def targets = ""
254+
archs.forEach { arch -> targets += "'${image}-${arch}' " }
255+
def cmd = "docker manifest create '${targetImage}' ${targets}"
256+
println "Executing '${cmd}'"
257+
executable shell
258+
args "-c", cmd
194259
}
260+
exec {
261+
def cmd = "docker manifest push '${targetImage}'"
262+
println "Executing '${cmd}'"
263+
executable shell
264+
args "-c", cmd
265+
}
266+
}
195267
}
196268

197269
tasks.named('jar') {

tools/trond/cmd/docker/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ var buildCmd = &cobra.Command{
4242
`),
4343
Run: func(cmd *cobra.Command, args []string) {
4444

45-
if yes, err := utils.IsJDK1_8(); err != nil || !yes {
45+
/* if yes, err := utils.IsJDK1_8(); err != nil || !yes {
4646
fmt.Println("Error: JDK version should be 1.8")
4747
return
48-
}
48+
} */
4949

5050
// Get the flag value
5151
org, _ := cmd.Flags().GetString("org")

tools/trond/cmd/docker/test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,27 @@ var testCmd = &cobra.Command{
2828
# Test java-tron docker image, defualt: tronprotocol/java-tron:latest
2929
$ ./trond docker test
3030
31-
# Test java-tron docker image for amd64 or arm64 with specified org, artifact and version
31+
# Test java-tron docker image for amd64 with specified org, artifact and version
3232
$ ./trond docker test -o tronprotocol -a java-tron -v latest
3333
$ ./trond docker test -o tronnile -a java-tron -v latest -n nile
34+
35+
# Test java-tron docker image for arm64 with specified org, artifact and version
36+
$ ./trond docker test -o tronprotocol -a java-tron -v latest -p linux/arm64
37+
$ ./trond docker test -o tronnile -a java-tron -v latest -n nile -p linux/arm64
3438
`),
3539
Run: func(cmd *cobra.Command, args []string) {
3640

37-
if yes, err := utils.IsJDK1_8(); err != nil || !yes {
41+
/* if yes, err := utils.IsJDK1_8(); err != nil || !yes {
3842
fmt.Println("Error: JDK version should be 1.8")
3943
return
40-
}
44+
} */
4145

4246
// Get the flag value
4347
org, _ := cmd.Flags().GetString("org")
4448
artifact, _ := cmd.Flags().GetString("artifact")
4549
version, _ := cmd.Flags().GetString("version")
4650
network, _ := cmd.Flags().GetString("network")
51+
platform, _ := cmd.Flags().GetString("platform")
4752

4853
fmt.Println("If you don't specify the flags for building, the default values will be used.")
4954
fmt.Println("The default result will be: tronprotocol/java-tron:latest")
@@ -53,6 +58,9 @@ var testCmd = &cobra.Command{
5358
if len(network) > 0 {
5459
cmd1 = fmt.Sprintf("./gradlew --no-daemon testDocker -PdockerOrgName=%s -PdockerArtifactName=%s -Prelease.releaseVersion=%s -Pnetwork=%s", org, artifact, version, network)
5560
}
61+
if len(platform) > 0 {
62+
cmd1 = fmt.Sprintf("%s -Pplatform=%s", cmd1, platform)
63+
}
5664
cmds := []string{cmd1}
5765
if err := utils.RunMultipleCommands(strings.Join(cmds, " && "), "./tools/gradlew"); err != nil {
5866
fmt.Println("Error: ", err)
@@ -66,6 +74,7 @@ func init() {
6674
testCmd.Flags().StringP("artifact", "a", "java-tron", "ArtifactName for the docker image")
6775
testCmd.Flags().StringP("version", "v", "latest", "Release version for the docker image")
6876
testCmd.Flags().StringP("network", "n", "mainnet", "Which code will be used for the docker image, mainnet or nile")
77+
testCmd.Flags().StringP("platform", "p", "linux/amd64", "Platform for the docker image, linux/amd64 or linux/arm64")
6978

7079
DockerCmd.AddCommand(testCmd)
7180
}

tools/trond/docs/trond_docker_test.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ trond docker test [flags]
2525
# Test java-tron docker image, defualt: tronprotocol/java-tron:latest
2626
$ ./trond docker test
2727
28-
# Test java-tron docker image for amd64 or arm64 with specified org, artifact and version
28+
# Test java-tron docker image for amd64 with specified org, artifact and version
2929
$ ./trond docker test -o tronprotocol -a java-tron -v latest
3030
$ ./trond docker test -o tronnile -a java-tron -v latest -n nile
3131
32+
# Test java-tron docker image for arm64 with specified org, artifact and version
33+
$ ./trond docker test -o tronprotocol -a java-tron -v latest -p linux/arm64
34+
$ ./trond docker test -o tronnile -a java-tron -v latest -n nile -p linux/arm64
35+
3236
```
3337

3438
### Options
@@ -38,6 +42,7 @@ $ ./trond docker test -o tronnile -a java-tron -v latest -n nile
3842
-h, --help help for test
3943
-n, --network string Which code will be used for the docker image, mainnet or nile (default "mainnet")
4044
-o, --org string OrgName for the docker image (default "tronprotocol")
45+
-p, --platform string Platform for the docker image, linux/amd64 or linux/arm64 (default "linux/amd64")
4146
-v, --version string Release version for the docker image (default "latest")
4247
```
4348

0 commit comments

Comments
 (0)