feat: add container + nfpm utilities

This commit is contained in:
wpetit 2022-10-03 14:34:17 +02:00
parent 57194c84eb
commit e670fb8bf6
3 changed files with 97 additions and 0 deletions

67
vars/container.groovy Normal file
View File

@ -0,0 +1,67 @@
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
}

25
vars/nfpm.groovy Normal file
View File

@ -0,0 +1,25 @@
// Package project with nfpm
// See https://nfpm.goreleaser.com/
def package(Map options = [:]) {
String installDir = options.get("nfpmInstallDir", "/usr/local/bin")
String nfpmVersion = options.get("nfpmVersion", "2.15.1")
String nfpmForceDownload = options.get("nfpmForceDownload", false)
String nfpmDownloadUrl = options.get("nfpmDownloadUrl", "https://github.com/goreleaser/nfpm/releases/download/${nfpmVersion}/nfpm_${nfpmVersion}_Linux_x86_64.tar.gz")
String nfpmConfig = options.get("nfpmConfig", "nfpm.yaml")
String nfpmTarget = options.get("nfpmTarget", ".")
String nfpmPackager = options.get("nfpmPackager", "")
String nfpmBin = sh(returnStdout: true, script: 'which nfpm').trim("")
if (nfpmBin == "" || nfpmForceDownload) {
sh("""
mkdir -p '${installDir}'
curl -L '${nfpmDownloadUrl}' > /tmp/nfpm.tar.gz
tar -C /usr/local/bin -xzf /tmp/nfpm.tar.gz
""")
}
sh("""
export PATH='${installDir}:${env.PATH}'
nfpm --config '${nfpmConfig}' ${nfpmPackager ? "--packager "+nfpmPackager : ""} --target '${nfpmTarget}'
""")
}

View File

@ -2,6 +2,11 @@ import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
def call(String baseImage = 'ubuntu:22.04') {
node {
stage('Cancel older jobs') {
def buildNumber = env.BUILD_NUMBER as int
if (buildNumber > 1) milestone(buildNumber - 1)
milestone(buildNumber)
}
stage('Checkout project') {
checkout(scm)
}