mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-08-07 09:24:22 +02:00
gitea can not consume the runner's api by version while it's version mismatches the module version:
```
go: gitea.com/gitea/runner@v3.0.1: invalid version: module contains a go.mod file,
so module path must match major version ("gitea.com/gitea/runner/v3")
```
Fix that by bumping the module version now. The existing `v3.0.0` and `v3.0.1` tags stay unusable, so a new tag is needed after this lands.
Also drop the version `-X` linker flags, which would otherwise have to repeat the new path in both `Makefile` and `.goreleaser.yaml`, where a stale path makes injection silently no-op. Go has recorded the module version in the build info since 1.24, so `Version()` reads it from there, keeping the variable as an override for builds without a VCS stamp.
That part started as https://gitea.com/gitea/runner/pulls/1137 but belongs here: the stamp resolves against the tags that are legal for the module path, so without the `/v3` bump it would report `v1.0.9-0.<ts>-<sha>`. Since `release-nightly.yml` triggers on every push to `main`, splitting them would publish a nightly with a `v1` version.
---------
Co-authored-by: bircni <bircni@icloud.com>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1136
Reviewed-by: techknowlogick <9+techknowlogick@noreply.gitea.com>
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package run
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.com/gitea/runner/v3/act/common"
|
|
"gitea.com/gitea/runner/v3/internal/pkg/process"
|
|
)
|
|
|
|
func (r *Runner) checkConfiguredHealth(ctx context.Context) (bool, string) {
|
|
script := r.cfg.HealthCheck.Script
|
|
if script == "" {
|
|
return true, "ok"
|
|
}
|
|
|
|
now := time.Now()
|
|
if r.now != nil {
|
|
now = r.now()
|
|
}
|
|
if !r.healthCheckLast.IsZero() && now.Sub(r.healthCheckLast) < r.cfg.HealthCheck.Interval {
|
|
return r.healthCheckReady, r.healthCheckReason
|
|
}
|
|
|
|
env := processEnvironment()
|
|
maps.Copy(env, r.cloneEnvs())
|
|
env["GITEA_RUNNER_HEALTH_CHECK"] = "true"
|
|
env["GITEA_RUNNER_NAME"] = r.name
|
|
if r.client != nil {
|
|
env["GITEA_INSTANCE_URL"] = r.client.Address()
|
|
}
|
|
env["GITEA_RUNNER_RUNNING_JOBS"] = strconv.FormatInt(r.RunningCount(), 10)
|
|
|
|
runner := r.runHealthCheck
|
|
if runner == nil {
|
|
runner = executeHealthCheck
|
|
}
|
|
err := runner(ctx, script, r.cfg.HealthCheck.Timeout, env)
|
|
r.healthCheckLast = now
|
|
r.healthCheckReady = err == nil
|
|
if err != nil {
|
|
r.healthCheckReason = "runner health check failed: " + err.Error()
|
|
} else {
|
|
r.healthCheckReason = "ok"
|
|
}
|
|
return r.healthCheckReady, r.healthCheckReason
|
|
}
|
|
|
|
func executeHealthCheck(ctx context.Context, script string, timeout time.Duration, env map[string]string) error {
|
|
checkCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(checkCtx, script)
|
|
cmd.Env = envListFromMap(env)
|
|
cmd.SysProcAttr = process.SysProcAttr(script, false)
|
|
writer := common.NewLineWriter(func(line string) bool {
|
|
line = strings.TrimRight(line, "\r\n")
|
|
if line != "" {
|
|
common.Logger(ctx).Infof("health check: %s", line)
|
|
}
|
|
return true
|
|
})
|
|
cmd.Stdout = writer
|
|
cmd.Stderr = writer
|
|
|
|
treeKill := process.NewTreeKill(cmd)
|
|
if err := cmd.Start(); err != nil {
|
|
return fmt.Errorf("start: %w", err)
|
|
}
|
|
if killer, err := treeKill.Capture(cmd.Process); err == nil {
|
|
defer killer.Close()
|
|
}
|
|
err := cmd.Wait()
|
|
common.FlushWriter(writer)
|
|
if errors.Is(checkCtx.Err(), context.DeadlineExceeded) {
|
|
return fmt.Errorf("timed out after %s", timeout)
|
|
}
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var exitErr *exec.ExitError
|
|
if errors.As(err, &exitErr) {
|
|
return fmt.Errorf("exited with code %d", exitErr.ExitCode())
|
|
}
|
|
return err
|
|
}
|
|
|
|
func processEnvironment() map[string]string {
|
|
environ := os.Environ()
|
|
env := make(map[string]string, len(environ))
|
|
for _, value := range environ {
|
|
if key, item, ok := strings.Cut(value, "="); ok {
|
|
env[key] = item
|
|
}
|
|
}
|
|
return env
|
|
}
|