Tonight I had to setup a new instance of Jenkins. Having never set Jenkins to work with Bitbucket before I thought this might help out others. This will cover Jenkins accessing only Bitbucket, having it deploy might be another post, but that isn’t covered here.
Starting out, I have a blank Digital Ocean droplet. There is basically nothing. I had to install Jenkins, which is pretty basic:
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c ‘echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list’
sudo apt-get update
sudo apt-get install jenkins
This will create the jenkins user and start up the Jenkins servlet on port 8080. If you need to setup Jenkins other ways checkout the How-to.
Jenkins Unix/security setup
Next I needed to setup Jenkins to use my unix user permissions. The security settings can be accessed by going to: http://your_server:8080/configureSecurity/
From the configureSecurity page select “Unix user/group database“ click on “Advanced” and add “sshd” as the Service Name. While setting this up I got on error saying the jenkins user could read from “/etc/shadow/”. So I had to add the jenkins user to the shadow group.
usermod -G shadow jenkins
Lastly to setup permissions I selected: “Logged-in users can do anything”.
Since this was a basic ubuntu install I had to install git:
sudo apt-get install git
Then setup the git user information:
git config –global user.email “firstname.lastname@example.org”
git config –global user.name “jenkins”
Not having the user info set resulted in Jenkins later through throwing: hudson.plugins.git.GitException: Could not apply tag jenkins-Pactsafe_-_TEST-2 when trying to run a build. Better set them now. I used the global config because this droplet will only be used for Jenkins.
Next install the git plug in for Jenkins. Using the Jenkins menu go to:
- Manage Jenkins
- Manage Plugins
- Then search for “Git Plugin”
Once Jenkins restarts you can move on to setup a new job.
New Job with SSH and Bitbucket
Finally, setting up Jenkins to simply access a repo from Bitbucket! They have a nice write up on this: SSH with Bitbucket
Since Digital Ocean allows for shell access I was able to generate an ssh key for my jenkins user. I logged in as jenkins and ran ssh-keygen. ssh-keygen will (by default) generate the public key in ~/.ssh/id_rsa.pub. For more information checkout Bitbucket’s How-to
Open Bitbucket and go to the repository you want Jenkins to access. As Admin, access the repository’s settings and setup a new deployment key using the previously generated contents from the id_rsa.pub file.
You will also need to setup a ~/.ssh/known_hosts file. If you don’t the build will fail with this error stderr: Host key verification failed.
The easiest way to create the known_host file is to run (as jenkins):
git ls-remote -h email@example.com:person/projectmarket.git HEAD
Doing this will prompt you to add Bitbucket as a known host. Once you confirm the host the key for bitbucket.org it will be added to the ~/.ssh/known_hosts file and you shouldn’t get this error anymore.
Run The Job
With all of that setup you should be able to run the job and receive a “Finished: SUCCESS” message in the Console Output.
There are many things that still need to be setup after this, but this gets Jenkins and Bitbucket playing together.