Start CI-CD for mobile with Jenkins and Fastlane

Luyen Ninh
Culi Tech Viet
Published in
4 min readNov 23, 2019

Sometimes, I have questions about CI and CD my project android, how to build my project android on the server and auto distribute apk to Play Store only with a git push command?

I’m android developer and I don’t have any knowledge about DevOp. When I research about CICD, I was face off with many new keywords like ‘centOS’, ‘root’, ‘permissions’ and etc. But In the article I will show my approach with Jenkins and CICD. I hope it will be helpful with readers.

Environment : I’m using Centos OS 7 to setup jenkins.

Install Jenkins

Because Jenkins is run on JVM, we need to install JDK and JRM

$ sudo yum install java-1.8.0-openjdk-devel

The current version of Jenkins does not support Java 10 (and Java 11) yet. If you have multiple versions of Java installed on your machine make sure Java 8 is the default Java version.

The next step is to enable the Jenkins repository. To do that, import the GPG key using the following curl command:

$ curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo

And add the repository to your system with:

$ sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

Now start to install Jenkins

$ sudo yum install jenkins

After the installation process is completed, start the Jenkins service with:

$ sudo systemctl start jenkins

To check Jenkins started :

$ systemctl status jenkins--output--
● jenkins.service - LSB: Jenkins Automation Server
Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
Active: active (running) since Thu 2018-09-20 14:58:21 UTC; 15s ago
Docs: man:systemd-sysv-generator(8)
Process: 2367 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/jenkins.service

Finally, we can set up Jenkins start-up when rebooting

sudo systemctl enable jenkins

If you are installing Jenkins on a remote CentOS server that is protected by a firewall you need to port 8080.

Use the following commands to open the necessary port:

$ sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload

Test Jenkins

Now we can test Jenkins from my client pc, open your browser and open url

http://your_ip_or_domain:8080

After that, we can create an account to login the Jenkins system.

we have a bridge, let’s run !
Photo by Fabio Comparelli on Unsplash

Create Jobs install Fastlane

To avoid the article too long to read, I was just create jobs with “execute shell ”, not using another integrate like github, etc

On the left side menu of the Jenkins home page click to

New Item -> Enter an item name of jobs "Install fastlane" and select -> Freestyle project -> ok

Input your “Description” of jobs and Scroll to “Build” section, click to “Add build step”, select “Execute shell” and parse below command to form:

$ sudo gem install fastlane -NV

Click to “Save” and ”Apply”.

On the screen Job detail, click to “Build Now” on the left side menu to run this job. On the section Build History, click to the lasted build time, and click to “Console Output” to see the detail output of the command.

Finished: SUCCESS

This really nice, but Some time we can face off with errors, I will list in below

Install Fastlane Error :

To quickly execute the command lines set up something we should be using ssh and using the terminal, to login with Jenkins user we need login and create a password, to switch Jenkins user:

su - jenkins -s /usr/bin/sh

Ruby does not install before, Because we have using “gem” to install Fastlane

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem”)

$ sudo yum install ruby
$ gem install rubygems-update

//Response your error to

Create Job Build project Android

Download Android-SDK

The first step we need to install android SDK and setup Android SDK variable environment,

$ wget --output-document=android-sdk.zip --quiet https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip$ unzip android-sdk.zip

To get lasted Android SDK version we can download here. And export Android SDK variable

$ export ANDROID_SDK_ROOT=/home/download/

/home/download is my folder exist android SDK,

Create Job clone source from git

The step by step to create a job like we have created a job to download Fastlane above. We just need to change the command install Fastlane to the command clone git below:

$ git clone `URL_YOU_REPO`

Create Job build android project with test

Create a job on Jenkins home page to execute shell below:

$ cd YOUR_PROJECT_FOLDER
$ /usr/local/bin/fastlane init
$ /usr/local/bin/fastlane android test

That’s it! fastlane will automatically generate a configuration for you based on the information provided.

Conclusion

Congratulation! You have to imagine about Jenkins, step by step create jobs with Jenkins and a little experience to resolve stuck with Jenkin and Fastlane. Ready to face off with new challenges using another feature of Jenkins, for example: how to autorun test job when you submit a merge request?

--

--