Files
SCM-Manager/scm-packaging/docker/build.gradle
Sebastian Sdorra af2cdfb739 Docker multi-arch builds (#2021)
Replaces the current docker build with a multi arch build powered by buildx.
The new build creates two scm-manager docker image variants.
One based on alpine which uses the openjdk distribution and the other based on debian and eclipse temurin:

scmmanager/scm-manager:<version>-alpine
- linux/amd64
- linux/arm64
scmmanager/scm-manager:<version>-debian
- linux/amd64
- linux/arm64
- linux/arm/v7
scmmanager/scm-manager:<version>
- linux/amd64 alpine based
- linux/arm64 alpine based
- linux/arm/v7 debian based
scmmanager/scm-manager:latest
- linux/amd64 alpine based
- linux/arm64 alpine based
- linux/arm/v7 debian based

The development build produces only a single amd64 image at cloudogu/scm-manager with a snapshot version.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
2022-05-04 14:38:31 +02:00

167 lines
4.7 KiB
Groovy

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
plugins {
id 'org.scm-manager.packaging'
id 'org.scm-manager.license'
}
import org.gradle.util.VersionNumber
import groovy.json.JsonSlurper
configurations {
server
webapp
packageYaml {
canBeConsumed = true
}
}
dependencies {
server project(':scm-server')
webapp project(path: ':scm-webapp', configuration: 'webapp')
}
task context(type: Copy) {
VersionNumber version = VersionNumber.parse(project.version)
destinationDir = project.buildDir
into('docker') {
into('etc') {
from('src/main/fs/etc')
expand([version: version])
}
into('opt') {
from('src/main/fs/opt')
}
into('opt/scm-server/lib') {
from project.configurations.server
}
into('opt/scm-server/var/webapp') {
from project.configurations.webapp
rename {
'scm-webapp.war'
}
}
from('.') {
include 'Dockerfile'
}
}
}
task setupBuilder() {
doLast {
def inspect = exec {
commandLine = ["docker", "buildx", "inspect", "scm-builder"]
ignoreExitValue = true
}
if (inspect.exitValue != 0) {
exec {
commandLine = ["docker", "run", "--privileged", "--rm", "tonistiigi/binfmt", "--install", "arm,arm64"]
}
exec {
commandLine = ["docker", "buildx", "create", "--name", "scm-builder", "--driver", "docker-container", "--platform", "linux/arm/v7,linux/arm64/v8,linux/amd64"]
}
exec {
commandLine = ["docker", "buildx", "inspect", "scm-builder"]
}
}
}
}
task build(type: Exec) {
commandLine = ["docker", "buildx", "bake", "--builder", "scm-builder", isSnapshot ? "dev": "prod"]
environment "VERSION", dockerTag
environment "COMMIT_SHA", revision
environment "IMAGE", dockerRepository
doLast {
File file = new File(project.buildDir, 'docker.tag')
file.text = dockerTag
}
dependsOn 'context', 'setupBuilder'
}
task pushImages(type: Exec) {
commandLine = ["docker", "buildx", "bake", "--builder", "scm-builder", isSnapshot ? "dev": "prod", "--push"]
environment "VERSION", dockerTag
environment "COMMIT_SHA", revision
environment "IMAGE", dockerRepository
dependsOn 'build'
}
task publish() {
doLast {
if (!isSnapshot) {
// get digest of debian arm v7 image
def stdout = new ByteArrayOutputStream()
exec {
commandLine = ["docker", "buildx", "imagetools", "inspect", "--raw", "${dockerRepository}:${dockerTag}-debian"]
standardOutput = stdout
}
def inspect = new JsonSlurper().parseText(stdout.toString())
def manifest = inspect.manifests.find { m -> m.platform.architecture == "arm" }
// append arm image to manifest with version and without os suffix
exec {
commandLine = ["docker", "buildx", "imagetools", "create", "--append", "-t", "${dockerRepository}:${dockerTag}", "${dockerRepository}:${dockerTag}-debian@${manifest.digest}"]
}
// append arm image to latest manifest
exec {
commandLine = ["docker", "buildx", "imagetools", "create", "--append", "-t", "${dockerRepository}:latest", "${dockerRepository}:${dockerTag}-debian@${manifest.digest}"]
}
}
}
dependsOn 'pushImages'
}
task distribution(type: PackageYaml) {
type = 'docker'
dependsOn build
}
artifacts {
packageYaml(file('build/libs/package.yml')) {
builtBy distribution
}
}
license {
header rootProject.file("LICENSE.txt")
lineEnding = "\n"
tasks {
build {
files.from("build.gradle", "Dockerfile")
}
main {
files.from("src")
}
}
}
task check {
dependsOn checkLicenses
}