Skip to content

Instantly share code, notes, and snippets.

@waleedsamy
Created October 17, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save waleedsamy/6f198e0264bdff7051645dd230190bea to your computer and use it in GitHub Desktop.
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
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