Using Jenkins and GitLab Together in Skytap Cloud
In my last blog post, Get the Most out of the GitLab Template in Skytap Cloud, we put together a new configuration that had a simple development machine along with the GitLab template for doing source control. This time we are going to expand that template to include doing automatic builds via Jenkins whenever someone pushes code to GitLab (think of it as Continuous Integration in the cloud).
Note: If you didn’t go through that blog post with me or keep the configuration, you can go back and review it first, since we will be expanding on it in this one.
First off, you need to spin up the configuration we set up last time. From there, let’s add in the Jenkins template:
- Click Add VMs
- In the search box enter ‘Jenkins’
- Select the Jenkins template and click Add
- Start the Jenkins template
You can use SmartClient to connect to the Jenkins VM. But first, we need to quickly install Git. Log in to the Jenkins VM as root and run the following command to install Git for Jenkins CI to use:
You also need to generate an SSH key so Jenkins can log in to GitLab. Run the following and just hit enter for any questions asked by ssh-keygen. Answer ‘yes’ to the scp command (don’t worry if you get an error, we just need to save the GitLab server’s ssh key). After running the cat command, copy the output (you’ll need to paste that into GitLab later):
Log out of the Jenkins VM and connect to your client VM. Both GitLab and Jenkins are built on Ubuntu 12.04.1 LTS Server, so neither of them has a web browser. You’ll be using the client machine for almost everything, but if you prefer, you could map HTTP Port 80 on both GitLab and Jenkins to Skytap published services and connect to them that way through your browser. However, if you do this, when we tie GitLab and Jenkins together, use the URLs I provide and not the published service ones.
Now it’s time to allow your Jenkins server to log in to GitLab. You can re-use your email@example.com account for this (though you could create separate accounts for the two). Launch Firefox (Applications > Internet > Firefox web browser). In the address bar, go to: http://host-1, then:
- Login as firstname.lastname@example.org
- Username: email@example.com
- Password: skytap
Now you can configure Jenkins. In the Firefox address bar, type: http://host-2 to get to Jenkins. Configure Jenkins CI so that it can connect to your GitLab instance and pull code from it:
- Click the Manage Jenkins link
- Click the Manage Plugins link
- Click the Available tab
- Type ‘git’ in the filter box
- Check the Gitlab Hook Plugin box
- Click the Download now and install after restart button
- Jenkins will install the Git Plugin, ruby-runtime, and Gitlab Hook Plugin
- Check the Restart Jenkins when installation is complete box
- Wait for Jenkins to restart
- You should see ruby-runtime plugin, the Jenkins Git plugin, and the Gitlab Hook Plugin:
- You should see that Git is installed and all ready to go
- Global Config User.Name = skytap
- Global Config User.Email = firstname.lastname@example.org
You are now ready to create your first job in Jenkins. Since Node.js doesn’t require any actual building, this project will check out the entire source tree from GitLab. (In my next post, we’ll actually do something with it while it is checked out.)
- Click New Job in Jenkins
- Enter ‘express_sample’ (or whatever you want) for ‘Job Name’
- For this example, use “Build a free-style software project” for the project type (again, feel free to change this to fit your needs)
- Click OK
- Scroll down to ‘Source Code Management’ and Select Git
- Repository URL: git@host-1:express_sample.git
- Branches to build: master
Jenkins should be good to go. You can run a manual build and see that Jenkins can check out the express_sample source code from GitLab. The last step is telling GitLab to trigger Jenkins for you when a check-in occurs. In the Firefox address bar, go back to GitLab (http://host-1), then:
- Log in as email@example.com
- Choose the express_sample project
- Click the Hooks lower tab
- In the url field, enter: http://host-2/git/notifyCommit?url=git@host-1:express_sample.git
- Click Add Web Hook
Now let’s test everything out. Open a terminal window on the client machine again and follow along (you’ll make a small change to your Node.js application to check-in).
Go over to Jenkins in Firefox after you push the changes to GitLab (git push origin master). You should see that Jenkins ran another build successfully.
Just like with GitLab, all three of these VMs are in the same configuration to make life easier. You could also move a combination of these parts to different configurations and tie them together with ICNR or VPN back to your internal network.
As always, if you have any questions, don’t hesitate to get in touch with Skytap Support or your Skytap representative. Also, you may want to keep this configuration around a bit longer. We’ll be using it again in my next blog post where we’ll make Jenkins do something cool with our Node.js application after it’s checked out of GitLab.
Until next time,