Jenkinsfile 6.5 KB

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