Continuous Integration — Setting up PHP Applications in Jenkins Part 1

The first time I setup Continuous Integration (CI) for a project was back in 2014 when I was working as a Software Engineer Intern at Konga. I remember working with a template Oyewale Oyediran had setup before modifying it to suit that project.]

I recently got my hands dirty setting up CI for adKandi. I learnt a few things through the process that are not well documented in articles on the subject, so I thought to share.

This is the first part of the series and we will be covering Jenkins Installation, Jenkins plugins installation, NGINX installation and reverse proxy setup.

NB: You need to have at least some prior experience with linux to get the most out of this series.

Introduction

Before going into the nitty gritty of setting Jenkins up, I will like to create a foundation by defining Continuous Integration.

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early. — https://www.thoughtworks.com/continuous-integration

Examples of CI tools are Jenkins, Atlassian Bamboo, Travis, Go, Strider and PHP CI.

Jenkins is the CI tool used for this series and the reason for that is not far fetched — I have prior experience with Jenkins and it is the leading open-source continuous integration server. Thanks to its thriving plugin ecosystem, it supports building and testing virtually any project.


Installing Jenkins

For this article we would be installing Jenkins on Ubuntu Server 14.04 LTS. You can check here for instructions to install on other operating systems.

It’s advised to install from Jenkins repositories, so add the key to the system to gain access to the repository on line 4.

Script to install Jenkins

Jenkins is added to the sources.list file on line 5, line 6 updates repositories and line 7 installs Jenkins.

By default Jenkins runs on port 8080; visiting the address for the server <server_address>:8080, you should see this.

Source: https://www.sitepoint.com/installing-securing-jenkins/

If you see the page above, voila! You have Jenkins successfully installed.


Installing Plugins

Plugins are installed to add more functionality to Jenkins; the following plugins are needed to build PHP applications.

Additional plugins:

  • git — To pull projects from Git distributed version control systems.
  • slack — Sends build notifications to slack channels.
  • build-monitor-plugin — Provides a highly visible view of the status of selected Jenkins jobs.

To install the plugins, download the Jenkins CLI tool shown on line 4 of the snippet below.

Line 5 installs all the plugins and line 6 is to restart Jenkins.

Confirm that the plugins have been installed by visiting Manage Jenkins >> Manage Plugins then clicking on the Installed tab.

Installed Plugins tab in Jenkins

Plugins can also be installed via the Available tab on the page shown above.


Setting up NGINX as Proxy Server (Optional)

If you are ok with accessing the Jenkins server with the port number, please skip this step, if not a reverse proxy server (NGINX in this case) allows the Jenkins server to be accessed without adding the port number (8080).

To install NGINX run the command below.

Run the command below to confirm the installation:

sudo service nginx status

If the installation was successful, you should see the output below:

* nginx is running

To setup NGINX as a reverse proxy for Jenkins server, modify the contents of the /etc/jenkins/sites-enabled/default file, I have written a bash script to make the setup easier.

A quick run through of what is happening in the bash script above.

The server name is requested, if for example you have ci.adegokeobasa.me pointing to the Jenkins server, this can be entered at the prompt shown below.

Enter the server name for the jenkins server and press [Enter]

The script replaces the server_name variable with the answer provided and overwrites the /etc/nginx/sites-enabled/default file.

On line 28 we ensure that the servername points to 127.0.0.1 (loopback address) in the hosts file /etc/hosts.

Then finally restart the NGINX server on line 29.

You should be able to reach your Jenkins server without the port, so for example visiting ci.adegokeobasa.me should lead to the Jenkins server.


Conclusion

In the first part of this series, we have been able to:

  • Install Jenkins
  • Install Jenkins Plugins
  • Setup NGINX as a reverse proxy server

In the part 2 of this series, we would be looking at securing the Jenkins server. Watch Out!



Questions? Comments?, please drop your thoughts in the comments section or shoot me a mail at adegokeobasa@gmail.com. Don’t forget to hit the recommend button if you found this article useful and feel free to share with your network. 🙂