Last active
March 16, 2023 13:13
-
-
Save giovannicandido/12fa97c15793a937a2a6ccd0505a2025 to your computer and use it in GitHub Desktop.
Gradle build multi arch rust release and build a image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file was generated by the Gradle 'init' task. | |
* | |
* This is a general purpose Gradle build. | |
* Learn more about Gradle by exploring our samples at https://docs.gradle.org/8.0.2/samples | |
* This project uses @Incubating APIs which are subject to change. | |
*/ | |
val isReleaseString: String = properties["release"].toString() ?: System.getenv("RELEASE") ?: "" | |
val isRelease = "true".equals(isReleaseString, ignoreCase = true) | |
val imageName = properties["imageName"] ?: "language-basic" | |
val projectName = project.name | |
task<Delete>("clean") { | |
delete = setOf( | |
"target" | |
) | |
} | |
tasks.register("buildCross") { | |
var x86args = mutableListOf("build", "--target", "x86_64-unknown-linux-gnu") | |
var aarch64args = mutableListOf("build", "--target", "aarch64-unknown-linux-gnu") | |
if(isRelease) { | |
x86args += "--release" | |
aarch64args += "--release" | |
} | |
exec { | |
inputs.dir("./src") | |
outputs.dir("./target/x86_64-unknown-linux-gnu") | |
executable = "cross" | |
args = x86args | |
} | |
exec { | |
inputs.dir("./src") | |
outputs.dir("./target/aarch64-unknown-linux-gnu") | |
executable = "cross" | |
args = aarch64args | |
} | |
} | |
tasks.create<Copy>("copyArch") { | |
dependsOn("buildCross") | |
val x86target = "target/x86_64-unknown-linux-gnu/release/${projectName}".takeIf { isRelease } ?: "target/x86_64-unknown-linux-gnu/debug/${projectName}" | |
val aarch64target = "target/aarch64-unknown-linux-gnu/release/${projectName}".takeIf { isRelease } ?: "target/aarch64-unknown-linux-gnu/debug/${projectName}" | |
from(x86target) { | |
rename(projectName, "amd64-${projectName}") | |
} | |
from(aarch64target) { | |
rename(projectName, "arm64-${projectName}") | |
} | |
into("target/binaries/") | |
} | |
tasks.create<Exec>("buildImage") { | |
dependsOn("buildCross", "copyArch") | |
commandLine("docker", "buildx", "build", "--platform", "linux/arm64,linux/amd64", "-t", imageName, | |
"--build-arg", "EXECUTABLENAME=${projectName}", ".") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment