universal-ci-cd-scripts/src/main/groovy/script/release_docker_build_push.groovy

82 lines
2.9 KiB
Groovy
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package script
/**
* Скрипт для публикации докер образа при сборке.
*/
@GrabResolver(name='gitea', root='https://git.tswf.io/api/packages/public-repos/maven')
@Grapes([
@Grab(group='io.tswf.groovy.better-groovy', module='better-groovy-scripting-shell', version='2.0.2-SNAPSHOT', changing = true),
@Grab(group='io.tswf.groovy.better-groovy', module='better-groovy-scripting-gitea', version='2.0.2-SNAPSHOT', changing = true)
])
_
import util.DockerLogin
import util.DockerBuildCommandFactory
import util.DockerTags
import util.Dockerfiles
import util.GitTags
import util.GlobalProperties
import util.ScriptLog
import util.CIProperties
import util.DockerImageNames
println """
#############################################################################
Push Docker Images Script
#############################################################################
"""
GlobalProperties.loadGlobalProperties()
def gitReleaseTags = GitTags.getDeployTags()
if (gitReleaseTags.isNullOrEmpty()) {
ScriptLog.printf "There is no release tags found!"
System.exit(1)
}
else
ScriptLog.printf "Found git release tags ${gitReleaseTags}"
ScriptLog.printf "Starting docker publishing..."
DockerLogin.perform()
def dockerfilesToPush = CIProperties.findListProperty("docker.image.push.files").orElse([])
gitReleaseTags.each { gitTag ->
Dockerfiles.getPresetDockerfiles().each { dockerfileName ->
if (!dockerfilesToPush.isEmpty() && dockerfileName !in dockerfilesToPush) {
ScriptLog.printf "Skipping push of dockerfile '${dockerfileName}' because it is not in push list: ${dockerfilesToPush}"
return
}
ScriptLog.printf "Building docker tag: '${gitTag}' with dockerfile named '${dockerfileName}'"
def dockerImageBaseName = DockerImageNames.getImageName(dockerfileName)
// Для докер-тегов образов используем гит-теги с отброшенным префиксом
def imageBaseTag = GitTags.sanitizeTagFromPrefixes(gitTag, GitTags.getReleasePrefixes())
def imageTag = DockerTags.getDockerTagForDockerfile(imageBaseTag, dockerfileName)
if (imageTag.isNullOrBlank()) {
throw new IllegalStateException("Build tag was not resolved (empty or null '${imageTag}')")
}
def imageFullName = "${dockerImageBaseName}:${imageTag}"
ScriptLog.printf "Full docker image name will be '${imageFullName}'"
def dockerBuildCommand = DockerBuildCommandFactory.getBuildCommand(dockerfileName, "-t ${imageFullName}")
ScriptLog.printf "Executing: '${dockerBuildCommand}'"
sh dockerBuildCommand
sh "docker push '${imageFullName}'"
ScriptLog.printf "Docker tag ${imageTag} build and push completed with full image name: '${imageFullName}'"
}
}
ScriptLog.printf "Publishing done!"