Jenkinsfile 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. DOCS_BUCKET = 'jc21-npm-site'
  24. DOCS_CDN = 'EN1G6DEWZUTDT'
  25. }
  26. stages {
  27. stage('Environment') {
  28. parallel {
  29. stage('Master') {
  30. when {
  31. branch 'master'
  32. }
  33. steps {
  34. script {
  35. buildxPushTags = "-t docker.io/jc21/${IMAGE}:${BUILD_VERSION} -t docker.io/jc21/${IMAGE}:${MAJOR_VERSION} -t docker.io/jc21/${IMAGE}:latest"
  36. }
  37. }
  38. }
  39. stage('Other') {
  40. when {
  41. not {
  42. branch 'master'
  43. }
  44. }
  45. steps {
  46. script {
  47. // Defaults to the Branch name, which is applies to all branches AND pr's
  48. buildxPushTags = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}"
  49. }
  50. }
  51. }
  52. stage('Versions') {
  53. steps {
  54. sh 'cat frontend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge frontend/package.json'
  55. sh 'echo -e "\\E[1;36mFrontend Version is:\\E[1;33m $(cat frontend/package.json | jq -r .version)\\E[0m"'
  56. sh 'cat backend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge backend/package.json'
  57. sh 'echo -e "\\E[1;36mBackend Version is:\\E[1;33m $(cat backend/package.json | jq -r .version)\\E[0m"'
  58. sh 'sed -i -E "s/(version-)[0-9]+\\.[0-9]+\\.[0-9]+(-green)/\\1${BUILD_VERSION}\\2/" README.md'
  59. }
  60. }
  61. }
  62. }
  63. stage('Build and Test') {
  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('Integration Tests Sqlite') {
  88. steps {
  89. // Bring up a stack
  90. sh 'docker-compose up -d fullstack-sqlite'
  91. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  92. // Stop and Start it, as this will test it's ability to restart with existing data
  93. sh 'docker-compose stop fullstack-sqlite'
  94. sh 'docker-compose start fullstack-sqlite'
  95. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-sqlite) 120'
  96. // Run tests
  97. sh 'rm -rf test/results'
  98. sh 'docker-compose up cypress-sqlite'
  99. // Get results
  100. sh 'docker cp -L "$(docker-compose ps --all -q cypress-sqlite):/test/results" test/'
  101. }
  102. post {
  103. always {
  104. // Dumps to analyze later
  105. sh 'mkdir -p debug'
  106. sh 'docker-compose logs fullstack-sqlite > debug/docker_fullstack_sqlite.log'
  107. sh 'docker-compose logs db > debug/docker_db.log'
  108. // Cypress videos and screenshot artifacts
  109. dir(path: 'test/results') {
  110. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  111. }
  112. junit 'test/results/junit/*'
  113. }
  114. }
  115. }
  116. stage('Integration Tests Mysql') {
  117. steps {
  118. // Bring up a stack
  119. sh 'docker-compose up -d fullstack-mysql'
  120. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-mysql) 120'
  121. // Run tests
  122. sh 'rm -rf test/results'
  123. sh 'docker-compose up cypress-mysql'
  124. // Get results
  125. sh 'docker cp -L "$(docker-compose ps --all -q cypress-mysql):/test/results" test/'
  126. }
  127. post {
  128. always {
  129. // Dumps to analyze later
  130. sh 'mkdir -p debug'
  131. sh 'docker-compose logs fullstack-mysql > debug/docker_fullstack_mysql.log'
  132. sh 'docker-compose logs db > debug/docker_db.log'
  133. // Cypress videos and screenshot artifacts
  134. dir(path: 'test/results') {
  135. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  136. }
  137. junit 'test/results/junit/*'
  138. }
  139. }
  140. }
  141. stage('Docs') {
  142. when {
  143. not {
  144. equals expected: 'UNSTABLE', actual: currentBuild.result
  145. }
  146. }
  147. steps {
  148. dir(path: 'docs') {
  149. sh 'yarn install'
  150. sh 'yarn build'
  151. }
  152. dir(path: 'docs/.vuepress/dist') {
  153. sh 'tar -czf ../../docs.tgz *'
  154. }
  155. archiveArtifacts(artifacts: 'docs/docs.tgz', allowEmptyArchive: false)
  156. }
  157. }
  158. stage('MultiArch Build') {
  159. when {
  160. not {
  161. equals expected: 'UNSTABLE', actual: currentBuild.result
  162. }
  163. }
  164. steps {
  165. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  166. sh 'docker login -u "${duser}" -p "${dpass}"'
  167. sh "./scripts/buildx --push ${buildxPushTags}"
  168. }
  169. }
  170. }
  171. stage('Docs Deploy') {
  172. when {
  173. allOf {
  174. branch 'master'
  175. not {
  176. equals expected: 'UNSTABLE', actual: currentBuild.result
  177. }
  178. }
  179. }
  180. steps {
  181. npmDocsRelease("$DOCS_BUCKET", "$DOCS_CDN")
  182. }
  183. }
  184. stage('PR Comment') {
  185. when {
  186. allOf {
  187. changeRequest()
  188. not {
  189. equals expected: 'UNSTABLE', actual: currentBuild.result
  190. }
  191. }
  192. }
  193. steps {
  194. script {
  195. 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)
  196. }
  197. }
  198. }
  199. }
  200. post {
  201. always {
  202. sh 'docker-compose down --remove-orphans --volumes -t 30'
  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. }