Jenkins – Connecting To GitHub and some Python

Welcome to Jenkins!

Jenkins is a tool to help with Continuous IntegrationContinuous Delivery (CI / CD). I originally looked at installing Jenkins as a Java web app on my Raspberry Pi during this post: https://geektechstuff.com/2019/05/26/installing-apache-tomcat-jenkins-raspberry-pi/ . I am now hoping to expand a little bit more on using Jenkins by building a freestyle project that connects to GitHub.

Jenkins is available for free from: https://jenkins.io

geektechstuff_jenkins_2
Jenkins

As noted above, I was originally using Jenkins on my Raspberry Pi which has since gone through a rebuild and changed purposes in my environment. I’m now going to run Jenkins in a virtual environment, using Oracle’s VirtualBox. VirtualBox can be downloaded for free from: https://www.virtualbox.org

Oracle Virtual Box Webpage
Oracle Virtual Box

The virtual environment I am going to use is CentOS, a Linux distribution based on Red Hat Enterprise Linux (RHEL). I like switching up between Linux distributions every so often just to see if I’m missing out on anything. CentOS can be downloaded for free from: https://centos.org/

CentOS Webpage
CentOS Webpage

I’m possibly going to look at setting up a virtual computer environment in the near future / in a future blog post.

Installing Jenkins in Raspbian (or running Jenkins from .war file)

My post here: https://geektechstuff.com/2019/05/26/installing-apache-tomcat-jenkins-raspberry-pi/ explains how to install Jenkins from a .war file.

Installing Jenkins in CentOS

From the terminal, first check that Java is installed using:

Java -version

If Java is not installed then the Java JDK can be installed using:

sudo yum install java-1.8.0-openjdk

Jenkins can then be installed using:

sudo yum install jenkins

If the CentOS firewall is enabled (for security it should be) then make sure to configure it to allow access to Jenkins. By default Jenkins tries to run on port 8080 so that access to it in a web browser is http://IP-ADDRESS-OF-DEVICE-RUNNING-JENKINS:8080. The firewall settings to allow Jenkins on port 8080 are:

firewall-cmd –permanent –new-service=jenkins
firewall-cmd –permanent –service=jenkins –set-short=”Jenkins Service Ports”
firewall-cmd –permanent –service=jenkins –set-description=”Jenkins service firewalld port exceptions”
firewall-cmd –permanent –service=jenkins –add-port=8080/tcp
firewall-cmd –permanent –add-service=jenkins
firewall-cmd –zone=public –add-service=http –permanent
firewall-cmd –reload

After installing Jenkins and configuring the firewall, it’s time to start it with:

sudo service jenkins start

The Jenkins service can also be stopped with stop or restarted with restart.

Initial Set Up

On first start up Jenkins will ask what plugins are needed. Unless you have a specific need I recommend accepting the recommended plugins and proceeding.

Jenkins is configuring
Jenkins is configuring

Jenkins will also ask for a key from Jenkins files, the initial start up page will show where this key is

Creating A Freestyle Project / Build

With Jenkins started and set up we can now create a Freestyle Project. This project requires that Python3, pip and Git are all installed on the system running Jenkins.

geektechstuff_jenkins_13
Jenkins Freestyle Project – General

I am going to use my Python Flask project as the source, and as it is hosted on GitHub I am going to choose “GitHub project” and enter the URL for the project.

geektechstuff_jenkins_11
Jenkins Freestyle Project – Source Code Management

I am also going to enter the same GitHub URL for the Git repository URL. This will allow Jenkins to copy the files from the repository for use in the freestyle project.

geektechstuff_jenkins_12
Jenkins Freestyle Project – Build Triggers

There are multiple possibilities for the build trigger, but for my test freestyle project I am going to use the “build periodically” option, which uses the same options as cron. The option I’ve entered is H/10 * * * * which sees the build attempt to run every ten minutes.

geektechstuff_jenkins_14
Jenkins Freestyle Project – Build

For build options I am using the “execute shell” option to run several commands:

PYENV_HOME=$WORKSPACE
python3 -m venv $PYENV_HOME
. $PYENV_HOME/bin/activate
pip install -r requirements.txt

These commands create a virtual Python environment , activates it and then uses pip to install all the requirements for my project.

geektechstuff_jenkins_3
Jenkins – Success (sunshine)

If Jenkins completes these commands then the build is a success, if not then a failure has happened and it would be time to investigate by looking at the console output.

geektechstuff_jenkins_4
Jenkins – Failure (Storm clouds)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.