Jenkinsfile 7.0 KB

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