Wednesday, January 20, 2016

Building a project hosted on Github using an Openshift Jenkins instance

In this blog, we will see how to build a project hosted on Github using an Openshift instance running Jenkins. You can also take it a step further and do continuous deployment. But, let first see how to make work giving that Openshift doesn't grant write permission on several folders on a Jenkins instance such as: .ssh, .m2.

1- Create a Jenkins instance

You can create a Jenkins instance either by logging into web account and using the dashboard, or using the command line by using rhc. Here a blog post to follow if you have not a jenkins instance already Launching a Jenkins instance on Openshift

2- Configuring SSH to connect to Github

In to be able to pull your source code from Github you need to generate an SSH key and deploy to your Github account. Let's assume that your jenkins instance is called buildserver.

First you need to connect to your server using ssh by running the following command

- rhc ssh buildserver

Since Openshift doesn't grant write permission on .ssh folder, we need to write an ssh-wrapper that will use a writable folder to store ssh keys and to be able to update know_hosts. Let's first create a writable folder for ssh.

- mkdir app-root/data/git-ssh

Then, we generate an SSH key that will be deployed on Github

- ssh-keygen -t rsa -b 4096 -C "youremail@yourdomain.com"

when prompted for the location where you want to store your keys, enter the following:

$OPENSHIFT_DATA_DIR/git-ssh/id_rsa ($OPENSHIFT_DATA_DIR is an env variable set automatically by Openshift)

Then, we need deploy id_rsa.pub to Github. This blog entry helps to do so if you are not familiar with that yet. Deploy SSH key to Github

At this time, you would have been able to run successfully this command if .ssh folder was writable.  You will get permission denied.

- ssh -T git@github.com

To overcome this problem, you need to create a script ssh-wrapper.sh that does the following:

#!/bin/bash

ID_RSA="$OPENSHIFT_DATA_DIR/git-ssh/id_rsa"
KNOWN_HOSTS="$OPENSHIFT_DATA_DIR/git-ssh/known_hosts"

ssh -o UserKnownHostsFile=$KNOWN_HOSTS -i $ID_RSA $1 $2

- chmod +x ssh-wrapper.sh (Don't forget to make it executable)

At this point, you should be able to SSH successfully Github

./ssh-wrapper.sh -T git@github.com
Hi Jenkins! You've successfully authenticated, but GitHub does not provide shell access.

Finally, you need to tell Jenkins to use this wrapper to be able to pull your source code from Github. You should go to Manage Jenkins > Configure System > Global Properties and create a new environment variable called GIT_SSH that refers to the location where you created your wrapper.

3- Conclusion

In this tutorial, we have seen how to allow an Openshift Jenkins instance to pull source code from Github. This involved writing an ssh wrapper due a lack of write permission on .ssh folder. As this point, you can start creating Jenkins jobs to build your CI pipeline. I have skipped installing any build tool since it's project specific. Please, don't hesitate to share any thoughts you have about this article or any comment you may have.

No comments:

Post a Comment