Jenkinsfile 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 set version berry'
  91. sh 'yarn install'
  92. sh 'yarn build'
  93. }
  94. dir(path: 'docs/.vuepress/dist') {
  95. sh 'tar -czf ../../docs.tgz *'
  96. }
  97. archiveArtifacts(artifacts: 'docs/docs.tgz', allowEmptyArchive: false)
  98. }
  99. }
  100. stage('Cypress') {
  101. steps {
  102. // Creating will also create the network prior to
  103. // using it in parallel stages below and mitigating
  104. // a race condition.
  105. sh 'docker-compose build cypress-sqlite'
  106. sh 'docker-compose build cypress-mysql'
  107. sh 'docker-compose create cypress-sqlite'
  108. sh 'docker-compose create cypress-mysql'
  109. }
  110. }
  111. }
  112. }
  113. stage('Integration Tests') {
  114. parallel {
  115. stage('Sqlite') {
  116. steps {
  117. // Bring up a stack
  118. sh 'docker-compose up -d fullstack-sqlite'
  119. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  120. // Stop and Start it, as this will test it's ability to restart with existing data
  121. sh 'docker-compose stop fullstack-sqlite'
  122. sh 'docker-compose start fullstack-sqlite'
  123. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  124. // Run tests
  125. sh 'rm -rf test/results-sqlite'
  126. sh 'docker-compose up cypress-sqlite'
  127. // Get results
  128. sh 'docker cp -L "$(docker-compose ps --all -q cypress-sqlite):/test/results" test/results-sqlite'
  129. }
  130. post {
  131. always {
  132. // Dumps to analyze later
  133. sh 'mkdir -p debug/sqlite'
  134. sh 'docker-compose logs fullstack-sqlite > debug/sqlite/docker_fullstack_sqlite.log'
  135. // Cypress videos and screenshot artifacts
  136. dir(path: 'test/results-sqlite') {
  137. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  138. }
  139. junit 'test/results-sqlite/junit/*'
  140. }
  141. }
  142. junit 'test/results/junit/*'
  143. }
  144. stage('Mysql') {
  145. steps {
  146. // Bring up a stack
  147. sh 'docker-compose up -d fullstack-mysql'
  148. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-mysql) 120'
  149. // Run tests
  150. sh 'rm -rf test/results-mysql'
  151. sh 'docker-compose up cypress-mysql'
  152. // Get results
  153. sh 'docker cp -L "$(docker-compose ps --all -q cypress-mysql):/test/results" test/results-mysql'
  154. }
  155. post {
  156. always {
  157. // Dumps to analyze later
  158. sh 'mkdir -p debug/mysql'
  159. sh 'docker-compose logs fullstack-mysql > debug/mysql/docker_fullstack_mysql.log'
  160. sh 'docker-compose logs db > debug/mysql/docker_db.log'
  161. // Cypress videos and screenshot artifacts
  162. dir(path: 'test/results-mysql') {
  163. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  164. }
  165. junit 'test/results-mysql/junit/*'
  166. }
  167. }
  168. }
  169. }
  170. }
  171. stage('MultiArch Build') {
  172. when {
  173. not {
  174. equals expected: 'UNSTABLE', actual: currentBuild.result
  175. }
  176. }
  177. steps {
  178. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  179. sh 'docker login -u "${duser}" -p "${dpass}"'
  180. sh "./scripts/buildx --push ${buildxPushTags}"
  181. }
  182. }
  183. }
  184. stage('Docs / Comment') {
  185. parallel {
  186. stage('Master Docs') {
  187. when {
  188. allOf {
  189. branch 'master'
  190. not {
  191. equals expected: 'UNSTABLE', actual: currentBuild.result
  192. }
  193. }
  194. }
  195. steps {
  196. npmDocsReleaseMaster()
  197. }
  198. }
  199. stage('Develop Docs') {
  200. when {
  201. allOf {
  202. branch 'develop'
  203. not {
  204. equals expected: 'UNSTABLE', actual: currentBuild.result
  205. }
  206. }
  207. }
  208. steps {
  209. npmDocsReleaseDevelop()
  210. }
  211. }
  212. stage('PR Comment') {
  213. when {
  214. allOf {
  215. changeRequest()
  216. not {
  217. equals expected: 'UNSTABLE', actual: currentBuild.result
  218. }
  219. }
  220. }
  221. steps {
  222. script {
  223. 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)
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. post {
  231. always {
  232. sh 'docker-compose down --remove-orphans --volumes -t 30'
  233. sh 'echo Reverting ownership'
  234. sh 'docker run --rm -v $(pwd):/data jc21/ci-tools chown -R $(id -u):$(id -g) /data'
  235. }
  236. success {
  237. juxtapose event: 'success'
  238. sh 'figlet "SUCCESS"'
  239. }
  240. failure {
  241. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  242. juxtapose event: 'failure'
  243. sh 'figlet "FAILURE"'
  244. }
  245. unstable {
  246. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  247. juxtapose event: 'unstable'
  248. sh 'figlet "UNSTABLE"'
  249. }
  250. }
  251. }
  252. def getVersion() {
  253. ver = sh(script: 'cat .version', returnStdout: true)
  254. return ver.trim()
  255. }
  256. def getCommit() {
  257. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  258. return ver.trim()
  259. }