Compare commits
1 Commits
master
...
redesign-t
Author | SHA1 | Date |
---|---|---|
wpetit | c73ddac0fd |
21
README.md
21
README.md
|
@ -1,18 +1,31 @@
|
|||
# Jenkins
|
||||
|
||||
Librairie utilitaire pour la création de pipeline Jenkins dans l'environnement Cadoles.
|
||||
Utilitaires pour la création de pipeline Jenkins dans l'environnement Cadoles.
|
||||
|
||||
## Méthodes exposées
|
||||
## Pipelines
|
||||
|
||||
### Création de paquets
|
||||
- [Pipeline d'empaquetage Debian](./pipelines/debian-packaging.jenkinsfile)
|
||||
|
||||
## Librairie
|
||||
|
||||
### Méthodes exposées
|
||||
|
||||
#### Création de paquets
|
||||
|
||||
- [`tamarin.buildPackage()`](./vars/tamarin.groovy#L48)
|
||||
- [`tamarin.buildPackageWithCPKG()`](./vars/tamarin.groovy#L1)
|
||||
|
||||
### Publication de paquets
|
||||
#### Publication de paquets
|
||||
|
||||
- [`vulcain.publish()`](./vars/vulcain.groovy#L1)
|
||||
|
||||
#### Pilotage d'OpenNebula
|
||||
|
||||
- [`nebula.initWithCredentials()`](./vars/nebula.groovy#L125)
|
||||
- [`nebula.runInNewVM() { client -> ... }`](./vars/nebula.groovy#L135)
|
||||
- [`client.findVMTemplate()`](./vars/nebula.groovy#L65)
|
||||
- [`client.withNewVM()`](./vars/nebula.groovy#L79)
|
||||
|
||||
## Licence
|
||||
|
||||
GPL-3.0
|
||||
|
|
|
@ -0,0 +1,186 @@
|
|||
@Library("cadoles@redesign-tamarin") _
|
||||
|
||||
pipeline {
|
||||
|
||||
agent {
|
||||
label 'common'
|
||||
}
|
||||
|
||||
environment {
|
||||
projectDir = "${env.project_name}_${env.BUILD_ID}"
|
||||
}
|
||||
|
||||
stages {
|
||||
|
||||
stage("Clone repository") {
|
||||
steps {
|
||||
checkout scm:
|
||||
[
|
||||
$class: 'GitSCM',
|
||||
userRemoteConfigs: [[url: env.repository_url, credentialsId: 'forge-jenkins']],
|
||||
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 {
|
||||
script {
|
||||
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'
|
||||
error("La référence `${env.ref}` ne fait pas partie de la branche `${env.packageBranch}` !")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage("Checkout ref") {
|
||||
steps {
|
||||
dir(env.projectDir) {
|
||||
sh """
|
||||
git checkout ${env.ref}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage("Build package") {
|
||||
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
|
||||
)
|
||||
|
||||
if (env.testPackageInstall == 'no') {
|
||||
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 packageName = new File(p).getName()
|
||||
stage("Test package '${packageName}' installation") {
|
||||
try {
|
||||
// On démarre une nouvelle VM et on lance l'installation du paquet publié
|
||||
testPackageInstallation(vmTemplate, r.distrib, r.env, packageName)
|
||||
} catch(e) {
|
||||
currentBuild.result = 'UNSTABLE'
|
||||
rocketSend (
|
||||
avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
|
||||
message: """
|
||||
[Installation du paquet `${packageName}` échouée sur `${vmTemplate}`](${env.RUN_DISPLAY_URL})
|
||||
|
||||
@${env.sender_login}
|
||||
""".stripIndent(),
|
||||
rawMessage: true
|
||||
)
|
||||
error("Installation du paquet `${packageName}` échouée.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
sh "rm -rf '${env.projectDir}'"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Cette fonction fait un simple "mapping"
|
||||
// entre les distributions cibles des paquets et
|
||||
// les templates de VM disponibles sur l'OpenNebula
|
||||
def findMatchingVMTemplate(String distrib) {
|
||||
def vmTemplatesMap = [
|
||||
'eole-2.7.0': 'eolebase-2.7.0-cadoles',
|
||||
'eole-2.6.2': 'eolebase-2.6.2-cadoles'
|
||||
]
|
||||
return vmTemplatesMap.get(distrib, null)
|
||||
}
|
||||
|
||||
// Cette fonction démarre une nouvelle VM, configure les dépôts tiers pour ajouter
|
||||
// celui de Cadoles correspondant à la cible du paquet et tente d'installer celui ci.
|
||||
def testPackageInstallation(String vmTemplate, String distrib, String env, String packageName) {
|
||||
def version = distrib.split('-')[1]
|
||||
def packageNameParts = packageName.split('_')
|
||||
nebula.runInNewVM([
|
||||
vmTemplate: vmTemplate,
|
||||
script: """
|
||||
set -xeo pipefail
|
||||
wget -qO - https://vulcain.cadoles.com/cadoles.gpg | apt-key add -
|
||||
echo 'deb https://vulcain.cadoles.com ${version}-${env} main' > /etc/apt/sources.list.d/${version}-${env}.list
|
||||
apt-get update -y
|
||||
apt-get install -y ${packageNameParts[0]}=${packageNameParts[1]}
|
||||
"""
|
||||
])
|
||||
}
|
|
@ -4,10 +4,11 @@ def buildPackageWithCPKG(
|
|||
String packageBranch = "",
|
||||
String baseImage = "",
|
||||
String destDir = "./packages",
|
||||
Boolean forceRebuild = false,
|
||||
Boolean publishPackages = true
|
||||
Boolean forceRebuild = false
|
||||
) {
|
||||
|
||||
def builds = []
|
||||
|
||||
// Retrieve commit tags
|
||||
def commitTags = sh(script: 'git describe --exact-match --abbrev=0', returnStdout: true).split(' ')
|
||||
if (commitTags.length == 0) {
|
||||
|
@ -30,20 +31,23 @@ def buildPackageWithCPKG(
|
|||
""".stripIndent()
|
||||
writeFile file: '.tamarinrc', text: tamarinrc
|
||||
|
||||
sh "rm -rf ${destDir}/*"
|
||||
|
||||
stage("Build ${packageEnv} package (version ${packageVersion}) for ${packageDistrib}") {
|
||||
|
||||
sh "rm -rf ${destDir}/*"
|
||||
buildPackage(packageProfile, packageArch, baseImage, destDir, forceRebuild)
|
||||
|
||||
if (publishPackages) {
|
||||
stage("Publish ${packageEnv} packages (version ${packageVersion}) on Vulcain") {
|
||||
vulcain.publish(destDir, packageEnv, packageBranch)
|
||||
}
|
||||
}
|
||||
def result = [:]
|
||||
result.put('tag', tag)
|
||||
result.put('env', packageEnv)
|
||||
result.put('version', packageVersion)
|
||||
result.put('distrib', packageDistrib)
|
||||
def packages = buildPackage(packageProfile, packageArch, baseImage, destDir, forceRebuild)
|
||||
result.put('packages', packages)
|
||||
builds << result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return builds
|
||||
|
||||
}
|
||||
|
||||
def buildPackage(
|
||||
|
@ -54,7 +58,8 @@ def buildPackage(
|
|||
Boolean forceRebuild = false
|
||||
) {
|
||||
|
||||
def tamarinImage
|
||||
def tamarinImage
|
||||
def packages = []
|
||||
|
||||
stage("Create Tamarin environment") {
|
||||
tamarinImage = buildDockerImage()
|
||||
|
@ -73,7 +78,14 @@ def buildPackage(
|
|||
tamarinImage.inside(dockerArgs) {
|
||||
sh 'run-tamarin'
|
||||
}
|
||||
|
||||
packages = sh(script: "find '${destDir}' -name '*.deb' -type f", returnStdout: true)
|
||||
.split(' ')
|
||||
.collect { return it.trim() }
|
||||
.findAll { it != '' }
|
||||
}
|
||||
|
||||
return packages
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
def publish(
|
||||
String packagesDir = './packages',
|
||||
List packages = [],
|
||||
String packagesEnv = 'dev',
|
||||
String packagesBranch = '',
|
||||
String sshCredentialsId = 'vulcain-packages-ssh-keypair',
|
||||
|
@ -8,14 +8,13 @@ def publish(
|
|||
if (!packagesBranch) {
|
||||
packagesBranch = env.BRANCH_NAME
|
||||
}
|
||||
|
||||
withCredentials([
|
||||
sshUserPrivateKey(credentialsId: sshCredentialsId, keyFileVariable: 'VULCAIN_SSH_KEY', usernameVariable: 'VULCAIN_SSH_USER')
|
||||
]) {
|
||||
echo "Publishing packages to '${packagesEnv}/${packagesBranch}'"
|
||||
sh """
|
||||
ssh -i '${VULCAIN_SSH_KEY}' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '${VULCAIN_SSH_USER}@${vulcainHost}' mkdir -p '/home/${VULCAIN_SSH_USER}/packages/${packagesEnv}/${packagesBranch}'
|
||||
scp -i '${VULCAIN_SSH_KEY}' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r ${packagesDir}/*.deb '${VULCAIN_SSH_USER}@${vulcainHost}:/home/${VULCAIN_SSH_USER}/packages/${packagesEnv}/${packagesBranch}/'
|
||||
"""
|
||||
sh "ssh -i '${VULCAIN_SSH_KEY}' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '${VULCAIN_SSH_USER}@${vulcainHost}' mkdir -p '/home/${VULCAIN_SSH_USER}/packages/${packagesEnv}/${packagesBranch}'"
|
||||
packages.each {
|
||||
sh "scp -i '${VULCAIN_SSH_KEY}' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '${it}' '${VULCAIN_SSH_USER}@${vulcainHost}:/home/${VULCAIN_SSH_USER}/packages/${packagesEnv}/${packagesBranch}/'"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue