From d5a9a62f73b177443a67e9bf55d5e562ffac511a Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Thu, 19 Dec 2019 14:27:29 +0100 Subject: [PATCH] Deploy with docker swarm --- Jenkinsfile | 62 ++++++++++++++-------------------------------- docker-compose.yml | 4 +++ gulpfile.js | 22 +++++++++++----- 3 files changed, 38 insertions(+), 50 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 0aaba1e93..b888e57bc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,19 +8,17 @@ pipeline { environment { PROJECT_NAME = 'salix' REGISTRY = 'registry.verdnatura.es' - DOCKER_HOST_1 = 'vch1.verdnatura.es' - DOCKER_HOST_2 = 'vch2.verdnatura.es' PORT_MASTER_FRONT = '5002' - PORT_MASTER_BACK = '3001-3002' + PORT_MASTER_BACK = '3001' PORT_TEST_FRONT = '5001' - PORT_TEST_BACK = '4001-4002' + PORT_TEST_BACK = '4001' TAG = "${env.BRANCH_NAME}" } stages { stage('Checkout') { steps { script { - env.COMPOSE_PROJECT_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" + env.STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" if (!env.GIT_COMMITTER_EMAIL) { env.COMMITTER_EMAIL = sh( @@ -65,18 +63,21 @@ pipeline { } } stage('Test') { - when { not { anyOf { - branch 'test' - branch 'master' - }}} environment { NODE_ENV = "" - FIREFOX_BIN = "/opt/firefox/firefox-bin" } - steps { - nodejs('node-lts') { - sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=1' - sh 'gulp backTestDockerOnce --junit --random' + parallel { + steps { + stage('Frontend') { + nodejs('node-lts') { + sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=1' + } + } + stage('Backend') { + nodejs('node-lts') { + sh 'gulp backTestDockerOnce --junit --random' + } + } } } } @@ -96,6 +97,7 @@ pipeline { sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY' sh 'docker-compose build --parallel' sh 'docker-compose push' + sh 'docker logout $REGISTRY' } } stage('Deploy') { @@ -107,27 +109,8 @@ pipeline { DOCKER_TLS_VERIFY = 1 } parallel { - stage('Host 1') { - environment { - DOCKER_HOST = "tcp://${env.DOCKER_HOST_1}:2376" - } - steps { - withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) { - sh 'docker-compose pull' - sh 'docker-compose up -d --scale back=2' - } - } - } - stage('Host 2') { - environment { - DOCKER_HOST = "tcp://${env.DOCKER_HOST_2}:2376" - } - steps { - withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) { - sh 'docker-compose pull' - sh 'docker-compose up -d --scale back=2' - } - } + steps { + sh "docker stack deploy --compose-file docker-compose.yml ${env.STACK_NAME}" } } } @@ -147,15 +130,6 @@ pipeline { sh 'db/import-changes.sh -f $NODE_ENV' } } - stage('Cleanup') { - when { anyOf { - branch 'test' - branch 'master' - }} - steps { - sh 'docker logout $REGISTRY' - } - } } post { always { diff --git a/docker-compose.yml b/docker-compose.yml index c0f02273e..b04ecd0fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,8 @@ services: - ${PORT_FRONT}:80 links: - back + deploy: + replicas: 3 back: image: registry.verdnatura.es/salix-back:${TAG} restart: unless-stopped @@ -22,3 +24,5 @@ services: - /containers/salix:/etc/salix - /mnt/storage/pdfs:/var/lib/salix/pdfs - /mnt/storage/dms:/var/lib/salix/dms + deploy: + replicas: 6 diff --git a/gulpfile.js b/gulpfile.js index 06c65e451..dcb0aa6f1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -110,17 +110,27 @@ backTestOnce.description = `Runs the backend tests once, can receive --junit arg async function backTestDockerOnce() { let containerId = await docker(); - await backTestOnce(); - if (argv['random']) - await execP(`docker rm -fv ${containerId}`); + try { + await backTestOnce(); + } catch (e) { + throw e; + } finally { + if (argv['random']) + await execP(`docker rm -fv ${containerId}`); + } } backTestDockerOnce.description = `Runs backend tests using in site container once`; async function backTestDocker() { let containerId = await docker(); - await backTest(); - if (argv['random']) - await execP(`docker rm -fv ${containerId}`); + try { + await backTest(); + } catch (e) { + throw e; + } finally { + if (argv['random']) + await execP(`docker rm -fv ${containerId}`); + } } backTestDocker.description = `Runs backend tests restoring fixtures first`;