fix missing dependencies during build

This commit is contained in:
Jonas Leder 2026-05-25 20:53:09 +02:00
commit 9289c18c0a
2 changed files with 88 additions and 44 deletions

View file

@ -1,7 +1,6 @@
package build
import (
"bufio"
"bytes"
"context"
"encoding/json"
@ -26,8 +25,10 @@ import (
// DockerRunner spawns a build container via the Docker SDK.
// Architecture is fixed to the host OS architecture — Docker cannot cross-compile.
type DockerRunner struct {
arch string
cli *dockerclient.Client
arch string
cli *dockerclient.Client
backendURL string
dockerNetwork string
}
func NewDockerRunner() (*DockerRunner, error) {
@ -38,7 +39,12 @@ func NewDockerRunner() (*DockerRunner, error) {
if err != nil {
return nil, err
}
return &DockerRunner{arch: runtime.GOARCH, cli: cli}, nil
return &DockerRunner{
arch: runtime.GOARCH,
cli: cli,
backendURL: os.Getenv("BACKEND_URL"),
dockerNetwork: os.Getenv("DOCKER_NETWORK"),
}, nil
}
func (r *DockerRunner) Architectures() []string { return []string{r.arch} }
@ -47,16 +53,7 @@ func (r *DockerRunner) Run(ctx context.Context, apiClient *api.Client, job *api.
ls := newLogStreamer(apiClient, job.BuildID)
ls.writef("[agent] pulling %s\n", job.BuildImage)
if rc, err := r.cli.ImagePull(ctx, job.BuildImage, image.PullOptions{}); err == nil {
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
var msg struct{ Status string }
if json.Unmarshal(scanner.Bytes(), &msg) == nil && msg.Status != "" {
ls.writef("%s\n", msg.Status)
}
}
rc.Close()
}
r.pullImage(ctx, job.BuildImage, ls)
containerID, err := r.createContainer(ctx, job)
if err != nil {
@ -97,22 +94,7 @@ func (r *DockerRunner) Run(ctx context.Context, apiClient *api.Client, job *api.
logRC.Close()
}
// Collect the exit status (log streaming is done by the time we reach here).
select {
case status := <-statusCh:
if status.StatusCode != 0 {
ls.writef("[agent] build failed: exit code %d\n", status.StatusCode)
ls.closeAndFlush("failed")
return
}
case err := <-waitErrCh:
if ctx.Err() != nil {
r.cli.ContainerKill(context.Background(), containerID, "KILL") //nolint:errcheck
ls.writef("[agent] build timed out\n")
} else {
ls.writef("[agent] wait: %v\n", err)
}
ls.closeAndFlush("failed")
if !r.waitForExit(ctx, containerID, statusCh, waitErrCh, ls) {
return
}
@ -144,23 +126,82 @@ func (r *DockerRunner) Run(ctx context.Context, apiClient *api.Client, job *api.
log.Printf("build %s succeeded (docker): version=%s", job.BuildID, version)
}
func (r *DockerRunner) pullImage(ctx context.Context, ref string, ls *logStreamer) {
rc, err := r.cli.ImagePull(ctx, ref, image.PullOptions{})
if err != nil {
return
}
defer rc.Close()
dec := json.NewDecoder(rc)
for {
var msg struct{ Status string }
if dec.Decode(&msg) != nil {
break
}
if msg.Status != "" {
ls.writef("%s\n", msg.Status)
}
}
}
func (r *DockerRunner) waitForExit(
ctx context.Context,
containerID string,
statusCh <-chan container.WaitResponse,
waitErrCh <-chan error,
ls *logStreamer,
) bool {
select {
case status := <-statusCh:
if status.StatusCode != 0 {
ls.writef("[agent] build failed: exit code %d\n", status.StatusCode)
ls.closeAndFlush("failed")
return false
}
case err := <-waitErrCh:
if ctx.Err() != nil {
r.cli.ContainerKill(context.Background(), containerID, "KILL") //nolint:errcheck
ls.writef("[agent] build timed out\n")
} else {
ls.writef("[agent] wait: %v\n", err)
}
ls.closeAndFlush("failed")
return false
}
return true
}
func (r *DockerRunner) createContainer(ctx context.Context, job *api.BuildJob) (string, error) {
env := []string{
"SOURCE_TYPE=" + job.SourceType,
"AUR_NAME=" + job.AURName,
"GIT_URL=" + job.GitURL,
"GIT_BRANCH=" + job.GitBranch,
"PKG_NAME=" + job.PackageName,
"ARCH=" + makepkgArch(job.Architecture),
"JOB_ARCH=" + job.Architecture,
"BUILD_ID=" + job.BuildID,
"SKIP_S3_UPLOAD=1",
"SKIP_SOURCE_FETCH=" + boolEnv(job.SourceType == "zip"),
"INIT_SCRIPT=" + job.InitScript,
}
var hostConfig *container.HostConfig
if r.dockerNetwork != "" {
hostConfig = &container.HostConfig{
NetworkMode: container.NetworkMode(r.dockerNetwork),
}
// Only pass BACKEND_URL when a network is configured — without explicit
// network access the URL (e.g. localhost or a compose hostname) would be
// unreachable from inside the container and cause pacman -Sy to fail.
if r.backendURL != "" {
env = append(env, "BACKEND_URL="+r.backendURL)
}
}
resp, err := r.cli.ContainerCreate(ctx, &container.Config{
Image: job.BuildImage,
Env: []string{
"SOURCE_TYPE=" + job.SourceType,
"AUR_NAME=" + job.AURName,
"GIT_URL=" + job.GitURL,
"GIT_BRANCH=" + job.GitBranch,
"PKG_NAME=" + job.PackageName,
"ARCH=" + makepkgArch(job.Architecture),
"JOB_ARCH=" + job.Architecture,
"BUILD_ID=" + job.BuildID,
"SKIP_S3_UPLOAD=1",
"SKIP_SOURCE_FETCH=" + boolEnv(job.SourceType == "zip"),
"INIT_SCRIPT=" + job.InitScript,
},
}, nil, nil, nil, "")
Env: env,
}, hostConfig, nil, nil, "")
if err != nil {
return "", err
}

View file

@ -50,6 +50,9 @@ services:
AGENT_TOKEN: ${INITIAL_AGENT_TOKEN:-change-this-agent-token}
# AGENT_MODE: docker (default) — set to "k8s" for Kubernetes mode
AGENT_MODE: docker
# Join build containers to this network so they can reach the backend
# and download pre-built AUR packages from the local repo.
DOCKER_NETWORK: ${COMPOSE_PROJECT_NAME:-aur-builder}_default
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on: