From a42f27e14a7ce04627e3e0f5eaf19e447efdb54f Mon Sep 17 00:00:00 2001 From: Giorgos Logiotatidis Date: Thu, 7 Jun 2018 16:56:22 +0300 Subject: [PATCH] [Issue #2967] Update Jenkinsfile. (#3245) - Build images - Run mocha and unit tests - Push images to docker hub. --- .circleci/config.yml | 7 -- Jenkinsfile | 135 ++++++++++++++++++------------ docker/bin/build-docker-images.sh | 1 - 3 files changed, 81 insertions(+), 62 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 284e094c3..74285c62c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -13,9 +13,6 @@ jobs: - run: name: Build docker images command: GIT_SHA=${CIRCLE_SHA1:0:7} LOCALE_ENV=production ./docker/bin/build-docker-images.sh - - run: - name: Push docker images - command: GIT_BRANCH=${CIRCLE_BRANCH} GIT_SHA=${CIRCLE_SHA1:0:7} ./docker/bin/push-docker-images.sh - run: name: Run flake8 command: docker run kitsune:full-no-locales-latest flake8 kitsune @@ -31,7 +28,3 @@ jobs: # Replace with urlwait or takis sleep 10s; docker-compose -f docker-compose.yml -f docker/composefiles/test.yml run web ./bin/run-unit-tests.sh - # Re-run push images to push the '-latest' tags if available. - - run: - name: Push images - command: GIT_BRANCH=${CIRCLE_BRANCH} GIT_SHA=${CIRCLE_SHA1:0:7} ./docker/bin/push-docker-images.sh diff --git a/Jenkinsfile b/Jenkinsfile index cfba18cb7..501f9a605 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,62 +1,89 @@ -@Library('github.com/mozmeao/jenkins-pipeline@20171123.1') +@Library('github.com/mozmeao/jenkins-pipeline@20170315.1') def config def docker_image +def dc_name conduit { - node { - stage("Prepare") { - checkout scm - setGitEnvironmentVariables() + node { + stage("Prepare") { + checkout scm + setGitEnvironmentVariables() - try { - config = readYaml file: "jenkins.yml" - } - catch (e) { - config = [] - } - println "config ==> ${config}" + try { + config = readYaml file: "jenkins.yml" + } + catch (e) { + config = [] + } + println "config ==> ${config}" + + if (!config || (config && config.pipeline && config.pipeline.enabled == false)) { + println "Pipeline disabled." + } + } + + docker_image = "${config.project.docker_name}:full-${GIT_COMMIT_SHORT}" + + stage("Build") { + if (!dockerImageExists(docker_image)) { + sh "GIT_SHA=${GIT_COMMIT_SHORT} LOCALE_ENV=production ./docker/bin/build-docker-images.sh" + } + else { + echo "Image ${docker_image} already exists." + } + } + + stage("Upload Images") { + dockerImagePush("${config.project.docker_name}:full-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + dockerImagePush("${config.project.docker_name}:full-no-locales-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + dockerImagePush("${config.project.docker_name}:locales-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + dockerImagePush("${config.project.docker_name}:staticfiles-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + dockerImagePush("${config.project.docker_name}:base-dev-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + dockerImagePush("${config.project.docker_name}:base-${GIT_COMMIT_SHORT}", "mozjenkins-docker-hub") + } + + stage("Run Flake8") { + sh "docker run ${config.project.docker_name}:full-no-locales-${GIT_COMMIT_SHORT} flake8 kitsune" + } + stage("Run Mocha Tests") { + sh "docker run kitsune:staticfiles-latest ./node_modules/.bin/mocha --compilers js:babel/register --recursive kitsune/*/static/*/js/tests/* \$@" + } + stage("Run Unit Tests") { + try { + dc_name = "${config.project.name}-${BUILD_NUMBER}-${GIT_COMMIT_SHORT}" + sh "docker-compose --project-name ${dc_name} up -d mariadb" + sh "docker-compose --project-name ${dc_name} up -d elasticsearch" + sh "docker-compose --project-name ${dc_name} up -d redis" + // Replace with urlwait or takis + sh "sleep 10s;" + sh "docker-compose --project-name ${dc_name} -f docker-compose.yml -f docker/composefiles/test.yml run web ./bin/run-unit-tests.sh" + } + finally { + sh "docker-compose --project-name ${dc_name} -f docker-compose.yml -f docker/composefiles/test.yml kill" + } + } + stage("Upload Latest Images") { + // When on master branch tag and push push the latest tag + onBranch("master") { + dockerImageTag("${config.project.docker_name}:full-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:full-latest") + dockerImagePush("${config.project.docker_name}:full-latest", "mozjenkins-docker-hub") + + dockerImageTag("${config.project.docker_name}:full-no-locales-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:full-no-locales-latest") + dockerImagePush("${config.project.docker_name}:full-no-locales-latest", "mozjenkins-docker-hub") + + dockerImagePush("${config.project.docker_name}:locales-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:locales-latest") + dockerImagePush("${config.project.docker_name}:locales-latest", "mozjenkins-docker-hub") + + dockerImagePush("${config.project.docker_name}:staticfiles-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:staticfiles-latest") + dockerImagePush("${config.project.docker_name}:staticfiles-latest", "mozjenkins-docker-hub") + + dockerImagePush("${config.project.docker_name}:base-dev-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:base-dev-latest") + dockerImagePush("${config.project.docker_name}:base-dev-latest", "mozjenkins-docker-hub") + + dockerImagePush("${config.project.docker_name}:base-${GIT_COMMIT_SHORT}", "${config.project.docker_name}:base-latest") + dockerImagePush("${config.project.docker_name}:base-latest", "mozjenkins-docker-hub") + } + } - if (!config || (config && config.pipeline && config.pipeline.enabled == false)) { - println "Pipeline disabled." - } } - - docker_image = "${config.project.docker_name}:${GIT_COMMIT_SHORT}" - - stage("Build") { - if (!dockerImageExists(docker_image)) { - sh "echo 'ENV GIT_SHA ${GIT_COMMIT}' >> Dockerfile" - - // Docker always copies data as root. Chowning files to root before - // the build command will prevent docker cache invalidation due to - // different file metadata. When we upgrade to a newer docker - // version we should probably use --chown flag for COPY. See - // https://github.com/moby/moby/pull/34263 - dockerRun('debian', [ - "docker_args": "-v `pwd`:/app", - "cmd": "cd /app && chown -c root.root -R requirements bower.json package.json && chmod -c a+rw -R requirements bower.json package.json", - "bash_wrap": true - ]) - dockerImagePull( "${config.project.docker_name}:latest") - dockerImageBuild(docker_image, [ - "pull": true, - "cache_from": "${config.project.docker_name}:latest", - ]) - } - else { - echo "Image ${docker_image} already exists." - } - } - - stage("Upload Images") { - dockerImagePush(docker_image, "mozjenkins-docker-hub") - onBranch("master") { - dockerImageTag(docker_image, "${config.project.docker_name}:latest") - dockerImagePush("${config.project.docker_name}:latest", "mozjenkins-docker-hub") - } - } - } - - milestone() - } diff --git a/docker/bin/build-docker-images.sh b/docker/bin/build-docker-images.sh index e5fad7d11..1dd8b3e49 100755 --- a/docker/bin/build-docker-images.sh +++ b/docker/bin/build-docker-images.sh @@ -15,7 +15,6 @@ for image in base base-dev staticfiles locales full-no-locales full; do docker build -t kitsune:${image}-latest \ -t ${DOCKER_REPO}:${image}-${GIT_SHA} \ - -t ${DOCKER_REPO}:${image}-latest \ --cache-from ${DOCKER_REPO}:${image}-latest \ --cache-from kitsune:${image}-latest \ -f docker/dockerfiles/${image} \