Automate Go Builds with Jenkins: From Source Code to Docker Image in One Pipeline
This guide walks you through setting up a Jenkins pipeline that automatically compiles a Go project, builds a Docker image with Kaniko, and pushes it to a registry, covering prerequisites, agent configuration, pipeline steps, tips, and verification screenshots.
Prerequisites
Running Jenkins instance
Pipeline, Git, and Credentials plugins installed
Golang and Kaniko Docker images pulled
Pipeline Overview
The CI/CD pipeline performs four main steps: detect the environment, checkout the source code, compile the Go program, and build & push the Docker image using Kaniko.
Agent Configuration
<code>agent {
kubernetes {
cloud 'kubernetes'
inheritFrom 'default'
namespace 'jenkins'
yaml '''
kind: Pod
spec:
containers:
- name: golang
image: core.jiaxzeng.com/library/golang:1.23.8-alpine
command: ["cat"]
tty: true
workingDir: /home/jenkins/agent
env:
- name: http_proxy
value: http://172.139.20.170:3888
- name: https_proxy
value: http://172.139.20.170:3888
- name: no_proxy
value: localhost,*.cluster.local,*.svc,172.139.20.0/24,10.96.0.0/16,10.244.0.0/16
- name: kaniko
image: core.jiaxzeng.com/library/kaniko-executor:debug
command: ["/busybox/cat"]
tty: true
workingDir: /home/jenkins/agent
volumeMounts:
- name: kaniko-secret
mountPath: /kaniko/.docker
volumes:
- name: kaniko-secret
secret:
secretName: harbor-user-secret
items:
- key: .dockerconfigjson
path: config.json
hostAliases:
- hostnames:
- core.jiaxzeng.com
ip: 172.139.20.100
'''
}
}
</code>Build and Push Stage
<code>stage('打包并推送镜像') {
steps {
container("kaniko") {
sh """
/kaniko/executor --skip-tls-verify --dockerfile=/home/jenkins/agent/workspace/k8s-agent/Dockerfile \
--context=/home/jenkins/agent/workspace/k8s-agent --destination=core.jiaxzeng.com/jiaxzeng/simple:v1.3.4
"""
}
}
}
</code>Tip: If plugin downloads are slow or fail, refer to the article on solving Jenkins plugin download issues. The pipeline actually uses three containers (golang, kaniko, and the default JNLP container). The golang container includes proxy environment variables, and the kaniko container requires a Harbor secret and host alias for domain resolution.
Execute the pipeline and verify the results using the screenshots below.
Conclusion
By following this tutorial you can automate the compilation of Go programs and the creation of Docker images with Jenkins, reducing manual errors, speeding up delivery, and providing a solid foundation for further automation such as testing and Kubernetes deployments.
Linux Ops Smart Journey
The operations journey never stops—pursuing excellence endlessly.
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.