Jenkins is a popular open-source automation server that can be used to automate building, testing, and deploying applications. Here's how you can set up a Jenkins pipeline for a Go project.
Create a Jenkinsfile
in the root of your Go project repository with the following content:
groovypipeline { agent any environment { GO_VERSION = '1.22' } stages { stage('Checkout') { steps { git url: 'https://github.com/your-repo/go-project.git', branch: 'main' } } stage('Set up Go') { steps { sh 'wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz' sh 'sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz' sh 'export PATH=$PATH:/usr/local/go/bin' sh 'go version' } } stage('Install Dependencies') { steps { sh 'go mod tidy' } } stage('Run Tests') { steps { sh 'go test -v ./...' } } } post { always { cleanWs() } } }
pipeline {}
: Defines the pipeline structure.
agent any
: Specifies that the pipeline can run on any available agent.
environment {}
: Sets up environment variables. Here, we define GO_VERSION
.
stages {}
: Contains the different stages of the pipeline.
stage('Checkout') {}
: Clones the repository from the specified URL and branch.
stage('Set up Go') {}
: Downloads and installs the specified version of Go, and adds it to the PATH.
stage('Install Dependencies') {}
: Runs go mod tidy
to ensure dependencies are correctly installed.
stage('Run Tests') {}
: Runs go test -v ./...
to execute tests.
post { always { cleanWs() } }
: Cleans up the workspace after the pipeline finishes, regardless of the outcome.
For a more advanced setup with additional steps like linting, building, and archiving artifacts, extend your Jenkinsfile
as follows:
groovypipeline { agent any environment { GO_VERSION = '1.22' } stages { stage('Checkout') { steps { git url: 'https://github.com/your-repo/go-project.git', branch: 'main' } } stage('Set up Go') { steps { sh 'wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz' sh 'sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz' sh 'export PATH=$PATH:/usr/local/go/bin' sh 'go version' } } stage('Install Dependencies') { steps { sh 'go mod tidy' } } stage('Run Linters') { steps { sh 'go install golang.org/x/lint/golint@latest' sh 'golint ./...' } } stage('Run Tests') { steps { sh 'go test -v ./...' } } stage('Build') { steps { sh 'go build -v ./...' } } stage('Archive Artifacts') { steps { archiveArtifacts artifacts: '**/bin/*', allowEmptyArchive: true } } } post { always { cleanWs() } success { echo 'Build completed successfully!' } failure { echo 'Build failed.' } } }
stage('Run Linters') {}
: Installs and runs golint
to lint the code.
stage('Build') {}
: Builds the Go project.
stage('Archive Artifacts') {}
: Archives the build artifacts, such as binaries, for later retrieval.
post {}
: Contains actions that run after the main pipeline stages:
always { cleanWs() }
: Cleans up the workspace.success { echo 'Build completed successfully!' }
: Echoes a success message if the build is successful.failure { echo 'Build failed.' }
: Echoes a failure message if the build fails.Jenkinsfile
to your repository.Jenkinsfile
).This setup will automate the build, test, and deployment processes for your Go projects using Jenkins pipelines. Adjust the Jenkinsfile
according to your specific project needs and environment configurations.