Jenkins Pipeline written on groovy as an example to clone golang sources of the project, run tests inside docker container and archive the latest Alpine Linux docker image with application binary as artifacts.
- Jenkins version 2.190.2 or higher.
- Linux jenkins node to run pipeline.
- Cloudbees Docker Workflow Jenkins plugin.
- Install docker.
- This pipeline may require groovy methods approve. If you see a message like:
'Scripts not permitted to use staticMethod ... Administrators can decide whether to approve or reject this signature'
that means you need to allow them to execute. Navigate to
Jenkins -> Manage Jenkins -> In-process script approval
, find a blocked method (the newest one is usually at the bottom) then click 'Approve'. Or refactor this pipeline and move all these methods to jenkins share library. - Internet access on Jenkins node(s) to download docker image and golang project.
- Create jenkins pipeline with 'Pipeline script from SCM', set-up SCM, Branch Specifier as
*/main
and Script Path asgolang-app-docker-ci/golang-app-docker-ci.groovy
. - Specify defaults for jenkins pipeline parameters in a global variables of pipeline code, or do it later in Jenkins GUI after the first pipeline run.
- Install Cloudbees Docker Workflow Jenkins plugin.
- Install docker on your jenkins node(s).
- Add jenkins user to docker group:
usermod -aG docker jenkins
. - Make sure you have installed all packages to post-test command execution (APP_POSTTEST_COMMAND jenkins pipeline parameter).
- Run pipeline twice. The first run injects jenkins pipeline parameters with your defaults which was specified on step 2.
- GIT_URL: Git URL of golang project.
- GIT_PROJECT_PATH: Project inner path, e.g.
folder/subfolder/app_name
. - JENKINS_NODE: List of possible jenkins nodes to execute.
- RACE_COVER_TEST_FLAGS: Enable
-race
and-cover
flags forgo test
command execution. - APP_POSTTEST_COMMAND: Post-test shell command to ensure go app is working. On success docker image artifacts will
be attached. Leave them empty to skip post-testing. E.g:
curl http://127.0.0.1:80
to check web app is up.