Setup Jenkins for private GitHub repository

Last couple of days I spent with Jenkins. It doesn’t seem like a funny weekend, right? Well it was really funny time. I ended up with no hair on my head and dozens of notes that actually don’t make sense at all.
So I decided to put it altogether here so whenever I need to go through this process again it’ll be easier. Feel free to use this article for your own purpose.


Note!

Jenkins may NOT work with Java version 9.x. This was the first problem I struggled with. If you have the same problem, download JDK 8u152 and install it.
Use this command in Terminal to remove Java 9.X from your Mac.

sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-9.X.jdk

Remember to restart your computer after that process.


Jenkins installs on its own

Basically there is lot of waiting while installing Jenkins and from time to time you just need to click something. That’s really cool part.
Download Jenkins from their official website and install on your local machine.

When it’s done installing, Jenkins will be locked and you have to unlock it manually by copying and pasting initial password. You can find this password here:

sudo more /Users/Shared/Jenkins/Home/secrets/initialAdminPassword

When you paste it to that text box, click continue and wait another bit.
Select Install suggested plug-ins and wait one more bit (as I said, installing Jenkins is mostly a waiting process 😃).

Preparing Jenkins GitHub account

Ok, while plug-ins are installing it’s a good time to prepare an account you’ll use for building the project. In my opinion it’s wise to create extra account for tools.


If you don’t want to create a separate email for this account, you’re welcome to do the same trick as I did.
My GitHub account is created on maciek.najbar@gmail.com email.
I created Jenkins on maciek.najbar+jenkins@gmail.com. This way I manage both accounts with just one email.


When you have the account already, let’s generate ssh key for that account and assign it to our Jenkins user. Open Terminal and follow steps below.

sudo su
(type your password)
whoami
(should tell you that currently you're 'root')
sudo su jenkins
whoami

(should tell you that currently you're 'jenkins')
ssh-keygen -t rsa -b 4096 -C "maciek.najbar+jenkins@gmail.com" # use your email for Jenkins GitHub account
(Leave file name empty)
(Don't set password, just leave empty) # Jenkins is a service and it won't be able to enter passphrase when connecting to ssh

Ok. Let’s leave it here for now.

Create a new Job for Jenkins

I assume Jenkins completed with all plug-ins installation. It will ask you to create Admin account to Jenkins panel. This account will be your point of control for the panel.

When you’re done and logged in to the panel, click at Create New Job. Name your job whatever you want and choose Freestyle project.

One picture can be more descriptive than hundred words

Next step is job configuration. Let’s set only General part. So, we need to set Jenkins to Discard old builds(experiment with your own values) and to Execute concurrent builds if neccessary.

Save progress.

Create GitHub repository

Login to your daily GitHub account and create new private repository.

Follow the steps that are displayed to connect your local Android project repository with GitHub remote server.

When it’s done, we need to add Jenkins GitHub account as a collaborator to our project. On the repository ribbon click Settings and go to Collaborators section.

Find your Jenkins account and send an invite. You’ll receive an email to accept an invitation. To accept it, you first need to re-login to Jenkins GitHub account.

Invitation accepted? Awesome! Taking advantage you’re logged in to this account, let’s add SSH key that we previously prepared. Go to your account Settings and enter section SSH and GPG keys. Click on Add new for SSH keys.

When generating SSH key for Jenkins user, two files were created (id_rsa and id_rsa.pub). One of them is public and the other is private. Public key goes to GitHub.

whoami # make sure it says 'jenkins'
cd /Users/Shared/Jenkins/.ssh/
more id_rsa.pub

(copy the entire output and paste to GitHub)

Let’s leave the private key for now. But let’s establish connection for Jenkins to GitHub server. We need to add GitHub to known hosts. Let’s do it by simply connecting to GitHub server.

whoami # make sure it says 'jenkins'
ssh -T git@github.com
(And yes, you're sure you want to continue)
(Result should be a welcome message)

If you got the welcome message it means your Jenkins account finally has access to your repository!

Continue job configuration

Let’s connect Jenkins job to our repository. In job configuration go to Source Code Management section and choose Git.

In the picture you can see I have some credentials already, it’s because I created them in the previous picture. Click Add and you’ll see the same screen. Pick Kind: SSH Username with private key and type username and again, let’s go to terminal.

more /Users/Shared/Jenkins/.ssh/id_rsa # This time private key
(space, space, space until you see the entire private key)

Copy entire private key as in the picture (including BEGIN and END stuff). Save and choose just created credentials (as in the last screenshot).

Go to Build Triggers and checkoff Poll SCM. Schedule it for every 15mins.

H/15 * * * *

When you save here and run the build, it will pass. It will connect via SSH to your repository (and that’s basically all it will do).

But we want it to do more. We want it to run our Gradle commands to create build for us. Let’s go back to job configuration view again.


Go to Build section and click Add build step. Choose Gradle script option and pick Use Gradle Wrapper (because this is what Android projects use). Set the tasks you want Gradle to execute.

Save the project and you can run the build again. Works? Excellent!

What if my build fails?

There is this possibility. I had it. I means that the project cannot find ANDROID_HOME environment variable. Well, first if your SDK folder is located in your computer user’s location, Jenkins user won’t be able to see it. Go to Android Studio and change that location to one that is shared by all the users.

When you change it, let your project rebuild. Install all missing tools and sdk platforms. When your project builds successfully again, you can delete the folder that previously was used to store all Android sdks.

Open Jenkins again, and go to Jenkins’ settings.

Find Environment variables and set ANDROID_HOME to location you’ve just set in Android Studio.

Save and go run the job again. Works? Awesome!


Still doesn’t work? Leave me a comment, together we can figure something out.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.