Jenkinsfile 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. }
  58. }
  59. stage('Builds') {
  60. parallel {
  61. stage('Project') {
  62. steps {
  63. script {
  64. // Frontend and Backend
  65. def shStatusCode = sh(label: 'Checking and Building', returnStatus: true, script: '''
  66. set -e
  67. ./scripts/ci/frontend-build > ${WORKSPACE}/tmp-sh-build 2>&1
  68. ./scripts/ci/test-and-build > ${WORKSPACE}/tmp-sh-build 2>&1
  69. ''')
  70. shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
  71. if (shStatusCode != 0) {
  72. error "${shOutput}"
  73. }
  74. }
  75. }
  76. post {
  77. always {
  78. sh 'rm -f ${WORKSPACE}/tmp-sh-build'
  79. }
  80. failure {
  81. npmGithubPrComment("CI Error:\n\n```\n${shOutput}\n```", true)
  82. }
  83. }
  84. }
  85. stage('Docs') {
  86. steps {
  87. dir(path: 'docs') {
  88. sh 'yarn install'
  89. sh 'yarn build'
  90. }
  91. }
  92. }
  93. }
  94. }
  95. stage('Test Sqlite') {
  96. environment {
  97. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}_sqlite"
  98. COMPOSE_FILE = 'docker/docker-compose.ci.yml:docker/docker-compose.ci.sqlite.yml'
  99. }
  100. when {
  101. not {
  102. equals expected: 'UNSTABLE', actual: currentBuild.result
  103. }
  104. }
  105. steps {
  106. sh 'rm -rf ./test/results/junit/*'
  107. sh './scripts/ci/fulltest-cypress'
  108. }
  109. post {
  110. always {
  111. // Dumps to analyze later
  112. sh 'mkdir -p debug/sqlite'
  113. sh 'docker logs $(docker-compose ps --all -q fullstack) > debug/sqlite/docker_fullstack.log 2>&1'
  114. sh 'docker logs $(docker-compose ps --all -q stepca) > debug/sqlite/docker_stepca.log 2>&1'
  115. sh 'docker logs $(docker-compose ps --all -q pdns) > debug/sqlite/docker_pdns.log 2>&1'
  116. sh 'docker logs $(docker-compose ps --all -q pdns-db) > debug/sqlite/docker_pdns-db.log 2>&1'
  117. sh 'docker logs $(docker-compose ps --all -q dnsrouter) > debug/sqlite/docker_dnsrouter.log 2>&1'
  118. junit 'test/results/junit/*'
  119. sh 'docker-compose down --remove-orphans --volumes -t 30 || true'
  120. }
  121. }
  122. }
  123. stage('Test Mysql') {
  124. environment {
  125. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}_mysql"
  126. COMPOSE_FILE = 'docker/docker-compose.ci.yml:docker/docker-compose.ci.mysql.yml'
  127. }
  128. when {
  129. not {
  130. equals expected: 'UNSTABLE', actual: currentBuild.result
  131. }
  132. }
  133. steps {
  134. sh 'rm -rf ./test/results/junit/*'
  135. sh './scripts/ci/fulltest-cypress'
  136. }
  137. post {
  138. always {
  139. // Dumps to analyze later
  140. sh 'mkdir -p debug/mysql'
  141. sh 'docker logs $(docker-compose ps --all -q fullstack) > debug/mysql/docker_fullstack.log 2>&1'
  142. sh 'docker logs $(docker-compose ps --all -q stepca) > debug/mysql/docker_stepca.log 2>&1'
  143. sh 'docker logs $(docker-compose ps --all -q pdns) > debug/mysql/docker_pdns.log 2>&1'
  144. sh 'docker logs $(docker-compose ps --all -q pdns-db) > debug/mysql/docker_pdns-db.log 2>&1'
  145. sh 'docker logs $(docker-compose ps --all -q dnsrouter) > debug/mysql/docker_dnsrouter.log 2>&1'
  146. junit 'test/results/junit/*'
  147. sh 'docker-compose down --remove-orphans --volumes -t 30 || true'
  148. }
  149. }
  150. }
  151. stage('MultiArch Build') {
  152. when {
  153. not {
  154. equals expected: 'UNSTABLE', actual: currentBuild.result
  155. }
  156. }
  157. steps {
  158. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  159. sh 'docker login -u "${duser}" -p "${dpass}"'
  160. sh "./scripts/buildx --push ${buildxPushTags}"
  161. }
  162. }
  163. }
  164. stage('Docs / Comment') {
  165. parallel {
  166. stage('Docs Job') {
  167. when {
  168. allOf {
  169. branch pattern: "^(develop|master)\$", comparator: "REGEXP"
  170. not {
  171. equals expected: 'UNSTABLE', actual: currentBuild.result
  172. }
  173. }
  174. }
  175. steps {
  176. build wait: false, job: 'nginx-proxy-manager-docs', parameters: [string(name: 'docs_branch', value: "$BRANCH_NAME")]
  177. }
  178. }
  179. stage('PR Comment') {
  180. when {
  181. allOf {
  182. changeRequest()
  183. not {
  184. equals expected: 'UNSTABLE', actual: currentBuild.result
  185. }
  186. }
  187. }
  188. steps {
  189. script {
  190. 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)
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. post {
  198. always {
  199. sh 'echo Reverting ownership'
  200. sh 'docker run --rm -v "$(pwd):/data" jc21/ci-tools chown -R "$(id -u):$(id -g)" /data'
  201. }
  202. success {
  203. juxtapose event: 'success'
  204. sh 'figlet "SUCCESS"'
  205. }
  206. failure {
  207. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  208. juxtapose event: 'failure'
  209. sh 'figlet "FAILURE"'
  210. }
  211. unstable {
  212. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  213. juxtapose event: 'unstable'
  214. sh 'figlet "UNSTABLE"'
  215. }
  216. }
  217. }
  218. def getVersion() {
  219. ver = sh(script: 'cat .version', returnStdout: true)
  220. return ver.trim()
  221. }
  222. def getCommit() {
  223. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  224. return ver.trim()
  225. }