Jenkinsfile 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}"
  20. COMPOSE_FILE = 'docker/docker-compose.ci.yml'
  21. COMPOSE_INTERACTIVE_NO_CLI = 1
  22. BUILDX_NAME = "${COMPOSE_PROJECT_NAME}"
  23. }
  24. stages {
  25. stage('Environment') {
  26. parallel {
  27. stage('Master') {
  28. when {
  29. branch 'master'
  30. }
  31. steps {
  32. script {
  33. buildxPushTags = "-t docker.io/jc21/${IMAGE}:${BUILD_VERSION} -t docker.io/jc21/${IMAGE}:${MAJOR_VERSION} -t docker.io/jc21/${IMAGE}:latest"
  34. }
  35. }
  36. }
  37. stage('Other') {
  38. when {
  39. not {
  40. branch 'master'
  41. }
  42. }
  43. steps {
  44. script {
  45. // Defaults to the Branch name, which is applies to all branches AND pr's
  46. buildxPushTags = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}"
  47. }
  48. }
  49. }
  50. stage('Versions') {
  51. steps {
  52. sh 'cat frontend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge frontend/package.json'
  53. sh 'echo -e "\\E[1;36mFrontend Version is:\\E[1;33m $(cat frontend/package.json | jq -r .version)\\E[0m"'
  54. sh 'cat backend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge backend/package.json'
  55. sh 'echo -e "\\E[1;36mBackend Version is:\\E[1;33m $(cat backend/package.json | jq -r .version)\\E[0m"'
  56. sh 'sed -i -E "s/(version-)[0-9]+\\.[0-9]+\\.[0-9]+(-green)/\\1${BUILD_VERSION}\\2/" README.md'
  57. }
  58. }
  59. }
  60. }
  61. stage('Builds') {
  62. parallel {
  63. stage('Project') {
  64. steps {
  65. script {
  66. // Frontend and Backend
  67. def shStatusCode = sh(label: 'Checking and Building', returnStatus: true, script: '''
  68. set -e
  69. ./scripts/ci/frontend-build > ${WORKSPACE}/tmp-sh-build 2>&1
  70. ./scripts/ci/test-and-build > ${WORKSPACE}/tmp-sh-build 2>&1
  71. ''')
  72. shOutput = readFile "${env.WORKSPACE}/tmp-sh-build"
  73. if (shStatusCode != 0) {
  74. error "${shOutput}"
  75. }
  76. }
  77. }
  78. post {
  79. always {
  80. sh 'rm -f ${WORKSPACE}/tmp-sh-build'
  81. }
  82. failure {
  83. npmGithubPrComment("CI Error:\n\n```\n${shOutput}\n```", true)
  84. }
  85. }
  86. }
  87. stage('Docs') {
  88. steps {
  89. dir(path: 'docs') {
  90. sh 'yarn install'
  91. sh 'yarn build'
  92. }
  93. dir(path: 'docs/.vuepress/dist') {
  94. sh 'tar -czf ../../docs.tgz *'
  95. }
  96. archiveArtifacts(artifacts: 'docs/docs.tgz', allowEmptyArchive: false)
  97. }
  98. }
  99. stage('Cypress') {
  100. steps {
  101. // Creating will also create the network prior to
  102. // using it in parallel stages below and mitigating
  103. // a race condition.
  104. sh 'docker-compose build cypress-sqlite'
  105. sh 'docker-compose build cypress-mysql'
  106. sh 'docker-compose create cypress-sqlite'
  107. sh 'docker-compose create cypress-mysql'
  108. }
  109. }
  110. }
  111. }
  112. stage('Integration Tests') {
  113. parallel {
  114. stage('Sqlite') {
  115. steps {
  116. // Bring up a stack
  117. sh 'docker-compose up -d fullstack-sqlite'
  118. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  119. // Stop and Start it, as this will test it's ability to restart with existing data
  120. sh 'docker-compose stop fullstack-sqlite'
  121. sh 'docker-compose start fullstack-sqlite'
  122. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  123. // Run tests
  124. sh 'rm -rf test/results-sqlite'
  125. sh 'docker-compose up cypress-sqlite'
  126. // Get results
  127. sh 'docker cp -L "$(docker-compose ps --all -q cypress-sqlite):/test/results" test/results-sqlite'
  128. }
  129. post {
  130. always {
  131. // Dumps to analyze later
  132. sh 'mkdir -p debug/sqlite'
  133. sh 'docker-compose logs fullstack-sqlite > debug/sqlite/docker_fullstack_sqlite.log'
  134. // Cypress videos and screenshot artifacts
  135. dir(path: 'test/results-sqlite') {
  136. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  137. }
  138. junit 'test/results-sqlite/junit/*'
  139. }
  140. }
  141. }
  142. stage('Mysql') {
  143. steps {
  144. // Bring up a stack
  145. sh 'docker-compose up -d fullstack-mysql'
  146. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-mysql) 120'
  147. // Run tests
  148. sh 'rm -rf test/results-mysql'
  149. sh 'docker-compose up cypress-mysql'
  150. // Get results
  151. sh 'docker cp -L "$(docker-compose ps --all -q cypress-mysql):/test/results" test/results-mysql'
  152. }
  153. post {
  154. always {
  155. // Dumps to analyze later
  156. sh 'mkdir -p debug/mysql'
  157. sh 'docker-compose logs fullstack-mysql > debug/mysql/docker_fullstack_mysql.log'
  158. sh 'docker-compose logs db > debug/mysql/docker_db.log'
  159. // Cypress videos and screenshot artifacts
  160. dir(path: 'test/results-mysql') {
  161. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  162. }
  163. junit 'test/results-mysql/junit/*'
  164. }
  165. }
  166. }
  167. }
  168. }
  169. stage('MultiArch Build') {
  170. when {
  171. not {
  172. equals expected: 'UNSTABLE', actual: currentBuild.result
  173. }
  174. }
  175. steps {
  176. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  177. sh 'docker login -u "${duser}" -p "${dpass}"'
  178. sh "./scripts/buildx --push ${buildxPushTags}"
  179. }
  180. }
  181. }
  182. stage('Docs / Comment') {
  183. parallel {
  184. stage('Master Docs') {
  185. when {
  186. allOf {
  187. branch 'master'
  188. not {
  189. equals expected: 'UNSTABLE', actual: currentBuild.result
  190. }
  191. }
  192. }
  193. steps {
  194. npmDocsReleaseMaster()
  195. }
  196. }
  197. stage('Develop Docs') {
  198. when {
  199. allOf {
  200. branch 'develop'
  201. not {
  202. equals expected: 'UNSTABLE', actual: currentBuild.result
  203. }
  204. }
  205. }
  206. steps {
  207. npmDocsReleaseDevelop()
  208. }
  209. }
  210. stage('PR Comment') {
  211. when {
  212. allOf {
  213. changeRequest()
  214. not {
  215. equals expected: 'UNSTABLE', actual: currentBuild.result
  216. }
  217. }
  218. }
  219. steps {
  220. script {
  221. 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)
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. post {
  229. always {
  230. sh 'docker-compose down --remove-orphans --volumes -t 30'
  231. sh 'echo Reverting ownership'
  232. sh 'docker run --rm -v $(pwd):/data jc21/ci-tools chown -R $(id -u):$(id -g) /data'
  233. }
  234. success {
  235. juxtapose event: 'success'
  236. sh 'figlet "SUCCESS"'
  237. }
  238. failure {
  239. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  240. juxtapose event: 'failure'
  241. sh 'figlet "FAILURE"'
  242. }
  243. unstable {
  244. archiveArtifacts(artifacts: 'debug/**/*.*', allowEmptyArchive: true)
  245. juxtapose event: 'unstable'
  246. sh 'figlet "UNSTABLE"'
  247. }
  248. }
  249. }
  250. def getVersion() {
  251. ver = sh(script: 'cat .version', returnStdout: true)
  252. return ver.trim()
  253. }
  254. def getCommit() {
  255. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  256. return ver.trim()
  257. }