mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-10-30 11:58:49 +08:00
Contains a number of small bug fixes where jobs behaved differently from GitHub or failed silently.
1. Hooks fail on a refused `::set-env::` or `::add-path::` and no longer inherit the last step's identity
1. Docker build, exec and wait errors are returned instead of swallowed, token-only registry credentials work, logs of `--tty` services decode
1. Env files split lines like GitHub on each OS, so a carriage return is kept on Linux and CRLF ends a line on Windows, empty heredoc names or delimiters are rejected
1. Post steps run after their pre step even when main was skipped, composites see undeclared inputs, `runs.args` wins without `with.args` being parsed
1. Docker actions skip evaluating defaults of inputs given in `with` and read `entryPoint` and `args` in any case, a lowercase `dockerfile` is found, undeclared inputs get GitHub's warning
1. A user-set `CI` is kept, `GITHUB_REPOSITORY_ID` and the other missing `GITHUB_*` variables are exported
1. `timeout-minutes` can read the step's own `env`, a step-level `ACTIONS_ALLOW_UNSECURE_COMMANDS: false` beats a job-level `true` while the runner's own opt-in still wins, `pwsh` quotes the script path
1. A step `env:` given as one expression such as `${{ fromJSON(matrix.env) }}` is applied, `hashFiles` fails with GitHub's messages on script errors and after 120 seconds, a job image without node still gets an empty hash
1. A step whose command files cannot be reset fails instead of importing stale ones, a timeout between steps fails the job instead of reporting success
1. One named volume can be mounted at several paths, host-mode PTY steps keep their last output when they fail, host scratch directories are removed when job setup fails
1. Cancelling a step right after it starts kills its whole process tree
1. Cache prefix restores pick the newest entry created within the same second, also on S3, cache commits with a wrong size are rejected
1. Artifact chunks are written at their offset, download URLs are escaped, listing a run without artifacts no longer crashes
1. Poll backoff reaches `fetch_interval_max`, proxy passwords are masked in more forms, secrets are also masked in step names and in their quoted form in the runner log, a failed final report is logged
1. On Windows hosts a differently cased unsecure-commands opt-in or proxy variable counts, and an early cancel no longer leaks a job object handle
1. Interactive `register` prompts for a missing token and stops on SIGTERM, `exec --list --detect-event` no longer panics without events, `config set` works on `cache.s3` keys
Reviewed-on: https://gitea.com/gitea/runner/pulls/1338
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
166 lines
5.2 KiB
Go
166 lines
5.2 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package run
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
|
|
"gitea.com/gitea/runner/act/common/logging"
|
|
"gitea.com/gitea/runner/act/container"
|
|
|
|
"golang.org/x/net/http/httpproxy"
|
|
)
|
|
|
|
// proxyFromEnv returns the runner's own proxy configuration, or nil when it has none.
|
|
func proxyFromEnv() *httpproxy.Config {
|
|
cfg := httpproxy.FromEnvironment()
|
|
if cfg.HTTPProxy == "" && cfg.HTTPSProxy == "" {
|
|
return nil
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// JobProxyEnv returns the proxy variables a job runs with, given what runner.envs already
|
|
// put in jobEnvs. Gitea is deliberately not made direct, so it stays reachable however the
|
|
// runner reaches it.
|
|
func JobProxyEnv(jobEnvs map[string]string, cacheURL string, serviceNames []string) map[string]string {
|
|
cfg := proxyFromEnv()
|
|
if cfg == nil {
|
|
return nil
|
|
}
|
|
|
|
// Go bypasses loopback on its own, curl and most other tools in a job do not.
|
|
direct := append([]string{"localhost", "127.0.0.1", "::1"}, serviceNames...)
|
|
direct = append(direct, hostOf(cacheURL))
|
|
|
|
proxyEnv := map[string]string{}
|
|
setPair := func(lower, upper, value string) {
|
|
if value == "" {
|
|
return
|
|
}
|
|
// Either spelling in runner.envs takes over both, so the pair cannot disagree.
|
|
if existing, ok := jobEnvs[lower]; ok {
|
|
value = existing
|
|
} else if existing, ok := jobEnvs[upper]; ok {
|
|
value = existing
|
|
}
|
|
proxyEnv[lower], proxyEnv[upper] = value, value
|
|
}
|
|
setPair("http_proxy", "HTTP_PROXY", cfg.HTTPProxy)
|
|
setPair("https_proxy", "HTTPS_PROXY", cfg.HTTPSProxy)
|
|
|
|
// no_proxy is merged rather than replaced: the hosts above are structural, and an
|
|
// operator cannot list the cache server's startup-assigned address in advance.
|
|
noProxy := appendNoProxy(cfg.NoProxy, direct...)
|
|
for _, name := range []string{"no_proxy", "NO_PROXY"} {
|
|
if existing, ok := jobEnvs[name]; ok {
|
|
noProxy = appendNoProxy(existing, strings.Split(noProxy, ",")...)
|
|
break
|
|
}
|
|
}
|
|
proxyEnv["no_proxy"], proxyEnv["NO_PROXY"] = noProxy, noProxy
|
|
return proxyEnv
|
|
}
|
|
|
|
// BypassProxyForDockerHost keeps the runner's Docker API traffic off the proxy: the docker
|
|
// client proxies every transport that is not a unix socket or a named pipe, so a tcp://
|
|
// daemon would be reached through a proxy that cannot route to it.
|
|
//
|
|
// It must run before the first Docker API call, because the standard library resolves the
|
|
// proxy environment once.
|
|
func BypassProxyForDockerHost(dockerHost string) {
|
|
cfg := proxyFromEnv()
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
host := hostOf(dockerHost)
|
|
if host == "" {
|
|
// A unix socket or named pipe is never proxied.
|
|
return
|
|
}
|
|
|
|
noProxy := appendNoProxy(cfg.NoProxy, host)
|
|
for _, name := range []string{"no_proxy", "NO_PROXY"} {
|
|
if err := os.Setenv(name, noProxy); err != nil {
|
|
logging.Warnf("cannot set %s for the runner process: %v", name, err)
|
|
}
|
|
}
|
|
logging.Debugf("docker host %s is reached directly, no_proxy is now %q", host, noProxy)
|
|
}
|
|
|
|
// WarnIfDaemonHasNoProxy points at the one part the runner cannot set: the docker daemon
|
|
// pulls the images, and a daemon in its own container needs its own proxy.
|
|
func WarnIfDaemonHasNoProxy(ctx context.Context) {
|
|
if proxyFromEnv() == nil {
|
|
return
|
|
}
|
|
info, err := container.GetHostInfo(ctx)
|
|
if err != nil {
|
|
logging.Debugf("cannot read the docker daemon's proxy configuration: %v", err)
|
|
return
|
|
}
|
|
if info.HTTPProxy == "" && info.HTTPSProxy == "" {
|
|
logging.Warn("the runner has a proxy but the docker daemon reports none, so image pulls will not use it: https://docs.docker.com/engine/daemon/proxy/")
|
|
}
|
|
}
|
|
|
|
// proxyPasswords returns the passwords embedded in the runner's proxy URLs, to mask before
|
|
// a job echoes its environment.
|
|
func proxyPasswords(envs map[string]string) []string {
|
|
cfg := httpproxy.FromEnvironment()
|
|
raws := []string{cfg.HTTPProxy, cfg.HTTPSProxy}
|
|
for name, value := range envs {
|
|
if strings.EqualFold(name, "http_proxy") || strings.EqualFold(name, "https_proxy") { // Windows reads env names in any case
|
|
raws = append(raws, value)
|
|
}
|
|
}
|
|
var passwords []string
|
|
for _, raw := range raws {
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
|
parsed, err = url.Parse("http://" + raw)
|
|
}
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if password, ok := parsed.User.Password(); ok && password != "" && !slices.Contains(passwords, password) {
|
|
passwords = append(passwords, password)
|
|
}
|
|
}
|
|
return passwords
|
|
}
|
|
|
|
// appendNoProxy adds hosts to a no_proxy list, keeping the operator's entries and adding
|
|
// none twice.
|
|
func appendNoProxy(noProxy string, hosts ...string) string {
|
|
entries := []string{}
|
|
for entry := range strings.SplitSeq(noProxy, ",") {
|
|
if entry = strings.TrimSpace(entry); entry != "" {
|
|
entries = append(entries, entry)
|
|
}
|
|
}
|
|
|
|
for _, host := range hosts {
|
|
if host == "" || slices.Contains(entries, host) {
|
|
continue
|
|
}
|
|
entries = append(entries, host)
|
|
}
|
|
return strings.Join(entries, ",")
|
|
}
|
|
|
|
// hostOf returns the host of a URL without its port, the form a no_proxy entry takes. It is
|
|
// empty for anything without a network host, such as a unix socket.
|
|
func hostOf(raw string) string {
|
|
parsed, err := url.Parse(strings.TrimSuffix(raw, "/"))
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return parsed.Hostname()
|
|
}
|