Part 8 - Setting Up Continuous Integration (CI)

This is the final post in the series of how to host your own git server on a Raspberry Pi

Throughout this series, you have successfully set up a Raspberry Pi, installed Postfix, GitLab, Nginx, and configured GitLab Runners. Additionally in the previous post, you have added SSL/HTTPs support to these services. Congratulations, these were no easy tasks!

You are nearly done — In this final post, we will go over creating a GitLab project and setting up continuous integration. We will also look into writing a CI file that will ingest commits to your project and deploy them to production.

From the main screen of the GitLab, create a new project with Initialize repository with a README option selected. This will allow you to clone the project from your Raspberry Pi to your computer immediately.

Run following command from your computer to clone the project via HTTPs:

git clone https://git.<HOSTNAME>.<DOMAIN>/<USER>/<PROJECT-SLUG>.git

Git will ask the user name and password for your GitLab account, then proceed to clone your project. You will see the downloaded project to contain a blank file.

In Part 6 of this series, we have created an index.html file to serve as the home page for our Raspberry Pi. We will use our git project to host the source files for this page.

As a reminder, the source files for this page is stored at /var/www/home/ and the Nginx configuration file is in /etc/nginx/sites-available/home on the Raspberry Pi. The page is accessible via https://<HOSTNAME>.<DOMAIN>

Create a new index.html for your home page as follows:

Home Page

Place this file in your git project repository.

GitLab uses yaml language to define CI scripts. You can create a file named .gitlab-ci.yml (with a leading dot) and add it your git project repository. The content of our CI script will look like the following:

- build
- test
- deploy

stage: build
- echo "*** Build Phase ***"
- echo "There are no build steps."

stage: test
- build
- echo "*** Test Phase ***"
- echo "There are no test steps."

stage: deploy
- test
- echo "*** Deploy Phase ***"
- echo "Current path:"
- pwd
- echo "Removing dot files..."
- find . -name ".*" -type f -delete
- echo "Removing git folder..."
- rm -rf "./.git"
- echo "Source directory:"
- ls -a
- echo "Clearing target..."
- find /var/www/home/ -mindepth 1 -exec sudo rm -rf {} +
- echo "Deploying files..."
- sudo cp -r . "/var/www/home"
- echo "Target directory:"
- ls -a /var/www/home/
- echo "Deploy success."

The script copies the project source files into /var/www/home/, replacing the old page contents. This will update the home page, essentially deploying your changes into production.

Use following commands to commit your project files to GitLab:

git add -A

git commit -m “First Commit”

git push origin main

GitLab will detect that your project contains a .gitlab-ci.yml and start the continuous integration pipeline:

This pipeline will execute each time you push a change to your project, deploying the new version of the home page.

When you check the home page for your Raspberry Pi, following https://<HOSTNAME>.<DOMAIN>, you will see the contents of the home page is updated!



You can follow my future articles on building Internet of Things devices and general chat regarding to life at Google at Baking Pi - An IoT Blog. :)

Systems Engineer, specialized in building Internet of Things (IoT) devices and applications. Harvesting crops at Google. 💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store