Jenkins is a powerful automation tool, it provides hundreds of plugins to support building, deploying and automating any project. So does for Go! I would like to follow an example of creating a task for CoreDNS to illustrate what should be paid attention to. This is a basic tutorial for beginners that starts from setting up a new Jenkins service from scratch.

0. Prerequisites

According to Jenkins official documentation,

Jenkins requires Java7 or above to function. Java8 is recommended. Jenkins requires a fair amount of memory to operate well. Smaller installations should start around 256MB-1GB.

1. Install Jenkins Container on CentOS (Preferred)

I highly recommend to install Jenkins with Container, it makes life easier if you want to deploy the same Jenkins instance to a new machine. If you want to install Jenkins directly in a VM, please skip this section and check Install Jenkins Directly on CentOS (Optional).

1.1 Get Docker CE for CentOS

You may refer to this guide to get Docker CE for CentOS. It is recommended to install from package manager. Don’t forget to setup Docker as a non-root user and configure Docker to start on boot, which are listed in Post-installation steps for Linux.

1.2 Launch Jenkins Container

Pull the official jenkins image from Docker repository,

1
2
3
4
5
6
7
# If you want to use official build of jenkins image
docker pull jenkins/jenkins

# It is recommended to use my Dockerfile
# It has more tools installed for Go project build, like `make`.
git clone https://github.com/zp-j/docker.git
cd docker && docker build -t zhipj/jenkins .

Run a container using this image and map data directory from the container to the host; e.g in the example below /var/jenkins_home from the container is mapped to jenkins/ directory from the current path on the host. Jenkins 8080 port is also exposed to the host as 49001.

1
2
3
4
5
# Launch official docker image
docker run -d -p 49001:8080 -v $PWD/jenkins:/var/jenkins_home -t jenkins/jenkins

# Launch customized docker image
docker run -d -p 49001:8080 -v $PWD/jenkins:/var/jenkins_home -t zhipj/jenkins

If you are using Azure VM, don’t forget to add inbound security rules:

img

PAY ATTENTION
Make sure local jenkins/ directory can be accessed by Docker, otherwise it would lead to launch failed due to no write permission. More details can be found here: Go Plugin for Jenkins.
You can simply change that:

1
chown 1000 jenkins

1.3 Jenkins Users Setup

Once container is launched, you should be able to access Jenkins now from your browser. For the first login, you are required to enter the initial password for user admin, you can find it either from docker log,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ docker logs [container_id]
Aug 09, 2017 3:22:03 PM jenkins.install.SetupWizard init
INFO:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password
generated. Please use the following password to proceed to installation:

695zzz4yyyy04aaaxxx9a1c3b1XXX576

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

Or, you can check your local file,

1
2
$ cat ~/jenkins/secrets/initialAdminPassword
695zzz4yyyy04aaaxxx9a1c3b1XXX576

After you login, you would see a welcome message like this

img

Don’t forget to change your admin’s password as well as create new users.

2. Install Jenkins Directly on CentOS (Optional)

If you are looking for the guide of installing Jenkins on a bare metal, here is the steps of installation on CentOS 7.3.

2.1 Install a Sun-compatible Java

1
2
3
4
5
6
$ yum remove java
$ yum install java-1.7.0-openjdk
$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (rhel-2.5.5.1.el6_6-x86_64 u79-b14)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

2.2 Install Jenkins

1
2
3
$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
$ sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
$ sudo yum install jenkins

2.3 Start/Stop

1
2
3
$ sudo systemctl enable jenkins
$ sudo systemctl start jenkins
$ sudo systemctl stop jenkins

2.4 Important Note

  • With the help of systemctl, Jenkins can be launched as a daemon on startup.
  • The jenkins user is created to run this service. If you change this to a different user via the config file, you must change the owner of /var/log/jenkins, /var/lib/jenkins, and /var/cache/jenkins.
  • Log file will be placed in /var/log/jenkins/jenkins.log. Check this file if you are troubleshooting Jenkins.
  • /etc/sysconfig/jenkins will capture configuration parameters for the launch.
  • By default, Jenkins listen on port 8080. Access this port with your browser to start configuration. Note that the built-in firewall may have to be opened to access this port from other computers. (See http://www.cyberciti.biz/faq/disable-linux-firewall-under-centos-rhel-fedora/ for instructions how to disable the firewall permanently)

2.5 Optional

Install some common tools to make troubleshooting easy.

1
2
sudo yum install epel-release
sudo yum install vim git htop bmon tmux

If you are using Azure VM, don’t forget to add inbound security rules:

img

3. Setup Go Build Environment

3.1 Install Golang Plugin

Get the Go Plugin for Jenkins, [Manage Jenkins] -> [Manage Plugins] -> [Available] -> [Go Plugin]. After the installation of plugin, restart your container.

Pick Go 1.7.6, there is configuration issue between Go 1.8 and glide.

3.2 Add New Build Item

Create new freestyle project, in the section Source Code Management, give your git repository and credential if needed, pick the right branch to build. In Additional Behaviours, choose to check out to a sub directory.

img

1
$WORKSPACE/../src/github.com/coredns/coredns

By default your repo will be checked out to $WORKSPACE/coredns, it does not follow the package structure of a normal Go project. Therefore, we move it to the right place beneath $GOPATH.

In Build Environment section, check Set up Go programming language tools, pick the right Go version that was marked when you installed go plugin. In Build section, choose to Execute shell, paste the script below to the input box. It creates an environment variable $GOPATH, which contains the repository and all its dependencies.

1
2
3
4
5
6
# Create GOPATH
export GOPATH=$WORKSPACE/..
export PATH=$GOPATH:$PATH

# Run make to perform the build
cd $GOPATH/src/github.com/coredns/coredns && make coredns

Click Save, start a build, check its console output and wait a second. Voilà!

1
2
3
4
5
6
7
8
...
github.com/coredns/coredns/vendor/k8s.io/client-go/1.5/tools/clientcmd
github.com/coredns/coredns/middleware/kubernetes
github.com/coredns/coredns/middleware/autopath
github.com/coredns/coredns/core
github.com/coredns/coredns/coremain
github.com/coredns/coredns
Finished: SUCCESS

References