Jenkinsfile 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. junit 'test/results/junit/*'
  142. }
  143. }
  144. }
  145. stage('Docs') {
  146. when {
  147. not {
  148. equals expected: 'UNSTABLE', actual: currentBuild.result
  149. }
  150. }
  151. steps {
  152. dir(path: 'docs') {
  153. sh 'yarn set version berry'
  154. sh 'yarn install'
  155. sh 'yarn build'
  156. }
  157. stage('Mysql') {
  158. steps {
  159. // Bring up a stack
  160. sh 'docker-compose up -d fullstack-mysql'
  161. sh './scripts/wait-healthy $(docker-compose ps --all -q fullstack-mysql) 120'
  162. // Run tests
  163. sh 'rm -rf test/results-mysql'
  164. sh 'docker-compose up cypress-mysql'
  165. // Get results
  166. sh 'docker cp -L "$(docker-compose ps --all -q cypress-mysql):/test/results" test/results-mysql'
  167. }
  168. post {
  169. always {
  170. // Dumps to analyze later
  171. sh 'mkdir -p debug/mysql'
  172. sh 'docker-compose logs fullstack-mysql > debug/mysql/docker_fullstack_mysql.log'
  173. sh 'docker-compose logs db > debug/mysql/docker_db.log'
  174. // Cypress videos and screenshot artifacts
  175. dir(path: 'test/results-mysql') {
  176. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  177. }
  178. junit 'test/results-mysql/junit/*'
  179. }
  180. }
  181. }
  182. }
  183. }
  184. stage('MultiArch Build') {
  185. when {
  186. not {
  187. equals expected: 'UNSTABLE', actual: currentBuild.result
  188. }
  189. }
  190. steps {
  191. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  192. sh 'docker login -u "${duser}" -p "${dpass}"'
  193. sh "./scripts/buildx --push ${buildxPushTags}"
  194. }
  195. }
  196. }
  197. stage('Docs / Comment') {
  198. parallel {
  199. stage('Master Docs') {
  200. when {
  201. allOf {
  202. branch 'master'
  203. not {
  204. equals expected: 'UNSTABLE', actual: currentBuild.result
  205. }
  206. }
  207. }
  208. steps {
  209. npmDocsReleaseMaster()
  210. }
  211. }
  212. stage('Develop Docs') {
  213. when {
  214. allOf {
  215. branch 'develop'
  216. not {
  217. equals expected: 'UNSTABLE', actual: currentBuild.result
  218. }
  219. }
  220. }
  221. steps {
  222. npmDocsReleaseDevelop()
  223. }
  224. }
  225. stage('PR Comment') {
  226. when {
  227. allOf {
  228. changeRequest()
  229. not {
  230. equals expected: 'UNSTABLE', actual: currentBuild.result
  231. }
  232. }
  233. }
  234. steps {
  235. script {
  236. 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)
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. post {
  244. always {
  245. sh 'docker-compose down --remove-orphans --volumes -t 30'
  246. sh 'echo Reverting ownership'
  247. sh 'docker run --rm -v $(pwd):/data jc21/ci-tools chown -R $(id -u):$(id -g) /data'
  248. }
  249. success {
  250. juxtapose event: 'success'
  251. sh 'figlet "SUCCESS"'
  252. }
  253. failure {
  254. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  255. juxtapose event: 'failure'
  256. sh 'figlet "FAILURE"'
  257. }
  258. unstable {
  259. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  260. juxtapose event: 'unstable'
  261. sh 'figlet "UNSTABLE"'
  262. }
  263. }
  264. }
  265. def getVersion() {
  266. ver = sh(script: 'cat .version', returnStdout: true)
  267. return ver.trim()
  268. }
  269. def getCommit() {
  270. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  271. return ver.trim()
  272. }