Created
October 17, 2017 16:14
-
-
Save waleedsamy/6f198e0264bdff7051645dd230190bea to your computer and use it in GitHub Desktop.
Jenkinsfile that extract git info, build multi-stage docker image, push to nexus and deploy to kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node { | |
def rock | |
def branch | |
def commit | |
def nexussrv = "nexus.example.de" | |
def nexussrv_office = "${nexussrv}:8843" | |
def nexussrv_datacenter = "${nexussrv}:9843" | |
stage('Clone repository') { | |
def scmVars = checkout scm | |
origin_branch = scmVars.GIT_BRANCH | |
branch = origin_branch.tokenize('/')[1] | |
long_commit = scmVars.GIT_COMMIT | |
commit = long_commit.take(8) | |
} | |
stage('Install dependencies') { | |
docker.build("rock/dependencies", "-f docker/000-Dependencies.Dockerfile .") | |
} | |
stage('Test') { | |
docker.build("rock/test", "-f docker/111-Test.Dockerfile .") | |
} | |
stage('Build') { | |
docker.build("rock/build", "-f docker/222-Build.Dockerfile .") | |
} | |
stage('Package') { | |
rock = docker.build("ll/rock", "-f docker/333-Nginx.Dockerfile .") | |
} | |
stage('Push image') { | |
switch (branch) { | |
case "master": | |
docker.withRegistry("http://${nexussrv_office}", 'nexus-credentials') { | |
rock.push("latest") | |
rock.push("${commit}") | |
} | |
default: | |
docker.withRegistry("http://${nexussrv_office}", 'nexus-credentials') { | |
rock.push("${branch}") | |
rock.push("${commit}") | |
} | |
} | |
} | |
stage('Deploy into Kubernetes') { | |
switch (branch) { | |
case "master": | |
sh("kubectl set image deployment/rock rock=${nexussrv_datacenter}/ll/rock:${commit} --namespace=staging --context kube01") | |
sh("kubectl set image deployment/rock rock=${nexussrv_datacenter}/ll/rock:${commit} --namespace=staging --context kube02") | |
default: | |
echo "docker image: ${nexussrv_office}/ll/rock:${commit} created." | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment