mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-06-15 06:31:30 +00:00
## Problem Several runner code paths could drop the **tail** of a step's log output, so a failing (or cancelled) step would show output that is missing its last line(s). This was observed in practice and traced to four independent issues. ## Root causes & fixes ### 1. Trailing line without a newline was never flushed `common.lineWriter` buffers output until it sees a `\n`. A final line **without** a trailing newline (e.g. an error message printed right before a process exits, a panic, `printf` without `\n`) stayed in the internal buffer and was never emitted — the writer exposed no flush at all. - Added `lineWriter.Flush()` (idempotent), a `Flusher` interface, and a `FlushWriter(io.Writer)` helper. - Flush at every stream EOF: the exec copy goroutine, the container `attach()` streaming goroutine, and at step end (`useStepLogger`). ### 2. Cancellation/timeout truncated output `waitForCommand` returned immediately on `ctx.Done()` and abandoned the output-copy goroutine, losing output the command had already produced. It now drains with a bounded grace period before returning. The response channel is buffered so the goroutine can't leak if the drain times out. ### 3. `attach()` raced the final bytes Container output was streamed in a fire-and-forget goroutine that `wait()` did not synchronize with, so the step could proceed before the last bytes were written. `wait()` now blocks on the streaming goroutine (bounded) so output is fully drained and flushed first. ### 4. `::stop-commands::` silently dropped lines from the step log Lines between `::stop-commands::<token>` and its end token were echoed without the `raw_output` field **and** short-circuited the handler chain (`return false`), so they never reached the step log (non-raw entries aren't appended while a step is running). Now returns `true` so they are still captured. Reviewed-on: https://gitea.com/gitea/runner/pulls/1028 Reviewed-by: Zettat123 <39446+zettat123@noreply.gitea.com>
204 lines
5.1 KiB
Go
204 lines
5.1 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package runner
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"gitea.com/gitea/runner/act/common"
|
|
)
|
|
|
|
var commandPatternGA *regexp.Regexp
|
|
|
|
var commandPatternADO *regexp.Regexp
|
|
|
|
func init() {
|
|
commandPatternGA = regexp.MustCompile("^::([^ ]+)( (.+))?::([^\r\n]*)[\r\n]+$")
|
|
commandPatternADO = regexp.MustCompile("^##\\[([^ ]+)( (.+))?]([^\r\n]*)[\r\n]+$")
|
|
}
|
|
|
|
func tryParseRawActionCommand(line string) (command string, kvPairs map[string]string, arg string, ok bool) {
|
|
if m := commandPatternGA.FindStringSubmatch(line); m != nil {
|
|
command = m[1]
|
|
kvPairs = parseKeyValuePairs(m[3], ",")
|
|
arg = m[4]
|
|
ok = true
|
|
} else if m := commandPatternADO.FindStringSubmatch(line); m != nil {
|
|
command = m[1]
|
|
kvPairs = parseKeyValuePairs(m[3], ";")
|
|
arg = m[4]
|
|
ok = true
|
|
}
|
|
return command, kvPairs, arg, ok
|
|
}
|
|
|
|
func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
|
|
logger := common.Logger(ctx)
|
|
resumeCommand := ""
|
|
return func(line string) bool {
|
|
command, kvPairs, arg, ok := tryParseRawActionCommand(line)
|
|
if !ok {
|
|
return true
|
|
}
|
|
|
|
if resumeCommand != "" && command != resumeCommand {
|
|
// There should not be any emojis in the log output for Gitea.
|
|
// The code in the switch statement is the same.
|
|
// Return true (not false) so the line still reaches the raw_output
|
|
// log handler; otherwise everything between ::stop-commands:: and
|
|
// its end token is silently dropped from the step log.
|
|
logger.Infof("%s", line)
|
|
return true
|
|
}
|
|
arg = UnescapeCommandData(arg)
|
|
kvPairs = unescapeKvPairs(kvPairs)
|
|
switch command {
|
|
case "set-env":
|
|
rc.setEnv(ctx, kvPairs, arg)
|
|
case "set-output":
|
|
rc.setOutput(ctx, kvPairs, arg)
|
|
case "add-path":
|
|
rc.addPath(ctx, arg)
|
|
case "debug":
|
|
logger.Infof("%s", line)
|
|
case "warning":
|
|
logger.Infof("%s", line)
|
|
case "error":
|
|
logger.Infof("%s", line)
|
|
case "add-mask":
|
|
rc.AddMask(arg)
|
|
logger.Infof("%s", "***")
|
|
case "stop-commands":
|
|
resumeCommand = arg
|
|
logger.Infof("%s", line)
|
|
case resumeCommand:
|
|
resumeCommand = ""
|
|
logger.Infof("%s", line)
|
|
case "save-state":
|
|
logger.Infof("%s", line)
|
|
rc.saveState(ctx, kvPairs, arg)
|
|
case "add-matcher":
|
|
logger.Infof("%s", line)
|
|
default:
|
|
logger.Infof("%s", line)
|
|
}
|
|
|
|
// return true to let gitea's logger handle these special outputs also
|
|
return true
|
|
}
|
|
}
|
|
|
|
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
name := kvPairs["name"]
|
|
common.Logger(ctx).Infof("::set-env:: %s=%s", name, arg)
|
|
if rc.Env == nil {
|
|
rc.Env = make(map[string]string)
|
|
}
|
|
if rc.GlobalEnv == nil {
|
|
rc.GlobalEnv = map[string]string{}
|
|
}
|
|
newenv := map[string]string{
|
|
name: arg,
|
|
}
|
|
mergeIntoMap := mergeIntoMapCaseSensitive
|
|
if rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
mergeIntoMap = mergeIntoMapCaseInsensitive
|
|
}
|
|
mergeIntoMap(rc.Env, newenv)
|
|
mergeIntoMap(rc.GlobalEnv, newenv)
|
|
}
|
|
|
|
func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
logger := common.Logger(ctx)
|
|
stepID := rc.CurrentStep
|
|
outputName := kvPairs["name"]
|
|
if outputMapping, ok := rc.OutputMappings[MappableOutput{StepID: stepID, OutputName: outputName}]; ok {
|
|
stepID = outputMapping.StepID
|
|
outputName = outputMapping.OutputName
|
|
}
|
|
|
|
result, ok := rc.StepResults[stepID]
|
|
if !ok {
|
|
logger.Infof("No outputs registered for step '%s'", stepID)
|
|
return
|
|
}
|
|
|
|
logger.Infof("::set-output:: %s=%s", outputName, arg)
|
|
result.Outputs[outputName] = arg
|
|
}
|
|
|
|
func (rc *RunContext) addPath(ctx context.Context, arg string) {
|
|
common.Logger(ctx).Infof("::add-path:: %s", arg)
|
|
extraPath := []string{arg}
|
|
for _, v := range rc.ExtraPath {
|
|
if v != arg {
|
|
extraPath = append(extraPath, v)
|
|
}
|
|
}
|
|
rc.ExtraPath = extraPath
|
|
}
|
|
|
|
func parseKeyValuePairs(kvPairs, separator string) map[string]string {
|
|
rtn := make(map[string]string)
|
|
kvPairList := strings.SplitSeq(kvPairs, separator)
|
|
for kvPair := range kvPairList {
|
|
kv := strings.Split(kvPair, "=")
|
|
if len(kv) == 2 {
|
|
rtn[kv[0]] = kv[1]
|
|
}
|
|
}
|
|
return rtn
|
|
}
|
|
|
|
func UnescapeCommandData(arg string) string {
|
|
escapeMap := map[string]string{
|
|
"%25": "%",
|
|
"%0D": "\r",
|
|
"%0A": "\n",
|
|
}
|
|
for k, v := range escapeMap {
|
|
arg = strings.ReplaceAll(arg, k, v)
|
|
}
|
|
return arg
|
|
}
|
|
|
|
func unescapeCommandProperty(arg string) string {
|
|
escapeMap := map[string]string{
|
|
"%25": "%",
|
|
"%0D": "\r",
|
|
"%0A": "\n",
|
|
"%3A": ":",
|
|
"%2C": ",",
|
|
}
|
|
for k, v := range escapeMap {
|
|
arg = strings.ReplaceAll(arg, k, v)
|
|
}
|
|
return arg
|
|
}
|
|
|
|
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
|
|
for k, v := range kvPairs {
|
|
kvPairs[k] = unescapeCommandProperty(v)
|
|
}
|
|
return kvPairs
|
|
}
|
|
|
|
func (rc *RunContext) saveState(_ context.Context, kvPairs map[string]string, arg string) {
|
|
stepID := rc.CurrentStep
|
|
if stepID != "" {
|
|
if rc.IntraActionState == nil {
|
|
rc.IntraActionState = map[string]map[string]string{}
|
|
}
|
|
state, ok := rc.IntraActionState[stepID]
|
|
if !ok {
|
|
state = map[string]string{}
|
|
rc.IntraActionState[stepID] = state
|
|
}
|
|
state[kvPairs["name"]] = arg
|
|
}
|
|
}
|