Good Morning, Guys!
Welcome back!
So, I would like to help you to deploy your Golang application to your server using Jenkins Pipeline. But first, what is Jenkins Pipeline? Well, based on Jenkins website, Jenkins pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. So, to use pipeline, We have to install its plugin first from Our Jenkins Plugin configuration page.
Next, If you never using Jenkins pipeline before, You need to know to use Pipeline script which is a Groovy language. To create pipeline script, we can create a file named Jenkinsfile and checked it into project’s source control repository. Or by write it on the Job’s configuration page.
In this example, I’ll create it on the Job’s configuration page. At My pipeline script, I will create 4 stages of deployment which is setup environment, doing test to the code, build the code, and deploy the code to server.
Here it is my pipeline script:
### setup environment: creates directories, sshagent, checking ### deployment date, set PATH environment stage "setup environment" node('master'){ sshagent (credentials: ['2e1b7262-0912-409f-83fb-179860abee79']){ sh 'ssh -o StrictHostKeyChecking=no -l ec2-user 111.222.123.121' sh 'DATE=`ssh ec2-user@111.222.123.121 "date +%Y%m%d%H%M%S"`' } dir('/var/lib/jenkins/jobs/[your_project]/workspace/bin'){} dir('/var/lib/jenkins/jobs/[your_project]/workspace/pkg'){} dir('/var/lib/jenkins/jobs/[your_project]/workspace/'){ sh 'rm -rf *.tar.gz' } dir('/var/lib/jenkins/jobs/[your_project]/workspace/src/bitbucket.org/[project]/[project_go]'){ sh 'rm -rf *' } withEnv(['PATH=$PATH:/opt/go/bin:/usr/local/bin','GOROOT=/opt/go','GOPATH=/var/lib/jenkins/jobs/[your_project]/workspace/']){ dir('/var/lib/jenkins/jobs/[your_project]/workspace/src/bitbucket.org/[project]/[project_go]') { git branch: 'develop', credentialsId: 'c1db80fe-1eab-43f7-bd08-f4f80b706e61', url: 'git@bitbucket.org:rezast/golang.git' sh 'go get github.com/spf13/viper' sh 'go get gopkg.in/gin-gonic/gin.v1' sh 'go get github.com/op/go-logging' sh 'go get github.com/dgrijalva/jwt-go' sh 'go get github.com/stretchr/testify' } } } ### Run test script stage "GO Test" node('master'){ withEnv(['PATH=$PATH:/opt/go/bin','GOROOT=/opt/go','GOPATH=/var/lib/jenkins/jobs/[your_project]/workspace/']){ dir('/var/lib/jenkins/jobs/[your_project]/workspace/src/bitbucket.org/[project]/[project_go]') { sh 'go test bill_test.go' } } } stage "Build Go!" node('master'){ withEnv(['PATH=$PATH:/opt/go/bin:','GOROOT=/opt/go','GOPATH=/var/lib/jenkins/jobs/[your_project]/workspace/']){ dir('/var/lib/jenkins/jobs/[your_project]/workspace/src/bitbucket.org/[project]/[project_go]'){ sh 'go install' } } } ### Send binary to server stage "Deploy Go" node('master'){ dir('/var/lib/jenkins/jobs/[your_project]/workspace'){ sh 'tar -czf deploy.tar.gz bin' sshagent (credentials: ['2e1b7262-0912-409f-83fb-179860abee79']){ sh 'scp deploy.tar.gz ec2-user@111.222.123.121:~/' sh 'ssh ec2-user@111.222.123.121 "cd ~;tar xzf deploy.tar.gz"' echo 'Finished!' } } }
Note to remember: This pipeline script just working until send the binary to the server, to run the binary as a daemon, you should make it on your own, ok? 🙂
Next, lets try to run our deployment!
Result:
At the #5, my deployment is failed, But as you can see at the #6 my deployment is success. You can see the process log by clicking on the stage you want to see. It is more easy to understand rather than the normal Jenkins job, right?
So, that’s all for today 🙂
Hope you enjoy this. Let me know if you have some suggestion! 🙂
Failure is the beginning of success! 😀
See you!