This commit is contained in:
Carlos Jimenez 2019-01-11 18:00:46 +01:00
commit 70029cfc08
2 changed files with 102 additions and 74 deletions

122
Jenkinsfile vendored
View File

@ -1,8 +1,31 @@
#!/usr/bin/env groovy #!/usr/bin/env groovy
def buildError pipeline {
agent any
environment {
REGISTRY = 'registry.verdnatura.es'
DOCKER_HOST_1 = 'tcp://vch1.verdnatura.es:2376'
DOCKER_HOST_2 = 'tcp://vch2.verdnatura.es:2376'
TAG = "${env.BRANCH_NAME}"
}
stages {
stage('Checkout') {
steps {
script {
env.COMPOSE_PROJECT_NAME = env.JOB_NAME.replace('/', '-');
env.GIT_COMMITTER_EMAIL = sh(
script: "git --no-pager show -s --format='%ae'",
returnStdout: true
).trim()
try { switch (env.BRANCH_NAME) {
case 'master':
env.PORT = 5002
break;
case 'test':
env.PORT = 5001
break;
}
switch (env.BRANCH_NAME) { switch (env.BRANCH_NAME) {
case 'master': case 'master':
env.NODE_ENV = 'production' env.NODE_ENV = 'production'
@ -14,67 +37,72 @@ try {
env.NODE_ENV = 'development' env.NODE_ENV = 'development'
break; break;
} }
}
node {
stage ('Checkout') {
checkout scm
env.GIT_COMMITTER_EMAIL = sh(
script: "git --no-pager show -s --format='%ae'",
returnStdout: true
).trim()
echo "Committer: ${env.GIT_COMMITTER_EMAIL}" echo "Committer: ${env.GIT_COMMITTER_EMAIL}"
} }
}
stage('Build') { stage('Build') {
environment {
CREDS = credentials('docker-registry')
}
steps {
nodejs('node-lts') { nodejs('node-lts') {
sh "env NODE_ENV= npm install --no-audit" withEnv(['NODE_ENV=']) {
sh "npm install --no-audit"
sh "gulp install" sh "gulp install"
}
sh "gulp build" sh "gulp build"
} }
sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY'
sh "docker-compose build --parallel"
sh "docker-compose push"
}
} }
stage('Deploy') { stage('Deploy') {
environment {
DOCKER_TLS_VERIFY = 1
}
parallel {
stage('Host 1') {
environment {
DOCKER_HOST = "${env.DOCKER_HOST_1}"
}
steps {
withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) { withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) {
switch (env.BRANCH_NAME) { sh "docker-compose pull"
case 'master': sh "docker-compose up -d"
env.PORT = 5000
break;
case 'test':
env.PORT = 5001
break;
}
env.COMPOSE_PROJECT_NAME = env.JOB_NAME.replace('/', '-');
env.DOCKER_TLS_VERIFY = 1
env.TAG = env.BRANCH_NAME
env.DOCKER_HOST = 'tcp://vch1.verdnatura.es:2376';
sh "docker-compose up --build -d"
env.DOCKER_HOST = 'tcp://vch2.verdnatura.es:2376';
sh "docker-compose up --build -d"
} }
} }
} }
} catch(e) { stage('Host 2') {
buildError = e environment {
DOCKER_HOST = "${env.DOCKER_HOST_2}"
} }
steps {
if (env.GIT_COMMITTER_EMAIL) { withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) {
def status sh "docker-compose pull"
def body = "Check status at ${env.BUILD_URL}" sh "docker-compose up -d"
if (buildError) {
status = 'Failed'
body += "\n\nError: ${buildError.toString()}"
} else {
status = 'Success'
} }
}
}
}
}
stage('Cleanup') {
steps {
sh 'docker logout $REGISTRY'
}
}
}
post {
always {
script {
if (!env.GIT_COMMITTER_EMAIL) return
mail( mail(
to: env.GIT_COMMITTER_EMAIL, to: env.GIT_COMMITTER_EMAIL,
subject: "Pipeline: ${status}: ${env.JOB_NAME} (${env.BUILD_NUMBER})", subject: "Pipeline: ${env.JOB_NAME} (${env.BUILD_NUMBER}): ${currentBuild.currentResult}",
body: body body: "Check status at ${env.BUILD_URL}"
) )
} }
}
if (buildError) }
throw buildError; }

View File

@ -1,20 +1,20 @@
version: '3.5' version: '3.5'
services: services:
app: app:
image: registry.verdnatura.es/salix-app:${TAG}
restart: unless-stopped
build: build:
context: . context: .
dockerfile: services/nginx/Dockerfile dockerfile: services/nginx/Dockerfile
ports: ports:
- ${PORT}:80 - ${PORT}:80
image: salix-app:${TAG}
restart: unless-stopped
links: links:
- api - api
api: api:
image: registry.verdnatura.es/salix-api:${TAG}
restart: unless-stopped
build: . build: .
environment: environment:
- NODE_ENV - NODE_ENV
restart: unless-stopped
image: salix-api:${TAG}
volumes: volumes:
- /containers/salix:/etc/salix - /containers/salix:/etc/salix