Jenkinsfile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import groovy.transform.Field
  2. @Field
  3. def shOutput = ""
  4. def buildxPushTags = ""
  5. pipeline {
  6. agent {
  7. label 'docker-multiarch'
  8. }
  9. options {
  10. buildDiscarder(logRotator(numToKeepStr: '5'))
  11. disableConcurrentBuilds()
  12. ansiColor('xterm')
  13. }
  14. environment {
  15. IMAGE = 'nginx-proxy-manager'
  16. BUILD_VERSION = getVersion()
  17. MAJOR_VERSION = '2'
  18. BRANCH_LOWER = "${BRANCH_NAME.toLowerCase().replaceAll('\\\\', '-').replaceAll('/', '-').replaceAll('\\.', '-')}"
  19. BUILDX_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}"
  20. COMPOSE_INTERACTIVE_NO_CLI = 1
  21. }
  22. stages {
  23. stage('Environment') {
  24. parallel {
  25. stage('Master') {
  26. when {
  27. branch 'master'
  28. }
  29. steps {
  30. script {
  31. buildxPushTags = "-t docker.io/jc21/${IMAGE}:${BUILD_VERSION} -t docker.io/jc21/${IMAGE}:${MAJOR_VERSION} -t docker.io/jc21/${IMAGE}:latest"
  32. }
  33. }
  34. }
  35. stage('Other') {
  36. when {
  37. not {
  38. branch 'master'
  39. }
  40. }
  41. steps {
  42. script {
  43. // Defaults to the Branch name, which is applies to all branches AND pr's
  44. buildxPushTags = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}"
  45. }
  46. }
  47. }
  48. stage('Versions') {
  49. steps {
  50. sh 'cat frontend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge frontend/package.json'
  51. sh 'echo -e "\\E[1;36mFrontend Version is:\\E[1;33m $(cat frontend/package.json | jq -r .version)\\E[0m"'
  52. sh 'cat backend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge backend/package.json'
  53. sh 'echo -e "\\E[1;36mBackend Version is:\\E[1;33m $(cat backend/package.json | jq -r .version)\\E[0m"'
  54. sh 'sed -i -E "s/(version-)[0-9]+\\.[0-9]+\\.[0-9]+(-green)/\\1${BUILD_VERSION}\\2/" README.md'
  55. }
  56. }
  57. stage('Docker Login') {
  58. steps {
  59. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  60. sh 'docker login -u "${duser}" -p "${dpass}"'
  61. }
  62. }
  63. }
  64. }
  65. }
  66. stage('Builds') {
  67. parallel {
  68. stage('Project') {
  69. steps {
  70. script {
  71. // Frontend and Backend
  72. def shStatusCode = sh(label: 'Checking and Building', returnStatus: true, script: '''
  73. set -e
  74. ./scripts/ci/frontend-build > ${WORKSPACE}/tmp-sh-build 2>&1
  75. ./scripts/ci/test-and-build > ${WORKSPACE}/tmp-sh-build 2>&1
  76. ''')
  77. shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
  78. if (shStatusCode != 0) {
  79. error "${shOutput}"
  80. }
  81. }
  82. }
  83. post {
  84. always {
  85. sh 'rm -f ${WORKSPACE}/tmp-sh-build'
  86. }
  87. failure {
  88. npmGithubPrComment("CI Error:\n\n```\n${shOutput}\n```", true)
  89. }
  90. }
  91. }
  92. stage('Docs') {
  93. steps {
  94. dir(path: 'docs') {
  95. sh 'yarn install'
  96. sh 'yarn build'
  97. }
  98. }
  99. }
  100. }
  101. }
  102. stage('Test Sqlite') {
  103. environment {
  104. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}_sqlite"
  105. COMPOSE_FILE = 'docker/docker-compose.ci.yml:docker/docker-compose.ci.sqlite.yml'
  106. }
  107. when {
  108. not {
  109. equals expected: 'UNSTABLE', actual: currentBuild.result
  110. }
  111. }
  112. steps {
  113. sh 'rm -rf ./test/results/junit/*'
  114. sh './scripts/ci/fulltest-cypress'
  115. }
  116. post {
  117. always {
  118. // Dumps to analyze later
  119. sh 'mkdir -p debug/sqlite'
  120. sh 'docker logs $(docker-compose ps --all -q fullstack) > debug/sqlite/docker_fullstack.log 2>&1'
  121. sh 'docker logs $(docker-compose ps --all -q stepca) > debug/sqlite/docker_stepca.log 2>&1'
  122. sh 'docker logs $(docker-compose ps --all -q pdns) > debug/sqlite/docker_pdns.log 2>&1'
  123. sh 'docker logs $(docker-compose ps --all -q pdns-db) > debug/sqlite/docker_pdns-db.log 2>&1'
  124. sh 'docker logs $(docker-compose ps --all -q dnsrouter) > debug/sqlite/docker_dnsrouter.log 2>&1'
  125. junit 'test/results/junit/*'
  126. sh 'docker-compose down --remove-orphans --volumes -t 30 || true'
  127. }
  128. }
  129. }
  130. stage('Test Mysql') {
  131. environment {
  132. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}_mysql"
  133. COMPOSE_FILE = 'docker/docker-compose.ci.yml:docker/docker-compose.ci.mysql.yml'
  134. }
  135. when {
  136. not {
  137. equals expected: 'UNSTABLE', actual: currentBuild.result
  138. }
  139. }
  140. steps {
  141. sh 'rm -rf ./test/results/junit/*'
  142. sh './scripts/ci/fulltest-cypress'
  143. }
  144. post {
  145. always {
  146. // Dumps to analyze later
  147. sh 'mkdir -p debug/mysql'
  148. sh 'docker logs $(docker-compose ps --all -q fullstack) > debug/mysql/docker_fullstack.log 2>&1'
  149. sh 'docker logs $(docker-compose ps --all -q stepca) > debug/mysql/docker_stepca.log 2>&1'
  150. sh 'docker logs $(docker-compose ps --all -q pdns) > debug/mysql/docker_pdns.log 2>&1'
  151. sh 'docker logs $(docker-compose ps --all -q pdns-db) > debug/mysql/docker_pdns-db.log 2>&1'
  152. sh 'docker logs $(docker-compose ps --all -q dnsrouter) > debug/mysql/docker_dnsrouter.log 2>&1'
  153. junit 'test/results/junit/*'
  154. sh 'docker-compose down --remove-orphans --volumes -t 30 || true'
  155. }
  156. }
  157. }
  158. stage('MultiArch Build') {
  159. when {
  160. not {
  161. equals expected: 'UNSTABLE', actual: currentBuild.result
  162. }
  163. }
  164. steps {
  165. sh "./scripts/buildx --push ${buildxPushTags}"
  166. }
  167. }
  168. stage('Docs / Comment') {
  169. parallel {
  170. stage('Docs Job') {
  171. when {
  172. allOf {
  173. branch pattern: "^(develop|master)\$", comparator: "REGEXP"
  174. not {
  175. equals expected: 'UNSTABLE', actual: currentBuild.result
  176. }
  177. }
  178. }
  179. steps {
  180. build wait: false, job: 'nginx-proxy-manager-docs', parameters: [string(name: 'docs_branch', value: "$BRANCH_NAME")]
  181. }
  182. }
  183. stage('PR Comment') {
  184. when {
  185. allOf {
  186. changeRequest()
  187. not {
  188. equals expected: 'UNSTABLE', actual: currentBuild.result
  189. }
  190. }
  191. }
  192. steps {
  193. script {
  194. npmGithubPrComment("Docker Image for build ${BUILD_NUMBER} is available on [DockerHub](https://cloud.docker.com/repository/docker/jc21/${IMAGE}) as `jc21/${IMAGE}:github-${BRANCH_LOWER}`\n\n**Note:** ensure you backup your NPM instance before testing this PR image! Especially if this PR contains database changes.", true)
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. post {
  202. always {
  203. sh 'echo Reverting ownership'
  204. sh 'docker run --rm -v "$(pwd):/data" jc21/ci-tools chown -R "$(id -u):$(id -g)" /data'
  205. }
  206. success {
  207. juxtapose event: 'success'
  208. sh 'figlet "SUCCESS"'
  209. }
  210. failure {
  211. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  212. juxtapose event: 'failure'
  213. sh 'figlet "FAILURE"'
  214. }
  215. unstable {
  216. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  217. juxtapose event: 'unstable'
  218. sh 'figlet "UNSTABLE"'
  219. }
  220. }
  221. }
  222. def getVersion() {
  223. ver = sh(script: 'cat .version', returnStdout: true)
  224. return ver.trim()
  225. }
  226. def getCommit() {
  227. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  228. return ver.trim()
  229. }