Using FreeBSD with GitHub Actions
Server
Published: 2024-12-22

This article is the 22nd entry in the Sakura Internet Advent Calendar 2024.

Introduction

There are times when you want to use FreeBSD with GitHub Actions.
In fact, since I use FreeBSD with Sakura Internet, there are occasions where I absolutely need to have it.

Currently, I am building in an SSH environment with Jenkins and FreeBSD, which is quite a laborious process, but if it can be done with GitHub Actions, I would prefer to do it that way.

Using Cross-Platform GitHub Action

With the Cross-Platform GitHub Action, you can use FreeBSD.
This allows you to run other operating systems as a VM on GitHub Actions using Qemu.

It’s quite a heavy-handed approach, but it’s great that FreeBSD can be utilized in GitHub Actions.

Creating Actions

First, create a repository to use Cross-Platform GitHub Action.

$ git init actions_freebsd
$ cd actions_freebsd

Next, create the .github/workflows directory and then create a main.yml file inside it.

$ mkdir -p .github/workflows
$ vi .github/workflows/main.yml

Add the following content to main.yml.

name: FreeBSD

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: test freebsd 13.4
        uses: cross-platform-actions/action@v0.26.0
        with:
          memory: 512M
          operating_system: freebsd
          version: "13.4"
          shell: sh
          run: |
            uname -a            

Then push to git to trigger GitHub Actions.

$ git remote add origin git@github.com:masa23/actions_freebsd.git
$ git add .
$ git commit -m "first commit"
$ git branch -M main
$ git push origin main

When you run GitHub Actions, it will execute uname -a on FreeBSD 13.4.

Check the results of the actions first commit #1.

I was able to easily use FreeBSD 🎉

Custom FreeBSD Image

Creating a Custom Image

Using freebsd-builder, you can utilize a custom FreeBSD image.

In the actual environment, I specify and use a customized image.
Embarrassingly, I have old FreeBSD versions like 11.2 running and sometimes need an image with golang installed, so I prepare and utilize a custom image.

This time, I will create an image with gcc installed to use golang and cgo on FreeBSD 13.4.

Clone the repository and create a custom image.

$ git clone https://github.com/cross-platform-actions/freebsd-builder.git
$ cd freebsd-builder
  • I am using custom.sh to create a custom image.

Installing gcc and Go to enable CGO builds.

$ vi resource/custom.sh
#!/bin/sh

set -exu

# Add your additional provisioning here for custom VM images.

GOLANG_VERSION=1.23.4

pkg install -y gcc
fetch -o - https://go.dev/dl/go${GOLANG_VERSION}.freebsd-amd64.tar.gz | tar xz -f - -C /usr/local/
ln -s /usr/local/go/bin/go /usr/local/bin/go

Setting Up the Build Environment

sudo curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install -y qemu-system-x86 packer
packer plugins install github.com/hashicorp/qemu

Building the Image

PACKER_LOG=1 sudo -E bash ./build.sh 13.4 x86-64 -var headless=true
# ls -lh output/
total 664M
-rw-r--r-- 1 root root 664M Dec 20 16:29 freebsd-13.4-x86-64.qcow2

Release

$ gh release create v0.0.1 --title "FreeBSD 13.4 add golang" --notes "FreeBSD 13.4" output/freebsd-13.4-x86-64.qcow2

Running CI with the Image

name: FreeBSD

on: 

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: test for freebsd 13.0
        uses: cross-platform-actions/action@v0.26.0
        with:
          memory: 512M
          operating_system: freebsd
          version: 13.4
          image_url: https://github.com/masa23/freebsd-builder/releases/download/v0.0.1/freebsd-13.4-x86-64.qcow2 # Specify the URL of the custom image here
          shell: sh
          run: |
            export CC=gcc
            go test -v ./...            

Checking the Results of Actions

Check the results of the actions golang test cgo #3.

Successfully executed CI using the custom image.

Conclusion

  • I introduced how to use FreeBSD with GitHub Actions.
  • It is important to be cautious about memory usage since using Qemu can be quite resource-intensive, but I was able to easily use FreeBSD.
  • Since using Qemu may prolong CI time, it seems better to limit its use to Pull Requests or Releases.