Jenkinsfile 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. -w /app \\
  69. node:latest \\
  70. sh -c "yarn install && yarn eslint . && rm -rf node_modules"
  71. '''
  72. echo 'Docker Build ...'
  73. sh '''docker build --pull --no-cache --squash --compress \\
  74. -t "${IMAGE}:ci-${BUILD_NUMBER}" \\
  75. -f docker/Dockerfile \\
  76. --build-arg TARGETPLATFORM=linux/amd64 \\
  77. --build-arg BUILDPLATFORM=linux/amd64 \\
  78. --build-arg BUILD_VERSION="${BUILD_VERSION}" \\
  79. --build-arg BUILD_COMMIT="${BUILD_COMMIT}" \\
  80. --build-arg BUILD_DATE="$(date '+%Y-%m-%d %T %Z')" \\
  81. .
  82. '''
  83. }
  84. }
  85. stage('Test') {
  86. steps {
  87. // Bring up a stack
  88. sh 'docker-compose up -d fullstack'
  89. sh './scripts/wait-healthy $(docker-compose ps -q fullstack) 120'
  90. // Run tests
  91. sh 'rm -rf test/results'
  92. sh 'docker-compose up cypress'
  93. // Get results
  94. sh 'docker cp -L "$(docker-compose ps -q cypress):/results" test/'
  95. }
  96. post {
  97. always {
  98. // Dumps to analyze later
  99. sh 'mkdir -p debug'
  100. sh 'docker-compose logs fullstack | gzip > debug/docker_fullstack.log.gz'
  101. sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
  102. // Cypress videos and screenshot artifacts
  103. dir(path: 'test/results') {
  104. archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
  105. }
  106. junit 'test/results/junit/*'
  107. }
  108. }
  109. }
  110. stage('Docs') {
  111. when {
  112. not {
  113. equals expected: 'UNSTABLE', actual: currentBuild.result
  114. }
  115. }
  116. steps {
  117. dir(path: 'docs') {
  118. sh 'yarn install'
  119. sh 'yarn build'
  120. }
  121. dir(path: 'docs/.vuepress/dist') {
  122. sh 'tar -czf ../../docs.tgz *'
  123. }
  124. archiveArtifacts(artifacts: 'docs/docs.tgz', allowEmptyArchive: false)
  125. }
  126. }
  127. stage('MultiArch Build') {
  128. when {
  129. not {
  130. equals expected: 'UNSTABLE', actual: currentBuild.result
  131. }
  132. }
  133. steps {
  134. withCredentials([usernamePassword(credentialsId: 'jc21-dockerhub', passwordVariable: 'dpass', usernameVariable: 'duser')]) {
  135. // Docker Login
  136. sh "docker login -u '${duser}' -p '${dpass}'"
  137. // Buildx with push from cache
  138. sh "./scripts/buildx --push ${BUILDX_PUSH_TAGS}"
  139. }
  140. }
  141. }
  142. stage('Docs Deploy') {
  143. when {
  144. allOf {
  145. branch 'master'
  146. not {
  147. equals expected: 'UNSTABLE', actual: currentBuild.result
  148. }
  149. }
  150. }
  151. steps {
  152. withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'npm-s3-docs', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
  153. sh """docker run --rm \\
  154. --name \${COMPOSE_PROJECT_NAME}-docs-upload \\
  155. -e S3_BUCKET=jc21-npm-site \\
  156. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  157. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  158. -v \$(pwd):/app \\
  159. -w /app \\
  160. jc21/ci-tools \\
  161. scripts/docs-upload /app/docs/.vuepress/dist/
  162. """
  163. sh """docker run --rm \\
  164. --name \${COMPOSE_PROJECT_NAME}-docs-invalidate \\
  165. -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \\
  166. -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \\
  167. jc21/ci-tools \\
  168. aws cloudfront create-invalidation --distribution-id EN1G6DEWZUTDT --paths '/*'
  169. """
  170. }
  171. }
  172. }
  173. stage('PR Comment') {
  174. when {
  175. allOf {
  176. changeRequest()
  177. not {
  178. equals expected: 'UNSTABLE', actual: currentBuild.result
  179. }
  180. }
  181. }
  182. steps {
  183. script {
  184. 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}`")
  185. }
  186. }
  187. }
  188. }
  189. post {
  190. always {
  191. sh 'docker-compose down --rmi all --remove-orphans --volumes -t 30'
  192. sh 'echo Reverting ownership'
  193. sh 'docker run --rm -v $(pwd):/data ${DOCKER_CI_TOOLS} chown -R $(id -u):$(id -g) /data'
  194. }
  195. success {
  196. juxtapose event: 'success'
  197. sh 'figlet "SUCCESS"'
  198. }
  199. failure {
  200. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  201. juxtapose event: 'failure'
  202. sh 'figlet "FAILURE"'
  203. }
  204. unstable {
  205. archiveArtifacts(artifacts: 'debug/**.*', allowEmptyArchive: true)
  206. juxtapose event: 'unstable'
  207. sh 'figlet "UNSTABLE"'
  208. }
  209. }
  210. }
  211. def getVersion() {
  212. ver = sh(script: 'cat .version', returnStdout: true)
  213. return ver.trim()
  214. }
  215. def getCommit() {
  216. ver = sh(script: 'git log -n 1 --format=%h', returnStdout: true)
  217. return ver.trim()
  218. }