Implementing a Jenkins Pipeline for Kubernetes CI/CD with Docker and Helm
This tutorial explains how to create a Jenkins Pipeline that builds a Go application, packages it into a Docker image, pushes it to a private Harbor registry, and deploys it to a Kubernetes cluster using Helm, covering node labeling, pod templates, credentials, and optional rollback.
Jenkins Pipeline provides a workflow framework that connects tasks across nodes, enabling complex, visualized build processes.
Core concepts include Node (execution environment), Stage (logical grouping such as Build, Test, Deploy), and Step (basic operation provided by plugins).
To create a simple pipeline, you can define the script directly in the Jenkins Web UI or store it in a Jenkinsfile. An example declarative script creates stages Clone, Test, Build, Deploy, each echoing a message.
node{
stage('Clone'){ echo "1.Clone Stage" }
stage('Test'){ echo "2.Test Stage" }
stage('Build'){ echo "3.Build Stage" }
stage('Deploy'){ echo "4. Deploy Stage" }
}Running the job on a Jenkins slave requires adding a label to the node and referencing it in the pipeline script.
node('ydzs-jnlp'){
// same stages as above
}For Kubernetes deployment, the CI/CD flow adds stages for Docker image build, push, YAML update, and kubectl deployment. The pipeline uses a podTemplate to define containers for Golang, Docker, Helm, and kubectl, mounting the Docker socket and kubeconfig as needed.
podTemplate(label: label,
containers:[
containerTemplate(name:'golang', image:'golang:1.14.2-alpine3.11', command:'cat', ttyEnabled:true),
containerTemplate(name:'docker', image:'docker:latest', command:'cat', ttyEnabled:true),
containerTemplate(name:'helm', image:'cnych/helm', command:'cat', ttyEnabled:true),
containerTemplate(name:'kubectl', image:'cnych/kubectl', command:'cat', ttyEnabled:true)
],
volumes:[
hostPathVolume(mountPath:'/var/run/docker.sock', hostPath:'/var/run/docker.sock')
]) { ... }The pipeline retrieves the Git commit hash to tag the Docker image, logs into a private Harbor registry, builds and pushes the image, and then deploys it with Helm, allowing environment selection via an input parameter.
def imageTag = sh(script:"git rev-parse --short HEAD", returnStdout:true).trim()
def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
// Docker login, build, push
// Helm deploy with --set image.tag=${imageTag}A final stage runs kubectl commands to verify the deployment and offers an optional rollback step using Helm.
stage('快速回滚?'){
input id:'userInput', message:'是否需要快速回滚?', parameters:[[ $class:'ChoiceParameterDefinition', choices:"Y
N", name:'回滚?' ]]
if (userInput == "Y") { sh "helm rollback devops-demo --namespace kube-ops" }
}Overall, the article demonstrates a complete end‑to‑end Jenkinsfile that automates building, containerizing, and deploying a Go application to a Kubernetes cluster with CI/CD best practices.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
