Jenkinsfile 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. pipeline {
  2. agent {
  3. label 'docker-multiarch'
  4. }
  5. options {
  6. buildDiscarder(logRotator(numToKeepStr: '5'))
  7. disableConcurrentBuilds()
  8. ansiColor('xterm')
  9. }
  10. environment {
  11. IMAGE = "nginx-proxy-manager"
  12. BUILD_VERSION = getVersion()
  13. MAJOR_VERSION = "2"
  14. BRANCH_LOWER = "${BRANCH_NAME.toLowerCase().replaceAll('/', '-')}"
  15. COMPOSE_PROJECT_NAME = "npm_${BRANCH_LOWER}_${BUILD_NUMBER}"
  16. COMPOSE_FILE = 'docker/docker-compose.ci.yml'
  17. COMPOSE_INTERACTIVE_NO_CLI = 1
  18. BUILDX_NAME = "${COMPOSE_PROJECT_NAME}"
  19. }
  20. stages {
  21. stage('Environment') {
  22. parallel {
  23. stage('Master') {
  24. when {
  25. branch 'master'
  26. }
  27. steps {
  28. script {
  29. env.BUILDX_PUSH_TAGS = "-t docker.io/jc21/${IMAGE}:${BUILD_VERSION} -t docker.io/jc21/${IMAGE}:${MAJOR_VERSION} -t docker.io/jc21/${IMAGE}:latest"
  30. }
  31. }
  32. }
  33. stage('Other') {
  34. when {
  35. not {
  36. branch 'master'
  37. }
  38. }
  39. steps {
  40. script {
  41. // Defaults to the Branch name, which is applies to all branches AND pr's
  42. env.BUILDX_PUSH_TAGS = "-t docker.io/jc21/${IMAGE}:github-${BRANCH_LOWER}"
  43. }
  44. }
  45. }
  46. stage('Versions') {
  47. steps {
  48. sh 'cat frontend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge frontend/package.json'
  49. sh 'echo -e "\\E[1;36mFrontend Version is:\\E[1;33m $(cat frontend/package.json | jq -r .version)\\E[0m"'
  50. sh 'cat backend/package.json | jq --arg BUILD_VERSION "${BUILD_VERSION}" \'.version = $BUILD_VERSION\' | sponge backend/package.json'
  51. sh 'echo -e "\\E[1;36mBackend Version is:\\E[1;33m $(cat backend/package.json | jq -r .version)\\E[0m"'
  52. sh 'sed -i -E "s/(version-)[0-9]+\\.[0-9]+\\.[0-9]+(-green)/\\1${BUILD_VERSION}\\2/" README.md'
  53. }
  54. }
  55. }
  56. }
  57. stage('Frontend') {
  58. steps {
  59. sh './scripts/frontend-build'
  60. }
  61. }
  62. stage('Backend') {
  63. steps {
  64. echo 'Checking Syntax ...'
  65. // See: https://github.com/yarnpkg/yarn/issues/3254
  66. sh '''docker run --rm \\
  67. -v "$(pwd)/backend:/app" \\
  68. -v "$(pwd)/global:/app/global" \\
  69. -w /app \\
  70. node:latest \\
  71. sh -c "yarn install && yarn eslint . && rm -rf node_modules"
  72. '''
  73. echo 'Docker Build ...'
  74. sh '''docker build --pull --no-cache --squash --compress \\
  75. -t "${IMAGE}:ci-${BUILD_NUMBER}" \\
  76. -f docker/Dockerfile \\
  77. --build-arg TARGETPLATFORM=linux/amd64 \\
  78. --build-arg BUILDPLATFORM=linux/amd64 \\
  79. --build-arg BUILD_VERSION="${BUILD_VERSION}" \\
  80. --build-arg BUILD_COMMIT="${BUILD_COMMIT}" \\
  81. --build-arg BUILD_DATE="$(date '+%Y-%m-%d %T %Z')" \\
  82. .
  83. '''
  84. }
  85. }
  86. stage('Integration Tests Sqlite') {
  87. steps {
  88. // Bring up a stack
  89. sh 'docker-compose up -d fullstack-sqlite'
  90. sh './scripts/wait-healthy $(docker-compose ps -q fullstack-sqlite) 120'
  91. // Run tests
  92. sh 'rm -rf test/results'
  93. sh 'docker-compose up cypress-sqlite'
  94. // Get results
  95. sh 'docker cp -L "$(docker-compose ps -q cypress-sqlite):/test/results" test/'
  96. }
  97. post {
  98. always {
  99. // Dumps to analyze later
  100. sh 'mkdir -p debug'
  101. sh 'docker-compose logs fullstack-sqlite | gzip > debug/docker_fullstack_sqlite.log.gz'
  102. sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
  103. // Cypress videos and screenshot artifacts
  104. dir(path: 'test/results') {
  105. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  106. }
  107. junit 'test/results/junit/*'
  108. }
  109. }
  110. }
  111. stage('Integration Tests Mysql') {
  112. steps {
  113. // Bring up a stack
  114. sh 'docker-compose up -d fullstack-mysql'
  115. sh './scripts/wait-healthy $(docker-compose ps -q fullstack-mysql) 120'
  116. // Run tests
  117. sh 'rm -rf test/results'
  118. sh 'docker-compose up cypress-mysql'
  119. // Get results
  120. sh 'docker cp -L "$(docker-compose ps -q cypress-mysql):/test/results" test/'
  121. }
  122. post {
  123. always {
  124. // Dumps to analyze later
  125. sh 'mkdir -p debug'
  126. sh 'docker-compose logs fullstack-mysql | gzip > debug/docker_fullstack_mysql.log.gz'
  127. sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
  128. // Cypress videos and screenshot artifacts
  129. dir(path: 'test/results') {
  130. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  131. }
  132. junit 'test/results/junit/*'
  133. }
  134. }
  135. }
  136. stage('Docs') {
  137. when {
  138. not {
  139. equals expected: 'UNSTABLE', actual: currentBuild.result
  140. }
  141. }
  142. steps {
  143. dir(path: 'docs') {
  144. sh 'yarn install'
  145. sh 'yarn build'
  146. }
  147. dir(path: 'docs/.vuepress/dist') {
  148. sh 'tar -czf ../../docs.tgz *'
  149. }
  150. archiveArtifacts(artifacts: 'docs/docs.tgz', allowEmptyArchive: false)
  151. }
  152. }
  153. stage('MultiArch Build') {
  154. when {
  155. not {
  156. equals expected: 'UNSTABLE', actual: currentBuild.result
  157. }
  158. }
  159. steps {
  160. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  161. // Docker Login
  162. sh "docker login -u '${duser}' -p '${dpass}'"
  163. // Buildx with push from cache
  164. sh "./scripts/buildx --push ${BUILDX_PUSH_TAGS}"
  165. }
  166. }
  167. }
  168. stage('Docs Deploy') {
  169. when {
  170. allOf {
  171. branch 'master'
  172. not {
  173. equals expected: 'UNSTABLE', actual: currentBuild.result
  174. }
  175. }
  176. }
  177. steps {
  178. withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'npm-s3-docs', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
  179. sh """docker run --rm \\
  180. --name \${COMPOSE_PROJECT_NAME}-docs-upload \\
  181. -e S3_BUCKET=jc21-npm-site \\
  182. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  183. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  184. -v \$(pwd):/app \\
  185. -w /app \\
  186. jc21/ci-tools \\
  187. scripts/docs-upload /app/docs/.vuepress/dist/
  188. """
  189. sh """docker run --rm \\
  190. --name \${COMPOSE_PROJECT_NAME}-docs-invalidate \\
  191. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  192. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  193. jc21/ci-tools \\
  194. aws cloudfront create-invalidation --distribution-id EN1G6DEWZUTDT --paths '/*'
  195. """
  196. }
  197. }
  198. }
  199. stage('PR Comment') {
  200. when {
  201. allOf {
  202. changeRequest()
  203. not {
  204. equals expected: 'UNSTABLE', actual: currentBuild.result
  205. }
  206. }
  207. }
  208. steps {
  209. script {
  210. def comment = pullRequest.comment("This is an automated message from CI:\n\nDocker 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.")
  211. }
  212. }
  213. }
  214. }
  215. post {
  216. always {
  217. sh 'docker-compose down --rmi all --remove-orphans --volumes -t 30'
  218. sh 'echo Reverting ownership'
  219. sh 'docker run --rm -v $(pwd):/data jc21/ci-tools chown -R $(id -u):$(id -g) /data'
  220. }
  221. success {
  222. juxtapose event: 'success'
  223. sh 'figlet "SUCCESS"'
  224. }
  225. failure {
  226. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  227. juxtapose event: 'failure'
  228. sh 'figlet "FAILURE"'
  229. }
  230. unstable {
  231. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  232. juxtapose event: 'unstable'
  233. sh 'figlet "UNSTABLE"'
  234. }
  235. }
  236. }
  237. def getVersion() {
  238. ver = sh(script: 'cat .version', returnStdout: true)
  239. return ver.trim()
  240. }
  241. def getCommit() {
  242. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  243. return ver.trim()
  244. }