How to Set Up a Raspberry Pi V: Set Up Build to Raspberry Pi Using Azure DevOps

If you want to run a website from your Raspberry Pi, there is good news for you, it can be done and I'll show you how!

If you want to run a website from your Raspberry Pi, there is good news for you. It can be done! If you read my other blog post, Creating a .Net Core Website on Your Pi. If you want to automate your builds and deploy them to a Raspberry Pi, you’ve come to the right place. This makes it so that you can do all of your development on a windows environment (or any environment, or from multiple environments) and deploy seamlessly with a click of a button to a Raspberry Pi. Bad news, it’s not very easy to figure out how to set it up initially. As far as I know, there isn’t a good tutorial on how to do this. So, here it goes.

Excited guy with text "Get Ready"

Prerequisites

1. Get a website working. I’m using a .NET Core app with REACT.

2. Get an account on Azure DevOps –  I will admit, this is the only thing that I never actually did myself. I used an existing account to do this.

3. Have a repository where you store your .NET Core solution.

Setting up Your Website

1. Go to dev.azure.com/azureaccount

2. In the top right corner, click on New Project 

"New Project" button

3. Fill out your project name and description and click Create 

new project fields

It will give you quite a few options on the side. You really aren’t interested in any of them except pipelines for this tutorial.

If you are unfamiliar with pipelines, then know that there are two main kinds of pipelines: build and release. 

The build pipeline tells Azure where to go to build something. So in my case, I’m getting my code repo from git and building it on my Raspberry Pi. This creates a build artifact. Once you have a build artifact, you can publish/deploy it using the second main kind of pipeline – the release pipeline. 

The release pipeline takes your build and publishes it to wherever you want it published to. In my case, I’m taking my build artifact from my Raspberry Pi and then publishing it to the web directory on the same Raspberry Pi.

So with our next steps, we need to set up a build pipeline and a release pipeline. The release pipeline will be covered in the next blog post.

Setup the Agent Pool

You can have Azure spin up its own server for you to build stuff for every time you run the build Pipeline. It’s probably easier than doing what I’m about to show you. However, I didn’t want to incur any charges to the account I was using, so I needed to build on my own equipment. If you have a default account, you can use the Azure (I think it’s just the default one) agent pool with enough free build minutes that you probably won’t have to worry about the cost. If that’s the case, you can just skip down to where we set up the release Pipelines. It won’t make me sad because I don’t actually know you’re reading this in the first place.

1. Before you can create the agent, you need to create a personal access token so that your Pi can authenticate with Azure. To do this, click on the little user Icon next to your user badge in the top right. Then click on Profile.

Example of creating a personal access token

2. Click on New Token, Then fill out the agent according to what’s on the screenshot (Your organization name is specific to your organization) and name it whatever you want. Then click Create.

Create a new personal access token

3. Copy your Personal Access token. You will use it later when configuring your Raspberry Pi.

4. Agent Pools are not on a project level. They are an organizational thing, that way, if you have a build server, you can have a bunch of different projects use the same build server whenever you need it. So, go to your home page at dev.azure.com/azureaccount.

5. In the bottom left-hand corner, click on Organizational Settings

Organization settings

6. Go to Pipelines -> Agent pools

7. Click Add pool and add a name/description and submit

8. Now, you need to access azure from your Raspberry Pi. So, log in to your Raspberry Pi, and navigate to the Agent pool you just created

9. Now Click on Agents and click on New Agent 

10. Select Linux and then ARM. Then click the Download button to download the agent

Download button

11. Then you want to run the commands listed from a terminal window on your raspberry pi.

mkdir myagent && cd myagent
tar zxvf ~/Downloads/vsts-agent-linux-arm-2.166.3.tar.gz
./config.sh

When you run this command, it’s going to ask you some questions

  • You need to accept the user agreement
  • Your server URL is the URL to your DevOps so it would be https://dev.azure.com./organizationname
  • For Authentication type, type PAT
  • Copy in your personal access token that we created earlier for the next step
  • Copy/Paste in the name of the agent pool you created earlier
  • Agent Name – This can be whatever you want that will help you remember what machine it’s on
  • Work Folder – This is the location that the build will be stored when you run builds on the machine
    ./run.sh

 

12. To see if the agent is online, go to dev.azure.com/organization -> Organization Settings (in the bottom left) 

Agent polls options

Click on the Agents tab to see if your agent is running. If it isn’t, then go to your terminal window where you installed it (in the case of this tutorial, it was installed at ~/myagent), run the command ./run.sh and it should soon enable your agent. If it still doesn’t work, you may have to recreate the build agent, or reboot the raspberry pi and try again.

Agents tab

Create the Pipeline

1. Go to Pipelines on the side navigation menu, then click on New Pipeline in the top right of the screen. 

New Pipeline button

2. There are two different ways that you can do an Azure Pipeline. YAML, or the Microsoft designer. Ideally, YAML gives you more control, but I chose the Microsoft designer way since it’s easier to start with. I also found YAML to be really boring. So, in my case, I had a Git repo on the very same Azure Account, but I chose Other Git for this so I could get the Microsoft designer features for the Pipeline. Note that you can switch to YAML if you want to later. However, if you choose any of the options tagged YAML, you can’t get any Microsoft designer features. 

other git selection

3. Now, you can select your Azure Git Repo here; just give it the information you need (If you are using another repository, select something else). Click Continue

selecting your Azure Git Repo

4. Next is your basic templates. Since this tutorial is running with a .NET Core application, I’d suggest selecting that just to get you started. 

.NET Core selection

5. This gets you set up with a basic starter pipeline. There is one thing I recommend changing about the default, and it’s on the publish step. So click on the Publish step, and add -r linux-arm into the Arguments text box.

Example of changing the default during the Publish step

The Artifact staging directory doesn’t matter all that much because you are grabbing from that same directory in the release pipeline that we will be setting up later.

6. Click on Save (Not save and queue as we aren’t fully set up yet).

Make the Build on Your Pipeline

Now that the Pi is set up as an agent, it’s time to finish the pipeline build.

1. Click on your Pipeline, and select the Agent Pool that you made earlier.

2. Click Queue to run the Pipeline.

Queue button

3. To see if your Pipeline is running, go back over to your Raspberry Pi and look at the agent that you started when you typed in /run.sh, it should look like this

example of what your Raspberry Pi should look like

4.  You can also view the status of the build by clicking on the running job in the Pipeline.

status of the build

5. Now, if you’re me, you ran into this issue: 

Error prompt

This means that you need to install node and npm. To do this, visit and follow the instructions on the npm site on your Raspberry Pi.

Note that on these instructions, it’s going to have you run a command tar gzipped file. I’ve found that this doesn’t work, and you may have to open up your file explorer, right-click on the downloaded file, and click on extract here for the step. Other than that, it will work just fine.

screen capture showing "extract here" option

6. Queue the build again in Azure DevOps, and see what happens.

7. Note that with your first publish/build, it may take some time for it to gather all of the resources it needs (for me, it took about 8 minutes to build on the Raspberry Pi 4).

Create the Build Services

You still aren’t done. Why? 

gif representing a "sigh"

Well, what happens when your Pi reboots? You have to manually run the ./run.sh and dotnet command to spin up your solution. So, we need to create services to automatically restart those things when your Pi restarts; here’s how to do that.

1. Create a Service for your dotnet app (and replace any appname texts with your app name)

  • Type in sudo nano /etc/systemd/system/appname-kestrel.service
  • Copy this into the opened text file (you may have to adjust where you put your solution files if it was different from how I had it set up).

[Unit]
Description=ASP.NET Core 3.1 App – App Name

[Service]
WorkingDirectory=/var/www/appname
ExecStart=/home/pi/dotnet/dotnet /var/www/appname/appname.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-appname
User=pi
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

  • Close the app that is running your dotnet project, then run these commands

sudo systemctl enable appName-kestrel.service

sudo systemctl start appName-kestrel.service

  • Try to get to your site and see if it works.

For me, I got a problem that happened where it kept on trying to run the site under https. I didn’t really want this to happen as I didn’t have https configured, so I went into my .net core Startup.cs and commented out this line, ​// app.UseHttpsRedirection();

showing command ​// app.UseHttpsRedirection();

Then I re-ran the build and copied it back to my web directory again.

After this, it should automatically start your web site even if your Pi has to reboot. There. One less thing to worry about. 

2. The build service – this is a bit easier to do, just go to the directory you installed it on and run these commands (cd ~/myagent)

sudo ./svc.sh install
sudo ./svc.sh start

3. You can then verify that it’s working by going to the Agent in Azure DevOps, it should say Online.

Verifying the Pi is working via the Agent in Azure DevOps

There. Phew. That was a long process to get the build server up and running. The next step is to set up your Pi release Pipeline. That way, your build can actually run as a website there. It’s kind of an involved process by itself…that’s why it’s in my other blog post.

Subscribe to Our Blog

Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.