tamarin: add prepareEnvironment() method

This commit is contained in:
wpetit 2020-03-27 11:50:44 +01:00
parent 3890170351
commit 073995cb1b
3 changed files with 179 additions and 138 deletions

View File

@ -10,165 +10,178 @@ pipeline {
projectDir = "${env.project_name}_${env.BUILD_ID}" projectDir = "${env.project_name}_${env.BUILD_ID}"
} }
triggers {
// Execute pipeline every day at 7h30 to prepare docker images
cron(env.BRANCH_NAME == 'develop' ? '30 7 * * 1-5' : '')
}
stages { stages {
stage("Clone repository") { stage("Prepare build environment") {
steps { when {
checkout scm: anyOf {
[ triggeredBy cause: "UserIdCause", detail: "wpetit"
$class: 'GitSCM', triggeredBy 'TimerTrigger'
userRemoteConfigs: [[url: env.repository_url, credentialsId: 'jenkins-forge-ssh']], }
branches: [[name: env.ref]],
extensions: [
[$class: 'RelativeTargetDirectory', relativeTargetDir: env.projectDir ],
[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: ''],
[$class: 'WipeWorkspace' ]
]
],
changelog: false,
poll: false
} }
}
stage("Ensure packaging branch") {
steps { steps {
script { script {
dir(env.projectDir) { tamarin.prepareEnvironment()
sh 'git checkout "${packageBranch}"'
def commitOrRef = env.commit ? env.commit : env.ref
def branchesWithCommitOrRef = sh(script: "git branch --contains '${commitOrRef}'", returnStdout: true).split(' ')
if (branchesWithCommitOrRef.findAll{env.packageBranch.contains(it)}.any{true}) {
currentBuild.result = 'ABORTED'
error("La référence `${env.ref}` ne fait pas partie de la branche `${env.packageBranch}` !")
}
}
} }
} }
} }
stage("Check [ci skip] in tag message") { stage("Package project") {
when {
not {
triggeredBy 'TimerTrigger'
}
}
steps { steps {
script { script {
dir(env.projectDir) { stage("Clone repository") {
sh 'git checkout "${packageBranch}"' checkout scm:
def commitTags = sh(script: 'git describe --exact-match --abbrev=0', returnStdout: true).split(' ') [
for (tag in commitTags) { $class: 'GitSCM',
tag = tag.trim() userRemoteConfigs: [[url: env.repository_url, credentialsId: 'jenkins-forge-ssh']],
def tagMessage = sh(script: "git tag --format='%(subject)' -l '${tag}'", returnStdout: true).trim() branches: [[name: env.ref]],
println("Tag '${tag}' message is: '${tagMessage}'") extensions: [
if (tagMessage.contains('[ci skip]')) { [$class: 'RelativeTargetDirectory', relativeTargetDir: env.projectDir ],
[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: ''],
[$class: 'WipeWorkspace' ]
]
],
changelog: false,
poll: false
}
stage("Ensure packaging branch") {
dir(env.projectDir) {
sh 'git checkout "${packageBranch}"'
def commitOrRef = env.commit ? env.commit : env.ref
def branchesWithCommitOrRef = sh(script: "git branch --contains '${commitOrRef}'", returnStdout: true).split(' ')
if (branchesWithCommitOrRef.findAll{env.packageBranch.contains(it)}.any{true}) {
currentBuild.result = 'ABORTED' currentBuild.result = 'ABORTED'
error("Le message du tag '${tag}' contient le marqueur '[ci-skip]' !") error("La référence `${env.ref}` ne fait pas partie de la branche `${env.packageBranch}` !")
} }
} }
} }
}
}
}
stage("Checkout ref") { stage("Check [ci skip] in tag message") {
steps { dir(env.projectDir) {
dir(env.projectDir) { sh 'git checkout "${packageBranch}"'
sh """ def commitTags = sh(script: 'git describe --exact-match --abbrev=0', returnStdout: true).split(' ')
git checkout ${env.ref} for (tag in commitTags) {
""" tag = tag.trim()
} def tagMessage = sh(script: "git tag --format='%(subject)' -l '${tag}'", returnStdout: true).trim()
} println("Tag '${tag}' message is: '${tagMessage}'")
} if (tagMessage.contains('[ci skip]')) {
currentBuild.result = 'ABORTED'
stage("Build package") { error("Le message du tag '${tag}' contient le marqueur '[ci-skip]' !")
steps {
script {
dir(env.projectDir) {
// On construit les paquets à partir des informations
// de contexte provenant de CPKG et du webhook
def result = tamarin.buildPackageWithCPKG(
env.packageProfile ? env.packageProfile : "debian",
env.packageArch ? env.packageArch : "",
env.packageBranch ? env.packageBranch : "",
env.baseImage ? env.baseImage : ""
)
// On publie chacun des paquets construits
result.each { r ->
vulcain.publish(
r.packages,
r.env,
env.packageBranch
)
}
// On liste l'ensemble des paquets construits
def publishedPackages = result.collect { r ->
return r.packages.collect { p ->
def file = new File(p)
return "- Paquet `${file.getName()}`, Dépôt `${r.env}`, Distribution `${r.distrib}`"
}
}.transpose().collectMany { it }
// On notifie le canal Rocket.Chat de la publication des paquets
rocketSend (
avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
message: """
Les paquets suivants ont été publiés pour le projet ${env.project_name}:
${publishedPackages.join('\n')}
[Visualiser le job](${env.RUN_DISPLAY_URL})
@${env.sender_login}
""".stripIndent(),
rawMessage: true,
attachments: lolops.getRandomDeliveryAttachment()
)
if (env.testPackageInstall != 'yes') {
println "Test d'intallation des paquets désactivé."
return
}
// Pour chaque construction de paquets...
result.each { r ->
// On essaye de trouver un template de VM compatible
// avec la distribution cible de la construction
def vmTemplate = findMatchingVMTemplate(r.distrib)
if (vmTemplate == null) {
println "Aucun template de VM n'a été trouvé correspondant à la distribution `${r.distrib}`."
return
}
// Pour chaque paquets construits...
r.packages.each { p ->
def packageFullName = new File(p).getName()
def packageRepository = r.distrib.split('-')[1] + '-' + r.env
def packageNameParts = packageFullName.split('_')
def packageName = packageNameParts[0]
def packageVersion = packageNameParts[1]
stage("Test package '${packageName}' installation") {
build job: 'Test de paquet Debian', wait: false, parameters: [
[$class: 'StringParameterValue', name: 'packageName', value: packageName],
[$class: 'StringParameterValue', name: 'packageVersion', value: packageVersion],
[$class: 'StringParameterValue', name: 'packageRepository', value: packageRepository],
[$class: 'StringParameterValue', name: 'vmTemplate', value: vmTemplate]
]
} }
} }
} }
} }
stage("Checkout ref") {
dir(env.projectDir) {
sh """
git checkout ${env.ref}
"""
}
}
stage("Build package") {
dir(env.projectDir) {
// On construit les paquets à partir des informations
// de contexte provenant de CPKG et du webhook
def result = tamarin.buildPackageWithCPKG(
env.packageProfile ? env.packageProfile : "debian",
env.packageArch ? env.packageArch : "",
env.packageBranch ? env.packageBranch : "",
env.baseImage ? env.baseImage : ""
)
// On publie chacun des paquets construits
result.each { r ->
vulcain.publish(
r.packages,
r.env,
env.packageBranch
)
}
// On liste l'ensemble des paquets construits
def publishedPackages = result.collect { r ->
return r.packages.collect { p ->
def file = new File(p)
return "- Paquet `${file.getName()}`, Dépôt `${r.env}`, Distribution `${r.distrib}`"
}
}.transpose().collectMany { it }
// On notifie le canal Rocket.Chat de la publication des paquets
rocketSend (
avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
message: """
Les paquets suivants ont été publiés pour le projet ${env.project_name}:
${publishedPackages.join('\n')}
[Visualiser le job](${env.RUN_DISPLAY_URL})
@${env.sender_login}
""".stripIndent(),
rawMessage: true,
attachments: lolops.getRandomDeliveryAttachment()
)
if (env.testPackageInstall != 'yes') {
println "Test d'intallation des paquets désactivé."
return
}
// Pour chaque construction de paquets...
result.each { r ->
// On essaye de trouver un template de VM compatible
// avec la distribution cible de la construction
def vmTemplate = findMatchingVMTemplate(r.distrib)
if (vmTemplate == null) {
println "Aucun template de VM n'a été trouvé correspondant à la distribution `${r.distrib}`."
return
}
// Pour chaque paquets construits...
r.packages.each { p ->
def packageFullName = new File(p).getName()
def packageRepository = r.distrib.split('-')[1] + '-' + r.env
def packageNameParts = packageFullName.split('_')
def packageName = packageNameParts[0]
def packageVersion = packageNameParts[1]
stage("Test package '${packageName}' installation") {
build job: 'Test de paquet Debian', wait: false, parameters: [
[$class: 'StringParameterValue', name: 'packageName', value: packageName],
[$class: 'StringParameterValue', name: 'packageVersion', value: packageVersion],
[$class: 'StringParameterValue', name: 'packageRepository', value: packageRepository],
[$class: 'StringParameterValue', name: 'vmTemplate', value: vmTemplate]
]
}
}
}
}
}
}
}
post {
always {
sh "rm -rf '${env.projectDir}'"
} }
} }
} }
} }
post {
always {
sh "rm -rf '${env.projectDir}'"
}
}
} }

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -xe
cp -r . /src cp -r . /src
@ -17,6 +17,7 @@ https_proxy=${https_proxy}
EOF EOF
[ "${TAMARIN_FORCE_REBUILD}" == "true" ] && PACKAGE_ARGS="${PACKAGE_ARGS} --rebuild" [ "${TAMARIN_FORCE_REBUILD}" == "true" ] && PACKAGE_ARGS="${PACKAGE_ARGS} --rebuild"
[ "${TAMARIN_PREPARE_ONLY}" == "true" ] && PACKAGE_ARGS="${PACKAGE_ARGS} --prepare-only"
[ ! -z "${TAMARIN_PACKAGE_ARCH}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -a ${TAMARIN_PACKAGE_ARCH}" [ ! -z "${TAMARIN_PACKAGE_ARCH}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -a ${TAMARIN_PACKAGE_ARCH}"
[ ! -z "${TAMARIN_BASE_IMAGE}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -b ${TAMARIN_BASE_IMAGE}" [ ! -z "${TAMARIN_BASE_IMAGE}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -b ${TAMARIN_BASE_IMAGE}"
[ ! -z "${TAMARIN_PROFILE}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -p ${TAMARIN_PROFILE}" [ ! -z "${TAMARIN_PROFILE}" ] && PACKAGE_ARGS="${PACKAGE_ARGS} -p ${TAMARIN_PROFILE}"
@ -25,4 +26,6 @@ EOF
DEST_DIR=${TAMARIN_DEST_DIR:-dist} DEST_DIR=${TAMARIN_DEST_DIR:-dist}
mkdir -p ${DEST_DIR} mkdir -p ${DEST_DIR}
cp -r /dist/* ./${DEST_DIR} for f in /dist/*; do
[ -e "$f" ] && cp "$f" ./${DEST_DIR}
done

View File

@ -89,6 +89,31 @@ def buildPackage(
} }
def prepareEnvironment(
String packageProfile = "debian",
String baseImage = ""
) {
def tamarinImage
stage("Create Tamarin environment") {
tamarinImage = buildDockerImage()
}
stage("Prepare Tamarin") {
def dockerArgs = """
-v /var/run/docker.sock:/var/run/docker.sock
${baseImage ? '-e TAMARIN_BASE_IMAGE='+baseImage : ''}
${packageProfile ? '-e TAMARIN_PROFILE='+packageProfile : ''}
-e TAMARIN_PREPARE_ONLY=true
-e TAMARIN_FORCE_REBUILD=true
""".stripIndent()
tamarinImage.inside(dockerArgs) {
sh 'run-tamarin'
}
}
}
def buildDockerImage() { def buildDockerImage() {
dir ('.tamarin') { dir ('.tamarin') {
def dockerfile = libraryResource 'com/cadoles/tamarin/Dockerfile' def dockerfile = libraryResource 'com/cadoles/tamarin/Dockerfile'