Stanley Solutions Blog

engineering and creativity - all under one hat

GitLab, Jenkins, Python, and the Raspberry Pi!


Think that's enough buzz words to catch the Google SEO engine's eye?

Probably not, I know, but I'm not going to spend anymore time on it at the moment. See, I've got bigger items to tackle! Namely, getting Jenkins set up on a Raspberry Pi, as the name of this article so implies.

Good news for you; I've cut out the "dirty-work" through the magic of "blog-posting."

As part of the work I've been tackling for some of the other open source projects I'm developing, I need to develop a local (on-premises) continuous integration solution to effectively slam my code with testing and verification. After all, what's great code without equally great tests? I need to have the system on-premises for a couple reasons; the largest of which being the fact that I need access to custom hardware.

So, why a Pi? A Pi 3-B no less?!

Well, that's quite simple; actually. It's the only spare computer I have at the moment.

So now that I've thoroughly introduced you, to my reasoning, and the topic at hand; let's get into it!

Installing Jenkins on the Pi

I already have GitLab set up on an old x86 laptop running Ubuntu Server 20.04, so for this article, I'm going to focus on setting up Jenkins on a Raspberry Pi, and getting the basics of the workflow between Jenkins and GitLab running.

  1. Start with a fresh Pi (latest build of the RaspberryPiOS). I had a Pi sitting around with an older build of Raspbian, but that's several years old, and I really just wanted to start fresh.

  2. Update the Raspberry Pi. Well, in the spirit of starting fresh, might as well update the system!

  3. Install Java with:

    $> sudo apt-get install openjdk-11-jre
    
  4. Verify Java Version with:

    $> java --version
    openjdk 11.0.9.1 2020-11-04
    OpenJDK Runtime Environment (build 11.0.9.1+1-post-Raspbian-1deb10u2)
    OpenJDK Server VM (build 11.0.9.1+1-post-Raspbian-1deb10u2, mixed mode)
    
  5. At this point, I took some time to get the Python system up to a state that would be a bit more useful for me. So I installed pip3, and a number of Python packages. I suppose this could really be done at any point during this whole process, but I felt like this was the most sensible time.

  6. Download and add the Jenkins Key with:

    $> wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
    
  7. Open a new file:

    $> sudo nano /etc/apt/sources.list.d/jenkins.list
    

    Then add the following line and save the file to add the Jenkins repository as a source:

    deb https://pkg.jenkins.io/debian binary/
    
  8. Next, another good sudo apt-get update is in order, followed by sudo apt-get install jenkins

  9. Using the command listed below, you can grab the initial admin password to get started:

    $> sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  10. Now it's time to navigate to <raspberry-pi-ip-address:8080 and use that fancy password to log in for the first time and start the setup wizard; or should I say butler?

After the "butler" has completed, it's time to get started with setting up some CI jobs.

Preparing a Simple pytest Job with Jenkins

Now, I'll caution that I this portion doesn't cover any of the GitLab/Jenkins interfacing, maybe I'll get to writing that in another article... As part of the material I'm skipping, I'm going to breeze right over the GitLab connection and repository information. I'm going to focus, instead, on the build operations.

  1. With the new Jenkins server up and running, create a "New Item," give it a descriptive, memorable name, and set it as a "Freestyle Project"

    Create a new project in Jenkins for CI.
  2. After configuring the various other settings relevant to the project (repository, build-triggers, etc.) find the "Build" section and from the "Add build step" select "Execute shell".

  3. In the new "Command" field of the "Execute shell" section, insert the commands necessary to navigate to the appropriate subdirectory and run pytest. In my case, my pytest "test folder" is located in the root directory, so I don't really need to change the working directory; I just go and run pytest. I do run a few other generic commands just to make sure that I've got a fair report of the build environment in case I need to go back and debug some things. So, here's a sample of what my configuration might look like.

    echo "Current Directory"
    pwd
    echo "List Folder Structure"
    ls -a ./<name-of-my-python-package-folder>
    echo "Run pytest"
    pytest -v
    

Summary

Well, that's a pretty rough intro into what I've been doing in Jenkins and GitLab. Kinda rough, but I hope I'll be looking to add more in the near future.