Building a Continuous Integration Server With Jenkins
Continuous Integration seems to be an up and coming trend in software development. In many conversations I’ve had with more experienced developers they recommended that I start learning both how it works, and if possible how to build a working system. As a solo developer with minimal experience, and even less budget my solution to this may not be ideal, but I’ll try to capture the gist of it.
Continuous Integration is a development practice that uses automated builds that are verified against the tests that are supplied during the development process. This process relies on a version control system that developers commit to regularly. Before any commits are made the software must be built and tested completely. If multiple developers are working on this code they may have to merge with the changes that have been made since the initial commit, and repeat the build/test cycle.
You may be asking what the benefit of this practice is. It’s certainly some work to set up, and involves developers constantly running tests and build scripts. That being said it’s the constant testing that is the core of what makes Continuous Integration worth it. Because developers are constantly testing their changes, bugs are caught quickly, and are usually found in the code that is still fresh in the developer’s mind. This is especially handy with larger teams, as it allows every developer to work in tandem without any major conflicts occurring. The earlier you can find the bug the less stress it can cause.
For a solo developer its definitely overkill to use a system like this, but I am using it for the learning experience more than the actual benefits of the practice.
For an android project the recommended set up is with a Jenkins server hooked up to a version control system, and Googles Firebase test suite. I will be using this GitHub repository for my project.
I’m going to assume you can figure out how to download and install Jenkins by yourself. Setting it up for my needs was a bit of a process, so I will outline that for you.
Create a new Item, I’ve been using the Freestyle project. It should open a larger options page afterwards.
Enter your GitHub repository in both of the areas that I have. You will have to set up your GitHub credentials in the specified area, and it should be pretty straight forward. In the second section, press the “Advanced” button to access more details. I’ve given my repository a local variable name of “origin”. I found this to be necessary as without it my Jenkins server would detach itself from the GitHub head.
Because I am developing on a budget, I am doing things a little unorthodox here. Because my Jenkins server is running on my local machine, I don’t have a static IP to notify when I commit to my GitHub. Because of this I am building on a specific branch, which I have named “Build”. If my build is successful then it will push to the master branch.
The steps above show that it will build the “Build” branch. Then it will query GitHub for changes between the Build branch and the master branch. If it notices changes it will merge the Build branch with the Master branch ensuring that we are using the most up to date code, and then will start the build process.
This image shows how I set up my Jenkins server to query GitHub for changes. It will check daily at 9 am and 5 pm.
I added a bunch of different steps to the “build step”. Most of them are using gradle to build the Android app, and the tests for the application so that I can pass them to the Firebase test suite.
The last step here was a little more complicated. To access Firebase you need to authenticate with your google account, and I found that the Jenkins plugins for authentication didn’t work properly.
First I had to install the Google Cloud SDK on my machine. I’ll assume you can install this by yourself, and create your project on Google Cloud.Then I went to this link and created a service account and matching key for my server. Then I placed the key given to me in the Jenkins server, and linked Jenkins to the key.
The code for all of the batch commands is below. I have subbed out all information that you must enter in between asterisks:
gcloud auth activate-service-account --key-file=*Your File Here*
gcloud firebase test android run --app *Your apk here* --device model=Nexus6,version=23,locale=en,orientation=portrait
gcloud firebase test android run --app *Your non-test apk here* --device model=Nexus6,version=23,locale=en,orientation=portrait --type instrumentation --test *Your test apk here*
Then I set the project so the Jenkins server would work with the proper project. The next two steps send the application to Firebase for tests.
Lastly we set the Post-build actions. Here I’ve set up the Git Publisher to push our changes to the cloud, and to set a tag locally on the machine, so that I can deploy this as a “Release”.
This should cover how to set up your Jenkins server. Please let me know if you have any troubles with this, as I will be happy to try and help sort out any issues you might run into.