Jenkinsfile 6.5 KB

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