Jenkinsfile 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. sh 'docker pull nginxproxymanager/nginx-full:certbot-node'
  66. // See: https://github.com/yarnpkg/yarn/issues/3254
  67. sh '''docker run --rm \\
  68. -v "$(pwd)/backend:/app" \\
  69. -v "$(pwd)/global:/app/global" \\
  70. -w /app \\
  71. nginxproxymanager/nginx-full:certbot-node \\
  72. sh -c "yarn install && yarn eslint . && rm -rf node_modules"
  73. '''
  74. echo 'Docker Build ...'
  75. sh '''docker build --pull --no-cache --squash --compress \\
  76. -t "${IMAGE}:ci-${BUILD_NUMBER}" \\
  77. -f docker/Dockerfile \\
  78. --build-arg TARGETPLATFORM=linux/amd64 \\
  79. --build-arg BUILDPLATFORM=linux/amd64 \\
  80. --build-arg BUILD_VERSION="${BUILD_VERSION}" \\
  81. --build-arg BUILD_COMMIT="${BUILD_COMMIT}" \\
  82. --build-arg BUILD_DATE="$(date '+%Y-%m-%d %T %Z')" \\
  83. .
  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 | gzip > debug/docker_fullstack_sqlite.log.gz'
  103. sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
  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 | gzip > debug/docker_fullstack_mysql.log.gz'
  128. sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
  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. // Docker Login
  163. sh "docker login -u '${duser}' -p '${dpass}'"
  164. // Buildx with push from cache
  165. sh "./scripts/buildx --push ${BUILDX_PUSH_TAGS}"
  166. }
  167. }
  168. }
  169. stage('Docs Deploy') {
  170. when {
  171. allOf {
  172. branch 'master'
  173. not {
  174. equals expected: 'UNSTABLE', actual: currentBuild.result
  175. }
  176. }
  177. }
  178. steps {
  179. withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'npm-s3-docs', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
  180. sh """docker run --rm \\
  181. --name \${COMPOSE_PROJECT_NAME}-docs-upload \\
  182. -e S3_BUCKET=jc21-npm-site \\
  183. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  184. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  185. -v \$(pwd):/app \\
  186. -w /app \\
  187. jc21/ci-tools \\
  188. scripts/docs-upload /app/docs/.vuepress/dist/
  189. """
  190. sh """docker run --rm \\
  191. --name \${COMPOSE_PROJECT_NAME}-docs-invalidate \\
  192. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  193. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  194. jc21/ci-tools \\
  195. aws cloudfront create-invalidation --distribution-id EN1G6DEWZUTDT --paths '/*'
  196. """
  197. }
  198. }
  199. }
  200. stage('PR Comment') {
  201. when {
  202. allOf {
  203. changeRequest()
  204. not {
  205. equals expected: 'UNSTABLE', actual: currentBuild.result
  206. }
  207. }
  208. }
  209. steps {
  210. script {
  211. 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.")
  212. }
  213. }
  214. }
  215. }
  216. post {
  217. always {
  218. sh 'docker-compose down --rmi all --remove-orphans --volumes -t 30'
  219. sh 'echo Reverting ownership'
  220. sh 'docker run --rm -v $(pwd):/data jc21/ci-tools chown -R $(id -u):$(id -g) /data'
  221. }
  222. success {
  223. juxtapose event: 'success'
  224. sh 'figlet "SUCCESS"'
  225. }
  226. failure {
  227. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  228. juxtapose event: 'failure'
  229. sh 'figlet "FAILURE"'
  230. }
  231. unstable {
  232. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  233. juxtapose event: 'unstable'
  234. sh 'figlet "UNSTABLE"'
  235. }
  236. }
  237. }
  238. def getVersion() {
  239. ver = sh(script: 'cat .version', returnStdout: true)
  240. return ver.trim()
  241. }
  242. def getCommit() {
  243. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  244. return ver.trim()
  245. }