I just love the simplicity of the logoI’ve created a small application in .Net Core 2.0 that needs to run periodically on my Raspberry Pi 3. I got tired of copying the binaries to the Pi, so I build a small CI/CD pipeline that will build, test and deploy the application automatically. I’ve chosen Bitbucket because they allow me to have free private repositories for small teams.

The parts

My setup has the following parts:

Development setupCI/CD setupPi setup
  • .Net Core 2.0 / C#
  • Visual Studio 2017
  • Git repo using Bitbucket
  • Bitbucket Pipeline (yml)
  • Bitbucket Downloads
  • Optional: Tweet when done
  • Raspbian Light Stretch
  • Crontab / bash to
    update & run app

So I basically develop op my laptop. I use the pipeline to do an automatic build upon a commit. The pipeline test and store the result in the downloads. The Raspberry Pi will pull the latest binaries from the downloads, unzip it and install it. I also send myself a Tweet when the build is done.

Assumptions

The pipeline assumes the following project structure in Git:

Unit test projects are presumed to have the word UnitTest in them and to be present in the test directory. The deployment assumes the following directory on the Raspberry Pi:

The my-project.sh script will be used to schedule execution of the update of the program. In my case it will run the program as well.

Peparations

We need to create a pipeline. Go to Repositories > your repository > Pipelines. Choose the .NET Core pipeline from the select box.

This will add a .yml file to your project. We will edit this file later.

Downloads access

The pipeline will add zip to your downloads. It needs an¬†app password to do this. You’ll need to generate one the following way:

  • Login to Bitbucket
  • Click on your Avatar in the bottom-left corner > Bitbucket settings
  • Access management > App passwords > Create app password
  • Add a label and enable Repositories \¬†write
  • Click the Create button
  • Hold on to the password, we’re going to use it soon.

Twitter access

Next you’ll need to go to Twitter and create an app (apps.twitter.com) and generate a¬†consumer key, consumer secret, user access key and user access secret. Don’t know how to do it? Just use this ;-).

Environment variables

Open up the environment variables by going to Repositories > your repository > Settings >  Pipelines > Environment variables. Here we add the information of the previous steps:

The PROJECT_NAME variable will be used by the script to build the main project. It will also be used to generate a zip.

The CI pipeline

A picture says more than a thousand words. This activity diagram shows the steps we’re going to take.

 

Let’s edit the¬†yml file. Go to¬†Repositories > your repository > source >¬†bitbucket-pipelines.yml and hit the¬†edit button. Add the following code:

The pipeline will be executed when something is committed to the repository. If you want to validate your yml you can use the validator. A docker container from Microsoft will be used.

The CD script

Now that we have created the CI part, let’s create the CD part. Login to the terminal of your pi and create a new script with¬†nano /home/pi/my-project.sh and fill it with the following content:

Adjust the variables LOCAL_NAME, CSPROJ_NAME, BITBUCKET_REPO_OWNER, BITBUCKET_REPO_SLUG and BITBUCKET_AUTH. You can generate a new app password for your deployment script in the same way as you did for the pipeline (note: it only needs read access).

If you want to run the program as well, you can add the following:

Save and exit the file (ctrl+x).

Make the script executable by doing a: chmod +x /home/pi/my-project.sh. This is needed for the crontab to run the script as it only executes executable scripts. You can check if the script works by running it: bash /home/pi/my-project.sh.

Schedule update using crontab -e

Scheduling the script is easy. Run the following: crontab -e. This will open up the editor. Add the following lines:

This will update the program every 10 minutes.

That’s it! Sit back and relax. Every 10 minutes your app will be updated.