Jenkins/vars/container.groovy

68 lines
2.6 KiB
Groovy

String buildAndPublishImage(Map options = [:]) {
def dockerfile = options.get("dockerfile", "./Dockerfile")
def contextDir = options.get("contextDir", ".")
def imageName = options.get("imageName", "")
def gitRef = sh(returnStdout: true, script: 'git describe --always').trim()
def imageTag = options.get("imageTag", gitRef)
def gitCredentialsId = options.get("gitCredentialsId", "forge-jenkins")
def dockerRepository = options.get("dockerRepository", "docker.io")
def dockerRepositoryCredentialsId = options.get("dockerRepositoryCredentialsId", "cadoles-docker-hub")
withCredentials([
usernamePassword([
credentialsId: dockerRepositoryCredentialsId,
usernameVariable: 'HUB_USERNAME',
passwordVariable: 'HUB_PASSWORD'
]),
]) {
String fullImageName = "${dockerRepository}/${env.HUB_USERNAME}/${imageName}"
stage("Build image '${imageName}:${imageTag}'") {
git.withHTTPCredentials(gitCredentialsId) {
sh """
docker build \
--build-arg="GIT_USERNAME=${env.GIT_USERNAME}" \
--build-arg="GIT_PASSWORD=${env.GIT_PASSWORD}" \
-t ${fullImageName}:${imageTag} \
-f '${dockerfile}' \
'${contextDir}'
"""
}
}
stage("Publish image '${fullImageName}:${imageTag}'") {
retry(2) {
sh """
echo ${env.HUB_PASSWORD} | docker login -u '${env.HUB_USERNAME}' --password-stdin
docker login '${dockerRepository}'
docker push '${fullImageName}:${imageTag}'
"""
}
}
}
}
def createPodmanPackage(Map options = [:]) {
String gomplateBin = getOrInstallGomplate()
}
String getOrInstallGomplate() {
String installDir = options.get("installDir", "/usr/local/bin")
String version = options.get("version", "3.10.0")
String forceDownload = options.get("forceDownload", false)
String downloadUrl = options.get("downloadUrl", "https://github.com/hairyhenderson/gomplate/releases/download/v${version}/gomplate_linux-amd64")
String gomplateBin = sh(returnStdout: true, script: 'which gomplate').trim("")
if (gomplateBin == "" || forceDownload) {
sh("""
mkdir -p '${installDir}'
curl -o tools/gomplate/bin/gomplate -sSL '${forceDownload}'
chmod +x '${installDir}/gomplate'
""")
gomplateBin = "${installDir}/gomplate"
}
return gomplateBin
}