You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Jenkinsfile 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. pipeline {
  2. agent {
  3. node {
  4. label 'hephaistos'
  5. }
  6. }
  7. parameters {
  8. string(
  9. name: 'images',
  10. defaultValue: 'openwrt, jenkins, alpine, eole-2.6.2, eole-2.5.2, ubuntu, centos, crous, eole-2.7.0, awx',
  11. description: 'Listes des images à déployer'
  12. )
  13. string(
  14. name: 'deployBranch',
  15. defaultValue: 'master',
  16. description: 'Branche de déploiement'
  17. )
  18. string(
  19. name: 'publicDir',
  20. defaultValue: '/var/www/html/market',
  21. description: 'Chemin du répertoire de publication HTTP des images'
  22. )
  23. }
  24. // On exécute le pipeline tous les jours sur la branche 'master'
  25. triggers {
  26. cron(env.BRANCH_NAME == 'master' ? '@daily' : '')
  27. }
  28. stages {
  29. stage('Install required tools') {
  30. steps {
  31. script {
  32. sh 'script/install'
  33. }
  34. }
  35. }
  36. stage('Build images') {
  37. steps {
  38. script {
  39. def images = params.images.split(',')
  40. for (int i = 0; i < images.length; i++) {
  41. try {
  42. def name = images[i].replaceAll("\\s","")
  43. stage("Building '${name}'") {
  44. timeout(time: 60, unit: 'MINUTES') {
  45. sh "PACKER_LOG=1 make ${name}"
  46. }
  47. }
  48. } catch(err) {
  49. println "[ERROR] '${err}'"
  50. currentBuild.result = 'UNSTABLE';
  51. }
  52. }
  53. }
  54. }
  55. }
  56. stage('Cleanup publication directories') {
  57. steps {
  58. script {
  59. // Cleanup pusblished directories that do not match existing branches
  60. def branches = sh(
  61. script: "git branch -r | sed 's/^.*\\///' | sort | uniq",
  62. returnStdout: true
  63. ).trim().split('\n')
  64. def directories = sh(
  65. script: "find '${params.publicDir}' -maxdepth 1 -type d -printf '%f\\n'",
  66. returnStdout: true
  67. ).trim().split('\n')
  68. for (int i = 0; i < directories.length; i++) {
  69. def dirname = directories[i]
  70. if (!branches.contains(dirname)) {
  71. println "Deleting directory '${params.publicDir}/${dirname}'"
  72. sh "rm -rf ${params.publicDir}/${dirname}"
  73. }
  74. }
  75. }
  76. }
  77. }
  78. stage('Synchronize publication directory') {
  79. steps {
  80. script {
  81. // Synchronize publication branch directory
  82. sh """
  83. mkdir -p '${params.publicDir}/${env.BRANCH_NAME}'
  84. rsync -rtvuc images/ '${params.publicDir}/${env.BRANCH_NAME}'
  85. """
  86. }
  87. }
  88. }
  89. stage('Deploy images') {
  90. when {
  91. branch params.deployBranch
  92. }
  93. steps {
  94. script {
  95. def images = params.images.split(',')
  96. for (int i = 0; i < images.length; i++) {
  97. try {
  98. def name = images[i].replaceAll("\\s","")
  99. stage("Deploying '${name}'") {
  100. withCredentials([
  101. string(credentialsId: 'opennebula-user', variable: 'OPENNEBULA_USER'),
  102. string(credentialsId: 'opennebula-password', variable: 'OPENNEBULA_PASSWORD'),
  103. string(credentialsId: 'opennebula-endpoint', variable: 'OPENNEBULA_ENDPOINT'),
  104. string(credentialsId: 'opennebula-datastore', variable: 'OPENNEBULA_DATASTORE'),
  105. string(credentialsId: 'opennebula-image-base-url', variable: 'IMAGE_BASE_URL'),
  106. ]) {
  107. timeout(time: 5, unit: 'MINUTES') {
  108. sh """
  109. export IMAGE_BASE_URL='${IMAGE_BASE_URL}/${env.BRANCH_NAME}'
  110. PACKER_LOG=1 make deploy-${name}
  111. """
  112. }
  113. }
  114. }
  115. } catch(err) {
  116. println "[ERROR] '${err}'"
  117. currentBuild.result = 'UNSTABLE';
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. post {
  125. always {
  126. script {
  127. if (currentBuild.currentResult != 'SUCCESS') {
  128. rocketSend (
  129. avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
  130. message: """
  131. ${env.JOB_NAME}/${env.BUILD_ID}: ${currentBuild.currentResult}
  132. ${env.RUN_DISPLAY_URL}
  133. """.stripIndent(),
  134. rawMessage: true
  135. )
  136. emailext (
  137. subject: "${currentBuild.currentResult} - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
  138. body: """
  139. Voir les étapes du job: ${env.RUN_DISPLAY_URL}
  140. Projet: ${env.GIT_URL}
  141. Branche: ${env.GIT_BRANCH}
  142. Commit: ${env.GIT_COMMIT}
  143. """.stripIndent(),
  144. recipientProviders: [developers(), requestor()],
  145. )
  146. }
  147. }
  148. }
  149. cleanup {
  150. cleanWs()
  151. }
  152. }
  153. }