Update Jenkins File with github webhooks and slack notifications (#251)

* Update Jenkinsfile
This commit is contained in:
Bruck Wubete 2018-08-24 14:54:32 -04:00 committed by Jesus Federico
parent f6742fd440
commit e477948713
1 changed files with 59 additions and 38 deletions

97
Jenkinsfile vendored
View File

@ -2,13 +2,25 @@ def project = 'ci-cd-for-bn'
def appName = 'greenlight' def appName = 'greenlight'
def greenlightVersion = 'v2' def greenlightVersion = 'v2'
def label = "jenkins-execution-worker-${UUID.randomUUID().toString()}" def label = "jenkins-execution-worker-${UUID.randomUUID().toString()}"
def releaseBuild = env.TAG_NAME && env.TAG_NAME.contains("release")
if (env.TAG_NAME && env.TAG_NAME.contains("release")) { String convert(long millsToConvert){
long seconds, minutes, hours;
seconds = millsToConvert / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
minutes = minutes % 60;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
if (releaseBuild) {
kubeCloud = "production" kubeCloud = "production"
kubecSecretsId = 'gl-launcher-prod-secrets' kubecSecretsId = 'greenlight-prod-secrets'
} else { } else {
kubeCloud = "staging" kubeCloud = "staging"
kubecSecretsId = 'gl-launcher-staging-secrets' kubecSecretsId = 'greenlight-staging-secrets'
} }
properties([ properties([
@ -27,47 +39,56 @@ volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock') hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]){ ]){
node(label) { node(label) {
def myRepo = checkout scm try {
def gitCommit = myRepo.GIT_COMMIT slackSend (color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
def gitBranch = myRepo.GIT_BRANCH def myRepo = checkout scm
def gitTag = env.TAG_NAME def gitCommit = myRepo.GIT_COMMIT
def shortGitCommit = "${gitCommit[0..10]}" def gitBranch = myRepo.GIT_BRANCH
def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true) def gitTag = env.TAG_NAME
def imageTag = "gcr.io/${project}/${appName}:${gitBranch}.${env.BUILD_NUMBER}.${gitCommit}" def shortGitCommit = "${gitCommit[0..10]}"
def previousGitCommit = myRepo.GIT_PREVIOUS_COMMIT
stage('Test') { def imageTag = "gcr.io/${project}/${appName}:${gitBranch}.${env.BUILD_NUMBER}.${gitCommit}"
container('ruby') { def stageBuild = (kubeCloud == "staging" && gitBranch == "master")
sh "bundle install --without development production && bundle exec rubocop && bundle exec rspec"
stage('Test') {
container('ruby') {
sh "bundle install && bundle exec rubocop && bundle exec rspec "
}
} }
}
stage('Build and Publish') {
stage('Build and Publish') { container('gcloud') {
container('gcloud') { withCredentials([file(credentialsId: 'cloud-datastore-user-account-creds', variable: 'FILE'), string(credentialsId: 'DOCKER_USER', variable: 'DOCKER_USER'), string(credentialsId: 'DOCKER_PASSWORD', variable: 'DOCKER_PASSWORD')]) {
withCredentials([file(credentialsId: 'cloud-datastore-user-account-creds', variable: 'FILE')]) { sh "gcloud auth activate-service-account --key-file=$FILE"
sh "gcloud auth activate-service-account --key-file=$FILE" if (stageBuild) {
if (kubeCloud == "staging") { sh "gcloud docker -- build -t ${imageTag} -t 'bigbluebutton/${appName}:master' . && gcloud docker -- push ${imageTag}"
sh "gcloud docker -- build -t ${imageTag} . && gcloud docker -- push ${imageTag}" sh "docker login -u $DOCKER_USER -p $DOCKER_PASSWORD"
} else { sh "docker push 'bigbluebutton/${appName}:master'"
imageTag = "gcr.io/${project}/${appName}:${gitTag}" } else if (releaseBuild) {
withCredentials([string(credentialsId: 'DOCKER_USER', variable: 'DOCKER_USER'), string(credentialsId: 'DOCKER_PASSWORD', variable: 'DOCKER_PASSWORD')]) { imageTag = "gcr.io/${project}/${appName}:${gitTag}"
sh "gcloud docker -- build -t ${imageTag} -t '$DOCKER_USER/${appName}:${greenlightVersion}' -t '$DOCKER_USER/${appName}:${gitTag}' . && gcloud docker -- push ${imageTag}" sh "gcloud docker -- build -t ${imageTag} -t 'bigbluebutton/${appName}:${greenlightVersion}' -t 'bigbluebutton/${appName}:${gitTag}' . && gcloud docker -- push ${imageTag}"
sh "docker login -u $DOCKER_USER -p $DOCKER_PASSWORD" sh "docker login -u $DOCKER_USER -p $DOCKER_PASSWORD"
sh "docker push '$DOCKER_USER/${appName}:${greenlightVersion}' && docker push '$DOCKER_USER/${appName}:${gitTag}'" sh "docker push 'bigbluebutton/${appName}:${greenlightVersion}' && docker push 'bigbluebutton/${appName}:${gitTag}'"
} }
} }
} }
} }
}
stage('Deploy') { stage('Deploy') {
container('kubectl') { container('kubectl') {
withCredentials([file(credentialsId: kubecSecretsId, variable: 'FILE')]) { if (stageBuild || releaseBuild) {
sh ''' withCredentials([file(credentialsId: kubecSecretsId, variable: 'FILE')]) {
kubectl apply -f $FILE sh '''
''' kubectl apply -f $FILE
} '''
sh "kubectl set image deployments/gl-deployment gl=${imageTag}" }
}
sh "kubectl set image deployments/gl-deployment gl=${imageTag}"
}
} }
slackSend (color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' in ${convert(currentBuild.duration)} (${env.BUILD_URL})")
} catch(e) {
slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' message: ${e} (${env.BUILD_URL})")
} }
} }
} }