Every now and then I’ve had to set up Jenkins (usually locally) either to test an idea out or because I’m waiting for one to be set up by the “Environments” team. Setting up Jenkins is pretty straight forward but it does have a foot print and can be troublesome further down the track if you don’t tidy up or forget about what resource its taking up. For example, Jenkins uses port 8080 by default. However this is a common port that is used by other web apps which mean you could end up with a conflict. (which is easy to fix if you remember installing Jenkins months ago or that it’s the app that’s using the port.)
One way of avoiding these sorts of problem is by setting up Jenkins in a virtual machine. This means you can quickly get everything up and running and tear it down quickly. And with the current tools this is easier to do than ever.
Step 1
- Install VirtualBox (https://www.virtualbox.org/wiki/Downloads)
- Install Vagrant (https://www.vagrantup.com/downloads.html)
Step 2
Create a file called “Vagrantfile” and copy the code below into it.
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "ubuntu/trusty64" # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. config.vm.network "forwarded_port", guest: 8080, host: 8080 config.vm.network "forwarded_port", guest: 80, host: 80 # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # config.vm.provider "virtualbox" do |vb| # Customize the amount of memory on the VM: vb.memory = "1024" end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. config.vm.provision "Provision Jenkins", type: "shell" do |s| s.inline = "wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - n" s.inline += "sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list' n" s.inline += "sudo apt-get update n" s.inline += "sudo apt-get install -y jenkins n" s.inline += "sudo service jenkins start n" s.inline += "echo Jenkins provisioning done!" end end
You can download the code from https://github.com/limsim/vagrant-jenkins
Step 3
From the command line run
vagrant up
And then the magic starts! Vagrant will create a virtual machine using VirtualBox that will be installed with Ubuntu and Jenkins. You can access Jenkins by going to http://127.0.0.1:8080 in your browser
When you’re done using Jenkins you can either shut down the virtual machine by running the command below from the command line
vagrant halt
Or run the below command to delete the virtual machine completely
vagrant destroy