CI/CD on the Edge — Part4

Implementation: Step by Step guide

Ashish Kaul
NATIX
5 min readMar 3, 2021

--

In the previous parts (1, 2, 3) we discussed the existing challenges, principles, and a technical design that is necessary to build delivery pipelines for edge networks. In this article, as the last part, a walk-through guide is provided to help the readers to set up their own CI/CD for their edge network.

Cloud Setup

Once a cloud server is created, the next step is to install Jenkins and run it as a service. To run Jenkins as a service, connect to your server (via SSH or PuTTY), execute this command:

sudo service jenkins start

Finally, to automatically start Jenkins when an instance is started, the following command needs to be executed:

sudo chkconfig - add jenkins

Jenkins runs on port 8080 on localhost. But to access Jenkins outside of the instance port8080 needs to be exposed to incoming requests to the instance.

PlatformIO Setup

The PlatformIO setup is further broken down into two more subsections, one as integration with Visual Studio Code IDE and another PlatformIO Core on a cloud server as a Command Line Interface (CLI)

A prerequisite for this setup is the installation of Visual Studio Code, which could be downloaded from the official website. The link is provided in the Appendix. After installation, search for the “PlatformIO” extension in the Visual Studio Code package manager and install the PlatformIO IDE.

With Visual Studio IDE

PlatformIO can be added as an extension to Visual Studio IDE.

PlatformIO extension

Once added it provides the opportunity to create a new project including the possibility to select the type of board and also the framework of the board.

Creating a new project using PlatformIO extension

A project created with PlatformIO extension has a structure as follows (all the content shown below may not be applicable to your project):

$ tree
├── README.md
├── lib
│ └── mod1
│ └── src
│ ├── mod1.cpp
│ └── mod1.h
├── src
│ └── main.cpp
├── platformio.ini
├── .travis.yml
├──test
│ └── native
│ ├── test_first.cpp
│ ├── nodemcuv2
└ └── test_main.cpp

PlatformIO Core

PlatformIO Core needs to be installed on two machines. One is the cloud server that has Jenkins running on it. The second is the edge device or SBC which is connected to the ESP-32 board.

To install PlatformIO Core the prerequisite is the installation of Python and preferably the Python package manager “pip”.

sudo yum install python3-pip

Post-installation of Python and “pip” PlatformIO can be installed using:

sudo pip install -U platformio

Jenkins Pipeline

Jenkins, as implied in previous parts, is fundamentally an orchestrator that automates different tasks and in turn different processes. Jenkins achieves this feat primarily using two ways described below. We would be using the 2nd option.

  1. A Freestyle Job, which is useful for performing sequential tasks. This job is less versatile when modelling complex real-world requirements.
  2. A Pipeline Job, which can have other types in Jenkins like the Multi-branch pipeline, but essentially models a users’ process (usually CI-CD process).

When creating a Pipeline job in Jenkins one can either write a Pipeline script, written in Groovy language, or write the same script in a separate file called the Jenkinsfile. The choice of creating Pipeline jobs, using Jenkinsfile, in Jenkins is driven by the ability of a team to version, edit, review and iterate upon their delivery pipeline since it is checked into their VCS. A pipeline script or a Jenkinsfile are of two types:

  1. Declarative Pipeline
  2. Scripted Pipeline

Declarative Pipeline is a more recent feature from Jenkins and it improves the readability of the Pipeline code along with providing richer syntactical features.

pipeline {
agent any
stages {
stage('Build') {
steps {
}
}
}
}

In layman's terms, this code defines a “Build” stage and some of the steps to be performed as part of that stage, while the Pipeline is executed on any of the available agents. The Pipeline here is the model for the CI-CD pipeline and is a descriptor of the entire build process. A stage block conceptually divides a
set of tasks performed during the execution of the pipeline while the step corresponds to a single task inside a stage.

Adding PlatformIO token to Jenkins

It will be necessary to get & save our PIO Token — PLATFORMIO_AUTH_TOKEN flag in order to authenticate the CI server later. We can do this the following way:

pio account login
<your login data>
pio account token
<Output>
Password:
Personal Authentication Token: xxxxxx11122233444aaabbbcccddee

Copy the token and store it somewhere else for later usage. The token can be added as a “Secret text” credential under “Global credentials” in Jenkins.

Jenkins PlatformIO auth token

Execution

To execute the job to build and deploy the code changes on to the edge device, you can add the following code to the build stage of your Jenkins pipeline script (see the previous section). The documentation for commands is available on the PlatformIO website.

script.withEnv(['PATH+PIO=/usr/local/bin/']){
script.withCredentials([script.string(credentialsId: '<token name here>', variable: 'token')]){
script.sh("set +x")
script.sh '''
PLATFORMIO_AUTH_TOKEN=token
'''
<.sh("pio run -t upload")
}

if you are interested to know more about the implementation, you can find the full project here.

This article is part of a series called “CI/CD on the Edge” (part 1, part 2, part 3, part 4) which has been extracted from the author’s thesis submitted for a master’s degree at the University of Applied Sciences, Kiel.

Note: For the purpose of this article, the word “embedded” has been interchangeably used with “edge”

DISCLAIMER: This post only reflects the author’s personal opinion, not any other organization’s. This is not official advice. The author is not responsible for any decisions that readers choose to make.

--

--

Ashish Kaul
NATIX
Writer for

A software engineer with MS in information engineering. Currently working in Germany