Something went wrong. Try again.
Ephemeral DigitalOcean droplets for Forgejo Actions and Radicle CI, from one daemon
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The Forgejo runner API calls the daemon makes, in either scope.//!//! Forgejo 15 introduced ephemeral runners: a runner created through the API//! runs exactly one job and is then deleted by Forgejo itself. That is what//! makes a droplet per job safe -- a leaked runner token can only ever fetch//! the job it was already running -- and it is also how the daemon knows a//! job is over: the runner it registered for it is gone.
const std = @import("std");const Allocator = std.mem.Allocator;
const api = @import("api");const Config = @import("Config.zig");
const Forgejo = @This();
client: *api.Client,/// `/api/v1/admin/actions/runners` or `/api/v1/user/actions/runners`./// `admin` needs instance admin rights; `user` exposes the same endpoints for/// an ordinary account, which is what a shared forge like Codeberg allows, and/// the runners are then the account's rather than the instance's.runners_path: []const u8,
pub fn init(client: *api.Client, scope: Config.Scope) Forgejo { return .{ .client = client, .runners_path = switch (scope) { .admin => "/api/v1/admin/actions/runners", .user => "/api/v1/user/actions/runners", }, };}
pub const Job = struct { id: u64, /// Opaque, and unique to one attempt of one job. handle: []const u8, /// `waiting` needs a runner; `running` has one. status: []const u8, runs_on: []const []const u8 = &.{}, /// The job's name in its workflow. Only shown, never relied on, so an /// instance that leaves it out costs nothing. name: ?[]const u8 = null,};
pub const Runner = struct { id: u64, /// `offline`, `idle` or `active`. A runner still booting reads offline. status: []const u8 = "",};
pub const Registered = struct { id: u64, uuid: []const u8, token: []const u8,};
/// Every job Forgejo lists for `label`, whatever its status.////// Fetched once a poll and used twice: the waiting ones are what needs a/// droplet, and the waiting and running ones together are what a droplet may/// still be for.pub fn jobs(f: Forgejo, arena: Allocator, label: []const u8) ![]Job { const path = try std.fmt.allocPrint(arena, "{s}/jobs?labels={s}", .{ f.runners_path, label }); const response = try f.client.request(arena, .get, path, null); // An empty queue is `null`, not `[]`. return try response.json(?[]Job, arena, "list jobs") orelse &.{};}
pub fn registeredRunners(f: Forgejo, arena: Allocator) ![]Runner { const response = try f.client.request(arena, .get, f.runners_path, null); return try response.json([]Runner, arena, "list runners");}
/// Registers a runner that will run one job and then be deleted by Forgejo.pub fn registerEphemeral(f: Forgejo, arena: Allocator, name: []const u8) !Registered { var out: std.Io.Writer.Allocating = .init(arena); try std.json.Stringify.value(.{ .name = name, .ephemeral = true, .description = "autoscaled droplet", }, .{}, &out.writer); const response = try f.client.request(arena, .post, f.runners_path, out.written()); return try response.json(Registered, arena, "register runner");}
pub fn deleteRunner(f: Forgejo, arena: Allocator, id: u64) !void { const path = try std.fmt.allocPrint(arena, "{s}/{d}", .{ f.runners_path, id }); const response = try f.client.request(arena, .delete, path, null); // Forgejo deleting it first -- its job finished -- is what was wanted. if (response.status == .not_found) return; _ = try response.ok("delete runner");}
const testing = std.testing;
test "an empty queue is null" { var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); const response: api.Response = .{ .status = .ok, .body = "null" }; const parsed = try response.json(?[]Job, arena.allocator(), "test"); try testing.expectEqual(null, parsed);}
test "a runner listing parses" { var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); const body = \\[{"description":"","ephemeral":false,"id":21,"labels":["nixos"],"name":"cloud03-01", \\"owner_id":0,"repo_id":0,"status":"idle","uuid":"81c2b00b","version":"v13.1.0"}] ; const response: api.Response = .{ .status = .ok, .body = body }; const runners = try response.json([]Runner, arena.allocator(), "test"); try testing.expectEqual(@as(u64, 21), runners[0].id); try testing.expectEqualStrings("idle", runners[0].status);}
test { std.testing.refAllDecls(@This());}