Cloud Native 6 min read

Automate Go Builds with Jenkins on Kubernetes: A Step‑by‑Step Guide

Learn how to set up a Jenkins pipeline that automatically pulls a Go project from GitLab, resolves dependencies, compiles the binary, and packages it within a Kubernetes‑based build environment, complete with detailed code snippets and tips for handling network and credential issues.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Automate Go Builds with Jenkins on Kubernetes: A Step‑by‑Step Guide

In today’s fast‑paced software development, manual compile, test and deploy are outdated. As an operations engineer, we aim for efficient, stable, repeatable automation, and Jenkins remains a top CI/CD tool due to its flexibility and rich plugin ecosystem.

Prerequisites

Running Jenkins service

Installed pipeline, Git and Credentials plugins

Downloaded Golang Docker image

Original project packaging steps

1. Project directory structure

$ ls
app  bootstrap  config  Dockerfile  go.mod  go.sum  Jenkinsfile  main.go  README.md

2. Download dependencies

$ go mod tidy

Tip: Ensure the build environment can access the network to download dependencies.

3. Compile

$ go build -ldflags "-X 'simple/app/controllers.Branch=$VERSION' -X 'simple/app/controllers.BuildTime=$BuildTime'" .
$ ls -l simple
-rwxrwxr-x. 1 ops ops 8845977 May 14 17:04 simple

Tip: The compiled binary is named simple.

Jenkins pipeline for Go program

Summary of steps:

Git pull code (requires GitLab credentials)

Download dependencies (requires network)

Compile

The Jenkins agent container already includes the git command, so no extra Git installation is needed, but GitLab credentials must be configured.

Stage: Pull code

stage('拉取代码') {
  steps {
    // Disable SSL verification for Git
    sh 'git config --global http.sslverify false; git config --global https.sslverify false'
    git branch: 'v1.3.3', credentialsId: 'gitlab-jiaxzeng', url: 'https://gitlab.jiaxzeng.com/jiaxzeng/simple.git'
  }
}

Stage: Compile

stage('编译') {
  steps {
    container("golang") {
      sh """
        cd ${WORKSPACE_PATH}
        go env -w GOPROXY=https://proxy.golang.com.cn,direct
        go mod tidy
        go build -ldflags "-X 'simple/app/controllers.Branch=$VERSION' -X 'simple/app/controllers.BuildTime=$BuildTime'" .
      """
    }
  }
}

Full pipeline file

pipeline {
  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
      '''
    }
  }

  stage('拉取代码') {
    steps {
      sh 'git config --global http.sslverify false; git config --global https.sslverify false'
      git branch: '$VERSION', credentialsId: 'gitlab-jiaxzeng', url: 'https://gitlab.jiaxzeng.com/jiaxzeng/simple.git'
    }
  }

  stage('编译') {
    steps {
      container("golang") {
        sh """
          cd ${WORKSPACE_PATH}
          go env -w GOPROXY=https://proxy.golang.com.cn,direct
          go mod tidy
          go build -ldflags "-X 'simple/app/controllers.Branch=$VERSION' -X 'simple/app/controllers.BuildTime=$BuildTime'" .
        """
      }
    }
  }
}

By following this guide, you have mastered the core skills to automate building a Golang project with Jenkins, enabling automatic compile, test, package, and deployment after code submission, thereby greatly improving delivery efficiency and system stability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdKubernetesGoPipelineJenkins
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.