- Shell 91.1%
- Dockerfile 8.9%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
Some checks failed
/ image (push) Failing after 12m22s
actions/tasks doesn't list queued jobs, and a second push cancels the build already running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> |
||
| .claude/skills/release | ||
| .forgejo/workflows | ||
| test | ||
| compose.yaml | ||
| Dockerfile | ||
| entrypoint.sh | ||
| LICENSE | ||
| README.md | ||
| runner.yml | ||
| vm-init.sh | ||
Forgejo Actions in Firecracker microVMs
Every CI job runs in its own throwaway VM with its own kernel and its own Docker. You pick the VM size per job, Blacksmith-style:
jobs:
build:
runs-on: fc-4vcpu-16gb # 4 vCPUs, 16 GB of RAM
host
└─ runner container (entrypoint.sh: network, base disk, templates, one loop per runner slot)
├─ Firecracker microVM (vm-init.sh: dockerd + forgejo-runner one-job)
│ └─ job containers
└─ … one VM per slot in RUNNERS, plus one per queued job of any other size (on demand)
Use it
- Check that the host has KVM:
ls /dev/kvm. On a cloud VM, this needs nested virtualization. - In Forgejo, go to Settings → Actions → Runners → Show registration token and copy the token. This works at site, org or repo level. Don't use Create new runner: it gives a UUID and token for a single fixed runner, which isn't what this setup needs. From the server shell,
forgejo actions generate-runner-tokengives the same registration token. - Put
FORGEJO_URLandRUNNER_TOKENintocompose.yaml, setRUNNERS, then rundocker compose up -d --build. The first build compiles the kernel, which takes about 3 minutes. - In your workflows, use
runs-on: fc-<N>vcpu-<M>gbfor any size listed inRUNNERS. - Optional, for any other size on demand: create an API token (Settings → Applications) that can read the level the registration token came from (
read:adminfor site level), put it intoFORGEJO_TOKEN, and setFORGEJO_SCOPEto match. Needs Forgejo 15 or later.
docker compose logs runner shows every VM's console, prefixed with its slot, e.g. [fc-4vcpu-16gb#3].
Settings (compose environment)
| Variable | Default | |
|---|---|---|
FORGEJO_URL |
(required) | |
RUNNER_TOKEN |
(required) | Registration token. It stays in the container and never enters a VM, and neither does FORGEJO_TOKEN. |
RUNNERS |
2vcpu-8gb=1 |
How many VMs of each size to keep warm, e.g. 2vcpu-8gb=4 8vcpu-32gb=1. Each one runs one job at a time and gets the label fc-<N>vcpu-<M>gb. RAM is a ceiling: a VM only uses what it touches, and an idle one sits at about 250 MB. Empty (RUNNERS: "") keeps none warm, so every job is on demand. |
JOB_IMAGE |
ghcr.io/catthehacker/ubuntu:act-24.04 |
The job container when a workflow doesn't set container:. It's pulled into the base disk at startup, so jobs don't pull it. The default is Ubuntu 24.04 with what workflows expect from GitHub's runner: sudo, the Docker CLI, Node 20/24, jq, Python, and the env vars setup-* actions read. |
VM_DISK_GB |
20 |
Disk per VM. Sparse: it only uses what the job writes. |
FORGEJO_TOKEN |
(unset) | API token that turns on on-demand sizes. Read access is enough. |
FORGEJO_SCOPE |
admin |
Where to look for queued jobs: admin (whole site), orgs/<org>, repos/<owner>/<repo> or user. Use the level the registration token came from. |
REGISTRY_MIRROR |
(unset) | Docker Hub pull-through cache. The compose file points it at the bundled registry. |
Limits for all VMs together: set cpus: and mem_limit: on the runner service. Every VM is a process inside the container, so Docker's limits cover all of them at once. At startup the log prints what RUNNERS adds up to next to the limit.
- CPU: going over is fine. Each VM still sees its full vCPU count, but busy VMs share the CPU time and run slower.
- RAM: going over is a bet that not every VM is busy at once. A VM only uses the RAM it touches, but a long build can touch nearly all of it, because Linux fills spare RAM with cache. If the busy VMs together go over
mem_limit, the kernel kills one of them and its job fails. To rule that out, setmem_limitto at least the sum of the RAM inRUNNERSplus about 1 GB.
On-demand sizes
With FORGEJO_TOKEN set, a job can ask for any size, not only the ones in RUNNERS. Every 3 s the container asks Forgejo for queued jobs. For each job whose runs-on is a single fc-<N>vcpu-<M>gb label that RUNNERS doesn't list, it starts one VM. That VM claims exactly that job by its handle (forgejo-runner one-job --handle) and is thrown away afterwards.
- The first job of a new size builds that size's template first, which adds a few seconds. Later jobs of that size restore it like a warm slot does.
- Sizes in
RUNNERSstay with their warm slots. On-demand VMs never start for them, even when those slots are busy. An on-demand VM racing an idle slot for a job would leave an unused runner behind. - RAM cap: on-demand VMs together never get more RAM than
mem_limit(or the host's RAM if it isn't set). A job that doesn't fit waits until one finishes, and the log says so once. Warm VMs don't count against this cap, so warm and on-demand VMs together can still go overmem_limit, with the same risk as above.
How it works
At startup:
mke2fs -dturns the guest filesystem shipped in the image into a 20 GB sparsebase.ext4.- A one-time "bake" VM boots on
base.ext4, pullsJOB_IMAGEinto it, and shuts down. From then on,base.ext4is never written again. - For each size in
RUNNERS, a template VM boots up to "Docker is running, waiting for a job". Firecracker snapshots it there. The snapshot only stores the memory the VM touched, about 500 MB for an 8 GB VM.
Per job, in each slot:
- Register an ephemeral runner. Forgejo deletes it after one job, so a job that breaks out into the VM finds no reusable credential.
- Restore a VM from its size's template. That skips the boot and the Docker startup entirely.
- Its root disk is a device-mapper snapshot: reads come from the shared
base.ext4, and writes go to a private sparsecow.img. Each job starts with a copy of the template's ~30 MBcow.img. - Clones share the template's memory file through the host page cache.
- Its root disk is a device-mapper snapshot: reads come from the shared
- The VM fetches its config over vsock: IP, current time, and the runner file. It runs
forgejo-runner one-job --wait, reports the exit code over vsock, and reboots, which ends Firecracker. The slot deletes the VM's files and starts over. - A non-zero exit code means the runner itself broke (a failed job still exits 0), so the slot waits 30 s before retrying.
Networking: VM n is 172.16.1.n on tap<n>, behind NAT. Every tap has the same gateway address, so all clones of a snapshot see an identical network. VMs can't reach each other. Their DNS goes to dnsmasq in the container, so compose service names such as registry resolve from VMs and job containers.
Measured with test/run.sh (3 jobs on 2vcpu-8gb=2 4vcpu-16gb=1, node:22-bookworm):
| Version | Wall time for 3 jobs |
|---|---|
| Cold boot, fresh disk | 25 s |
| + shared disk with baked image | 7 s |
| + snapshot restore | 6 s |
The image went from 1.5 GB to 553 MB.
Files
Dockerfile: builds the kernel (kernel.org 6.18 with Firecracker's guest config, plus device-mapper), the guest filesystem (Alpine + Docker + git + forgejo-runner), and the final Alpine image. All versions are at the top.entrypoint.sh: startup and the per-slot loop.vm-init.sh: PID 1 inside every VM.runner.yml: forgejo-runner settings inside every VM. It mounts the VM's Docker into job containers, so jobs candocker build,docker runanddocker push. That's safe because the VM is thrown away after the job.compose.yaml: what you deploy..forgejo/workflows/image.yml: builds this image on its ownfc-4vcpu-8gbrunners and pushes it topkg.bar/firecracker-forgejo-actions(:<year>.<month>-<run number>and:<commit>always,:latestfrommain) and creates a Forgejo release for every push tomain(a prerelease for every PR). It needs the repo secretsREGISTRY_USERNAMEandREGISTRY_PASSWORD. A build takes about 6 minutes on 4 vCPUs, mostly the kernel.test/run.sh: end-to-end test. It spins up a throwaway Forgejo, pushes jobs of two warm sizes and one on-demand size, and checks each job's vCPU count, RAM and clock, and that a job candocker build.KEEP=1 ./run.shleaves the stack running afterwards for poking around.
Known limits
- Jobs can reach anything the container can, such as your LAN and Forgejo. Cloud metadata (
169.254.0.0/16) and other VMs are blocked. Addiptables -A FORWARD -i tap+ -d <net> -j DROPlines inentrypoint.shto fence off more. - Stopping the container leaves one offline runner per idle slot in Forgejo's list. They never ran a job, so Forgejo never deleted them. The same happens when a job is cancelled after its on-demand VM registered. Remove these by hand if they bother you.
- Sizes are fixed per slot. A queued
fc-8vcpu-32gbjob waits for that exact size even if smaller slots are idle. WithoutFORGEJO_TOKEN, a size that isn't inRUNNERSnever runs. JOB_IMAGEis pulled once per container start. Restart the container to pick up a newer image under the same tag.- Snapshots use Firecracker's "diff" type, which Firecracker labels developer preview. It works here, but if a Firecracker upgrade breaks restore, switch
make_templateto aFullsnapshot. - Clones of one template share state the kernel doesn't reseed. Firecracker's VMGenID reseeds the kernel's random numbers on every restore, and the runner starts fresh after restore. But dockerd was already running in the template, so its own in-memory state is identical across clones. That only affects things like generated container names.
- The kernel has a one-line patch that allows the
snapshottarget indm-mod.create=. actions/cachestarts empty every job, because its cache server lives and dies with the VM. The same goes for Docker's build cache, so every image build starts from scratch.- The mirror only caches Docker Hub. Pulls from ghcr.io, quay.io and others go straight out.
- arm64 is wired in but untested. The Dockerfile picks the right kernel config, kernel image and binaries via
uname -m/TARGETARCH. Build it on an arm64 machine: cross-building the kernel under QEMU emulation takes hours.
AI disclosure
Project was made using an LLM, the idea around it is human