Last active
November 3, 2017 02:06
-
-
Save rohancme/8192c2cb58c019f2c4b121d6eb232b51 to your computer and use it in GitHub Desktop.
Jenkinsfile for a Linux build with the K8S plugin
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
podTemplate(label: 'my-build-pod', containers: [ | |
// This is the official jnlp base image with added support for ssh access to git repos | |
containerTemplate(name: 'jnlp', image: 'larribas/jenkins-jnlp-slave-with-ssh:1.0.0', args: '${computer.jnlpmac} ${computer.name}'), | |
// This image gives us a container with kubectl installed so we can deploy the image we build | |
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl', ttyEnabled: true, command: 'cat'), | |
// A docker container to build our image | |
containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true)]) { | |
node ('my-build-pod') { | |
// Store ssh credentials in Jenkins, replace GITHUB_URL with your Github Repo | |
git branch: "${env.BRANCH_NAME}", credentialsId: "{{JENKINS_CREDENTIALS}}", url: '{{GITHUB_URL}}' | |
stage 'Build docker image' | |
container('docker') { | |
sh """ | |
docker build -t ${imageTag} . | |
""" | |
} | |
stage 'Push image to the registry' | |
container('docker') { | |
// Store these credentials in Jenkins too | |
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '{{DOCKER_CREDENTIALS}}', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { | |
sh """ | |
docker login -u $USERNAME -p $PASSWORD | |
docker push ${imageTag} | |
""" | |
} | |
} | |
stage 'Deploy app' | |
container('kubectl') { | |
// This uses a kubeconfig file stored as a Jenkins secret | |
withCredentials([[ | |
$class: 'FileBinding', | |
credentialsId: 'k8s-credentials', | |
variable: 'KUBECONFIG' | |
]]){ | |
switch (env.BRANCH_NAME) | |
{ | |
case "master": | |
sh("kubectl --kubeconfig=$KUBECONFIG --namespace={{APP_NAMESPACE}} apply -f ./services.yaml") | |
sh("kubectl --kubeconfig=$KUBECONFIG --namespace={{APP_NAMESPACE}} apply -f ./deployment.yaml") | |
break | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment