Jenkins
Pipelines
Full pipeline deployment
In this guide, we will walk you through setting up a complete Jenkins pipeline to build, test, and run a Go web application. In previous lessons, you learned how to test your code, pull it from GitHub, and build it. Now, we'll execute an entire code pipeline end-to-end, ensuring that every stage of your Go application's lifecycle is automated.
Jenkins Pipeline Overview
The final configuration provided below defines a Jenkins pipeline with three key stages:
- Test: Runs the Go test suite.
- Build: Compiles the Go application.
- Run: Executes the compiled binary.
Each stage checks out the source code from GitHub and executes the corresponding commands to progress your application through the pipeline.
pipeline {
agent any
tools {
go 'go-1.17'
}
environment {
GO111MODULE = 'on'
}
stages {
stage('Test') {
steps {
git 'https://github.com/AdminTurnedDevOps/go-webapp-sample.git'
sh 'go test ./...'
}
}
stage('Build') {
steps {
git 'https://github.com/AdminTurnedDevOps/go-webapp-sample.git'
sh 'go build .'
}
}
stage('Run') {
steps {
sh 'cd /var/lib/jenkins/workspace/go-full-pipeline && go-webapp-sample &'
}
}
}
}
Important
Ensure that the "Run" stage uses the Jenkins workspace directory to locate and execute the binary built in the earlier stage. Without running the build commands, the go-webapp-sample
binary would not be available, leading to execution errors.
Pipeline Configuration in YAML
Below is an example of the equivalent pipeline written in YAML. Note that the environment variable typo has been corrected in this version.
agent any
tools {
go 'go-1.17'
}
environment {
GO111MODULE='on'
}
stages {
stage('Test') {
steps {
git 'https://github.com/AdminTurnedDevOps/go-webapp-sample.git'
sh 'go test ./...'
}
}
stage('Build') {
steps {
git 'https://github.com/AdminTurnedDevOps/go-webapp-sample.git'
sh 'go build .'
}
}
stage('Run') {
steps {
sh 'cd /var/lib/jenkins/workspace/go-full-pipeline && go-webapp-sample &'
}
}
}
After configuring your pipeline job in Jenkins, simply run it by following these steps:
- Open your web browser and navigate to the Jenkins dashboard.
- Create a new pipeline job and name it "GoFullPipeline" (or choose an appropriate name and update related configurations if necessary).
Observing the Pipeline in Action
When you run the pipeline, you will see output in the Jenkins console similar to the following:
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f 09b607224df9bb27580dafc7ec249fe2e805cbfbb # timeout=10
...
+ go test ./...
? github.com/ybkuroki/go-webapp-sample [no test files]
? github.com/ybkuroki/go-webapp-sample/config [no test files]
? github.com/ybkuroki/go-webapp-sample/controller (cached)
...
The test stage confirms that the tests run successfully, even if some directories do not contain test files. After testing, the build stage compiles the binary, and the run stage then navigates to the workspace directory to start the Go application.
After a successful build, open your browser and navigate to the Jenkins host IP address on the default port (8000) to see your application running.
Handling Console Output Warnings
Sometimes, you might see a message similar to this in the console output:
+ cd /var/lib/jenkins/workspace/go-full-pipeline
+ go-webapp-sample
/var/lib/jenkins/workspace/go-full-pipeline@tmp/durable-c32dd620/script.sh: 1: go-webapp-sample: not found
Finished: SUCCESS
Warning
Although the error message appears due to the shell's execution reporting, this does not prevent your application from running successfully. This behavior has been observed consistently in previous builds.
Next Steps
Now that you have seen the complete Jenkins pipeline in action, it's time for you to experiment with the configuration. Tweak the settings as needed and verify that each stage—testing, building, and running—executes as expected.
For more detailed guidance on Jenkins and Go application deployment, refer to the following resources:
Happy deploying!
Watch Video
Watch video content
Practice Lab
Practice lab