mirror of
https://github.com/hanzoai/git-runner.git
synced 2026-11-14 02:38:33 +00:00
The forge serves its runner surface as five typed zip operations. This is the half that calls them: zip.Dial and zip.Call by op name, so the body carries the same bytes both ends hold in memory, and the address is derived rather than spelled — the forge registers POST /v1/runner/<name>, zip derives post_runner_<name>, and TestOpNames pins our five literals against zip.ID so the two cannot drift. The types moved with the wire, and both moves were forced by it rather than chosen. A map is refused at encode, so secrets, vars, needs and a job's outputs cross as []Pair and are indexed once, at the boundary act wants a map at. A time.Time is accepted and then arrives as the zero time with no error anywhere, so every instant is an int64 of unix nanoseconds. Falling out of it: - Task.Context is a struct. As a map the forge wrote git_runtime_token while this runner read gitea_runtime_token and quietly fell back to the task token, so ACTIONS_RUNTIME_TOKEN was the wrong secret and the mask that keeps a runtime token out of a job's log covered nothing. A field is a compile error. - A transport timeout is an error again. It was read as "the designed idle path for a long poll" — fiction, the forge has never blocked — which counted it as an empty response AND reset the error counter, so a forge answering in six seconds against a five second wait was indistinguishable from an idle one in every log line and every metric, with the counter that would have raised the alarm suppressed. There is no long poll: task answers immediately and TaskIn.Queue is what makes an idle ask cost nothing. - fetch_timeout is now the ceiling on the wire. A zip.Call cannot be cancelled once it has left, so a per-request context bounds nothing and the wait has to sit on the transport, where it bounds every call. - The credential is a field, so header.go is gone. client.Forge stamps it and no caller carries it. - logs -> log, Row -> Line, NoMore -> Last, SentOutputs -> Stored. You append to a log, and the other four addresses are singular. - Insecure() had no caller. client.take copies every string and []byte out of a reply, and it is not belt-and-braces. ZAP reads text zero-copy — zap.Object.Text is an unsafe.String over the message — and zip.Call hands the response back to fasthttp's pool as it returns, so a reply's strings point at memory the next call writes over. A token registered as "runner-secret" read back "1.2.3r-secret" one call later, the version string of the call in between laid over its first five bytes, and the forge answered 401 to a runner that was registered. TestOwn fails without the copy. Delete it when zip copies on decode. client.dial declares its own transport scheme rather than taking http and https: declaring a scheme REPLACES what is registered under that name, and zip's own entries carry a Serve half this one does not — taking them left the process able to dial plain HTTP through zip and no longer able to serve it. .runner is untouched. Its uuid and token are exactly the credential the new wire sends, so a registration made before the cutover keeps working after it, and a runner only has to be rebuilt. Requires github.com/hanzoai/git at 92b2e84dba or later. Nothing here has been exercised against a live forge.
436 lines
14 KiB
Go
436 lines
14 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package run
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"maps"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hanzoai/git-runner/act/artifactcache"
|
|
"github.com/hanzoai/git-runner/act/common"
|
|
"github.com/hanzoai/git-runner/act/model"
|
|
"github.com/hanzoai/git-runner/act/runner"
|
|
"github.com/hanzoai/git-runner/internal/pkg/client"
|
|
"github.com/hanzoai/git-runner/internal/pkg/config"
|
|
"github.com/hanzoai/git-runner/internal/pkg/labels"
|
|
"github.com/hanzoai/git-runner/internal/pkg/metrics"
|
|
"github.com/hanzoai/git-runner/internal/pkg/report"
|
|
"github.com/hanzoai/git-runner/internal/pkg/ver"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
forge "github.com/hanzoai/git/modules/actions/runner"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Runner runs the pipeline.
|
|
type Runner struct {
|
|
name string
|
|
|
|
cfg *config.Config
|
|
|
|
client client.Client
|
|
labels labels.Labels
|
|
envs map[string]string
|
|
cacheHandler *artifactcache.Handler
|
|
|
|
runningTasks sync.Map
|
|
runningCount atomic.Int64
|
|
}
|
|
|
|
func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client) *Runner {
|
|
ls := labels.Labels{}
|
|
for _, v := range reg.Labels {
|
|
if l, err := labels.Parse(v); err == nil {
|
|
ls = append(ls, l)
|
|
}
|
|
}
|
|
envs := make(map[string]string, len(cfg.Runner.Envs))
|
|
maps.Copy(envs, cfg.Runner.Envs)
|
|
var cacheHandler *artifactcache.Handler
|
|
if cfg.Cache.Enabled == nil || *cfg.Cache.Enabled {
|
|
if cfg.Cache.ExternalServer != "" {
|
|
envs["ACTIONS_CACHE_URL"] = cfg.Cache.ExternalServer
|
|
} else {
|
|
handler, err := artifactcache.StartHandler(
|
|
cfg.Cache.Dir,
|
|
cfg.Cache.Host,
|
|
cfg.Cache.Port,
|
|
"",
|
|
log.StandardLogger().WithField("module", "cache_request"),
|
|
)
|
|
if err != nil {
|
|
log.Errorf("cannot init cache server, it will be disabled: %v", err)
|
|
// go on
|
|
} else {
|
|
cacheHandler = handler
|
|
envs["ACTIONS_CACHE_URL"] = handler.ExternalURL() + "/"
|
|
}
|
|
}
|
|
}
|
|
|
|
// Where a job's artifact steps upload to. actions/upload-artifact@v3 and its
|
|
// download counterpart concatenate "_apis/pipelines/…" onto ACTIONS_RUNTIME_URL,
|
|
// so the trailing slash is part of the contract.
|
|
//
|
|
// ACTIONS_RESULTS_URL is the v4 protocol's, and only its origin survives:
|
|
// @actions/artifact v2 reduces the value to a scheme and host and appends its
|
|
// own "twirp/…" path, so there is nothing to place here.
|
|
forgeURL := strings.TrimSuffix(cli.Address(), "/")
|
|
envs["ACTIONS_RUNTIME_URL"] = forgeURL + "/v1/artifact/"
|
|
envs["ACTIONS_RESULTS_URL"] = forgeURL
|
|
|
|
// Set specific environments to distinguish between Hanzo Git and GitHub
|
|
envs["GIT_ACTIONS"] = "true"
|
|
envs["GIT_ACTIONS_RUNNER_VERSION"] = ver.Version()
|
|
|
|
return &Runner{
|
|
name: reg.Name,
|
|
cfg: cfg,
|
|
client: cli,
|
|
labels: ls,
|
|
envs: envs,
|
|
cacheHandler: cacheHandler,
|
|
}
|
|
}
|
|
|
|
func (r *Runner) Run(ctx context.Context, task *forge.Task) error {
|
|
if _, ok := r.runningTasks.Load(task.ID); ok {
|
|
return fmt.Errorf("task %d is already running", task.ID)
|
|
}
|
|
r.runningTasks.Store(task.ID, struct{}{})
|
|
defer r.runningTasks.Delete(task.ID)
|
|
|
|
r.runningCount.Add(1)
|
|
|
|
start := time.Now()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, r.cfg.Runner.Timeout)
|
|
defer cancel()
|
|
reporter := report.NewReporter(ctx, cancel, r.client, task, r.cfg)
|
|
var runErr error
|
|
defer func() {
|
|
r.runningCount.Add(-1)
|
|
|
|
lastWords := ""
|
|
if runErr != nil {
|
|
lastWords = runErr.Error()
|
|
}
|
|
_ = reporter.Close(lastWords)
|
|
|
|
metrics.JobDuration.Observe(time.Since(start).Seconds())
|
|
metrics.JobsTotal.WithLabelValues(metrics.ResultToStatusLabel(reporter.Result())).Inc()
|
|
}()
|
|
reporter.RunDaemon()
|
|
runErr = r.run(ctx, task, reporter)
|
|
|
|
return nil
|
|
}
|
|
|
|
// defaultActionsURLFallback is used when the server sends no actions URL at all.
|
|
// It must never be empty: an empty value produces "https:///actions/checkout",
|
|
// which fails as `http: no Host in request URL`.
|
|
const defaultActionsURLFallback = "https://github.com"
|
|
|
|
// getDefaultActionsURL resolves where `uses: owner/repo@ref` is fetched from.
|
|
//
|
|
// The value normally arrives in the task context. When it is "https://github.com"
|
|
// and github_mirror is set, the mirror wins.
|
|
//
|
|
// ⛔ The empty case is the one that matters. act concatenates this value, so an
|
|
// empty one builds "https:///actions/checkout" — three slashes, no host — and
|
|
// the job dies before its first step on `http: no Host in request URL`, unless
|
|
// the action happens to already be in this runner's act cache. Which makes a
|
|
// build's outcome depend on which runner claimed it.
|
|
//
|
|
// github_mirror cannot rescue that by itself: it is consulted only when the
|
|
// value is EXACTLY "https://github.com", so an empty one falls straight
|
|
// through. Falling back here keeps the runner correct whatever the forge sends,
|
|
// which is the right place for it — the runner is what builds the URL.
|
|
func (r *Runner) getDefaultActionsURL(task *forge.Task) string {
|
|
declared := task.Context.ActionsURL
|
|
if declared == "" {
|
|
if r.cfg.Runner.GithubMirror != "" {
|
|
return r.cfg.Runner.GithubMirror
|
|
}
|
|
return defaultActionsURLFallback
|
|
}
|
|
if declared == defaultActionsURLFallback && r.cfg.Runner.GithubMirror != "" {
|
|
return r.cfg.Runner.GithubMirror
|
|
}
|
|
return declared
|
|
}
|
|
|
|
func (r *Runner) run(ctx context.Context, task *forge.Task, reporter *report.Reporter) (err error) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
err = fmt.Errorf("panic: %v", r)
|
|
}
|
|
}()
|
|
|
|
reporter.Logf("%s(version:%s) received task %v of job %v, be triggered by event: %s", r.name, ver.Version(), task.ID, task.Context.Job, task.Context.EventName)
|
|
|
|
workflow, jobID, err := generateWorkflow(task)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
plan, err := model.CombineWorkflowPlanner(workflow).PlanJob(jobID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
job := workflow.GetJob(jobID)
|
|
reporter.ResetSteps(len(job.Steps))
|
|
|
|
gitCtx := &task.Context
|
|
secrets := index(task.Secrets)
|
|
|
|
// act reads the event as a decoded object and writes it back out as the
|
|
// event file, so the payload is decoded once here and re-encoded below.
|
|
var event map[string]any
|
|
if len(gitCtx.Event) > 0 {
|
|
if err := json.Unmarshal(gitCtx.Event, &event); err != nil {
|
|
return fmt.Errorf("task %d: event payload: %w", task.ID, err)
|
|
}
|
|
}
|
|
|
|
log.Infof("task %v repo is %v %v %v", task.ID, gitCtx.Repository,
|
|
r.getDefaultActionsURL(task),
|
|
r.client.Address())
|
|
|
|
preset := &model.GithubContext{
|
|
Event: event,
|
|
RunID: gitCtx.RunID,
|
|
RunNumber: gitCtx.RunNumber,
|
|
RunAttempt: gitCtx.RunAttempt,
|
|
Actor: gitCtx.Actor,
|
|
Repository: gitCtx.Repository,
|
|
EventName: gitCtx.EventName,
|
|
Sha: gitCtx.Sha,
|
|
Ref: gitCtx.Ref,
|
|
RefName: gitCtx.RefName,
|
|
RefType: gitCtx.RefType,
|
|
HeadRef: gitCtx.HeadRef,
|
|
BaseRef: gitCtx.BaseRef,
|
|
Token: gitCtx.Token,
|
|
RepositoryOwner: gitCtx.RepositoryOwner,
|
|
RetentionDays: gitCtx.RetentionDays,
|
|
// The server declares where its API lives. Dropping it here left
|
|
// APIURL empty, so act fell back to <instance>/api/v1 and every
|
|
// octokit-based action 404'd on a server that serves it elsewhere.
|
|
APIURL: gitCtx.APIURL,
|
|
}
|
|
if t := secrets["GIT_TOKEN"]; t != "" {
|
|
preset.Token = t
|
|
} else if t := secrets["GITHUB_TOKEN"]; t != "" {
|
|
preset.Token = t
|
|
}
|
|
|
|
runtimeToken := gitCtx.RuntimeToken
|
|
if runtimeToken == "" {
|
|
runtimeToken = preset.Token
|
|
}
|
|
r.envs["ACTIONS_RUNTIME_TOKEN"] = runtimeToken
|
|
// Mask the runtime token so it cannot be echoed in user step output; it is
|
|
// now also the cache server's bearer credential and leaking it would let
|
|
// any reader of the log impersonate this job against the cache.
|
|
if runtimeToken != "" {
|
|
secrets["ACTIONS_RUNTIME_TOKEN"] = runtimeToken
|
|
}
|
|
|
|
// Register this job's runtime token with the local cache server so that
|
|
// cache requests from the job container can authenticate. The credential
|
|
// is removed when the task finishes, so a leaked token stops working as
|
|
// soon as the job ends rather than remaining valid for the runner's
|
|
// lifetime. Only applies to the embedded cache server; when the operator
|
|
// points the runner at an external cache via cfg.Cache.ExternalServer, it
|
|
// is that server's responsibility to authenticate requests.
|
|
defer r.registerCacheForTask(runtimeToken, preset.Repository, reporter)()
|
|
|
|
eventJSON, err := json.Marshal(preset.Event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
maxLifetime := 3 * time.Hour
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
maxLifetime = time.Until(deadline)
|
|
}
|
|
|
|
workdirParent := strings.TrimLeft(r.cfg.Container.WorkdirParent, "/")
|
|
if r.cfg.Container.BindWorkdir {
|
|
// Append the task ID to isolate concurrent jobs from the same repo.
|
|
workdirParent = fmt.Sprintf("%s/%d", workdirParent, task.ID)
|
|
}
|
|
workdir := filepath.FromSlash(fmt.Sprintf("/%s/%s", workdirParent, preset.Repository))
|
|
|
|
runnerConfig := &runner.Config{
|
|
// On Linux, Workdir will be like "/<parent_directory>/<owner>/<repo>"
|
|
// On Windows, Workdir will be like "\<parent_directory>\<owner>\<repo>"
|
|
Workdir: workdir,
|
|
BindWorkdir: r.cfg.Container.BindWorkdir,
|
|
ActionCacheDir: filepath.FromSlash(r.cfg.Host.WorkdirParent),
|
|
|
|
ReuseContainers: false,
|
|
ForcePull: r.cfg.Container.ForcePull,
|
|
ForceRebuild: r.cfg.Container.ForceRebuild,
|
|
LogOutput: true,
|
|
JSONLogger: false,
|
|
Env: r.envs,
|
|
Secrets: secrets,
|
|
GitHubInstance: strings.TrimSuffix(r.client.Address(), "/"),
|
|
ServerURL: gitCtx.ServerURL,
|
|
AutoRemove: true,
|
|
NoSkipCheckout: true,
|
|
PresetGitHubContext: preset,
|
|
EventJSON: string(eventJSON),
|
|
ContainerNamePrefix: fmt.Sprintf("GITEA-ACTIONS-TASK-%d", task.ID),
|
|
ContainerMaxLifetime: maxLifetime,
|
|
ContainerNetworkMode: container.NetworkMode(r.cfg.Container.Network),
|
|
ContainerOptions: r.cfg.Container.Options,
|
|
ContainerDaemonSocket: r.cfg.Container.DockerHost,
|
|
Privileged: r.cfg.Container.Privileged,
|
|
DefaultActionInstance: r.getDefaultActionsURL(task),
|
|
PlatformPicker: r.labels.PickPlatform,
|
|
Vars: index(task.Vars),
|
|
ValidVolumes: r.cfg.Container.ValidVolumes,
|
|
InsecureSkipTLS: r.cfg.Runner.Insecure,
|
|
}
|
|
|
|
rr, err := runner.New(runnerConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
executor := rr.NewPlanExecutor(plan)
|
|
|
|
reporter.Logf("workflow prepared")
|
|
|
|
// add logger recorders
|
|
ctx = common.WithLoggerHook(ctx, reporter)
|
|
|
|
if !log.IsLevelEnabled(log.DebugLevel) {
|
|
ctx = runner.WithJobLoggerFactory(ctx, NullLogger{})
|
|
}
|
|
|
|
execErr := executor(ctx)
|
|
reporter.SetOutputs(job.Outputs)
|
|
|
|
if r.cfg.Container.BindWorkdir {
|
|
// Remove the entire task-specific directory (e.g. /workspace/<task_id>).
|
|
taskDir := filepath.FromSlash("/" + workdirParent)
|
|
if err := os.RemoveAll(taskDir); err != nil {
|
|
log.Warnf("failed to clean up workspace %s: %v", taskDir, err)
|
|
}
|
|
}
|
|
|
|
return execErr
|
|
}
|
|
|
|
// registerCacheForTask tells the cache server to accept requests authenticated
|
|
// with the given runtime token for the duration of this task. Returns a
|
|
// function the caller must invoke (typically via defer) to revoke the
|
|
// credential when the task finishes.
|
|
//
|
|
// Three modes:
|
|
// - Embedded handler: register in-process via RegisterJob.
|
|
// - external_server + external_secret: POST to the remote server's
|
|
// /_internal/register, defer a POST to /_internal/revoke. This is what
|
|
// enables full per-job auth and repo scoping over the network.
|
|
// - external_server alone (no secret): no-op revoker. The remote server is
|
|
// in legacy openMode and ignores the runtime token; trust is at the
|
|
// network layer.
|
|
//
|
|
// Safe with an empty token (older servers did not issue one).
|
|
func (r *Runner) registerCacheForTask(token, repo string, reporter *report.Reporter) func() {
|
|
if token == "" {
|
|
return func() {}
|
|
}
|
|
if r.cacheHandler != nil {
|
|
return r.cacheHandler.RegisterJob(token, repo)
|
|
}
|
|
if r.cfg.Cache.ExternalServer != "" && r.cfg.Cache.ExternalSecret != "" {
|
|
return r.registerExternalCacheJob(token, repo, reporter)
|
|
}
|
|
return func() {}
|
|
}
|
|
|
|
// registerExternalCacheJob POSTs to the remote cache-server's control-plane.
|
|
// Failures are logged but not fatal: if registration fails, the cache will
|
|
// 401 the job's requests — better than failing the whole task for a cache
|
|
// outage. The warning is mirrored to the job log so users can see why their
|
|
// cache calls 401, instead of having to read the runner daemon's stderr.
|
|
func (r *Runner) registerExternalCacheJob(token, repo string, reporter *report.Reporter) func() {
|
|
base := strings.TrimRight(r.cfg.Cache.ExternalServer, "/")
|
|
if err := postInternalCache(base+"/_internal/register", r.cfg.Cache.ExternalSecret,
|
|
map[string]string{"token": token, "repo": repo}); err != nil {
|
|
log.Warnf("cache external_server register failed (%s): %v", base, err)
|
|
if reporter != nil {
|
|
reporter.Logf("::warning::cache external_server register failed (%s): %v — cache requests from this job will be unauthenticated and likely return 401", base, err)
|
|
}
|
|
}
|
|
return func() {
|
|
if err := postInternalCache(base+"/_internal/revoke", r.cfg.Cache.ExternalSecret,
|
|
map[string]string{"token": token}); err != nil {
|
|
log.Warnf("cache external_server revoke failed (%s): %v", base, err)
|
|
if reporter != nil {
|
|
reporter.Logf("::warning::cache external_server revoke failed (%s): %v", base, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func postInternalCache(url, secret string, body map[string]string) error {
|
|
buf, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(buf))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+secret)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode/100 != 2 {
|
|
return fmt.Errorf("status %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) RunningCount() int64 {
|
|
return r.runningCount.Load()
|
|
}
|
|
|
|
func (r *Runner) Declare(ctx context.Context, labels []string) (*forge.DeclareOut, error) {
|
|
return r.client.Declare(ctx, &forge.DeclareIn{
|
|
Version: ver.Version(),
|
|
Labels: labels,
|
|
})
|
|
}
|
|
|
|
// index reads a list of name/value pairs as the map act takes. The wire carries
|
|
// a list because a map cannot cross the plane; act wants lookups, so the
|
|
// conversion happens once, here, at the boundary between the two.
|
|
func index(pairs []forge.Pair) map[string]string {
|
|
out := make(map[string]string, len(pairs))
|
|
for _, p := range pairs {
|
|
out[p.Name] = p.Value
|
|
}
|
|
return out
|
|
}
|