Build, distribute and run binaries using Github
In short
In this example we show how to use Github to deploy a binary file to your fleet of Raspberry Pis. The specific binary is written in go. The process starts by pushing the changes of your source code to your Github repository. Then, using Github actions, your source code is cross compiled for your Raspberry Pi and finally the binary is placed into your qbee.io file manager. From there on you can distribute and run it on the group of devices you have specified.
If we want to build an executable (called hi-pi) for our Raspberry Pi we can use the following code
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello Norway")
}
we can easily do that locally using
$ env GOARCH=arm GOOS=linux go build -o hi-pi main.go
However, we aim to setup an automatic workflow, where we
- use git as for version control management
- setup a Github runner (using Github actions) to build our code once a push is triggered
- copy the created binary to the
qbee.io
file manager via API calls - use the
qbee.io
file distribution to the distribute (and run) the executable on a list of devices
How to create a repository on Github
Check out the official documentation on how to create a repository on Github.
Once a repository is set up, before creating a runner up Github secrets as shown in the following screenshot.
There we specify our
qbee.io
username and password, as we do not want them to be exposed.
Once that is done, we create a Github actions which runs our setup script on the runners provided by Github (or you can set up your own runners). Github actions can be created as follows (we use "set up a workflow yourself")
The script we used is the following:
go-package-distribution.yml
name: Go package distribution
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
env:
APPNAME: hi-pi
steps:
- uses: actions/checkout@v2
- name: create go executable
env:
GOARCH: arm
GOOS: linux
run: go build -o $APPNAME main.go
- name: copy to qbee file manager
run: |
curl --digest -u ${{ secrets.QM }}:${{ secrets.QP }} -i -X "DELETE" -d "path=/$APPNAME" -H "Content-type: application/x-www-form-urlencoded" https://www.app.qbee.io/api/v2/file
curl --digest -u ${{ secrets.QM }}:${{ secrets.QP }} -i -X POST -H "Content-Type:multipart/form-data" -F "path=/" -F "file=@$APPNAME" https://www.app.qbee.io/api/v2/file
Comments on the yaml file
- this action is triggered on pushes to the
master
branch - the runner is a virtual machine with an
ubuntu
OS as we specifiedubuntu-latest
- on that virtual machine (freshly created for you on every single run) the code from your repository is checked out using the
actions/checkout@v2
option - we set up a step, where we build the go application using cross compilation (due to the specified environment variables
GOARCH
andGOOS
- the output is placed into the root directory of your
qbee.io
file manager (c.f. file distribution via API)
Note that your credentials aren't revealed as we use Github secrets to encrypt them as you can see in the action output
Finally, we distribute our files to the remote devices as usual with the file distribution.
Automatic deployment
Using this workflow, every time you push your code changes to the repository the output binary (or binaries) will always be replaced within the qbee.io
file manager. If the binary file differs from the one previously placed in the file manager, then the qbee.io
file distribution is triggered along with the run command that you provided.
Hence, all your edge devices are updated by a simple git push :)