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

168
Jenkinsfile vendored
View File

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