mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-06 07:10:28 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
470969dc74 | ||
|
|
be7ddcd15a | ||
|
|
c3c647ef83 | ||
|
|
160e18e705 | ||
|
|
20c8b4abc4 | ||
|
|
ccb9afd7a5 | ||
|
|
49e34813ae | ||
|
|
e0ef637805 | ||
|
|
8502663d0e | ||
|
|
4e8037a93b | ||
|
|
76110557ab | ||
|
|
3bc5cc03f8 | ||
|
|
de961b91eb | ||
|
|
ad9eeb668a | ||
|
|
b741335222 | ||
|
|
20143c9558 | ||
|
|
8b14a38d4a | ||
|
|
179e435cd0 | ||
|
|
f963e9cfe8 | ||
|
|
e5aa9add02 | ||
|
|
329115b148 | ||
|
|
ff30f39b1b | ||
|
|
3802feb3fe | ||
|
|
27e9b4de87 | ||
|
|
4bedadef9d | ||
|
|
83b63250a3 | ||
|
|
2e59141ae9 | ||
|
|
020f0d2861 | ||
|
|
711b72735b | ||
|
|
436d0d2942 | ||
|
|
a284a62d96 | ||
|
|
2b1e64e414 |
No files matched your search
@@ -19,7 +19,7 @@ jobs:
|
|||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||||
with:
|
with:
|
||||||
node-version: 24
|
node-version: 24
|
||||||
- run: make lint-pr-title
|
- run: make lint-pr-title
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
name: checks
|
name: checks
|
||||||
on:
|
on:
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
pull_request:
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
# The runner image ships a stale docker.io login; point docker at an empty config so
|
# The runner image ships a stale docker.io login; point docker at an empty config so
|
||||||
@@ -30,7 +28,7 @@ jobs:
|
|||||||
# daemon retains them between runs, so this is usually a fast manifest re-check.
|
# daemon retains them between runs, so this is usually a fast manifest re-check.
|
||||||
- name: pre-pull test images
|
- name: pre-pull test images
|
||||||
env:
|
env:
|
||||||
TEST_JOB_IMAGE: node:24-bookworm-slim@sha256:0e0ff40c39bc087845bfb27465a0df4ea419520094bc35842ff83dd8cbe6f9b6 # renovate: datasource=docker
|
TEST_JOB_IMAGE: node:24-bookworm-slim@sha256:d6aa754f16b3197301076f047b5def2f02ea1dbbc2ca920407d46d7ec7f87b20 # renovate: datasource=docker
|
||||||
TEST_SERVICE_IMAGE: nginx:alpine@sha256:df221db836e1754089190208cee7eeda94f233197056426eda74a43ab1abeac2 # renovate: datasource=docker
|
TEST_SERVICE_IMAGE: nginx:alpine@sha256:df221db836e1754089190208cee7eeda94f233197056426eda74a43ab1abeac2 # renovate: datasource=docker
|
||||||
run: |
|
run: |
|
||||||
for image in "$TEST_JOB_IMAGE" "$TEST_SERVICE_IMAGE"; do
|
for image in "$TEST_JOB_IMAGE" "$TEST_SERVICE_IMAGE"; do
|
||||||
|
|||||||
@@ -39,6 +39,15 @@ linters:
|
|||||||
deny:
|
deny:
|
||||||
- pkg: golang.org/x/exp
|
- pkg: golang.org/x/exp
|
||||||
desc: it's experimental and unreliable
|
desc: it's experimental and unreliable
|
||||||
|
logging:
|
||||||
|
files:
|
||||||
|
- $all
|
||||||
|
- "!**/act/common/logging/**"
|
||||||
|
deny:
|
||||||
|
- pkg: github.com/sirupsen/logrus
|
||||||
|
desc: log through gitea.com/gitea/runner/act/common/logging
|
||||||
|
- pkg: log/slog
|
||||||
|
desc: log through gitea.com/gitea/runner/act/common/logging
|
||||||
nolintlint:
|
nolintlint:
|
||||||
allow-unused: false
|
allow-unused: false
|
||||||
require-explanation: true
|
require-explanation: true
|
||||||
|
|||||||
@@ -31,6 +31,23 @@ depending on the prefix:
|
|||||||
- Command *properties* also escape `%3A` and `%2C`, which the UI never decodes, so the reporter
|
- Command *properties* also escape `%3A` and `%2C`, which the UI never decodes, so the reporter
|
||||||
decodes exactly those two when folding a location into an annotation.
|
decodes exactly those two when folding a location into an annotation.
|
||||||
|
|
||||||
|
## Logging
|
||||||
|
|
||||||
|
Log through `act/common/logging`; the linter rejects importing `log/slog` or logrus elsewhere.
|
||||||
|
A job's logger comes from `common.Logger(ctx)`; code with no job uses the package functions
|
||||||
|
(`logging.Infof`, `logging.With`). Both take printf-style messages, and `With("key", value)` adds
|
||||||
|
fields.
|
||||||
|
|
||||||
|
- The process-wide logger masks the secrets of every live task, the job logger masks its own.
|
||||||
|
Going around either leaks them.
|
||||||
|
- Libraries that log through logrus's or slog's global logger are forwarded to the process-wide
|
||||||
|
one, so they follow its level and masking too.
|
||||||
|
- That forwarding only works while logrus's global logger is left as `logging` configures it. A
|
||||||
|
dependency that calls `logrus.SetOutput` or `SetLevel` breaks it, so check
|
||||||
|
`logging.LogrusBridgeIntact()` when adding or upgrading one.
|
||||||
|
- Tests read what was logged with `logtest.NewLogger()`, or `logtest.SetDefault(t)` for the
|
||||||
|
package functions.
|
||||||
|
|
||||||
## Built-in actions
|
## Built-in actions
|
||||||
|
|
||||||
Each lives in `internal/pkg/action/<name>` and is registered in `builtinActions` in
|
Each lives in `internal/pkg/action/<name>` and is registered in `builtinActions` in
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ XGO_VERSION := go-1.27.x
|
|||||||
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/[email protected] # renovate: datasource=go
|
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/[email protected] # renovate: datasource=go
|
||||||
|
|
||||||
PODMAN_TEST_IMAGE ?= quay.io/containers/podman:v5.8.4-immutable@sha256:90405e3c6a86f33c5a6f8dbdfa8e02a98143a8ba4337b84bdaec5336d2ef008a # renovate: datasource=docker
|
PODMAN_TEST_IMAGE ?= quay.io/containers/podman:v5.8.4-immutable@sha256:90405e3c6a86f33c5a6f8dbdfa8e02a98143a8ba4337b84bdaec5336d2ef008a # renovate: datasource=docker
|
||||||
E2E_JOB_IMAGE ?= node:24-bookworm@sha256:64af3819f9275802414d7cdc38c27e9d82bd564dec4d4da87d008255d36c63b4 # renovate: datasource=docker
|
E2E_JOB_IMAGE ?= docker.gitea.com/runner-images:ubuntu-latest@sha256:0e62e56b382ebf485bff1e51a05cbe27c708545a2e91425467fd31eb3e249521 # renovate: datasource=docker
|
||||||
SERVICE_IMAGE ?= nginx:1.31.4-alpine@sha256:db35bfc6b2951e7f8a72db5db120288c127ffaeeb4a6d4b95a26fead017d5913 # renovate: datasource=docker
|
SERVICE_IMAGE ?= nginx:1.31.4-alpine@sha256:db35bfc6b2951e7f8a72db5db120288c127ffaeeb4a6d4b95a26fead017d5913 # renovate: datasource=docker
|
||||||
E2E_GITEA_IMAGE ?= gitea/gitea:main-nightly
|
E2E_GITEA_IMAGE ?= gitea/gitea:main-nightly
|
||||||
E2E_K3S_IMAGE ?= rancher/k3s:v1.36.4-k3s1@sha256:edad48e12bf81c3a09ac1c05c0c0ffaaa22145980b989d6fae84543a76b83657 # renovate: datasource=docker
|
E2E_K3S_IMAGE ?= rancher/k3s:v1.36.4-k3s1@sha256:edad48e12bf81c3a09ac1c05c0c0ffaaa22145980b989d6fae84543a76b83657 # renovate: datasource=docker
|
||||||
@@ -24,7 +24,7 @@ DOCKER_TAG ?= nightly
|
|||||||
DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
|
DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
|
||||||
DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
|
DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
|
||||||
|
|
||||||
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/[email protected]3.2 # renovate: datasource=go
|
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/[email protected]4.0 # renovate: datasource=go
|
||||||
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/[email protected] # renovate: datasource=go
|
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/[email protected] # renovate: datasource=go
|
||||||
|
|
||||||
GOTEST_FLAGS ?= -race -timeout 20m -parallel 8
|
GOTEST_FLAGS ?= -race -timeout 20m -parallel 8
|
||||||
|
|||||||
@@ -4,7 +4,19 @@
|
|||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
|
|
||||||
Docker Engine Community version is required for docker mode. To install Docker CE, follow the official [install instructions](https://docs.docker.com/engine/install/).
|
On the runner:
|
||||||
|
|
||||||
|
- `git` 2.34.1 or newer on `PATH`, to download actions and reusable workflows.
|
||||||
|
- `docker` or `podman` with `podman.socket` enabled, for docker mode, Docker actions and `docker://` steps. To install Docker CE, follow the official [install instructions](https://docs.docker.com/engine/install/).
|
||||||
|
|
||||||
|
Where steps run, in the job image or on the runner in host mode:
|
||||||
|
|
||||||
|
- `bash` or `sh` for `run` steps, `pwsh` or `powershell` on a Windows host.
|
||||||
|
- `node` for JavaScript actions and `hashFiles()`.
|
||||||
|
- `git` 2.34.1 or newer for [`builtin:checkout`](docs/builtin-actions.md).
|
||||||
|
- `git-lfs` and `ssh` for `builtin:checkout`, only when its `lfs` or `ssh-key` input is used.
|
||||||
|
|
||||||
|
Kubernetes mode needs [a few more tools](docs/kubernetes.md) in job images.
|
||||||
|
|
||||||
### Download pre-built binary
|
### Download pre-built binary
|
||||||
|
|
||||||
@@ -157,7 +169,7 @@ Of the [runner images](https://gitea.com/gitea/runner-images), the `-full` flavo
|
|||||||
|
|
||||||
#### Environment variables
|
#### Environment variables
|
||||||
|
|
||||||
Earlier releases let a few environment variables (`GITEA_DEBUG`, `GITEA_TRACE`, `GITEA_RUNNER_CAPACITY`, `GITEA_RUNNER_FILE`, `GITEA_RUNNER_ENVIRON`, `GITEA_RUNNER_ENV_FILE`) override parts of the config. They are gone, use the YAML file for all settings. The Docker images still read their own variables, such as `RUNNER_STATE_FILE`, see [scripts/run.sh](scripts/run.sh) and the container documentation below.
|
Earlier releases let a few environment variables (`GITEA_DEBUG`, `GITEA_TRACE`, `GITEA_RUNNER_CAPACITY`, `GITEA_RUNNER_FILE`, `GITEA_RUNNER_ENVIRON`, `GITEA_RUNNER_ENV_FILE`) override parts of the config. They are gone, use the YAML file for all settings. Config values can read the runner's environment as `${NAME}`, for example `capacity: ${RUNNER_CAPACITY}`. An unset `NAME` fails the load, and `$${NAME}` is a literal `${NAME}`. The Docker images still read their own variables, such as `RUNNER_STATE_FILE`, see [scripts/run.sh](scripts/run.sh) and the container documentation below.
|
||||||
|
|
||||||
### Labels
|
### Labels
|
||||||
|
|
||||||
@@ -205,7 +217,7 @@ a workflow with `runs-on: ubuntu-latest` is executed in the `runner-images:ubunt
|
|||||||
|
|
||||||
Names may themselves contain a colon, for example `pool:e57e18d4-10d4-406f-93bf-60f127221bdd`. Only `host`, `docker` and `kubernetes` are treated as schemas.
|
Names may themselves contain a colon, for example `pool:e57e18d4-10d4-406f-93bf-60f127221bdd`. Only `host`, `docker` and `kubernetes` are treated as schemas.
|
||||||
|
|
||||||
If a job's `runs-on` matches none of the runner's labels, or sets no `runs-on` at all, it still runs in `runner.default_image`: as a container when the runner has `docker` labels, as a pod when it has `kubernetes` labels, else as a container where docker is available and on the host where it is not. Images maintained for this purpose are listed at [gitea/runner-images](https://gitea.com/gitea/runner-images).
|
Jobs run on `runner.default_image` when no runner label matches their `runs-on`: as a pod on runners with `kubernetes` labels, otherwise in a container, or on the host when docker is unavailable. Suitable images are listed at [gitea/runner-images](https://gitea.com/gitea/runner-images).
|
||||||
|
|
||||||
Labels are chosen at registration time (`--labels`, or the interactive prompt) and can be changed afterwards by editing `runner.labels` in the config file, or in the Gitea UI under the runner's settings.
|
Labels are chosen at registration time (`--labels`, or the interactive prompt) and can be changed afterwards by editing `runner.labels` in the config file, or in the Gitea UI under the runner's settings.
|
||||||
|
|
||||||
@@ -252,7 +264,7 @@ volumes:
|
|||||||
- ${GITEA_DOCKER_WORKSPACE:-.}/data:/app/data
|
- ${GITEA_DOCKER_WORKSPACE:-.}/data:/app/data
|
||||||
```
|
```
|
||||||
|
|
||||||
Linux container jobs use a Docker proxy when socket sharing and permissions allow it. Other setups use the daemon socket directly. The proxy removes containers, networks and volumes after post steps and the completed hook. Named volumes created through it are job-scoped. To retain resources, mount the daemon socket explicitly in `container.options`. Host jobs use their existing Docker access, where `.` works.
|
Linux container jobs use a Docker proxy when socket sharing and permissions allow it. Other setups use the daemon socket directly. Host jobs use their existing Docker access, where `.` works. The proxy removes the containers, networks and volumes a job creates, `docker compose` services included, after post steps and the completed hook. Set `container.sweep: false` when jobs deploy services to the runner's Docker daemon.
|
||||||
|
|
||||||
#### Proxy
|
#### Proxy
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"encoding/json/v2"
|
"encoding/json/v2"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
@@ -30,10 +29,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/internal/pkg/disk"
|
"gitea.com/gitea/runner/internal/pkg/disk"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/timshannon/bolthold"
|
"github.com/timshannon/bolthold"
|
||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
@@ -85,7 +84,7 @@ type Handler struct {
|
|||||||
listener net.Listener
|
listener net.Listener
|
||||||
port int
|
port int
|
||||||
server *http.Server
|
server *http.Server
|
||||||
logger logrus.FieldLogger
|
logger *logging.Logger
|
||||||
|
|
||||||
gcing atomic.Bool
|
gcing atomic.Bool
|
||||||
gcAt time.Time
|
gcAt time.Time
|
||||||
@@ -133,7 +132,7 @@ type Options struct {
|
|||||||
InternalSecret string
|
InternalSecret string
|
||||||
|
|
||||||
Policy Policy
|
Policy Policy
|
||||||
Logger logrus.FieldLogger
|
Logger *logging.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartHandler starts an embedded cache or forwards cache requests to Upstream.
|
// StartHandler starts an embedded cache or forwards cache requests to Upstream.
|
||||||
@@ -147,11 +146,9 @@ func StartHandler(opts Options) (*Handler, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if logger == nil {
|
if logger == nil {
|
||||||
discard := logrus.New()
|
logger = logging.New(logging.DiscardHandler)
|
||||||
discard.Out = io.Discard
|
|
||||||
logger = discard
|
|
||||||
}
|
}
|
||||||
logger = logger.WithField("module", "artifactcache")
|
logger = logger.With("module", "artifactcache")
|
||||||
h.logger = logger
|
h.logger = logger
|
||||||
|
|
||||||
if opts.OutboundIP != "" {
|
if opts.OutboundIP != "" {
|
||||||
|
|||||||
@@ -230,7 +230,8 @@ func Serve(ctx context.Context, artifactPath, addr, port string) context.CancelF
|
|||||||
go func() {
|
go func() {
|
||||||
logger.Infof("Start server on http://%s", server.Addr)
|
logger.Infof("Start server on http://%s", server.Addr)
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
logger.Fatal(err)
|
logger.Error(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,15 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDryrunContext(t *testing.T) {
|
func TestDryrunContext(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
if Dryrun(ctx) {
|
if Dryrun(ctx) {
|
||||||
t.Fatal("plain context should not be dryrun")
|
t.Fatal("plain context should not be dryrun")
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@ func TestDryrunContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestJobErrorContainer(t *testing.T) {
|
func TestJobErrorContainer(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
err := errors.New("job failed")
|
err := errors.New("job failed")
|
||||||
|
|
||||||
SetJobError(ctx, err)
|
SetJobError(ctx, err)
|
||||||
@@ -40,34 +40,27 @@ func TestJobErrorContainer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoggerAndHookContext(t *testing.T) {
|
func TestLoggerAndHandlerContext(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
if Logger(ctx) != logrus.StandardLogger() {
|
if Logger(ctx) != logging.Default() {
|
||||||
t.Fatal("plain context should use standard logger")
|
t.Fatal("plain context should use the default logger")
|
||||||
}
|
}
|
||||||
if LoggerHook(ctx) != nil {
|
if LoggerHandler(ctx) != nil {
|
||||||
t.Fatal("plain context should not have a logger hook")
|
t.Fatal("plain context should not have a logger handler")
|
||||||
|
}
|
||||||
|
if StepIDs(ctx) != nil {
|
||||||
|
t.Fatal("plain context should not have step IDs")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := logrus.New()
|
logger := logging.New(logtest.New())
|
||||||
ctx = WithLogger(ctx, logger)
|
ctx = WithLogger(ctx, logger)
|
||||||
if Logger(ctx) != logger {
|
if Logger(ctx) != logger {
|
||||||
t.Fatal("WithLogger should set logger")
|
t.Fatal("WithLogger should set logger")
|
||||||
}
|
}
|
||||||
|
|
||||||
hook := testHook{}
|
handler := logtest.New()
|
||||||
ctx = WithLoggerHook(ctx, hook)
|
ctx = WithLoggerHandler(ctx, handler)
|
||||||
if LoggerHook(ctx) != hook {
|
if LoggerHandler(ctx) != handler {
|
||||||
t.Fatal("WithLoggerHook should set hook")
|
t.Fatal("WithLoggerHandler should set handler")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type testHook struct{}
|
|
||||||
|
|
||||||
func (testHook) Levels() []logrus.Level {
|
|
||||||
return logrus.AllLevels
|
|
||||||
}
|
|
||||||
|
|
||||||
func (testHook) Fire(*logrus.Entry) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Executor define contract for the steps of a workflow
|
// Executor define contract for the steps of a workflow
|
||||||
@@ -74,33 +74,33 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
|
|||||||
errs := make(chan error, len(executors))
|
errs := make(chan error, len(executors))
|
||||||
|
|
||||||
if 1 > parallel {
|
if 1 > parallel {
|
||||||
log.Debugf("Parallel tasks (%d) below minimum, setting to 1", parallel)
|
logging.Debugf("Parallel tasks (%d) below minimum, setting to 1", parallel)
|
||||||
parallel = 1
|
parallel = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors))
|
logging.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors))
|
||||||
|
|
||||||
for i := 0; i < parallel; i++ {
|
for i := 0; i < parallel; i++ {
|
||||||
go func(workerID int, work <-chan Executor, errs chan<- error) {
|
go func(workerID int, work <-chan Executor, errs chan<- error) {
|
||||||
log.Debugf("Worker %d started", workerID)
|
logging.Debugf("Worker %d started", workerID)
|
||||||
taskCount := 0
|
taskCount := 0
|
||||||
for executor := range work {
|
for executor := range work {
|
||||||
taskCount++
|
taskCount++
|
||||||
log.Debugf("Worker %d executing task %d", workerID, taskCount)
|
logging.Debugf("Worker %d executing task %d", workerID, taskCount)
|
||||||
// Recover from panics in executors to avoid crashing the worker
|
// Recover from panics in executors to avoid crashing the worker
|
||||||
// goroutine which would leave the runner process hung.
|
// goroutine which would leave the runner process hung.
|
||||||
// https://gitea.com/gitea/runner/issues/371
|
// https://gitea.com/gitea/runner/issues/371
|
||||||
errs <- func() (err error) {
|
errs <- func() (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Errorf("panic in executor: %v\n%s", r, debug.Stack())
|
logging.Errorf("panic in executor: %v\n%s", r, debug.Stack())
|
||||||
err = fmt.Errorf("panic: %v", r)
|
err = fmt.Errorf("panic: %v", r)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return executor(ctx)
|
return executor(ctx)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
log.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount)
|
logging.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount)
|
||||||
}(i, work, errs)
|
}(i, work, errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
func TestNewWorkflow(t *testing.T) {
|
func TestNewWorkflow(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
// empty
|
// empty
|
||||||
emptyWorkflow := NewPipelineExecutor()
|
emptyWorkflow := NewPipelineExecutor()
|
||||||
@@ -72,7 +72,7 @@ func concurrencyProbe(wantActive int32) (exec Executor, count, maxActive *atomic
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewParallelExecutor(t *testing.T) {
|
func TestNewParallelExecutor(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
exec, count, maxActive := concurrencyProbe(2)
|
exec, count, maxActive := concurrencyProbe(2)
|
||||||
require.NoError(t, NewParallelExecutor(2, exec, exec, exec)(ctx))
|
require.NoError(t, NewParallelExecutor(2, exec, exec, exec)(ctx))
|
||||||
@@ -89,10 +89,10 @@ func TestNewParallelExecutor(t *testing.T) {
|
|||||||
func TestNewParallelExecutorEmpty(t *testing.T) {
|
func TestNewParallelExecutorEmpty(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
require.NoError(t, NewParallelExecutor(2)(ctx))
|
require.NoError(t, NewParallelExecutor(2)(ctx))
|
||||||
|
|
||||||
canceledCtx, cancel := context.WithCancel(context.Background())
|
canceledCtx, cancel := context.WithCancel(t.Context())
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
err := NewParallelExecutor(2)(canceledCtx)
|
err := NewParallelExecutor(2)(canceledCtx)
|
||||||
@@ -102,7 +102,7 @@ func TestNewParallelExecutorEmpty(t *testing.T) {
|
|||||||
func TestNewParallelExecutorFailed(t *testing.T) {
|
func TestNewParallelExecutorFailed(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
@@ -118,7 +118,7 @@ func TestNewParallelExecutorFailed(t *testing.T) {
|
|||||||
func TestNewParallelExecutorCanceled(t *testing.T) {
|
func TestNewParallelExecutorCanceled(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
errExpected := errors.New("fake error")
|
errExpected := errors.New("fake error")
|
||||||
@@ -150,12 +150,12 @@ func TestNewParallelExecutorRunsRemainingAfterFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Error(t, NewParallelExecutor(2, executors...)(context.Background()))
|
require.Error(t, NewParallelExecutor(2, executors...)(t.Context()))
|
||||||
assert.Equal(t, int32(4), successCount.Load(), "a failing executor must not stop the others")
|
assert.Equal(t, int32(4), successCount.Load(), "a failing executor must not stop the others")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExecutorConditionalsAndFinally(t *testing.T) {
|
func TestExecutorConditionalsAndFinally(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
var calls []string
|
var calls []string
|
||||||
record := func(name string) Executor {
|
record := func(name string) Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
@@ -180,7 +180,7 @@ func TestExecutorFinallyReturnsFinallyErrorWithOriginal(t *testing.T) {
|
|||||||
mainErr := errors.New("main failed")
|
mainErr := errors.New("main failed")
|
||||||
finalErr := errors.New("cleanup failed")
|
finalErr := errors.New("cleanup failed")
|
||||||
|
|
||||||
err := NewErrorExecutor(mainErr).Finally(NewErrorExecutor(finalErr))(context.Background())
|
err := NewErrorExecutor(mainErr).Finally(NewErrorExecutor(finalErr))(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
if !strings.Contains(err.Error(), "cleanup failed") || !strings.Contains(err.Error(), "main failed") {
|
if !strings.Contains(err.Error(), "cleanup failed") || !strings.Contains(err.Error(), "main failed") {
|
||||||
t.Fatalf("finally error = %q, want both cleanup and original error", err)
|
t.Fatalf("finally error = %q, want both cleanup and original error", err)
|
||||||
|
|||||||
+292
-394
@@ -6,62 +6,66 @@ package git
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"net/url"
|
||||||
nethttp "net/http"
|
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/internal/pkg/lock"
|
"gitea.com/gitea/runner/internal/pkg/lock"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"github.com/go-git/go-git/v5/config"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/storer"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/client"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
||||||
"github.com/mattn/go-isatty"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
codeCommitHTTPRegex = regexp.MustCompile(`^https?://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
codeCommitHTTPRegex = regexp.MustCompile(`^https?://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
||||||
codeCommitSSHRegex = regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
codeCommitSSHRegex = regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
|
||||||
githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`)
|
hexRefRegex = regexp.MustCompile(`^[0-9a-fA-F]+$`)
|
||||||
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+?)(?:.git)?$`)
|
|
||||||
|
LocalEnvVars = []string{ // `git rev-parse --local-env-vars`, inherited ones point git at another repository
|
||||||
|
"GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_CONFIG", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", "GIT_OBJECT_DIRECTORY",
|
||||||
|
"GIT_DIR", "GIT_WORK_TREE", "GIT_IMPLICIT_WORK_TREE", "GIT_GRAFT_FILE", "GIT_INDEX_FILE", "GIT_NO_REPLACE_OBJECTS",
|
||||||
|
"GIT_REPLACE_REF_BASE", "GIT_PREFIX", "GIT_SHALLOW_FILE", "GIT_COMMON_DIR",
|
||||||
|
}
|
||||||
|
|
||||||
cloneLocks lock.Keyed[string] // key: clone target directory
|
cloneLocks lock.Keyed[string] // key: clone target directory
|
||||||
|
|
||||||
ErrShortRef = errors.New("short SHA references are not supported")
|
ErrShortRef = errors.New("short SHA references are not supported")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
// AcquireCloneLock serializes access to dir across runner processes, or within this one where no lock file can be created. Readers of dir must hold it too.
|
||||||
httpClient := newHTTPClient(&nethttp.HTTP2Config{SendPingTimeout: 30 * time.Second})
|
func AcquireCloneLock(ctx context.Context, dir string) (func(), error) {
|
||||||
client.InstallProtocol("http", httpClient)
|
dir = filepath.Clean(dir)
|
||||||
client.InstallProtocol("https", httpClient)
|
if runtime.GOOS == "plan9" {
|
||||||
}
|
return cloneLocks.Lock(dir), nil
|
||||||
|
}
|
||||||
// newHTTPClient pings idle HTTP/2 connections to drop stalled ones.
|
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
|
||||||
func newHTTPClient(h2 *nethttp.HTTP2Config) transport.Transport {
|
return nil, err
|
||||||
defaultTransport, _ := nethttp.DefaultTransport.(*nethttp.Transport)
|
}
|
||||||
httpTransport := defaultTransport.Clone()
|
for {
|
||||||
httpTransport.HTTP2 = h2
|
release, err := lock.TryLock(dir)
|
||||||
return http.NewClient(&nethttp.Client{Transport: httpTransport})
|
if err == nil {
|
||||||
}
|
return func() { _ = release() }, nil
|
||||||
|
}
|
||||||
// AcquireCloneLock returns an unlock function after locking the per-directory mutex for dir.
|
if !errors.Is(err, lock.ErrLocked) {
|
||||||
// Only concurrent operations targeting the same directory are serialized; clones into different directories run in parallel.
|
return cloneLocks.Lock(dir), nil
|
||||||
// Callers reading files inside dir (e.g. tarring a checked-out action into a job container) must hold this lock too,
|
}
|
||||||
// otherwise a concurrent NewGitCloneExecutor on the same dir can mutate the worktree mid-read.
|
select {
|
||||||
func AcquireCloneLock(dir string) func() {
|
case <-ctx.Done():
|
||||||
return cloneLocks.Lock(dir)
|
return nil, ctx.Err()
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Error struct {
|
type Error struct {
|
||||||
@@ -69,196 +73,97 @@ type Error struct {
|
|||||||
commit string
|
commit string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Error) Error() string {
|
func (e *Error) Error() string { return e.err.Error() }
|
||||||
return e.err.Error()
|
func (e *Error) Unwrap() error { return e.err }
|
||||||
}
|
func (e *Error) Commit() string { return e.commit }
|
||||||
|
|
||||||
func (e *Error) Unwrap() error {
|
|
||||||
return e.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Error) Commit() string {
|
|
||||||
return e.commit
|
|
||||||
}
|
|
||||||
|
|
||||||
// goGitMu serializes go-git repository access across the process. go-git is not safe for
|
|
||||||
// concurrent use of the same repository (even read access decodes packfiles into shared
|
|
||||||
// state), so parallel jobs inspecting the shared workdir repo race without this. The guarded
|
|
||||||
// operations are fast local reads; gitea runs one job per process, so the lock is effectively
|
|
||||||
// uncontended in production.
|
|
||||||
var goGitMu sync.Mutex
|
|
||||||
|
|
||||||
// FindGitRevision get the current git revision
|
// FindGitRevision get the current git revision
|
||||||
func FindGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) {
|
func FindGitRevision(ctx context.Context, file string) (shortSHA, sha string, err error) {
|
||||||
goGitMu.Lock()
|
|
||||||
defer goGitMu.Unlock()
|
|
||||||
return findGitRevision(ctx, file)
|
|
||||||
}
|
|
||||||
|
|
||||||
func findGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) {
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
sha, err = gitOutput(ctx, file, "rev-parse", "--verify", "HEAD^{commit}")
|
||||||
gitDir, err := git.PlainOpenWithOptions(
|
|
||||||
file,
|
|
||||||
&git.PlainOpenOptions{
|
|
||||||
DetectDotGit: true,
|
|
||||||
EnableDotGitCommonDir: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.WithError(err).Error("path", file, "not located inside a git repository")
|
logger.WithError(err).Error("path", file, "not located inside a git repository")
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
if len(sha) < 7 {
|
||||||
head, err := gitDir.Reference(plumbing.HEAD, true)
|
return "", "", errors.New("HEAD could not be resolved")
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
}
|
||||||
|
logger.Debugf("Found revision: %s", sha)
|
||||||
if head.Hash().IsZero() {
|
return sha[:7], sha, nil
|
||||||
return "", "", errors.New("HEAD sha1 could not be resolved")
|
|
||||||
}
|
|
||||||
|
|
||||||
hash := head.Hash().String()
|
|
||||||
|
|
||||||
logger.Debugf("Found revision: %s", hash)
|
|
||||||
return hash[:7], strings.TrimSpace(hash), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindGitRef get the current git ref
|
// FindGitRef get the current git ref
|
||||||
func FindGitRef(ctx context.Context, file string) (string, error) {
|
func FindGitRef(ctx context.Context, file string) (string, error) {
|
||||||
goGitMu.Lock()
|
|
||||||
defer goGitMu.Unlock()
|
|
||||||
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
logger.Debugf("Loading revision from git directory")
|
logger.Debugf("Loading revision from git directory")
|
||||||
_, ref, err := findGitRevision(ctx, file)
|
_, revision, err := FindGitRevision(ctx, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
logger.Debugf("HEAD points to '%s'", revision)
|
||||||
|
|
||||||
logger.Debugf("HEAD points to '%s'", ref)
|
refs, err := gitOutput(ctx, file, "for-each-ref", "--points-at=HEAD", "--format=%(refname)", "refs/tags", "refs/heads")
|
||||||
|
|
||||||
// Prefer the git library to iterate over the references and find a matching tag or branch.
|
|
||||||
refTag := ""
|
|
||||||
refBranch := ""
|
|
||||||
repo, err := git.PlainOpenWithOptions(
|
|
||||||
file,
|
|
||||||
&git.PlainOpenOptions{
|
|
||||||
DetectDotGit: true,
|
|
||||||
EnableDotGitCommonDir: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
var tag, branch string
|
||||||
iter, err := repo.References()
|
for ref := range strings.FieldsSeq(refs) {
|
||||||
if err != nil {
|
switch {
|
||||||
return "", err
|
case strings.HasPrefix(ref, "refs/tags/"):
|
||||||
}
|
tag = ref
|
||||||
|
case strings.HasPrefix(ref, "refs/heads/"):
|
||||||
// find the reference that matches the revision's has
|
branch = ref
|
||||||
err = iter.ForEach(func(r *plumbing.Reference) error {
|
|
||||||
/* tags and branches will have the same hash
|
|
||||||
* when a user checks out a tag, it is not mentioned explicitly
|
|
||||||
* in the go-git package, we must identify the revision
|
|
||||||
* then check if any tag matches that revision,
|
|
||||||
* if so then we checked out a tag
|
|
||||||
* else we look for branches and if matches,
|
|
||||||
* it means we checked out a branch
|
|
||||||
*
|
|
||||||
* If a branches matches first we must continue and check all tags (all references)
|
|
||||||
* in case we match with a tag later in the interation
|
|
||||||
*/
|
|
||||||
if r.Hash().String() == ref {
|
|
||||||
if r.Name().IsTag() {
|
|
||||||
refTag = r.Name().String()
|
|
||||||
}
|
|
||||||
if r.Name().IsBranch() {
|
|
||||||
refBranch = r.Name().String()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we found what we where looking for
|
|
||||||
if refTag != "" && refBranch != "" {
|
|
||||||
return storer.ErrStop
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
if tag != "" {
|
||||||
// order matters here see above comment.
|
return tag, nil
|
||||||
if refTag != "" {
|
|
||||||
return refTag, nil
|
|
||||||
}
|
}
|
||||||
if refBranch != "" {
|
if branch != "" {
|
||||||
return refBranch, nil
|
return branch, nil
|
||||||
}
|
}
|
||||||
|
return "", fmt.Errorf("failed to identify reference (tag/branch) for the checked-out revision '%s'", revision)
|
||||||
return "", fmt.Errorf("failed to identify reference (tag/branch) for the checked-out revision '%s'", ref)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindGithubRepo get the repo
|
// FindGithubRepo get the repo
|
||||||
func FindGithubRepo(ctx context.Context, file, githubInstance string) (string, error) {
|
func FindGithubRepo(ctx context.Context, file string) (string, error) {
|
||||||
goGitMu.Lock()
|
url, err := findGitRemoteURL(ctx, file)
|
||||||
defer goGitMu.Unlock()
|
|
||||||
|
|
||||||
url, err := findGitRemoteURL(ctx, file, "origin")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
_, slug := findGitSlug(url, githubInstance)
|
return findGitSlug(url)
|
||||||
return slug, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findGitRemoteURL(_ context.Context, file, remoteName string) (string, error) {
|
func findGitRemoteURL(ctx context.Context, file string) (string, error) {
|
||||||
repo, err := git.PlainOpenWithOptions(
|
urls, err := gitOutput(ctx, file, "config", "--get-all", "remote.origin.url") // `remote get-url` applies insteadOf
|
||||||
file,
|
|
||||||
&git.PlainOpenOptions{
|
|
||||||
DetectDotGit: true,
|
|
||||||
EnableDotGitCommonDir: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if urls == "" {
|
||||||
remote, err := repo.Remote(remoteName)
|
return "", errors.New("remote 'origin' exists but has no URL")
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
url, _, _ := strings.Cut(urls, "\n")
|
||||||
if len(remote.Config().URLs) < 1 {
|
return url, nil
|
||||||
return "", fmt.Errorf("remote '%s' exists but has no URL", remoteName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return remote.Config().URLs[0], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findGitSlug(url, githubInstance string) (string, string) {
|
func findGitSlug(remoteURL string) (string, error) {
|
||||||
if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil {
|
if matches := codeCommitHTTPRegex.FindStringSubmatch(remoteURL); matches != nil {
|
||||||
return "CodeCommit", matches[2]
|
return matches[2], nil
|
||||||
} else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil {
|
} else if matches := codeCommitSSHRegex.FindStringSubmatch(remoteURL); matches != nil {
|
||||||
return "CodeCommit", matches[2]
|
return matches[2], nil
|
||||||
} else if matches := githubHTTPRegex.FindStringSubmatch(url); matches != nil {
|
|
||||||
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2])
|
|
||||||
} else if matches := githubSSHRegex.FindStringSubmatch(url); matches != nil {
|
|
||||||
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2])
|
|
||||||
} else if githubInstance != "github.com" {
|
|
||||||
gheHTTPRegex := regexp.MustCompile(fmt.Sprintf(`^https?://%s/(.+)/(.+?)(?:.git)?$`, githubInstance))
|
|
||||||
gheSSHRegex := regexp.MustCompile(githubInstance + "[:/](.+)/(.+?)(?:.git)?$")
|
|
||||||
if matches := gheHTTPRegex.FindStringSubmatch(url); matches != nil {
|
|
||||||
return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2])
|
|
||||||
} else if matches := gheSSHRegex.FindStringSubmatch(url); matches != nil {
|
|
||||||
return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "", url
|
if host, repoPath, ok := strings.Cut(remoteURL, ":"); ok && filepath.VolumeName(remoteURL) == "" && !strings.Contains(host, "/") && !strings.HasPrefix(repoPath, "//") {
|
||||||
|
remoteURL = "ssh://" + host + "/" + repoPath // scp-like [user@]host:path
|
||||||
|
}
|
||||||
|
parsed, err := url.Parse(remoteURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New("cannot parse the origin remote URL")
|
||||||
|
}
|
||||||
|
owner, repo := path.Split(strings.TrimSuffix(strings.Trim(parsed.Path, "/"), ".git"))
|
||||||
|
owner = path.Base(owner)
|
||||||
|
if parsed.Host == "" || repo == "" || owner == "." || owner == "/" {
|
||||||
|
return "", errors.New("cannot determine owner and repository from the origin remote URL")
|
||||||
|
}
|
||||||
|
return owner + "/" + repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
||||||
@@ -278,69 +183,38 @@ type NewGitCloneExecutorInput struct {
|
|||||||
Quiet bool
|
Quiet bool
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
InsecureSkipTLS bool
|
InsecureSkipTLS bool
|
||||||
|
ClientCertFile, ClientKeyFile string // PEM, absolute as git runs in other directories
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloneIfRequired returns the repository and a boolean indicating whether an existing local clone was reused.
|
// CloneIfRequired reports whether an existing local clone was reused.
|
||||||
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, bool, error) {
|
func CloneIfRequired(ctx context.Context, input NewGitCloneExecutorInput, logger *logging.Logger) (bool, error) {
|
||||||
r, err := git.PlainOpen(input.Dir)
|
var origin string
|
||||||
|
_, err := os.Stat(filepath.Join(input.Dir, ".git")) // without it git would discover a repository around Dir
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Verify the cached clone still points to the resolved URL before reusing it.
|
origin, err = findGitRemoteURL(ctx, input.Dir)
|
||||||
remote, err := r.Remote("origin")
|
|
||||||
if err == nil && len(remote.Config().URLs) > 0 && remote.Config().URLs[0] == input.URL {
|
|
||||||
// Reuse existing clone
|
|
||||||
return r, true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case err != nil:
|
|
||||||
logger.Debugf("Removing cached clone at %s because origin cannot be read: %v", input.Dir, err)
|
|
||||||
case len(remote.Config().URLs) == 0:
|
|
||||||
logger.Debugf("Removing cached clone at %s because origin has no URL", input.Dir)
|
|
||||||
default:
|
|
||||||
logger.Debugf("Removing cached clone at %s because origin URL changed from %s to %s", input.Dir, remote.Config().URLs[0], input.URL)
|
|
||||||
}
|
|
||||||
if err := os.RemoveAll(input.Dir); err != nil {
|
|
||||||
return nil, false, fmt.Errorf("remove cached clone %s: %w", input.Dir, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if err == nil && origin == input.URL {
|
||||||
var progressWriter io.Writer
|
return true, nil
|
||||||
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
|
|
||||||
if entry, ok := logger.(*log.Entry); ok {
|
|
||||||
progressWriter = entry.WriterLevel(log.DebugLevel)
|
|
||||||
} else if lgr, ok := logger.(*log.Logger); ok {
|
|
||||||
progressWriter = lgr.WriterLevel(log.DebugLevel)
|
|
||||||
} else {
|
|
||||||
log.Errorf("Unable to get writer from logger (type=%T)", logger)
|
|
||||||
progressWriter = os.Stdout
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if ctxErr := ctx.Err(); ctxErr != nil { // a cancelled origin lookup must not discard a valid clone
|
||||||
cloneOptions := git.CloneOptions{
|
return false, ctxErr
|
||||||
URL: input.URL,
|
|
||||||
Progress: progressWriter,
|
|
||||||
|
|
||||||
InsecureSkipTLS: input.InsecureSkipTLS, // For Gitea
|
|
||||||
}
|
}
|
||||||
if input.Token != "" {
|
if err == nil {
|
||||||
cloneOptions.Auth = &http.BasicAuth{
|
logger.Debugf("Removing cached clone at %s because origin URL changed from %s to %s", input.Dir, origin, input.URL)
|
||||||
Username: "token",
|
} else if _, statErr := os.Stat(input.Dir); statErr == nil {
|
||||||
Password: input.Token,
|
logger.Debugf("Removing cached clone at %s because origin cannot be read: %v", input.Dir, err)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if err := os.RemoveAll(input.Dir); err != nil {
|
||||||
r, err = cloneAtDepth(ctx, input, cloneOptions, logger)
|
return false, fmt.Errorf("remove cached clone %s: %w", input.Dir, err)
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("Unable to clone %v %s: %v", input.URL, refName, err)
|
|
||||||
return nil, false, err
|
|
||||||
}
|
}
|
||||||
|
if err := clone(ctx, input); err != nil {
|
||||||
if err = os.Chmod(input.Dir, 0o755); err != nil {
|
return false, err
|
||||||
return nil, false, err
|
|
||||||
}
|
}
|
||||||
|
if err := os.Chmod(input.Dir, 0o755); err != nil {
|
||||||
return r, false, nil
|
return false, err
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGitCloneExecutor creates an executor to clone git repos
|
// NewGitCloneExecutor creates an executor to clone git repos
|
||||||
@@ -353,134 +227,129 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
|
|||||||
logger.Infof("git clone '%s' # ref=%s", input.URL, input.Ref)
|
logger.Infof("git clone '%s' # ref=%s", input.URL, input.Ref)
|
||||||
}
|
}
|
||||||
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
|
||||||
|
unlock, err := AcquireCloneLock(ctx, input.Dir)
|
||||||
defer AcquireCloneLock(input.Dir)()
|
|
||||||
|
|
||||||
refName := plumbing.ReferenceName("refs/heads/" + input.Ref)
|
|
||||||
r, reused, err := CloneIfRequired(ctx, refName, input, logger)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer unlock()
|
||||||
|
|
||||||
isOfflineMode := input.OfflineMode
|
reused, err := CloneIfRequired(ctx, input, logger)
|
||||||
|
if err != nil {
|
||||||
fetchOptions := git.FetchOptions{
|
return err
|
||||||
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD", "refs/heads/*:refs/remotes/origin/*"},
|
|
||||||
Force: true,
|
|
||||||
InsecureSkipTLS: input.InsecureSkipTLS,
|
|
||||||
}
|
}
|
||||||
if input.Token != "" {
|
resolved, err := resolveRef(ctx, input.Dir, input.Ref)
|
||||||
fetchOptions.Auth = &http.BasicAuth{
|
if errors.Is(err, ErrShortRef) || (err != nil && input.OfflineMode) {
|
||||||
Username: "token",
|
return err
|
||||||
Password: input.Token,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if !input.OfflineMode && (err != nil || (reused && !isFullHash(input.Ref, resolved))) {
|
||||||
// Action clones only ever need the tip commit, so keep a shallow cache cheap on update at depth 1 regardless of its original depth
|
if err := fetch(ctx, input); err != nil {
|
||||||
// Turning action_shallow_clone off does not convert an existing shallow cache; evict it for a full clone.
|
|
||||||
if isShallow(r) {
|
|
||||||
fetchOptions.Depth = 1
|
|
||||||
if spec, ok := shallowFetchRefSpec(r, input.Ref); ok {
|
|
||||||
fetchOptions.RefSpecs = []config.RefSpec{spec}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A just-cloned ref is as current as a fetch would make it, and a commit hash never moves.
|
|
||||||
// TODO: revalidate a mutable ref with a conditional archive request instead, once every
|
|
||||||
// supported Gitea sends an ETag for them: https://github.com/go-gitea/gitea/pull/39289
|
|
||||||
_, present := refRevision(r, input.Ref)
|
|
||||||
refresh := !isOfflineMode && (!present || (reused && !plumbing.IsHash(input.Ref)))
|
|
||||||
|
|
||||||
if refresh {
|
|
||||||
err = r.FetchContext(ctx, &fetchOptions)
|
|
||||||
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
resolved, err = resolveRef(ctx, input.Dir, input.Ref)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
var hash *plumbing.Hash
|
|
||||||
rev := plumbing.Revision(input.Ref)
|
|
||||||
if hash, err = r.ResolveRevision(rev); err != nil {
|
|
||||||
// ResolveRevision returns a nil hash on error, and a branch ref legitimately fails
|
|
||||||
// here (no local refs/heads/<ref>); the duck-typing below resolves it.
|
|
||||||
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
|
||||||
} else if hash.String() != input.Ref && strings.HasPrefix(hash.String(), input.Ref) {
|
|
||||||
return &Error{
|
|
||||||
err: ErrShortRef,
|
|
||||||
commit: hash.String(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rev, _ = refRevision(r, input.Ref)
|
|
||||||
|
|
||||||
if hash, err = r.ResolveRevision(rev); err != nil {
|
|
||||||
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
logger.Errorf("Unable to resolve %s: %v", input.Ref, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := runGit(ctx, input.Dir, nil, "checkout", "--force", "--detach", resolved); err != nil {
|
||||||
var w *git.Worktree
|
return fmt.Errorf("checkout %s: %w", resolved, err)
|
||||||
if w, err = r.Worktree(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reusedMsg := ""
|
reusedMsg := ""
|
||||||
if isOfflineMode && reused {
|
if input.OfflineMode && reused {
|
||||||
reusedMsg = " (reused in offline mode)"
|
reusedMsg = " (reused in offline mode)"
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debugf("Cloned %s to %s%s", input.URL, input.Dir, reusedMsg)
|
logger.Debugf("Cloned %s to %s%s", input.URL, input.Dir, reusedMsg)
|
||||||
|
|
||||||
if err = w.Checkout(&git.CheckoutOptions{
|
|
||||||
Hash: *hash,
|
|
||||||
Force: true,
|
|
||||||
}); err != nil {
|
|
||||||
logger.Errorf("Unable to checkout %s: %v", *hash, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf("Checked out %s", input.Ref)
|
logger.Debugf("Checked out %s", input.Ref)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cloneAtDepth falls back to a full clone when the shallow attempt fails.
|
func clone(ctx context.Context, input NewGitCloneExecutorInput) error {
|
||||||
func cloneAtDepth(ctx context.Context, input NewGitCloneExecutorInput, opts git.CloneOptions, logger log.FieldLogger) (*git.Repository, error) {
|
if input.Depth > 0 && isFullObjectID(input.Ref) {
|
||||||
|
if err := shallowPinnedClone(ctx, input); err == nil {
|
||||||
|
return nil
|
||||||
|
} else if err := removePartialClone(input.Dir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
args := []string{"clone", "--no-checkout"}
|
||||||
if input.Depth > 0 {
|
if input.Depth > 0 {
|
||||||
if plumbing.IsHash(input.Ref) {
|
args = append(args, "--no-local", "--depth", strconv.Itoa(input.Depth), "--no-tags")
|
||||||
r, err := fetchPinnedSHA(ctx, input, opts)
|
if input.Ref != "HEAD" && !strings.HasPrefix(input.Ref, "refs/") {
|
||||||
if err == nil {
|
args = append(args, "--branch", input.Ref)
|
||||||
return r, nil
|
}
|
||||||
}
|
}
|
||||||
logger.Debugf("Shallow fetch of %s at %s failed: %v", input.URL, input.Ref, err)
|
args = append(args, input.URL, input.Dir)
|
||||||
if err := removePartialClone(input.Dir); err != nil {
|
if _, err := runGit(ctx, "", &input, args...); err == nil {
|
||||||
return nil, err
|
return nil
|
||||||
}
|
} else if input.Depth == 0 {
|
||||||
} else {
|
return err
|
||||||
refNames := []plumbing.ReferenceName{plumbing.NewBranchReferenceName(input.Ref), plumbing.NewTagReferenceName(input.Ref)}
|
}
|
||||||
if input.Ref == plumbing.HEAD.String() || strings.HasPrefix(input.Ref, "refs/") {
|
if err := removePartialClone(input.Dir); err != nil {
|
||||||
refNames = []plumbing.ReferenceName{plumbing.ReferenceName(input.Ref)} // HEAD is the remote's default branch
|
return err
|
||||||
}
|
}
|
||||||
for _, refName := range refNames {
|
_, err := runGit(ctx, "", &input, "clone", "--no-checkout", input.URL, input.Dir)
|
||||||
shallowOpts := opts
|
return err
|
||||||
shallowOpts.Depth = input.Depth
|
}
|
||||||
shallowOpts.SingleBranch = true
|
|
||||||
shallowOpts.ReferenceName = refName
|
|
||||||
shallowOpts.Tags = git.NoTags
|
|
||||||
|
|
||||||
r, err := git.PlainCloneContext(ctx, input.Dir, false, &shallowOpts)
|
func shallowPinnedClone(ctx context.Context, input NewGitCloneExecutorInput) error {
|
||||||
if err == nil {
|
if _, err := runGit(ctx, "", &input, "init", "--object-format="+ObjectFormat(input.Ref), input.Dir); err != nil {
|
||||||
return r, nil
|
return err
|
||||||
}
|
}
|
||||||
logger.Debugf("Shallow clone of %s as %s failed: %v", input.URL, refName, err)
|
if _, err := runGit(ctx, input.Dir, &input, "remote", "add", "origin", input.URL); err != nil {
|
||||||
if err := removePartialClone(input.Dir); err != nil {
|
return err
|
||||||
return nil, err
|
}
|
||||||
}
|
_, err := runGit(ctx, input.Dir, &input, "fetch", "--depth", strconv.Itoa(input.Depth), "--no-tags", "origin", "+"+input.Ref+":refs/pinned/"+input.Ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetch(ctx context.Context, input NewGitCloneExecutorInput) error {
|
||||||
|
args := []string{"fetch", "--force", "--prune"}
|
||||||
|
if _, err := os.Stat(filepath.Join(input.Dir, ".git", "shallow")); err == nil {
|
||||||
|
args = append(args, "--depth", "1", "--no-tags")
|
||||||
|
}
|
||||||
|
_, err := runGit(ctx, input.Dir, &input, append(args, "origin", "+"+input.Ref+":"+cachedRef(input.Ref))...) // git picks the remote ref, a tag before a branch
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func cachedRef(ref string) string {
|
||||||
|
return fmt.Sprintf("refs/runner/%x", sha256.Sum256([]byte(ref)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveRef(ctx context.Context, dir, ref string) (string, error) {
|
||||||
|
if !isFullObjectID(ref) {
|
||||||
|
for _, name := range []string{cachedRef(ref), "refs/tags/" + ref, "refs/remotes/origin/" + ref} {
|
||||||
|
if sha, err := gitOutput(ctx, dir, "rev-parse", "--verify", name+"^{commit}"); err == nil {
|
||||||
|
return sha, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.Debugf("Falling back to a full clone of %s for ref %q", input.URL, input.Ref)
|
|
||||||
}
|
}
|
||||||
|
sha, err := gitOutput(ctx, dir, "rev-parse", "--verify", ref+"^{commit}")
|
||||||
|
if err == nil && hexRefRegex.MatchString(ref) && len(sha) != len(ref) {
|
||||||
|
return "", &Error{err: ErrShortRef, commit: sha}
|
||||||
|
}
|
||||||
|
return sha, err
|
||||||
|
}
|
||||||
|
|
||||||
return git.PlainCloneContext(ctx, input.Dir, false, &opts)
|
func isFullObjectID(ref string) bool {
|
||||||
|
return ObjectFormat(ref) != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func isFullHash(ref, resolved string) bool {
|
||||||
|
return isFullObjectID(ref) && len(ref) == len(resolved)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectFormat returns the object format of a full commit hash, empty for anything else.
|
||||||
|
func ObjectFormat(sha string) string {
|
||||||
|
switch {
|
||||||
|
case !hexRefRegex.MatchString(sha):
|
||||||
|
return ""
|
||||||
|
case len(sha) == 40:
|
||||||
|
return "sha1"
|
||||||
|
case len(sha) == 64:
|
||||||
|
return "sha256"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func removePartialClone(dir string) error {
|
func removePartialClone(dir string) error {
|
||||||
@@ -490,71 +359,100 @@ func removePartialClone(dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchPinnedSHA(ctx context.Context, input NewGitCloneExecutorInput, opts git.CloneOptions) (*git.Repository, error) {
|
func gitOutput(ctx context.Context, dir string, args ...string) (string, error) {
|
||||||
r, err := git.PlainInit(input.Dir, false)
|
output, err := runGit(ctx, dir, nil, args...)
|
||||||
|
return strings.TrimSpace(string(output)), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoteConfig(token string, insecureSkipTLS bool) []string {
|
||||||
|
var config []string
|
||||||
|
if insecureSkipTLS {
|
||||||
|
config = append(config, "http.sslVerify=false")
|
||||||
|
}
|
||||||
|
if token != "" {
|
||||||
|
config = append(config, "http.extraHeader=", "http.extraHeader=Authorization: Basic "+base64.StdEncoding.EncodeToString([]byte("token:"+token)))
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigEnv applies `-c` style entries through the environment, keeping secrets out of argv.
|
||||||
|
func ConfigEnv(config ...string) map[string]string {
|
||||||
|
env := map[string]string{"GIT_CONFIG_COUNT": strconv.Itoa(len(config))}
|
||||||
|
for i, entry := range config {
|
||||||
|
key, value, _ := strings.Cut(entry, "=")
|
||||||
|
env["GIT_CONFIG_KEY_"+strconv.Itoa(i)] = key
|
||||||
|
env["GIT_CONFIG_VALUE_"+strconv.Itoa(i)] = value
|
||||||
|
}
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListFiles(ctx context.Context, dir string) ([]string, error) {
|
||||||
|
listed, err := runGit(ctx, dir, nil, "ls-files", "-z", "-t", "--stage", "--others", "--exclude-standard")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if _, err := r.CreateRemote(&config.RemoteConfig{Name: "origin", URLs: []string{input.URL}}); err != nil {
|
var files, repos []string
|
||||||
return nil, err
|
for entry := range strings.SplitSeq(string(listed), "\x00") {
|
||||||
|
tag, file, ok := strings.Cut(entry, " ")
|
||||||
|
if tag != "?" {
|
||||||
|
var meta string
|
||||||
|
meta, file, ok = strings.Cut(file, "\t")
|
||||||
|
if strings.HasPrefix(meta, "160000 ") {
|
||||||
|
file += "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if repo, isRepo := strings.CutSuffix(file, "/"); isRepo {
|
||||||
|
repos = append(repos, repo)
|
||||||
|
} else if ok {
|
||||||
|
files = append(files, file)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := r.FetchContext(ctx, &git.FetchOptions{
|
slices.Sort(repos)
|
||||||
RefSpecs: []config.RefSpec{pinnedRefSpec(input.Ref)},
|
for _, repo := range slices.Compact(repos) {
|
||||||
Depth: input.Depth,
|
files = append(files, repo)
|
||||||
Tags: git.NoTags,
|
if _, err := os.Lstat(filepath.Join(dir, repo, ".git")); err != nil {
|
||||||
Auth: opts.Auth,
|
continue // git lists an uninitialized submodule as "./" from inside it
|
||||||
Progress: opts.Progress,
|
}
|
||||||
InsecureSkipTLS: opts.InsecureSkipTLS,
|
nested, err := ListFiles(ctx, filepath.Join(dir, repo))
|
||||||
}); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, file := range nested {
|
||||||
|
files = append(files, repo+"/"+file)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return r, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// pinnedRefSpec keeps a hash-shaped name out of refs/heads and refs/tags.
|
func runGit(ctx context.Context, dir string, input *NewGitCloneExecutorInput, args ...string) ([]byte, error) {
|
||||||
func pinnedRefSpec(sha string) config.RefSpec {
|
config := []string{"core.autocrlf=false", "core.eol=lf"} // keep action files LF
|
||||||
return config.RefSpec(fmt.Sprintf("+%s:refs/pinned/%s", sha, sha))
|
if input != nil {
|
||||||
}
|
config = append(config, RemoteConfig(input.Token, input.InsecureSkipTLS)...)
|
||||||
|
if input.ClientCertFile != "" {
|
||||||
// refRevision picks the revision to check out and reports whether it resolves locally.
|
config = append(config, "http.sslCert="+input.ClientCertFile, "http.sslKey="+input.ClientKeyFile)
|
||||||
func refRevision(r *git.Repository, ref string) (plumbing.Revision, bool) {
|
}
|
||||||
if plumbing.IsHash(ref) {
|
|
||||||
// git ignores a ref named as 40 hex digits, so a full hash always denotes the commit itself.
|
|
||||||
_, err := r.CommitObject(plumbing.NewHash(ref))
|
|
||||||
return plumbing.Revision(ref), err == nil
|
|
||||||
}
|
}
|
||||||
if _, err := r.Tag(ref); err == nil {
|
if dir != "" {
|
||||||
return plumbing.Revision(path.Join("refs", "tags", ref)), true
|
args = append([]string{"-C", dir}, args...)
|
||||||
}
|
}
|
||||||
remoteRef := plumbing.ReferenceName(path.Join("refs", "remotes", "origin", ref))
|
cmd := exec.CommandContext(ctx, "git", args...)
|
||||||
if _, err := r.Reference(remoteRef, false); err == nil {
|
cmd.Env = slices.DeleteFunc(os.Environ(), func(entry string) bool {
|
||||||
return plumbing.Revision(remoteRef), true
|
name, _, _ := strings.Cut(entry, "=")
|
||||||
|
return slices.Contains(LocalEnvVars, name)
|
||||||
|
})
|
||||||
|
for key, value := range ConfigEnv(config...) {
|
||||||
|
cmd.Env = append(cmd.Env, key+"="+value)
|
||||||
}
|
}
|
||||||
rev := plumbing.Revision(ref)
|
cmd.Env = append(cmd.Env, "GIT_TERMINAL_PROMPT=0")
|
||||||
_, err := r.ResolveRevision(rev)
|
if runtime.GOOS != "windows" {
|
||||||
return rev, err == nil
|
cmd.Cancel = func() error { return cmd.Process.Signal(os.Interrupt) } // lets git remove its lock files
|
||||||
}
|
|
||||||
|
|
||||||
// isShallow reports whether the local repository was cloned with a limited depth.
|
|
||||||
func isShallow(r *git.Repository) bool {
|
|
||||||
shallows, err := r.Storer.Shallow()
|
|
||||||
return err == nil && len(shallows) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// shallowFetchRefSpec limits a shallow update to the requested ref, falling back to the broad refspec when it is not present locally.
|
|
||||||
func shallowFetchRefSpec(r *git.Repository, ref string) (config.RefSpec, bool) {
|
|
||||||
tagRef := plumbing.NewTagReferenceName(ref)
|
|
||||||
if _, err := r.Reference(tagRef, false); err == nil {
|
|
||||||
return config.RefSpec(fmt.Sprintf("+%s:%s", tagRef, tagRef)), true
|
|
||||||
}
|
}
|
||||||
remoteRef := plumbing.NewRemoteReferenceName("origin", ref)
|
cmd.WaitDelay = time.Second
|
||||||
if _, err := r.Reference(remoteRef, false); err == nil {
|
var stderr strings.Builder
|
||||||
branchRef := plumbing.NewBranchReferenceName(ref)
|
cmd.Stderr = &stderr
|
||||||
return config.RefSpec(fmt.Sprintf("+%s:%s", branchRef, remoteRef)), true
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return output, fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String()))
|
||||||
}
|
}
|
||||||
if plumbing.IsHash(ref) {
|
return output, nil
|
||||||
// The broad refspec carries only advertised tips, never a pinned commit.
|
|
||||||
return pinnedRefSpec(ref), true
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
+194
-204
@@ -6,9 +6,8 @@ package git
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
@@ -17,48 +16,48 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
"gitea.com/gitea/runner/internal/pkg/lock"
|
||||||
|
|
||||||
gogit "github.com/go-git/go-git/v5"
|
|
||||||
gogitconfig "github.com/go-git/go-git/v5/config"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
||||||
gogitclient "github.com/go-git/go-git/v5/plumbing/transport/client"
|
|
||||||
gogitfile "github.com/go-git/go-git/v5/plumbing/transport/file"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
logrustest "github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindGitSlug(t *testing.T) {
|
func TestFindGitSlug(t *testing.T) {
|
||||||
assert := assert.New(t)
|
for _, testcase := range []struct {
|
||||||
|
remoteURL string
|
||||||
slugTests := []struct {
|
slug string
|
||||||
url string // input
|
|
||||||
provider string // expected result
|
|
||||||
slug string // expected result
|
|
||||||
}{
|
}{
|
||||||
{"https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo-name", "CodeCommit", "my-repo-name"},
|
{"https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo-name", "my-repo-name"},
|
||||||
{"ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/my-repo", "CodeCommit", "my-repo"},
|
{"ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/my-repo", "my-repo"},
|
||||||
{"[email protected]:nektos/act.git", "GitHub", "nektos/act"},
|
{"[email protected]:nektos/act.git", "nektos/act"},
|
||||||
{"[email protected]:nektos/act", "GitHub", "nektos/act"},
|
{"[email protected]:nektos/act", "nektos/act"},
|
||||||
{"https://github.com/nektos/act.git", "GitHub", "nektos/act"},
|
{"https://github.com/nektos/act.git", "nektos/act"},
|
||||||
{"http://github.com/nektos/act.git", "GitHub", "nektos/act"},
|
{"http://github.com/nektos/act.git", "nektos/act"},
|
||||||
{"https://github.com/nektos/act", "GitHub", "nektos/act"},
|
{"https://github.com/nektos/act", "nektos/act"},
|
||||||
{"http://github.com/nektos/act", "GitHub", "nektos/act"},
|
{"http://github.com/nektos/act", "nektos/act"},
|
||||||
{"git+ssh://[email protected]/owner/repo.git", "GitHub", "owner/repo"},
|
{"git+ssh://[email protected]/owner/repo.git", "owner/repo"},
|
||||||
{"http://myotherrepo.com/act.git", "", "http://myotherrepo.com/act.git"},
|
{"https://example.com:3000/gitea/owner/repo.git/", "owner/repo"},
|
||||||
}
|
{"ssh://[email protected]:2222/gitea/owner/repo.git", "owner/repo"},
|
||||||
|
{"example.com:owner/repo.git", "owner/repo"},
|
||||||
for _, tt := range slugTests {
|
{"http://myotherrepo.com/act.git", ""},
|
||||||
provider, slug := findGitSlug(tt.url, "github.com")
|
{"https://user:[email protected]/act.git", ""},
|
||||||
assert.Equal(tt.provider, provider)
|
{"/tmp/owner/repo.git", ""},
|
||||||
assert.Equal(tt.slug, slug)
|
{"./owner/repo:branch", ""},
|
||||||
|
} {
|
||||||
|
t.Run(testcase.remoteURL, func(t *testing.T) {
|
||||||
|
slug, err := findGitSlug(testcase.remoteURL)
|
||||||
|
assert.Equal(t, testcase.slug == "", err != nil)
|
||||||
|
assert.NotContains(t, fmt.Sprint(err), "secret")
|
||||||
|
assert.Equal(t, testcase.slug, slug)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,23 +89,37 @@ func cleanGitHooks(dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFindGithubRepoUsesOrigin(t *testing.T) {
|
func TestFindGitMetadataOfSHA256RepositoryUsesOrigin(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
basedir := t.TempDir()
|
basedir := t.TempDir()
|
||||||
const remoteURL = "https://github.com/owner/repo.git"
|
const remoteURL = "https://github.com/owner/repo.git"
|
||||||
require.NoError(t, gitCmd("init", basedir))
|
require.NoError(t, gitCmd("init", "--object-format=sha256", "--initial-branch=main", basedir))
|
||||||
require.NoError(t, cleanGitHooks(basedir))
|
require.NoError(t, cleanGitHooks(basedir))
|
||||||
|
require.NoError(t, gitCmd("-C", basedir, "commit", "--allow-empty", "-m", "init"))
|
||||||
require.NoError(t, gitCmd("-C", basedir, "remote", "add", "origin", remoteURL))
|
require.NoError(t, gitCmd("-C", basedir, "remote", "add", "origin", remoteURL))
|
||||||
|
require.NoError(t, gitCmd("-C", basedir, "config", "--add", "remote.origin.url", "https://github.com/other/repo.git"))
|
||||||
|
|
||||||
url, err := findGitRemoteURL(context.Background(), basedir, "origin")
|
shortSHA, sha, err := FindGitRevision(t.Context(), basedir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, gitRevParse(t, basedir, "HEAD"), sha)
|
||||||
|
require.Len(t, sha, 64)
|
||||||
|
require.Equal(t, sha[:7], shortSHA)
|
||||||
|
|
||||||
|
ref, err := FindGitRef(t.Context(), basedir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "refs/heads/main", ref)
|
||||||
|
|
||||||
|
url, err := findGitRemoteURL(t.Context(), basedir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, remoteURL, url)
|
require.Equal(t, remoteURL, url)
|
||||||
|
|
||||||
slug, err := FindGithubRepo(context.Background(), basedir, "github.com")
|
slug, err := FindGithubRepo(t.Context(), basedir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "owner/repo", slug)
|
require.Equal(t, "owner/repo", slug)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitFindRef(t *testing.T) {
|
func TestGitFindRef(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
basedir := t.TempDir()
|
basedir := t.TempDir()
|
||||||
|
|
||||||
for name, tt := range map[string]struct {
|
for name, tt := range map[string]struct {
|
||||||
@@ -177,7 +190,7 @@ func TestGitFindRef(t *testing.T) {
|
|||||||
require.NoError(t, gitCmd("-C", dir, "init", "--initial-branch=master"))
|
require.NoError(t, gitCmd("-C", dir, "init", "--initial-branch=master"))
|
||||||
require.NoError(t, cleanGitHooks(dir))
|
require.NoError(t, cleanGitHooks(dir))
|
||||||
tt.Prepare(t, dir)
|
tt.Prepare(t, dir)
|
||||||
ref, err := FindGitRef(context.Background(), dir)
|
ref, err := FindGitRef(t.Context(), dir)
|
||||||
tt.Assert(t, ref, err)
|
tt.Assert(t, ref, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -193,7 +206,9 @@ func TestGitCloneExecutor(t *testing.T) {
|
|||||||
workDir := t.TempDir()
|
workDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
require.NoError(t, os.WriteFile(filepath.Join(workDir, "action.sh"), []byte("#!/bin/sh\necho hi\n"), 0o755))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "add", "action.sh"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "-m", "initial"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "tag", "v2"))
|
require.NoError(t, gitCmd("-C", workDir, "tag", "v2"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v2"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v2"))
|
||||||
@@ -207,6 +222,13 @@ func TestGitCloneExecutor(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fullSha := strings.TrimSpace(string(out))
|
fullSha := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main:refs/heads/"+fullSha[:4]))
|
||||||
|
|
||||||
|
hostConfig := filepath.Join(t.TempDir(), "gitconfig")
|
||||||
|
require.NoError(t, os.WriteFile(hostConfig, []byte("[core]\n\tautocrlf = true\n[url \""+remoteDir+"\"]\n\tinsteadOf = https://example.invalid/action\n"), 0o644))
|
||||||
|
t.Setenv("GIT_CONFIG_GLOBAL", hostConfig)
|
||||||
|
t.Setenv("GIT_DIR", t.TempDir())
|
||||||
|
|
||||||
for name, tt := range map[string]struct {
|
for name, tt := range map[string]struct {
|
||||||
Err error
|
Err error
|
||||||
Ref string
|
Ref string
|
||||||
@@ -227,26 +249,42 @@ func TestGitCloneExecutor(t *testing.T) {
|
|||||||
Err: &Error{ErrShortRef, fullSha},
|
Err: &Error{ErrShortRef, fullSha},
|
||||||
Ref: fullSha[:7],
|
Ref: fullSha[:7],
|
||||||
},
|
},
|
||||||
|
"HEAD": {
|
||||||
|
Ref: "HEAD",
|
||||||
|
},
|
||||||
|
"hex branch named like its own commit": {
|
||||||
|
Ref: fullSha[:4],
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: remoteDir,
|
URL: "https://example.invalid/action",
|
||||||
Ref: tt.Ref,
|
Ref: tt.Ref,
|
||||||
Dir: t.TempDir(),
|
Dir: dir,
|
||||||
})
|
})
|
||||||
|
|
||||||
err := clone(context.Background())
|
err := clone(t.Context())
|
||||||
if tt.Err != nil {
|
if tt.Err != nil {
|
||||||
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.Err, err)
|
assert.Equal(t, tt.Err, err)
|
||||||
} else {
|
assert.Equal(t, tt.Err, clone(t.Context()), "a retry on the cache keeps the error")
|
||||||
assert.Empty(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
return
|
||||||
}
|
}
|
||||||
|
assert.Empty(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
marker := filepath.Join(dir, "marker")
|
||||||
|
require.NoError(t, os.WriteFile(marker, nil, 0o644))
|
||||||
|
require.NoError(t, clone(t.Context()))
|
||||||
|
assert.FileExists(t, marker, "an insteadOf rewrite must not evict the cache")
|
||||||
|
script, err := os.ReadFile(filepath.Join(dir, "action.sh"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "#!/bin/sh\necho hi\n", string(script), "host core.autocrlf must not convert action files")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
|
func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
createRemote := func(message string) string {
|
createRemote := func(message string) string {
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
@@ -270,16 +308,27 @@ func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
|
|||||||
Dir: cacheDir,
|
Dir: cacheDir,
|
||||||
})(t.Context()))
|
})(t.Context()))
|
||||||
|
|
||||||
|
nested := filepath.Join(cacheDir, "nested")
|
||||||
|
require.NoError(t, os.Mkdir(nested, 0o755))
|
||||||
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{URL: oldRemoteDir, Ref: "main", Dir: nested})(t.Context()))
|
||||||
|
assert.DirExists(t, filepath.Join(nested, ".git"), "an empty cache dir inside a clone of the same URL gets its own repository")
|
||||||
|
|
||||||
markerPath := filepath.Join(cacheDir, "stale-marker")
|
markerPath := filepath.Join(cacheDir, "stale-marker")
|
||||||
require.NoError(t, os.WriteFile(markerPath, []byte("stale"), 0o644))
|
require.NoError(t, os.WriteFile(markerPath, []byte("stale"), 0o644))
|
||||||
|
|
||||||
|
cancelled, cancel := context.WithCancel(t.Context())
|
||||||
|
cancel()
|
||||||
|
_, err := CloneIfRequired(cancelled, NewGitCloneExecutorInput{URL: oldRemoteDir, Ref: "main", Dir: cacheDir}, logging.New(logtest.New()))
|
||||||
|
require.ErrorIs(t, err, context.Canceled)
|
||||||
|
require.FileExists(t, markerPath)
|
||||||
|
|
||||||
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: newRemoteDir,
|
URL: newRemoteDir,
|
||||||
Ref: "main",
|
Ref: "main",
|
||||||
Dir: cacheDir,
|
Dir: cacheDir,
|
||||||
})(t.Context()))
|
})(t.Context()))
|
||||||
|
|
||||||
originURL, err := findGitRemoteURL(t.Context(), cacheDir, "origin")
|
originURL, err := findGitRemoteURL(t.Context(), cacheDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, newRemoteDir, originURL)
|
assert.Equal(t, newRemoteDir, originURL)
|
||||||
|
|
||||||
@@ -291,61 +340,36 @@ func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
|
|||||||
require.True(t, os.IsNotExist(err), "stale cached directory should be removed before recloning")
|
require.True(t, os.IsNotExist(err), "stale cached directory should be removed before recloning")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorNonFastForwardRef(t *testing.T) {
|
func TestGitCloneExecutorFollowsRemoteRefChanges(t *testing.T) {
|
||||||
// Simulate the scenario where a remote ref (e.g. a GitHub PR head ref) changes
|
t.Parallel()
|
||||||
// non-fast-forward between two fetches. Before the fix, the fetch used Force=false,
|
|
||||||
// causing go-git to return ErrForceNeeded and short-circuit the checkout.
|
|
||||||
|
|
||||||
// Create a bare "remote" repo with an initial commit on main and a feature branch.
|
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
|
|
||||||
// We need a working clone to push commits from.
|
|
||||||
workDir := t.TempDir()
|
workDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
require.NoError(t, gitCmd("clone", remoteDir, workDir))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "feature-1"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
|
||||||
|
|
||||||
// Create a feature branch (simulates refs/pull/N/head).
|
dir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "feature"))
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{URL: remoteDir, Ref: "main", Dir: dir})
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "feature-1"))
|
require.NoError(t, clone(t.Context()))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "feature"))
|
|
||||||
|
|
||||||
// First clone via the executor — should succeed and cache the repo.
|
require.NoError(t, gitCmd("-C", workDir, "reset", "--hard", "HEAD~1"))
|
||||||
cloneDir := t.TempDir()
|
|
||||||
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
|
||||||
URL: remoteDir,
|
|
||||||
Ref: "main",
|
|
||||||
Dir: cloneDir,
|
|
||||||
})
|
|
||||||
require.NoError(t, clone(context.Background()))
|
|
||||||
|
|
||||||
// Now force-push the feature branch to a non-fast-forward commit (simulates
|
|
||||||
// a PR rebase). This makes refs/heads/feature non-fast-forward.
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "main"))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "branch", "-D", "feature"))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "feature"))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "feature-rewritten"))
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "feature-rewritten"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "--force", "origin", "feature"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "--force", "origin", "main"))
|
||||||
|
require.NoError(t, clone(t.Context()), "a non-fast-forward ref must update")
|
||||||
|
assert.Equal(t, "feature-rewritten", gitHeadSubject(t, dir))
|
||||||
|
|
||||||
// Also advance main so we can verify the clone picks up the new commit.
|
tagClone := NewGitCloneExecutor(NewGitCloneExecutorInput{URL: remoteDir, Ref: "v9", Dir: t.TempDir()})
|
||||||
require.NoError(t, gitCmd("-C", workDir, "checkout", "main"))
|
require.Error(t, tagClone(t.Context()))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "second"))
|
require.NoError(t, gitCmd("-C", workDir, "tag", "v9"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v9"))
|
||||||
|
require.NoError(t, tagClone(t.Context()), "a tag published after a failed lookup must be fetched")
|
||||||
// Second clone to the same directory — before the fix this returned ErrForceNeeded
|
|
||||||
// and left the working tree at the old commit.
|
|
||||||
err := clone(context.Background())
|
|
||||||
require.NoError(t, err, "fetch with non-fast-forward refs must not fail when Force=true")
|
|
||||||
|
|
||||||
// Verify the working tree was actually updated to the latest main commit.
|
|
||||||
out, err := exec.Command("git", "-C", cloneDir, "log", "--oneline", "-1", "--format=%s").Output()
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "second", strings.TrimSpace(string(out)), "working tree should be at the latest commit")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
// Build a local "remote" with a single commit on main.
|
// Build a local "remote" with a single commit on main.
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
@@ -361,7 +385,7 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
|||||||
URL: remoteDir,
|
URL: remoteDir,
|
||||||
Ref: "main",
|
Ref: "main",
|
||||||
Dir: cacheDir,
|
Dir: cacheDir,
|
||||||
})(context.Background()))
|
})(t.Context()))
|
||||||
|
|
||||||
t.Run("cached branch resolves without fetching", func(t *testing.T) {
|
t.Run("cached branch resolves without fetching", func(t *testing.T) {
|
||||||
// Offline reuse of a cached branch must succeed even though ResolveRevision(input.Ref)
|
// Offline reuse of a cached branch must succeed even though ResolveRevision(input.Ref)
|
||||||
@@ -371,7 +395,7 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
|||||||
Ref: "main",
|
Ref: "main",
|
||||||
Dir: cacheDir,
|
Dir: cacheDir,
|
||||||
OfflineMode: true,
|
OfflineMode: true,
|
||||||
})(context.Background())
|
})(t.Context())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
out, err := exec.Command("git", "-C", cacheDir, "log", "--oneline", "-1", "--format=%s").Output()
|
out, err := exec.Command("git", "-C", cacheDir, "log", "--oneline", "-1", "--format=%s").Output()
|
||||||
@@ -386,12 +410,13 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
|
|||||||
Ref: "never-fetched",
|
Ref: "never-fetched",
|
||||||
Dir: cacheDir,
|
Dir: cacheDir,
|
||||||
OfflineMode: true,
|
OfflineMode: true,
|
||||||
})(context.Background())
|
})(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
workDir := t.TempDir()
|
workDir := t.TempDir()
|
||||||
@@ -403,9 +428,9 @@ func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
|||||||
// Quiet callers report the download themselves, so the clone line must not reach the job log.
|
// Quiet callers report the download themselves, so the clone line must not reach the job log.
|
||||||
for name, quiet := range map[string]bool{"quiet": true, "not quiet": false} {
|
for name, quiet := range map[string]bool{"quiet": true, "not quiet": false} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
logger, hook := logrustest.NewNullLogger()
|
hook := logtest.New()
|
||||||
logger.SetLevel(log.InfoLevel)
|
logger := logging.New(logging.NewLevelHandler(hook, logging.LevelInfo))
|
||||||
ctx := common.WithLogger(context.Background(), logger.WithField("job", "j1"))
|
ctx := common.WithLogger(t.Context(), logger.With("job", "j1"))
|
||||||
|
|
||||||
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: remoteDir,
|
URL: remoteDir,
|
||||||
@@ -415,7 +440,7 @@ func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
|||||||
})(ctx))
|
})(ctx))
|
||||||
|
|
||||||
var cloneLines int
|
var cloneLines int
|
||||||
for _, entry := range hook.AllEntries() {
|
for _, entry := range hook.AllRecords() {
|
||||||
if strings.HasPrefix(entry.Message, "git clone ") {
|
if strings.HasPrefix(entry.Message, "git clone ") {
|
||||||
cloneLines++
|
cloneLines++
|
||||||
}
|
}
|
||||||
@@ -430,6 +455,7 @@ func TestGitCloneExecutorQuietDemotesCloneLine(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorShallow(t *testing.T) {
|
func TestGitCloneExecutorShallow(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
// Build a local "remote" with several commits on main plus a tag, so a full clone would pull noticeably more history than a shallow one.
|
// Build a local "remote" with several commits on main plus a tag, so a full clone would pull noticeably more history than a shallow one.
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
@@ -473,12 +499,11 @@ func TestGitCloneExecutorShallow(t *testing.T) {
|
|||||||
return remote
|
return remote
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("commit hash falls back to a full clone when the remote refuses unadvertised objects", func(t *testing.T) {
|
t.Run("commit hash resolves when the remote accepts a direct shallow fetch", func(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: unadvertisedRemote(t, false), Ref: unadvertisedSHA, Dir: dir, Depth: 1,
|
URL: unadvertisedRemote(t, false), Ref: unadvertisedSHA, Dir: dir, Depth: 1,
|
||||||
})(t.Context()))
|
})(t.Context()))
|
||||||
assert.NoFileExists(t, shallowMarker(dir))
|
|
||||||
assert.Equal(t, unadvertisedSHA, gitRevParse(t, dir, "HEAD"))
|
assert.Equal(t, unadvertisedSHA, gitRevParse(t, dir, "HEAD"))
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -505,76 +530,46 @@ func TestGitCloneExecutorShallow(t *testing.T) {
|
|||||||
assert.Equal(t, olderSHA, gitRevParse(t, dir, "HEAD"))
|
assert.Equal(t, olderSHA, gitRevParse(t, dir, "HEAD"))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("moving branch updates while staying shallow", func(t *testing.T) {
|
t.Run("moving hexadecimal-named branch updates while staying shallow", func(t *testing.T) {
|
||||||
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main:deadbeef"))
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1,
|
URL: remoteDir, Ref: "deadbeef", Dir: dir, Depth: 1,
|
||||||
})(t.Context()))
|
})
|
||||||
|
require.NoError(t, clone(t.Context()))
|
||||||
require.Equal(t, "c3", gitHeadSubject(t, dir))
|
require.Equal(t, "c3", gitHeadSubject(t, dir))
|
||||||
|
|
||||||
// Advance main on the remote, then reuse the existing shallow clone.
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "c4"))
|
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "c4"))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main"))
|
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main:deadbeef"))
|
||||||
|
|
||||||
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
|
require.NoError(t, clone(t.Context()))
|
||||||
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1,
|
|
||||||
})(t.Context()))
|
|
||||||
assert.Equal(t, "c4", gitHeadSubject(t, dir), "reused shallow clone should update to the new tip")
|
assert.Equal(t, "c4", gitHeadSubject(t, dir), "reused shallow clone should update to the new tip")
|
||||||
assert.FileExists(t, shallowMarker(dir), "repo should remain shallow after update")
|
assert.FileExists(t, shallowMarker(dir), "repo should remain shallow after update")
|
||||||
assert.Equal(t, 1, gitRevCount(t, dir))
|
assert.Equal(t, 1, gitRevCount(t, dir))
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func TestGitCloneExecutorTransportSessions(t *testing.T) {
|
t.Run("sha256 full hash is cloned shallowly, reused without the remote, and its 40-character prefix is short", func(t *testing.T) {
|
||||||
workDir := t.TempDir()
|
remote := filepath.Join(t.TempDir(), "remote.git")
|
||||||
require.NoError(t, gitCmd("init", "--initial-branch=main", workDir))
|
require.NoError(t, gitCmd("init", "--bare", "--object-format=sha256", "--initial-branch=main", remote))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "c1"))
|
work := t.TempDir()
|
||||||
require.NoError(t, gitCmd("-C", workDir, "tag", "v1"))
|
require.NoError(t, gitCmd("init", "--object-format=sha256", "--initial-branch=main", work))
|
||||||
require.NoError(t, gitCmd("-C", workDir, "tag", "-a", "v2", "-m", "v2"))
|
require.NoError(t, gitCmd("-C", work, "remote", "add", "origin", remote))
|
||||||
|
require.NoError(t, gitCmd("-C", work, "commit", "--allow-empty", "-m", "pinned"))
|
||||||
for name, tt := range map[string]struct {
|
require.NoError(t, gitCmd("-C", work, "push", "origin", "main"))
|
||||||
Ref string
|
sha := gitRevParse(t, work, "HEAD")
|
||||||
Depth int
|
dir := t.TempDir()
|
||||||
}{
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{URL: remote, Ref: sha, Dir: dir, Depth: 1})
|
||||||
"shallow branch": {"main", 1},
|
require.NoError(t, clone(t.Context()))
|
||||||
"full clone branch": {"main", 0},
|
assert.FileExists(t, shallowMarker(dir))
|
||||||
"full lightweight tag": {"v1", 0},
|
require.ErrorIs(t, NewGitCloneExecutor(NewGitCloneExecutorInput{URL: remote, Ref: sha[:40], Dir: dir, Depth: 1, OfflineMode: true})(t.Context()), ErrShortRef)
|
||||||
"full annotated tag": {"v2", 0},
|
require.NoError(t, os.RemoveAll(remote))
|
||||||
} {
|
require.NoError(t, clone(t.Context()))
|
||||||
t.Run(name, func(t *testing.T) {
|
assert.Equal(t, sha, gitRevParse(t, dir, "HEAD"))
|
||||||
counter := installCountingTransport(t)
|
})
|
||||||
dir := t.TempDir()
|
|
||||||
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
|
||||||
URL: workDir, Ref: tt.Ref, Dir: dir, Depth: tt.Depth,
|
|
||||||
})
|
|
||||||
require.NoError(t, clone(t.Context()))
|
|
||||||
assert.Equal(t, int64(1), counter.sessions.Swap(0), "cold clone")
|
|
||||||
assert.Equal(t, gitRevParse(t, workDir, tt.Ref+"^{commit}"), gitRevParse(t, dir, "HEAD"))
|
|
||||||
|
|
||||||
require.NoError(t, clone(t.Context()))
|
|
||||||
assert.Equal(t, int64(1), counter.sessions.Swap(0), "unchanged warm cache")
|
|
||||||
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(workDir, "action.yml"), []byte(name), 0o644))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "add", "action.yml"))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "commit", "-m", name))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "tag", "--force", "v1"))
|
|
||||||
require.NoError(t, gitCmd("-C", workDir, "tag", "--force", "-a", "v2", "-m", "v2"))
|
|
||||||
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "action.yml"), []byte("staged"), 0o644))
|
|
||||||
require.NoError(t, gitCmd("-C", dir, "add", "action.yml"))
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "action.yml"), []byte("unstaged"), 0o644))
|
|
||||||
|
|
||||||
require.NoError(t, clone(t.Context()))
|
|
||||||
assert.Equal(t, int64(1), counter.sessions.Load(), "updated warm cache")
|
|
||||||
assert.Equal(t, gitRevParse(t, workDir, "HEAD"), gitRevParse(t, dir, "HEAD"))
|
|
||||||
status, err := exec.Command("git", "-C", dir, "status", "--porcelain").Output()
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Empty(t, string(status))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) {
|
func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
remoteDir := t.TempDir()
|
remoteDir := t.TempDir()
|
||||||
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
|
||||||
workDir := t.TempDir()
|
workDir := t.TempDir()
|
||||||
@@ -598,25 +593,6 @@ func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type countingTransport struct {
|
|
||||||
transport.Transport
|
|
||||||
sessions atomic.Int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *countingTransport) NewUploadPackSession(ep *transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) {
|
|
||||||
c.sessions.Add(1)
|
|
||||||
return c.Transport.NewUploadPackSession(ep, auth)
|
|
||||||
}
|
|
||||||
|
|
||||||
func installCountingTransport(t *testing.T) *countingTransport {
|
|
||||||
t.Helper()
|
|
||||||
counter := &countingTransport{Transport: gogitfile.DefaultClient}
|
|
||||||
gogitclient.InstallProtocol("file", counter)
|
|
||||||
t.Cleanup(func() { gogitclient.InstallProtocol("file", gogitfile.DefaultClient) })
|
|
||||||
|
|
||||||
return counter
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitRevParse(t *testing.T, dir, rev string) string {
|
func gitRevParse(t *testing.T, dir, rev string) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
out, err := exec.Command("git", "-C", dir, "rev-parse", rev).Output()
|
out, err := exec.Command("git", "-C", dir, "rev-parse", rev).Output()
|
||||||
@@ -667,14 +643,42 @@ func gitCmd(args ...string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAcquireCloneLock(t *testing.T) {
|
func TestAcquireCloneLock(t *testing.T) {
|
||||||
|
t.Run("blocks on a lock held by another process despite a trailing separator", func(t *testing.T) {
|
||||||
|
dir := filepath.Join(t.TempDir(), "cache")
|
||||||
|
release, err := lock.TryLock(dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() { require.NoError(t, release()) }()
|
||||||
|
synctest.Test(t, func(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_, err := AcquireCloneLock(ctx, dir+string(filepath.Separator))
|
||||||
|
require.ErrorIs(t, err, context.DeadlineExceeded)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("read-only parent falls back to an in-process lock", func(t *testing.T) {
|
||||||
|
parent := t.TempDir()
|
||||||
|
dir := filepath.Join(parent, "cache")
|
||||||
|
require.NoError(t, os.Mkdir(dir, 0o755))
|
||||||
|
require.NoError(t, os.Chmod(parent, 0o555))
|
||||||
|
defer func() { require.NoError(t, os.Chmod(parent, 0o755)) }()
|
||||||
|
unlock, err := AcquireCloneLock(t.Context(), dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
unlock()
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("same directory serializes", func(t *testing.T) {
|
t.Run("same directory serializes", func(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
|
|
||||||
unlock1 := AcquireCloneLock(dir)
|
unlock1, err := AcquireCloneLock(t.Context(), dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
secondAcquired := make(chan struct{})
|
secondAcquired := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
unlock := AcquireCloneLock(dir)
|
unlock, err := AcquireCloneLock(t.Context(), dir)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
close(secondAcquired)
|
close(secondAcquired)
|
||||||
unlock()
|
unlock()
|
||||||
}()
|
}()
|
||||||
@@ -698,12 +702,16 @@ func TestAcquireCloneLock(t *testing.T) {
|
|||||||
dirA := t.TempDir()
|
dirA := t.TempDir()
|
||||||
dirB := t.TempDir()
|
dirB := t.TempDir()
|
||||||
|
|
||||||
unlockA := AcquireCloneLock(dirA)
|
unlockA, err := AcquireCloneLock(t.Context(), dirA)
|
||||||
|
require.NoError(t, err)
|
||||||
defer unlockA()
|
defer unlockA()
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
unlock := AcquireCloneLock(dirB)
|
unlock, err := AcquireCloneLock(t.Context(), dirB)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
unlock()
|
unlock()
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
@@ -717,12 +725,17 @@ func TestAcquireCloneLock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// An unresponsive remote must not pin a job: the refresh has to be interruptible.
|
// An unresponsive remote must not pin a job: the refresh has to be interruptible.
|
||||||
func TestNewGitCloneExecutorFetchHonoursContext(t *testing.T) {
|
func TestNewGitCloneExecutorFetchSendsTokenAndHonoursContext(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
block := make(chan struct{})
|
block := make(chan struct{})
|
||||||
reached := make(chan struct{})
|
reached := make(chan struct{})
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
var authorization string
|
||||||
once.Do(func() { close(reached) })
|
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
||||||
|
once.Do(func() {
|
||||||
|
authorization = request.Header.Get("Authorization")
|
||||||
|
close(reached)
|
||||||
|
})
|
||||||
<-block
|
<-block
|
||||||
}))
|
}))
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
@@ -731,17 +744,15 @@ func TestNewGitCloneExecutorFetchHonoursContext(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
dir := filepath.Join(t.TempDir(), "cached-action")
|
dir := filepath.Join(t.TempDir(), "cached-action")
|
||||||
repo, err := gogit.PlainInit(dir, false)
|
require.NoError(t, gitCmd("init", dir))
|
||||||
require.NoError(t, err)
|
require.NoError(t, gitCmd("-C", dir, "remote", "add", "origin", server.URL))
|
||||||
_, err = repo.CreateRemote(&gogitconfig.RemoteConfig{Name: "origin", URLs: []string{server.URL}})
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(t.Context())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
done <- NewGitCloneExecutor(NewGitCloneExecutorInput{URL: server.URL, Ref: "main", Dir: dir})(ctx)
|
done <- NewGitCloneExecutor(NewGitCloneExecutorInput{URL: server.URL, Ref: "main", Dir: dir, Token: "secret"})(ctx)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -749,6 +760,7 @@ func TestNewGitCloneExecutorFetchHonoursContext(t *testing.T) {
|
|||||||
case <-time.After(10 * time.Second):
|
case <-time.After(10 * time.Second):
|
||||||
t.Fatal("the executor never reached the remote")
|
t.Fatal("the executor never reached the remote")
|
||||||
}
|
}
|
||||||
|
assert.Equal(t, "Basic "+base64.StdEncoding.EncodeToString([]byte("token:secret")), authorization)
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -758,25 +770,3 @@ func TestNewGitCloneExecutorFetchHonoursContext(t *testing.T) {
|
|||||||
t.Fatal("fetch ignored context cancellation")
|
t.Fatal("fetch ignored context cancellation")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewGitCloneExecutorFailsOnSilentHTTP2Connection(t *testing.T) {
|
|
||||||
server := httptest.NewUnstartedServer(nil)
|
|
||||||
server.EnableHTTP2 = true
|
|
||||||
server.Config.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){
|
|
||||||
"h2": func(_ *http.Server, conn *tls.Conn, _ http.Handler) { _, _ = io.Copy(io.Discard, conn) },
|
|
||||||
}
|
|
||||||
server.StartTLS()
|
|
||||||
t.Cleanup(server.Close)
|
|
||||||
|
|
||||||
previous := gogitclient.Protocols["https"]
|
|
||||||
gogitclient.InstallProtocol("https", newHTTPClient(&http.HTTP2Config{SendPingTimeout: 50 * time.Millisecond, PingTimeout: 50 * time.Millisecond}))
|
|
||||||
t.Cleanup(func() { gogitclient.InstallProtocol("https", previous) })
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
err := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
|
||||||
URL: server.URL + "/action", Ref: "main", Dir: filepath.Join(t.TempDir(), "action"), InsecureSkipTLS: true,
|
|
||||||
})(ctx)
|
|
||||||
require.Error(t, err)
|
|
||||||
require.NoError(t, ctx.Err())
|
|
||||||
}
|
|
||||||
+30
-23
@@ -7,46 +7,53 @@ package common
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type loggerContextKey string
|
type loggerContextKey string
|
||||||
|
|
||||||
const loggerContextKeyVal = loggerContextKey("logrus.FieldLogger")
|
const loggerContextKeyVal = loggerContextKey("logging.Logger")
|
||||||
|
|
||||||
// Logger returns the appropriate logger for current context
|
// Logger returns the appropriate logger for current context
|
||||||
func Logger(ctx context.Context) logrus.FieldLogger {
|
func Logger(ctx context.Context) *logging.Logger {
|
||||||
val := ctx.Value(loggerContextKeyVal)
|
if logger, ok := ctx.Value(loggerContextKeyVal).(*logging.Logger); ok {
|
||||||
if val != nil {
|
return logger
|
||||||
if logger, ok := val.(logrus.FieldLogger); ok {
|
|
||||||
return logger
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return logrus.StandardLogger()
|
return logging.Default()
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithLogger adds a value to the context for the logger
|
// WithLogger adds a value to the context for the logger
|
||||||
func WithLogger(ctx context.Context, logger logrus.FieldLogger) context.Context {
|
func WithLogger(ctx context.Context, logger *logging.Logger) context.Context {
|
||||||
return context.WithValue(ctx, loggerContextKeyVal, logger)
|
return context.WithValue(ctx, loggerContextKeyVal, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
type loggerHookKey string
|
type loggerHandlerKey string
|
||||||
|
|
||||||
const loggerHookKeyVal = loggerHookKey("logrus.Hook")
|
const loggerHandlerKeyVal = loggerHandlerKey("logging.Handler")
|
||||||
|
|
||||||
// LoggerHook returns the appropriate logger hook for current context
|
// LoggerHandler returns the extra handler the job logger sends its records to, or nil.
|
||||||
// the hook affects job logger, not global logger
|
// It affects the job logger, not the global logger.
|
||||||
func LoggerHook(ctx context.Context) logrus.Hook {
|
func LoggerHandler(ctx context.Context) logging.Handler {
|
||||||
val := ctx.Value(loggerHookKeyVal)
|
if h, ok := ctx.Value(loggerHandlerKeyVal).(logging.Handler); ok {
|
||||||
if val != nil {
|
return h
|
||||||
if hook, ok := val.(logrus.Hook); ok {
|
|
||||||
return hook
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithLoggerHook adds a value to the context for the logger hook
|
// WithLoggerHandler adds the extra handler for the job logger to the context
|
||||||
func WithLoggerHook(ctx context.Context, hook logrus.Hook) context.Context {
|
func WithLoggerHandler(ctx context.Context, h logging.Handler) context.Context {
|
||||||
return context.WithValue(ctx, loggerHookKeyVal, hook)
|
return context.WithValue(ctx, loggerHandlerKeyVal, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
type stepIDsContextKey struct{}
|
||||||
|
|
||||||
|
// StepIDs returns the IDs of the step and the composite steps enclosing the current one, outermost first.
|
||||||
|
func StepIDs(ctx context.Context) []string {
|
||||||
|
ids, _ := ctx.Value(stepIDsContextKey{}).([]string)
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithStepIDs replaces the step IDs in the context
|
||||||
|
func WithStepIDs(ctx context.Context, ids []string) context.Context {
|
||||||
|
return context.WithValue(ctx, stepIDsContextKey{}, ids)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,287 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"slices"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ansiReset = "\x1b[0m"
|
||||||
|
ansiRed = "\x1b[31m"
|
||||||
|
ansiYellow = "\x1b[33m"
|
||||||
|
ansiCyan = "\x1b[36m"
|
||||||
|
ansiDimCyan = "\x1b[2;36m"
|
||||||
|
ansiDimWhite = "\x1b[2;37m"
|
||||||
|
)
|
||||||
|
|
||||||
|
// consoleStart is the zero of the elapsed-seconds stamp used when full timestamps are off.
|
||||||
|
var consoleStart = time.Now()
|
||||||
|
|
||||||
|
// console is the state shared by every consoleHandler derived from one another.
|
||||||
|
type console struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
out io.Writer
|
||||||
|
terminal bool
|
||||||
|
opts TextOptions
|
||||||
|
reportCaller bool
|
||||||
|
level slog.LevelVar
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConsole(out io.Writer) *console {
|
||||||
|
c := &console{}
|
||||||
|
c.setOutput(out)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *console) setOutput(w io.Writer) {
|
||||||
|
terminal := checkIfTerminal(w)
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
c.out, c.terminal = w, terminal
|
||||||
|
}
|
||||||
|
|
||||||
|
// consoleHandler is a slog.Handler writing the text format the runner has always used, so that
|
||||||
|
// moving off logrus changes nothing a user can see: sorted key=value fields on a plain stream,
|
||||||
|
// and a colored prefix with elapsed or full time on a terminal.
|
||||||
|
type consoleHandler struct {
|
||||||
|
c *console
|
||||||
|
attrs []slog.Attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *consoleHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||||
|
return level >= h.c.level.Level()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *consoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
|
return &consoleHandler{c: h.c, attrs: append(slices.Clone(h.attrs), attrs...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithGroup is a no-op, as only NewSlogHandler wraps a consoleHandler and it never groups.
|
||||||
|
func (h *consoleHandler) WithGroup(string) slog.Handler { return h }
|
||||||
|
|
||||||
|
func (h *consoleHandler) Handle(_ context.Context, r slog.Record) error {
|
||||||
|
c := h.c
|
||||||
|
c.mu.Lock()
|
||||||
|
opts, reportCaller, terminal := c.opts, c.reportCaller, c.terminal
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
data := make(map[string]any, len(h.attrs)+r.NumAttrs())
|
||||||
|
for _, a := range h.attrs {
|
||||||
|
data[a.Key] = a.Value.Resolve().Any()
|
||||||
|
}
|
||||||
|
r.Attrs(func(a slog.Attr) bool {
|
||||||
|
data[a.Key] = a.Value.Resolve().Any()
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
var caller *runtime.Frame
|
||||||
|
if reportCaller && r.PC != 0 {
|
||||||
|
frame, _ := runtime.CallersFrames([]uintptr{r.PC}).Next()
|
||||||
|
caller = &frame
|
||||||
|
}
|
||||||
|
prefixFieldClashes(data, caller != nil)
|
||||||
|
|
||||||
|
keys := make([]string, 0, len(data))
|
||||||
|
for k := range data {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
slices.Sort(keys)
|
||||||
|
|
||||||
|
var b bytes.Buffer
|
||||||
|
if terminal && !opts.DisableColors {
|
||||||
|
printColored(&b, opts, r, caller, keys, data)
|
||||||
|
} else {
|
||||||
|
printPlain(&b, opts, r, caller, keys, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
line := b.Bytes()
|
||||||
|
if opts.Filter != nil {
|
||||||
|
line = opts.Filter(line)
|
||||||
|
}
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
_, err := c.out.Write(line)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// prefixFieldClashes moves fields that share a name with a fixed key out of its way.
|
||||||
|
func prefixFieldClashes(data map[string]any, reportCaller bool) {
|
||||||
|
for _, key := range []string{"time", "level", "msg"} {
|
||||||
|
if v, ok := data[key]; ok {
|
||||||
|
data["fields."+key] = v
|
||||||
|
delete(data, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if reportCaller {
|
||||||
|
for _, key := range []string{"func", "file"} {
|
||||||
|
if v, ok := data[key]; ok {
|
||||||
|
data["fields."+key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printPlain(b *bytes.Buffer, opts TextOptions, r slog.Record, caller *runtime.Frame, keys []string, data map[string]any) {
|
||||||
|
if !opts.DisableTimestamp {
|
||||||
|
appendKeyValue(b, "time", r.Time.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
appendKeyValue(b, "level", plainLevel(Level(r.Level)))
|
||||||
|
if r.Message != "" {
|
||||||
|
appendKeyValue(b, "msg", r.Message)
|
||||||
|
}
|
||||||
|
if caller != nil {
|
||||||
|
funcVal, fileVal := callerText(opts, caller)
|
||||||
|
if funcVal != "" {
|
||||||
|
appendKeyValue(b, "func", funcVal)
|
||||||
|
}
|
||||||
|
if fileVal != "" {
|
||||||
|
appendKeyValue(b, "file", fileVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, k := range keys {
|
||||||
|
appendKeyValue(b, k, data[k])
|
||||||
|
}
|
||||||
|
b.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
func callerText(opts TextOptions, caller *runtime.Frame) (funcVal, fileVal string) {
|
||||||
|
if opts.CallerPrettyfier != nil {
|
||||||
|
return opts.CallerPrettyfier(caller)
|
||||||
|
}
|
||||||
|
return caller.Function, caller.File + ":" + strconv.Itoa(caller.Line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printColored(b *bytes.Buffer, opts TextOptions, r slog.Record, caller *runtime.Frame, keys []string, data map[string]any) {
|
||||||
|
msg := strings.TrimSuffix(r.Message, "\n")
|
||||||
|
|
||||||
|
var callerStr string
|
||||||
|
if caller != nil {
|
||||||
|
funcVal, fileVal := callerText(opts, caller)
|
||||||
|
if opts.CallerPrettyfier == nil && caller.Function != "" {
|
||||||
|
funcVal += "()"
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case fileVal == "":
|
||||||
|
callerStr = funcVal
|
||||||
|
case funcVal == "":
|
||||||
|
callerStr = fileVal
|
||||||
|
default:
|
||||||
|
callerStr = fileVal + " " + funcVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
level := Level(r.Level)
|
||||||
|
prefix := colorize(level, strings.ToUpper(plainLevel(level))[:4])
|
||||||
|
switch {
|
||||||
|
case opts.DisableTimestamp:
|
||||||
|
fmt.Fprintf(b, "%s%s %-44s ", prefix, callerStr, msg)
|
||||||
|
case !opts.FullTimestamp:
|
||||||
|
fmt.Fprintf(b, "%s[%04d]%s %-44s ", prefix, int(r.Time.Sub(consoleStart)/time.Second), callerStr, msg)
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(b, "%s[%s]%s %-44s ", prefix, r.Time.Format(time.RFC3339), callerStr, msg)
|
||||||
|
}
|
||||||
|
for _, k := range keys {
|
||||||
|
b.WriteByte(' ')
|
||||||
|
b.WriteString(colorize(level, k))
|
||||||
|
b.WriteByte('=')
|
||||||
|
appendValue(b, data[k])
|
||||||
|
}
|
||||||
|
b.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
// plainLevel spells Warn the way logrus did, as "warning".
|
||||||
|
func plainLevel(l Level) string {
|
||||||
|
if s := l.String(); s != "warn" {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return "warning"
|
||||||
|
}
|
||||||
|
|
||||||
|
func colorize(l Level, s string) string {
|
||||||
|
var color string
|
||||||
|
switch {
|
||||||
|
case l <= LevelTrace:
|
||||||
|
color = ansiDimWhite
|
||||||
|
case l <= LevelDebug:
|
||||||
|
color = ansiDimCyan
|
||||||
|
case l <= LevelInfo:
|
||||||
|
color = ansiCyan
|
||||||
|
case l <= LevelWarn:
|
||||||
|
color = ansiYellow
|
||||||
|
default:
|
||||||
|
color = ansiRed
|
||||||
|
}
|
||||||
|
return color + s + ansiReset
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendKeyValue(b *bytes.Buffer, key string, value any) {
|
||||||
|
if b.Len() > 0 {
|
||||||
|
b.WriteByte(' ')
|
||||||
|
}
|
||||||
|
b.WriteString(key)
|
||||||
|
b.WriteByte('=')
|
||||||
|
appendValue(b, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendValue(b *bytes.Buffer, value any) {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
appendString(b, v)
|
||||||
|
case []byte:
|
||||||
|
appendString(b, string(v))
|
||||||
|
case error:
|
||||||
|
appendFormatted(b, v, v.Error)
|
||||||
|
case fmt.Stringer:
|
||||||
|
appendFormatted(b, v, v.String)
|
||||||
|
default:
|
||||||
|
appendString(b, fmt.Sprint(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendFormatted writes what get returns, and "<nil>" if that panics on a nil receiver.
|
||||||
|
func appendFormatted(b *bytes.Buffer, v any, get func() string) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if rv := reflect.ValueOf(v); rv.Kind() == reflect.Pointer && rv.IsNil() {
|
||||||
|
appendString(b, "<nil>")
|
||||||
|
} else {
|
||||||
|
appendString(b, fmt.Sprintf("%%!v(PANIC=%v)", r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
appendString(b, get())
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendString(b *bytes.Buffer, s string) {
|
||||||
|
if needsQuoting(s) {
|
||||||
|
b.WriteString(strconv.Quote(s))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.WriteString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func needsQuoting(s string) bool {
|
||||||
|
for i := range len(s) {
|
||||||
|
c := s[i]
|
||||||
|
switch {
|
||||||
|
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
|
||||||
|
case c == '-', c == '.', c == '_', c == '/', c == '@', c == '^', c == '+':
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func consoleLine(t *testing.T, opts TextOptions, terminal, reportCaller bool, build func(*Logger)) string {
|
||||||
|
t.Helper()
|
||||||
|
var out bytes.Buffer
|
||||||
|
c := newConsole(&out)
|
||||||
|
c.opts, c.terminal, c.reportCaller = opts, terminal, reportCaller
|
||||||
|
c.level.Set(slog.LevelDebug)
|
||||||
|
build(New(NewSlogHandler(&consoleHandler{c: c})))
|
||||||
|
return out.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConsolePlain(t *testing.T) {
|
||||||
|
line := consoleLine(t, TextOptions{DisableTimestamp: true}, false, false, func(l *Logger) {
|
||||||
|
l.With("job", "build x", "n", 3).WithError(errors.New("boom")).Warnf("hello world")
|
||||||
|
l.With("msg", "clash", "empty", "").Info("")
|
||||||
|
})
|
||||||
|
assert.Equal(t, "level=warning msg=\"hello world\" error=boom job=\"build x\" n=3\n"+
|
||||||
|
"level=info empty= fields.msg=clash\n", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConsolePlainTimeFilterAndCaller(t *testing.T) {
|
||||||
|
opts := TextOptions{
|
||||||
|
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
|
||||||
|
return "[fn]", "[" + strings.TrimPrefix(f.Function, "x") + "]"
|
||||||
|
},
|
||||||
|
Filter: func(b []byte) []byte { return bytes.ReplaceAll(b, []byte("secret"), []byte("***")) },
|
||||||
|
}
|
||||||
|
line := consoleLine(t, opts, false, true, func(l *Logger) { l.With("k", "secret").Debugf("a secret") })
|
||||||
|
assert.Regexp(t, `^time="[^"]+" level=debug msg="a \*\*\*" func="\[fn\]" file="\[.*TestConsolePlainTimeFilterAndCaller[^"]*\]" k=\*\*\*\n$`, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConsoleColored(t *testing.T) {
|
||||||
|
line := consoleLine(t, TextOptions{DisableTimestamp: true}, true, false, func(l *Logger) {
|
||||||
|
l.With("job", "j").Warn("careful")
|
||||||
|
})
|
||||||
|
assert.Equal(t, "\x1b[33mWARN\x1b[0m "+"careful"+strings.Repeat(" ", 44-len("careful"))+" \x1b[33mjob\x1b[0m=j\n", line)
|
||||||
|
|
||||||
|
full := consoleLine(t, TextOptions{FullTimestamp: true}, true, false, func(l *Logger) { l.Error("bad") })
|
||||||
|
assert.Regexp(t, `^\x1b\[31mERRO\x1b\[0m\[\d{4}-\d\d-\d\dT[^\]]+\] bad`, full)
|
||||||
|
|
||||||
|
elapsed := consoleLine(t, TextOptions{}, true, false, func(l *Logger) { l.Info("x") })
|
||||||
|
assert.Regexp(t, `^\x1b\[36mINFO\x1b\[0m\[\d{4}\] x`, elapsed)
|
||||||
|
|
||||||
|
plain := consoleLine(t, TextOptions{DisableColors: true, DisableTimestamp: true}, true, false, func(l *Logger) { l.Info("x") })
|
||||||
|
assert.Equal(t, "level=info msg=x\n", plain)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConsoleLevelAndOutput(t *testing.T) {
|
||||||
|
var out bytes.Buffer
|
||||||
|
c := newConsole(&out)
|
||||||
|
h := &consoleHandler{c: c}
|
||||||
|
assert.False(t, h.Enabled(t.Context(), slog.LevelDebug)) // info unless raised
|
||||||
|
c.level.Set(slog.LevelError)
|
||||||
|
assert.False(t, h.Enabled(t.Context(), slog.LevelWarn))
|
||||||
|
assert.True(t, h.Enabled(t.Context(), slog.LevelError))
|
||||||
|
|
||||||
|
rec := slog.NewRecord(time.Unix(0, 0), slog.LevelError, "m", 0)
|
||||||
|
rec.AddAttrs(slog.String("c", "3"))
|
||||||
|
require.NoError(t, h.WithAttrs([]slog.Attr{slog.Int("a", 1)}).Handle(t.Context(), rec))
|
||||||
|
assert.Contains(t, out.String(), "a=1 c=3")
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TextOptions describes how the process-wide Logger writes a record as text.
|
||||||
|
type TextOptions struct {
|
||||||
|
DisableColors bool
|
||||||
|
DisableTimestamp bool
|
||||||
|
FullTimestamp bool
|
||||||
|
// CallerPrettyfier renames the function and file reported with a record when ReportCaller is on.
|
||||||
|
CallerPrettyfier func(*runtime.Frame) (function, file string)
|
||||||
|
// Filter rewrites each formatted line, for example to mask secrets.
|
||||||
|
Filter func(line []byte) []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
std atomic.Pointer[Logger]
|
||||||
|
stdState = newConsole(os.Stderr)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
std.Store(New(NewSlogHandler(&consoleHandler{c: stdState})))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default returns the process-wide Logger.
|
||||||
|
func Default() *Logger { return std.Load() }
|
||||||
|
|
||||||
|
// SetDefault replaces the process-wide Logger, which tests use to capture what the package
|
||||||
|
// functions log. SetLevel, GetLevel, SetOutput and the format functions keep configuring the
|
||||||
|
// initial console Logger.
|
||||||
|
func SetDefault(l *Logger) { std.Store(l) }
|
||||||
|
|
||||||
|
// SetLevel sets the minimum level of the initial process-wide Logger and of what libraries log through logrus.
|
||||||
|
func SetLevel(level Level) {
|
||||||
|
stdState.level.Set(slog.Level(level))
|
||||||
|
logrus.SetLevel(toLogrusLevel(level))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLevel returns the minimum level of the initial process-wide Logger.
|
||||||
|
func GetLevel() Level { return Level(stdState.level.Level()) }
|
||||||
|
|
||||||
|
// SetOutput sets where the initial process-wide Logger writes.
|
||||||
|
func SetOutput(w io.Writer) { stdState.setOutput(w) }
|
||||||
|
|
||||||
|
// Output returns where the initial process-wide Logger writes.
|
||||||
|
func Output() io.Writer {
|
||||||
|
stdState.mu.Lock()
|
||||||
|
defer stdState.mu.Unlock()
|
||||||
|
return stdState.out
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTextFormat sets the text format of the initial process-wide Logger.
|
||||||
|
func SetTextFormat(o TextOptions) {
|
||||||
|
stdState.mu.Lock()
|
||||||
|
defer stdState.mu.Unlock()
|
||||||
|
stdState.opts = o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReportCaller makes the initial process-wide Logger record where each call came from, which costs time.
|
||||||
|
func SetReportCaller(on bool) {
|
||||||
|
stdState.mu.Lock()
|
||||||
|
defer stdState.mu.Unlock()
|
||||||
|
stdState.reportCaller = on
|
||||||
|
logrus.SetReportCaller(on)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Enabled(level Level) bool { return Default().Enabled(level) }
|
||||||
|
|
||||||
|
func With(args ...any) *Logger { return Default().With(args...) }
|
||||||
|
func WithError(err error) *Logger { return Default().WithError(err) }
|
||||||
|
func Tracef(format string, args ...any) { Default().logf(LevelTrace, format, args) }
|
||||||
|
func Debugf(format string, args ...any) { Default().logf(LevelDebug, format, args) }
|
||||||
|
func Infof(format string, args ...any) { Default().logf(LevelInfo, format, args) }
|
||||||
|
func Warnf(format string, args ...any) { Default().logf(LevelWarn, format, args) }
|
||||||
|
func Errorf(format string, args ...any) { Default().logf(LevelError, format, args) }
|
||||||
|
func Trace(args ...any) { Default().log(LevelTrace, args) }
|
||||||
|
func Debug(args ...any) { Default().log(LevelDebug, args) }
|
||||||
|
func Info(args ...any) { Default().log(LevelInfo, args) }
|
||||||
|
func Warn(args ...any) { Default().log(LevelWarn, args) }
|
||||||
|
func Error(args ...any) { Default().log(LevelError, args) }
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Attr is a structured key/value pair attached to a log record.
|
||||||
|
type Attr struct {
|
||||||
|
Key string
|
||||||
|
Value any
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record is one log event. Attrs holds the call-site attributes only, the ones added with
|
||||||
|
// Logger.With are accumulated by the Handler.
|
||||||
|
type Record struct {
|
||||||
|
Time time.Time
|
||||||
|
Level Level
|
||||||
|
Message string
|
||||||
|
PC uintptr // program counter of the logging call, 0 if unknown
|
||||||
|
Attrs []Attr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attr returns the value of the last attribute named key.
|
||||||
|
func (r Record) Attr(key string) (any, bool) {
|
||||||
|
for _, a := range slices.Backward(r.Attrs) {
|
||||||
|
if a.Key == key {
|
||||||
|
return a.Value, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler consumes records, and is shaped like slog.Handler so that a slog backend can replace
|
||||||
|
// the current one without touching callers. Handle must not modify the Attrs slice it is given.
|
||||||
|
type Handler interface {
|
||||||
|
Enabled(ctx context.Context, level Level) bool
|
||||||
|
Handle(ctx context.Context, r Record) error
|
||||||
|
// WithAttrs returns a Handler that adds attrs to every record it handles.
|
||||||
|
WithAttrs(attrs []Attr) Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
type multiHandler []Handler
|
||||||
|
|
||||||
|
// NewMultiHandler fans every record out to all handlers that have its level enabled.
|
||||||
|
func NewMultiHandler(handlers ...Handler) Handler {
|
||||||
|
return multiHandler(handlers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m multiHandler) Enabled(ctx context.Context, level Level) bool {
|
||||||
|
for _, h := range m {
|
||||||
|
if h.Enabled(ctx, level) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m multiHandler) Handle(ctx context.Context, r Record) error {
|
||||||
|
var errs []error
|
||||||
|
for _, h := range m {
|
||||||
|
if h.Enabled(ctx, r.Level) {
|
||||||
|
errs = append(errs, h.Handle(ctx, r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m multiHandler) WithAttrs(attrs []Attr) Handler {
|
||||||
|
out := make(multiHandler, len(m))
|
||||||
|
for i, h := range m {
|
||||||
|
out[i] = h.WithAttrs(attrs)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
type discardHandler struct{}
|
||||||
|
|
||||||
|
// DiscardHandler drops every record.
|
||||||
|
var DiscardHandler Handler = discardHandler{}
|
||||||
|
|
||||||
|
func (discardHandler) Enabled(context.Context, Level) bool { return false }
|
||||||
|
func (discardHandler) Handle(context.Context, Record) error { return nil }
|
||||||
|
func (d discardHandler) WithAttrs([]Attr) Handler { return d }
|
||||||
|
|
||||||
|
type levelHandler struct {
|
||||||
|
h Handler
|
||||||
|
minLevel Level
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLevelHandler passes only records at or above minLevel on to h.
|
||||||
|
func NewLevelHandler(h Handler, minLevel Level) Handler {
|
||||||
|
return levelHandler{h: h, minLevel: minLevel}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l levelHandler) Enabled(ctx context.Context, level Level) bool {
|
||||||
|
return level >= l.minLevel && l.h.Enabled(ctx, level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l levelHandler) Handle(ctx context.Context, r Record) error {
|
||||||
|
if r.Level < l.minLevel {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return l.h.Handle(ctx, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l levelHandler) WithAttrs(attrs []Attr) Handler {
|
||||||
|
return levelHandler{h: l.h.WithAttrs(attrs), minLevel: l.minLevel}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Level is a log severity. The values match log/slog, with Trace added below Debug.
|
||||||
|
type Level int
|
||||||
|
|
||||||
|
const (
|
||||||
|
LevelTrace Level = -8
|
||||||
|
LevelDebug Level = -4
|
||||||
|
LevelInfo Level = 0
|
||||||
|
LevelWarn Level = 4
|
||||||
|
LevelError Level = 8
|
||||||
|
)
|
||||||
|
|
||||||
|
func (l Level) String() string {
|
||||||
|
switch {
|
||||||
|
case l <= LevelTrace:
|
||||||
|
return "trace"
|
||||||
|
case l <= LevelDebug:
|
||||||
|
return "debug"
|
||||||
|
case l <= LevelInfo:
|
||||||
|
return "info"
|
||||||
|
case l <= LevelWarn:
|
||||||
|
return "warn"
|
||||||
|
default:
|
||||||
|
return "error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseLevel accepts every name logrus does. "fatal" and "panic" map to LevelError because
|
||||||
|
// the Logger has no level above it.
|
||||||
|
func ParseLevel(s string) (Level, error) {
|
||||||
|
switch strings.ToLower(s) {
|
||||||
|
case "trace":
|
||||||
|
return LevelTrace, nil
|
||||||
|
case "debug":
|
||||||
|
return LevelDebug, nil
|
||||||
|
case "info":
|
||||||
|
return LevelInfo, nil
|
||||||
|
case "warn", "warning":
|
||||||
|
return LevelWarn, nil
|
||||||
|
case "error", "fatal", "panic":
|
||||||
|
return LevelError, nil
|
||||||
|
}
|
||||||
|
return LevelInfo, fmt.Errorf("not a valid log level: %q", s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const badKey = "!BADKEY"
|
||||||
|
|
||||||
|
// Logger writes printf-style messages with structured attributes to a Handler.
|
||||||
|
type Logger struct {
|
||||||
|
h Handler
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(h Handler) *Logger {
|
||||||
|
return &Logger{h: h, ctx: context.Background()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// With returns a Logger that adds the key/value pairs to every record, like slog.Logger.With.
|
||||||
|
func (l *Logger) With(args ...any) *Logger {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
return &Logger{h: l.h.WithAttrs(argsToAttrs(args)), ctx: l.ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) WithError(err error) *Logger {
|
||||||
|
return l.With("error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext returns a Logger that passes ctx to its Handler.
|
||||||
|
func (l *Logger) WithContext(ctx context.Context) *Logger {
|
||||||
|
return &Logger{h: l.h, ctx: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Enabled(level Level) bool {
|
||||||
|
return l.h.Enabled(l.ctx, level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Tracef(format string, args ...any) { l.logf(LevelTrace, format, args) }
|
||||||
|
func (l *Logger) Debugf(format string, args ...any) { l.logf(LevelDebug, format, args) }
|
||||||
|
func (l *Logger) Infof(format string, args ...any) { l.logf(LevelInfo, format, args) }
|
||||||
|
func (l *Logger) Warnf(format string, args ...any) { l.logf(LevelWarn, format, args) }
|
||||||
|
func (l *Logger) Errorf(format string, args ...any) { l.logf(LevelError, format, args) }
|
||||||
|
|
||||||
|
// Trace and the methods below format their arguments like fmt.Sprint.
|
||||||
|
func (l *Logger) Trace(args ...any) { l.log(LevelTrace, args) }
|
||||||
|
func (l *Logger) Debug(args ...any) { l.log(LevelDebug, args) }
|
||||||
|
func (l *Logger) Info(args ...any) { l.log(LevelInfo, args) }
|
||||||
|
func (l *Logger) Warn(args ...any) { l.log(LevelWarn, args) }
|
||||||
|
func (l *Logger) Error(args ...any) { l.log(LevelError, args) }
|
||||||
|
|
||||||
|
func (l *Logger) logf(level Level, format string, args []any) {
|
||||||
|
if !l.Enabled(level) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.emit(callerPC(4), level, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) log(level Level, args []any) {
|
||||||
|
if !l.Enabled(level) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.emit(callerPC(4), level, fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// emit drops the Handler error, as a logger has nowhere to report it.
|
||||||
|
func (l *Logger) emit(pc uintptr, level Level, msg string) {
|
||||||
|
_ = l.h.Handle(l.ctx, Record{Time: time.Now(), Level: level, Message: msg, PC: pc})
|
||||||
|
}
|
||||||
|
|
||||||
|
// callerPC returns the program counter skip frames up, 4 being the caller of an exported method.
|
||||||
|
func callerPC(skip int) uintptr {
|
||||||
|
var pcs [1]uintptr
|
||||||
|
runtime.Callers(skip, pcs[:])
|
||||||
|
return pcs[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func argsToAttrs(args []any) []Attr {
|
||||||
|
attrs := make([]Attr, 0, len(args)/2+1)
|
||||||
|
for len(args) > 0 {
|
||||||
|
switch k := args[0].(type) {
|
||||||
|
case Attr:
|
||||||
|
attrs = append(attrs, k)
|
||||||
|
args = args[1:]
|
||||||
|
case string:
|
||||||
|
if len(args) == 1 {
|
||||||
|
attrs = append(attrs, Attr{Key: badKey, Value: k})
|
||||||
|
args = nil
|
||||||
|
} else {
|
||||||
|
attrs = append(attrs, Attr{Key: k, Value: args[1]})
|
||||||
|
args = args[2:]
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
attrs = append(attrs, Attr{Key: badKey, Value: k})
|
||||||
|
args = args[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return attrs
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseLevel(t *testing.T) {
|
||||||
|
for in, want := range map[string]logging.Level{
|
||||||
|
"trace": logging.LevelTrace, "DEBUG": logging.LevelDebug, "info": logging.LevelInfo,
|
||||||
|
"warn": logging.LevelWarn, "warning": logging.LevelWarn,
|
||||||
|
"error": logging.LevelError, "fatal": logging.LevelError, "panic": logging.LevelError,
|
||||||
|
} {
|
||||||
|
got, err := logging.ParseLevel(in)
|
||||||
|
require.NoError(t, err, in)
|
||||||
|
assert.Equal(t, want, got, in)
|
||||||
|
}
|
||||||
|
_, err := logging.ParseLevel("loud")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Equal(t, "warn", logging.LevelWarn.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
type countingStringer struct{ n *int }
|
||||||
|
|
||||||
|
func (c countingStringer) String() string { *c.n++; return "x" }
|
||||||
|
|
||||||
|
func TestLogger(t *testing.T) {
|
||||||
|
h := logtest.NewWithLevel(logging.LevelTrace)
|
||||||
|
l := logging.New(h)
|
||||||
|
|
||||||
|
l.With("job", "j1").WithError(errors.New("boom")).Warnf("step %d", 2)
|
||||||
|
l.Info("a", 1, 2, "b")
|
||||||
|
l.With("k").With(7).Debug("x")
|
||||||
|
|
||||||
|
recs := h.AllRecords()
|
||||||
|
require.Len(t, recs, 3)
|
||||||
|
assert.Equal(t, "step 2", recs[0].Message)
|
||||||
|
assert.Equal(t, logging.LevelWarn, recs[0].Level)
|
||||||
|
assert.False(t, recs[0].Time.IsZero())
|
||||||
|
job, _ := recs[0].Attr("job")
|
||||||
|
assert.Equal(t, "j1", job)
|
||||||
|
errv, _ := recs[0].Attr("error")
|
||||||
|
require.EqualError(t, errv.(error), "boom")
|
||||||
|
assert.Equal(t, "a1 2b", recs[1].Message) // fmt.Sprint spacing
|
||||||
|
bad, _ := recs[2].Attr("!BADKEY")
|
||||||
|
assert.Equal(t, 7, bad)
|
||||||
|
|
||||||
|
h.Reset()
|
||||||
|
assert.Nil(t, h.LastRecord())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerSkipsFormattingWhenDisabled(t *testing.T) {
|
||||||
|
n := 0
|
||||||
|
l := logging.New(logging.NewLevelHandler(logtest.NewWithLevel(logging.LevelTrace), logging.LevelWarn))
|
||||||
|
l.Debugf("%v", countingStringer{&n})
|
||||||
|
assert.Zero(t, n)
|
||||||
|
assert.False(t, l.Enabled(logging.LevelInfo))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultiHandler(t *testing.T) {
|
||||||
|
assert.False(t, logging.DiscardHandler.Enabled(t.Context(), logging.LevelError))
|
||||||
|
all, warnOnly := logtest.New(), logtest.NewWithLevel(logging.LevelTrace)
|
||||||
|
l := logging.New(logging.NewMultiHandler(all, logging.NewLevelHandler(warnOnly, logging.LevelWarn), logging.DiscardHandler)).With("a", 1)
|
||||||
|
l.Info("i")
|
||||||
|
l.Error("e")
|
||||||
|
assert.Len(t, all.AllRecords(), 2)
|
||||||
|
require.Len(t, warnOnly.AllRecords(), 1)
|
||||||
|
v, _ := warnOnly.LastRecord().Attr("a")
|
||||||
|
assert.Equal(t, 1, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func frameOf(t *testing.T, pc uintptr) runtime.Frame {
|
||||||
|
t.Helper()
|
||||||
|
require.NotZero(t, pc)
|
||||||
|
f, _ := runtime.CallersFrames([]uintptr{pc}).Next()
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCallerPC(t *testing.T) {
|
||||||
|
l, h := logtest.NewLogger()
|
||||||
|
l.Infof("method")
|
||||||
|
l.Info("method")
|
||||||
|
|
||||||
|
prev := logging.Default()
|
||||||
|
t.Cleanup(func() { logging.SetDefault(prev) })
|
||||||
|
logging.SetDefault(l)
|
||||||
|
logging.Infof("global")
|
||||||
|
logging.Error("global")
|
||||||
|
|
||||||
|
require.Len(t, h.AllRecords(), 4)
|
||||||
|
for _, r := range h.AllRecords() {
|
||||||
|
assert.True(t, strings.HasSuffix(frameOf(t, r.PC).Function, "TestCallerPC"), r.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultLevel(t *testing.T) {
|
||||||
|
prev := logging.GetLevel()
|
||||||
|
t.Cleanup(func() { logging.SetLevel(prev) })
|
||||||
|
|
||||||
|
logging.SetLevel(logging.LevelWarn)
|
||||||
|
assert.Equal(t, logging.LevelWarn, logging.GetLevel())
|
||||||
|
assert.True(t, logging.Enabled(logging.LevelError))
|
||||||
|
assert.False(t, logging.Enabled(logging.LevelInfo))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlogHandler(t *testing.T) {
|
||||||
|
var buf strings.Builder
|
||||||
|
slogger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||||
|
l := logging.New(logging.NewSlogHandler(slogger.Handler())).With("job", "j1")
|
||||||
|
|
||||||
|
l.Tracef("dropped")
|
||||||
|
assert.False(t, l.Enabled(logging.LevelTrace))
|
||||||
|
l.WithError(errors.New("boom")).Errorf("fail %s", "x")
|
||||||
|
|
||||||
|
out := buf.String()
|
||||||
|
assert.Contains(t, out, "level=ERROR")
|
||||||
|
assert.Contains(t, out, `msg="fail x"`)
|
||||||
|
assert.Contains(t, out, "job=j1")
|
||||||
|
assert.Contains(t, out, "error=boom")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLogrusBridge(t *testing.T) {
|
||||||
|
prevLevel, prevDefault := logging.GetLevel(), logging.Default()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
logging.SetLevel(prevLevel)
|
||||||
|
logging.SetReportCaller(false)
|
||||||
|
logging.SetDefault(prevDefault)
|
||||||
|
})
|
||||||
|
logging.SetLevel(logging.LevelInfo)
|
||||||
|
assert.False(t, logrus.IsLevelEnabled(logrus.DebugLevel))
|
||||||
|
logging.SetLevel(logging.LevelTrace)
|
||||||
|
logging.SetReportCaller(true)
|
||||||
|
h := logtest.NewWithLevel(logging.LevelTrace)
|
||||||
|
logging.SetDefault(logging.New(h))
|
||||||
|
|
||||||
|
logrus.WithField("lib", "docker").Warnf("from a library %d", 1)
|
||||||
|
_, _, line, _ := runtime.Caller(0)
|
||||||
|
logrus.Debug("below")
|
||||||
|
logrus.Trace("lowest")
|
||||||
|
|
||||||
|
recs := h.AllRecords()
|
||||||
|
require.Len(t, recs, 3)
|
||||||
|
assert.Equal(t, "from a library 1", recs[0].Message)
|
||||||
|
assert.Equal(t, logging.LevelWarn, recs[0].Level)
|
||||||
|
v, _ := recs[0].Attr("lib")
|
||||||
|
assert.Equal(t, "docker", v)
|
||||||
|
assert.Equal(t, logging.LevelDebug, recs[1].Level)
|
||||||
|
f := frameOf(t, recs[1].PC)
|
||||||
|
assert.True(t, strings.HasSuffix(f.Function, "TestLogrusBridge"), f.Function)
|
||||||
|
assert.Equal(t, line+1, f.Line)
|
||||||
|
assert.Equal(t, logging.LevelTrace, recs[2].Level)
|
||||||
|
|
||||||
|
h = logtest.New() // logrus passes Debug, the Logger does not
|
||||||
|
logging.SetDefault(logging.New(h))
|
||||||
|
logrus.Debug("dropped")
|
||||||
|
assert.Empty(t, h.AllRecords())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlogBridge(t *testing.T) {
|
||||||
|
h := logtest.SetDefault(t)
|
||||||
|
slog.With("lib", "actionslib").Warn("from a library", "n", 1)
|
||||||
|
_, _, line, _ := runtime.Caller(0)
|
||||||
|
slog.Debug("dropped")
|
||||||
|
|
||||||
|
recs := h.AllRecords()
|
||||||
|
require.Len(t, recs, 1)
|
||||||
|
assert.Equal(t, "from a library", recs[0].Message)
|
||||||
|
assert.Equal(t, logging.LevelWarn, recs[0].Level)
|
||||||
|
lib, _ := recs[0].Attr("lib")
|
||||||
|
assert.Equal(t, "actionslib", lib)
|
||||||
|
n, _ := recs[0].Attr("n")
|
||||||
|
assert.EqualValues(t, 1, n)
|
||||||
|
assert.Equal(t, line-1, frameOf(t, recs[0].PC).Line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLogrusBridgeIntact(t *testing.T) {
|
||||||
|
assert.True(t, logging.LogrusBridgeIntact())
|
||||||
|
|
||||||
|
prev := logrus.StandardLogger().Out
|
||||||
|
t.Cleanup(func() { logrus.SetOutput(prev) })
|
||||||
|
logrus.SetOutput(&strings.Builder{}) // what a dependency doing this would do
|
||||||
|
assert.False(t, logging.LogrusBridgeIntact())
|
||||||
|
logrus.SetOutput(prev)
|
||||||
|
assert.True(t, logging.LogrusBridgeIntact())
|
||||||
|
|
||||||
|
prevLevel := logrus.GetLevel()
|
||||||
|
t.Cleanup(func() { logrus.SetLevel(prevLevel) })
|
||||||
|
logrus.SetLevel(logrus.PanicLevel)
|
||||||
|
assert.False(t, logging.LogrusBridgeIntact())
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Libraries the runner depends on, such as docker/cli and moby/go-archive, log through
|
||||||
|
// logrus's global logger. Hand what they log to the process-wide Logger, so it follows the same
|
||||||
|
// level, format and secret masking as the rest of the output.
|
||||||
|
func init() {
|
||||||
|
logrus.SetOutput(io.Discard)
|
||||||
|
logrus.SetFormatter(discardFormatter{})
|
||||||
|
logrus.SetLevel(toLogrusLevel(GetLevel()))
|
||||||
|
logrus.AddHook(bridgeHook{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type discardFormatter struct{}
|
||||||
|
|
||||||
|
func (discardFormatter) Format(*logrus.Entry) ([]byte, error) { return nil, nil }
|
||||||
|
|
||||||
|
type bridgeHook struct{}
|
||||||
|
|
||||||
|
func (bridgeHook) Levels() []logrus.Level { return logrus.AllLevels }
|
||||||
|
|
||||||
|
func (bridgeHook) Fire(e *logrus.Entry) error {
|
||||||
|
l := Default()
|
||||||
|
level := fromLogrusLevel(e.Level)
|
||||||
|
ctx := e.Context
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
if !l.h.Enabled(ctx, level) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
attrs := make([]Attr, 0, len(e.Data))
|
||||||
|
for k, v := range e.Data {
|
||||||
|
attrs = append(attrs, Attr{Key: k, Value: v})
|
||||||
|
}
|
||||||
|
r := Record{Time: e.Time, Level: level, Message: e.Message, Attrs: attrs}
|
||||||
|
if e.Caller != nil {
|
||||||
|
r.PC = e.Caller.PC + 1 // Frame.PC is one below the return address Record.PC holds
|
||||||
|
}
|
||||||
|
return l.h.Handle(ctx, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toLogrusLevel(l Level) logrus.Level {
|
||||||
|
switch {
|
||||||
|
case l <= LevelTrace:
|
||||||
|
return logrus.TraceLevel
|
||||||
|
case l <= LevelDebug:
|
||||||
|
return logrus.DebugLevel
|
||||||
|
case l <= LevelInfo:
|
||||||
|
return logrus.InfoLevel
|
||||||
|
case l <= LevelWarn:
|
||||||
|
return logrus.WarnLevel
|
||||||
|
default:
|
||||||
|
return logrus.ErrorLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromLogrusLevel(l logrus.Level) Level {
|
||||||
|
switch l {
|
||||||
|
case logrus.TraceLevel:
|
||||||
|
return LevelTrace
|
||||||
|
case logrus.DebugLevel:
|
||||||
|
return LevelDebug
|
||||||
|
case logrus.InfoLevel:
|
||||||
|
return LevelInfo
|
||||||
|
case logrus.WarnLevel:
|
||||||
|
return LevelWarn
|
||||||
|
default:
|
||||||
|
return LevelError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogrusBridgeIntact reports whether logrus's global logger is still set up as the bridge needs,
|
||||||
|
// discarding its own output and following the process-wide level. A dependency that calls
|
||||||
|
// logrus.SetOutput or SetLevel breaks that, and its lines then show up twice, or at the wrong level.
|
||||||
|
func LogrusBridgeIntact() bool {
|
||||||
|
std := logrus.StandardLogger()
|
||||||
|
if std.Out != io.Discard || std.GetLevel() != toLogrusLevel(GetLevel()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, level := range logrus.AllLevels {
|
||||||
|
found := false
|
||||||
|
for _, h := range std.Hooks[level] {
|
||||||
|
if _, ok := h.(bridgeHook); ok {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// Package logtest provides a Handler that keeps records in memory for assertions.
|
||||||
|
package logtest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"slices"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
type store struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
records []logging.Record
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler records what it receives at or above its level. Loggers derived with WithAttrs share one store.
|
||||||
|
type Handler struct {
|
||||||
|
store *store
|
||||||
|
attrs []logging.Attr
|
||||||
|
minLevel logging.Level
|
||||||
|
}
|
||||||
|
|
||||||
|
// New records from LevelInfo up, like the logger of logrus's hooks/test.
|
||||||
|
func New() *Handler {
|
||||||
|
return NewWithLevel(logging.LevelInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLogger returns a Logger writing to a new Handler from New, and that Handler.
|
||||||
|
func NewLogger() (*logging.Logger, *Handler) {
|
||||||
|
h := New()
|
||||||
|
return logging.New(h), h
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWithLevel(minLevel logging.Level) *Handler {
|
||||||
|
return &Handler{store: &store{}, minLevel: minLevel}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Enabled(_ context.Context, level logging.Level) bool { return level >= h.minLevel }
|
||||||
|
|
||||||
|
// Handle stores the record with the attributes accumulated by WithAttrs placed first.
|
||||||
|
func (h *Handler) Handle(_ context.Context, r logging.Record) error {
|
||||||
|
r.Attrs = append(slices.Clone(h.attrs), r.Attrs...)
|
||||||
|
h.store.mu.Lock()
|
||||||
|
defer h.store.mu.Unlock()
|
||||||
|
h.store.records = append(h.store.records, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) WithAttrs(attrs []logging.Attr) logging.Handler {
|
||||||
|
return &Handler{store: h.store, attrs: append(slices.Clone(h.attrs), attrs...), minLevel: h.minLevel}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) AllRecords() []logging.Record {
|
||||||
|
h.store.mu.Lock()
|
||||||
|
defer h.store.mu.Unlock()
|
||||||
|
return slices.Clone(h.store.records)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastRecord returns nil when nothing was logged.
|
||||||
|
func (h *Handler) LastRecord() *logging.Record {
|
||||||
|
h.store.mu.Lock()
|
||||||
|
defer h.store.mu.Unlock()
|
||||||
|
if len(h.store.records) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r := h.store.records[len(h.store.records)-1]
|
||||||
|
return &r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Reset() {
|
||||||
|
h.store.mu.Lock()
|
||||||
|
defer h.store.mu.Unlock()
|
||||||
|
h.store.records = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefault makes the process-wide Logger record into a new Handler until the test ends.
|
||||||
|
func SetDefault(t testing.TB) *Handler {
|
||||||
|
t.Helper()
|
||||||
|
prev := logging.Default()
|
||||||
|
logger, h := NewLogger()
|
||||||
|
logging.SetDefault(logger)
|
||||||
|
t.Cleanup(func() { logging.SetDefault(prev) })
|
||||||
|
return h
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
slog.SetDefault(slog.New(slogBridge{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// slogBridge hands what libraries log through slog's global logger to the process-wide Logger.
|
||||||
|
type slogBridge struct {
|
||||||
|
attrs []Attr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b slogBridge) Enabled(ctx context.Context, level slog.Level) bool {
|
||||||
|
return Default().h.Enabled(ctx, Level(level))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b slogBridge) Handle(ctx context.Context, r slog.Record) error {
|
||||||
|
h := Default().h
|
||||||
|
if len(b.attrs) > 0 {
|
||||||
|
h = h.WithAttrs(b.attrs)
|
||||||
|
}
|
||||||
|
attrs := make([]Attr, 0, r.NumAttrs())
|
||||||
|
r.Attrs(func(a slog.Attr) bool {
|
||||||
|
attrs = append(attrs, fromSlogAttr(a))
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return h.Handle(ctx, Record{Time: r.Time, Level: Level(r.Level), Message: r.Message, PC: r.PC, Attrs: attrs})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b slogBridge) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
|
out := slices.Clone(b.attrs)
|
||||||
|
for _, a := range attrs {
|
||||||
|
out = append(out, fromSlogAttr(a))
|
||||||
|
}
|
||||||
|
return slogBridge{attrs: out}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b slogBridge) WithGroup(string) slog.Handler { return b }
|
||||||
|
|
||||||
|
func fromSlogAttr(a slog.Attr) Attr {
|
||||||
|
return Attr{Key: a.Key, Value: a.Value.Resolve().Any()}
|
||||||
|
}
|
||||||
|
|
||||||
|
type slogHandler struct {
|
||||||
|
h slog.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSlogHandler writes to h. Levels map one to one, as Level uses slog's values.
|
||||||
|
func NewSlogHandler(h slog.Handler) Handler {
|
||||||
|
return slogHandler{h: h}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s slogHandler) Enabled(ctx context.Context, level Level) bool {
|
||||||
|
return s.h.Enabled(ctx, slog.Level(level))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s slogHandler) Handle(ctx context.Context, r Record) error {
|
||||||
|
rec := slog.NewRecord(r.Time, slog.Level(r.Level), r.Message, r.PC)
|
||||||
|
rec.AddAttrs(toSlogAttrs(r.Attrs)...)
|
||||||
|
return s.h.Handle(ctx, rec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s slogHandler) WithAttrs(attrs []Attr) Handler {
|
||||||
|
return slogHandler{h: s.h.WithAttrs(toSlogAttrs(attrs))}
|
||||||
|
}
|
||||||
|
|
||||||
|
func toSlogAttrs(attrs []Attr) []slog.Attr {
|
||||||
|
out := make([]slog.Attr, len(attrs))
|
||||||
|
for i, a := range attrs {
|
||||||
|
out[i] = slog.Any(a.Key, a.Value)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
if f, ok := w.(*os.File); ok {
|
||||||
|
return term.IsTerminal(int(f.Fd()))
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
// checkIfTerminal also turns on virtual terminal processing, which the console needs to show colors.
|
||||||
|
func checkIfTerminal(w io.Writer) bool {
|
||||||
|
f, ok := w.(*os.File)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
handle := windows.Handle(f.Fd())
|
||||||
|
var mode uint32
|
||||||
|
if err := windows.GetConsoleMode(handle, &mode); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return windows.SetConsoleMode(handle, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == nil
|
||||||
|
}
|
||||||
@@ -8,10 +8,10 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
cerrdefs "github.com/containerd/errdefs"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/moby/moby/client"
|
"github.com/moby/moby/client"
|
||||||
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageExistsLocally returns a boolean indicating if an image with the
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
||||||
@@ -30,11 +30,23 @@ func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool,
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if platform == "" || platform == "any" || fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
if platform == "" || platform == "any" {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
requested, err := parsePlatform(platform)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return platformMatches(requested, &specs.Platform{OS: inspectImage.Os, Architecture: inspectImage.Architecture, Variant: inspectImage.Variant}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func platformMatches(requested, image *specs.Platform) bool {
|
||||||
|
variant := image.Variant
|
||||||
|
if variant == "" {
|
||||||
|
variant = map[string]string{"amd64": "v1", "arm": "v7", "arm64": "v8"}[image.Architecture]
|
||||||
|
}
|
||||||
|
return image.OS == requested.OS && image.Architecture == requested.Architecture && (requested.Variant == "" || requested.Variant == variant)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveImage removes image from local store, the function is used to run different
|
// RemoveImage removes image from local store, the function is used to run different
|
||||||
|
|||||||
@@ -5,20 +5,20 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildScratchImage builds a tiny empty image for the given platform locally (FROM scratch, no
|
// buildScratchImage builds a tiny empty image for the given platform locally (FROM scratch, no
|
||||||
@@ -41,7 +41,7 @@ func buildScratchImage(t *testing.T, platform string) string {
|
|||||||
|
|
||||||
func TestImageExistsLocally(t *testing.T) {
|
func TestImageExistsLocally(t *testing.T) {
|
||||||
requireDocker(t)
|
requireDocker(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
// a non-existent image is reported absent
|
// a non-existent image is reported absent
|
||||||
missing, err := ImageExistsLocally(ctx, "library/alpine:this-random-tag-will-never-exist", "linux/amd64")
|
missing, err := ImageExistsLocally(ctx, "library/alpine:this-random-tag-will-never-exist", "linux/amd64")
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type dockerMessage struct {
|
type dockerMessage struct {
|
||||||
@@ -26,7 +26,7 @@ type dockerMessage struct {
|
|||||||
Progress string `json:"progress"`
|
Progress string `json:"progress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func logDockerResponse(logger logrus.FieldLogger, dockerResponse io.ReadCloser, isError bool) error {
|
func logDockerResponse(logger *logging.Logger, dockerResponse io.ReadCloser, isError bool) error {
|
||||||
if dockerResponse == nil {
|
if dockerResponse == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func logDockerResponse(logger logrus.FieldLogger, dockerResponse io.ReadCloser,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeLog(logger logrus.FieldLogger, isError bool, format string, args ...any) {
|
func writeLog(logger *logging.Logger, isError bool, format string, args ...any) {
|
||||||
if isError {
|
if isError {
|
||||||
logger.Errorf(format, args...)
|
logger.Errorf(format, args...)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -87,45 +87,54 @@ func NewDockerNetworkCreateExecutor(name string, opts NewDockerNetworkCreateExec
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
return createDockerNetwork(ctx, cli, name, opts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Only create the network if it doesn't exist
|
func createDockerNetwork(ctx context.Context, cli client.APIClient, name string, opts NewDockerNetworkCreateExecutorInput) error {
|
||||||
networks, err := cli.NetworkList(ctx, client.NetworkListOptions{})
|
// Only create the network if it doesn't exist
|
||||||
if err != nil {
|
networks, err := cli.NetworkList(ctx, client.NetworkListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var options map[string]string
|
||||||
|
// For Gitea, reduce log noise
|
||||||
|
// common.Logger(ctx).Debugf("%v", networks)
|
||||||
|
for _, n := range networks.Items {
|
||||||
|
if n.Name == name {
|
||||||
|
common.Logger(ctx).Debugf("Network %v exists", name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// the daemon's mtu only reaches its default bridge, Docker's default is left out to keep default-network-opts
|
||||||
|
if mtu := n.Options["com.docker.network.driver.mtu"]; n.Name == network.NetworkBridge && mtu != "" && mtu != "1500" {
|
||||||
|
options = map[string]string{"com.docker.network.driver.mtu": mtu}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range networkCreateAttempts {
|
||||||
|
if i > 0 {
|
||||||
|
common.Logger(ctx).Infof("Waiting for a free docker address pool to create network %s", name)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-time.After(time.Duration(i) * networkCreateRetryDelay):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err = cli.NetworkCreate(ctx, name, client.NetworkCreateOptions{
|
||||||
|
Driver: "bridge",
|
||||||
|
Scope: "local",
|
||||||
|
EnableIPv4: opts.EnableIPv4,
|
||||||
|
EnableIPv6: opts.EnableIPv6,
|
||||||
|
Options: options,
|
||||||
|
Labels: runnerLabels(opts.RunnerUUID),
|
||||||
|
}); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !isAddressPoolExhausted(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// For Gitea, reduce log noise
|
|
||||||
// common.Logger(ctx).Debugf("%v", networks)
|
|
||||||
for _, n := range networks.Items {
|
|
||||||
if n.Name == name {
|
|
||||||
common.Logger(ctx).Debugf("Network %v exists", name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range networkCreateAttempts {
|
|
||||||
if i > 0 {
|
|
||||||
common.Logger(ctx).Infof("Waiting for a free docker address pool to create network %s", name)
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
case <-time.After(time.Duration(i) * networkCreateRetryDelay):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err = cli.NetworkCreate(ctx, name, client.NetworkCreateOptions{
|
|
||||||
Driver: "bridge",
|
|
||||||
Scope: "local",
|
|
||||||
EnableIPv4: opts.EnableIPv4,
|
|
||||||
EnableIPv6: opts.EnableIPv6,
|
|
||||||
Labels: runnerLabels(opts.RunnerUUID),
|
|
||||||
}); err == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if !isAddressPoolExhausted(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("docker has no address pool left for this job's network, lower runner.capacity or widen default-address-pools in the docker daemon config: %w", err)
|
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("docker has no address pool left for this job's network, lower runner.capacity or widen default-address-pools in the docker daemon config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runnerLabels(runnerUUID string) map[string]string {
|
func runnerLabels(runnerUUID string) map[string]string {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestIsolatedCacheContainer(t *testing.T) {
|
func TestIsolatedCacheContainer(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cache, lan := netip.MustParseAddr("172.18.0.3"), netip.MustParseAddr("192.168.1.20")
|
cache, lan := netip.MustParseAddr("172.18.0.3"), netip.MustParseAddr("192.168.1.20")
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{}).Return(mobyclient.ContainerListResult{Items: []container.Summary{{}, {
|
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{}).Return(mobyclient.ContainerListResult{Items: []container.Summary{{}, {
|
||||||
@@ -57,6 +57,27 @@ func TestIsAddressPoolExhausted(t *testing.T) {
|
|||||||
assert.False(t, isAddressPoolExhausted(cerrdefs.ErrInvalidArgument.WithMessage("invalid subnet 10.0.0.0/8: it overlaps with an existing network")))
|
assert.False(t, isAddressPoolExhausted(cerrdefs.ErrInvalidArgument.WithMessage("invalid subnet 10.0.0.0/8: it overlaps with an existing network")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDockerNetworkInheritsDaemonMTU(t *testing.T) {
|
||||||
|
ctx := t.Context()
|
||||||
|
for _, tc := range []struct {
|
||||||
|
bridgeOptions, want map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
map[string]string{"com.docker.network.bridge.host_binding_ipv4": "127.0.0.1", "com.docker.network.driver.mtu": "1450"},
|
||||||
|
map[string]string{"com.docker.network.driver.mtu": "1450"},
|
||||||
|
},
|
||||||
|
{map[string]string{"com.docker.network.driver.mtu": "1500"}, nil},
|
||||||
|
} {
|
||||||
|
client := &mockDockerClient{}
|
||||||
|
client.On("NetworkList", ctx, mobyclient.NetworkListOptions{}).
|
||||||
|
Return(mobyclient.NetworkListResult{Items: []network.Summary{{Name: network.NetworkBridge, Options: tc.bridgeOptions}}}, nil)
|
||||||
|
client.On("NetworkCreate", ctx, "job-network", mobyclient.NetworkCreateOptions{Driver: "bridge", Scope: "local", Options: tc.want}).
|
||||||
|
Return(mobyclient.NetworkCreateResult{}, nil)
|
||||||
|
require.NoError(t, createDockerNetwork(ctx, client, "job-network", NewDockerNetworkCreateExecutorInput{}))
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) NetworkConnect(ctx context.Context, id string, opts mobyclient.NetworkConnectOptions) (mobyclient.NetworkConnectResult, error) {
|
func (m *mockDockerClient) NetworkConnect(ctx context.Context, id string, opts mobyclient.NetworkConnectOptions) (mobyclient.NetworkConnectResult, error) {
|
||||||
return mobyclient.NetworkConnectResult{}, m.Called(ctx, id, opts).Error(0)
|
return mobyclient.NetworkConnectResult{}, m.Called(ctx, id, opts).Error(0)
|
||||||
}
|
}
|
||||||
@@ -66,7 +87,7 @@ func (m *mockDockerClient) NetworkDisconnect(ctx context.Context, id string, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectAndDisconnectNetwork(t *testing.T) {
|
func TestConnectAndDisconnectNetwork(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
connect := func(answer error) error {
|
connect := func(answer error) error {
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("NetworkConnect", ctx, "jobs", mobyclient.NetworkConnectOptions{
|
client.On("NetworkConnect", ctx, "jobs", mobyclient.NetworkConnectOptions{
|
||||||
@@ -96,7 +117,7 @@ func TestConnectAndDisconnectNetwork(t *testing.T) {
|
|||||||
// and old enough to predate any job now starting are the runner's to reclaim. An unexpected
|
// and old enough to predate any job now starting are the runner's to reclaim. An unexpected
|
||||||
// NetworkRemove fails the test on its own, since testify has no expectation to match it against.
|
// NetworkRemove fails the test on its own, since testify has no expectation to match it against.
|
||||||
func TestRemoveOrphanNetworks(t *testing.T) {
|
func TestRemoveOrphanNetworks(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cutoff := time.Date(2026, time.April, 29, 20, 0, 0, 0, time.UTC)
|
cutoff := time.Date(2026, time.April, 29, 20, 0, 0, 0, time.UTC)
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("NetworkList", ctx, mobyclient.NetworkListOptions{
|
client.On("NetworkList", ctx, mobyclient.NetworkListOptions{
|
||||||
|
|||||||
@@ -61,3 +61,26 @@ func TestParsePlatform(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlatformMatches(t *testing.T) {
|
||||||
|
for _, testcase := range []struct {
|
||||||
|
requested, image string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"linux/amd64", "linux/amd64", true},
|
||||||
|
{"linux/amd64/v1", "linux/amd64", true},
|
||||||
|
{"linux/arm64", "linux/amd64", false},
|
||||||
|
{"linux/arm64/v8", "linux/arm64", true},
|
||||||
|
{"linux/arm/v7", "linux/arm/v7", true},
|
||||||
|
{"linux/arm", "linux/arm/v7", true},
|
||||||
|
{"linux/arm/v6", "linux/arm/v7", false},
|
||||||
|
{"linux/arm", "linux/arm/v6", true},
|
||||||
|
{"windows/arm/v7", "linux/arm/v7", false},
|
||||||
|
} {
|
||||||
|
requested, err := parsePlatform(testcase.requested)
|
||||||
|
require.NoError(t, err)
|
||||||
|
image, err := parsePlatform(testcase.image)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, testcase.want, platformMatches(requested, image), "%s on %s", testcase.requested, testcase.image)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
jobLabel = "com.gitea.runner.job"
|
jobLabel = "com.gitea.runner.job."
|
||||||
maxCreateBody = 8 << 20
|
maxCreateBody = 8 << 20
|
||||||
|
|
||||||
dockerProxyProbeTimeout = 5 * time.Second
|
dockerProxyProbeTimeout = 5 * time.Second
|
||||||
@@ -302,7 +302,7 @@ func rewriteCreate(r *http.Request, job string, mounts map[string]string) error
|
|||||||
if config.Labels == nil {
|
if config.Labels == nil {
|
||||||
config.Labels = make(map[string]string)
|
config.Labels = make(map[string]string)
|
||||||
}
|
}
|
||||||
config.Labels[jobLabel] = job
|
config.Labels[jobLabel+job] = "" // per-job key, so nested proxies keep each other's
|
||||||
if fields["Labels"], err = json.Marshal(config.Labels); err != nil {
|
if fields["Labels"], err = json.Marshal(config.Labels); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -560,7 +560,7 @@ func RemoveDockerJobResources(ctx context.Context, job string) error {
|
|||||||
|
|
||||||
func removeLabelled(ctx context.Context, cli client.APIClient, job string) error {
|
func removeLabelled(ctx context.Context, cli client.APIClient, job string) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
filters := make(client.Filters).Add("label", jobLabel+"="+job)
|
filters := make(client.Filters).Add("label", jobLabel+job)
|
||||||
containers, err := cli.ContainerList(ctx, client.ContainerListOptions{All: true, Filters: filters})
|
containers, err := cli.ContainerList(ctx, client.ContainerListOptions{All: true, Filters: filters})
|
||||||
errs := []error{err}
|
errs := []error{err}
|
||||||
for _, c := range containers.Items {
|
for _, c := range containers.Items {
|
||||||
|
|||||||
@@ -146,52 +146,52 @@ func TestDockerProxy(t *testing.T) {
|
|||||||
t.Run("labels creates", func(t *testing.T) {
|
t.Run("labels creates", func(t *testing.T) {
|
||||||
for _, testCase := range []struct{ path, body, want string }{
|
for _, testCase := range []struct{ path, body, want string }{
|
||||||
{
|
{
|
||||||
"/v1.47/containers/create", `{"Image":"alpine","Unknown":{"enabled":true},"Labels":{"own":"1","com.gitea.runner.job":"other"}}`,
|
"/v1.47/containers/create", `{"Image":"alpine","Unknown":{"enabled":true},"Labels":{"own":"1","com.gitea.runner.job.job-0":""}}`,
|
||||||
`{"Image":"alpine","Unknown":{"enabled":true},"Labels":{"own":"1","com.gitea.runner.job":"job-1"}}`,
|
`{"Image":"alpine","Unknown":{"enabled":true},"Labels":{"own":"1","com.gitea.runner.job.job-0":"","com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/containers/create", `{"HostConfig":{"Binds":["/workspace/o/r:/src:ro","/workspace/o/r/../r/sub/:/sub","workspace/o/r:/relative","/workspace/o/rest:/rest","/workspace/o/r/tmp/x:/tmp","named:/named","/anonymous","/workspace/o/r:ro","/volumes/job/_data/x:/daemon-path"]},"DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"Foo":"1","foo":"2"}}`,
|
"/containers/create", `{"HostConfig":{"Binds":["/workspace/o/r:/src:ro","/workspace/o/r/../r/sub/:/sub","workspace/o/r:/relative","/workspace/o/rest:/rest","/workspace/o/r/tmp/x:/tmp","named:/named","/anonymous","/workspace/o/r:ro","/volumes/job/_data/x:/daemon-path"]},"DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"Foo":"1","foo":"2"}}`,
|
||||||
`{"HostConfig":{"Binds":["/volumes/job/_data:/src:ro","/volumes/job/_data/sub:/sub","workspace/o/r:/relative","/workspace/o/rest:/rest","/workspace/o/r/tmp/x:/tmp","named:/named","/anonymous","/workspace/o/r:ro","/volumes/job/_data/x:/daemon-path"]},"DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"Foo":"1","foo":"2","com.gitea.runner.job":"job-1"}}`,
|
`{"HostConfig":{"Binds":["/volumes/job/_data:/src:ro","/volumes/job/_data/sub:/sub","workspace/o/r:/relative","/workspace/o/rest:/rest","/workspace/o/r/tmp/x:/tmp","named:/named","/anonymous","/workspace/o/r:ro","/volumes/job/_data/x:/daemon-path"]},"DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"Foo":"1","foo":"2","com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/containers/create", `{"HostConfig":{"Mounts":[{"Type":"bind","Source":"/var/run/docker.sock","Target":"/var/run/docker.sock"},{"type":"bind","source":"/workspace/o/r/sub/","target":"/m"},{"Type":"bind","Source":"/workspace/o/r/sub/../data","Target":"/dotted"},{"Type":"bind","Source":"/workspace/o/r/../r","Target":"/escaping"},{"Type":"BIND","Source":"/workspace/o/r"},{"Type":"volume","Source":"/workspace/o/r"}]}}`,
|
"/containers/create", `{"HostConfig":{"Mounts":[{"Type":"bind","Source":"/var/run/docker.sock","Target":"/var/run/docker.sock"},{"type":"bind","source":"/workspace/o/r/sub/","target":"/m"},{"Type":"bind","Source":"/workspace/o/r/sub/../data","Target":"/dotted"},{"Type":"bind","Source":"/workspace/o/r/../r","Target":"/escaping"},{"Type":"BIND","Source":"/workspace/o/r"},{"Type":"volume","Source":"/workspace/o/r"}]}}`,
|
||||||
`{"HostConfig":{"Mounts":[{"Type":"bind","Source":"/tmp/p/docker.sock","Target":"/var/run/docker.sock"},{"Type":"bind","Source":"/volumes/job/_data/sub/","target":"/m"},{"Type":"bind","Source":"/volumes/job/_data/sub/../data","Target":"/dotted"},{"Type":"bind","Source":"/volumes/job/_data","Target":"/escaping"},{"Type":"BIND","Source":"/workspace/o/r"},{"Type":"volume","Source":"/workspace/o/r"}]},"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"HostConfig":{"Mounts":[{"Type":"bind","Source":"/tmp/p/docker.sock","Target":"/var/run/docker.sock"},{"Type":"bind","Source":"/volumes/job/_data/sub/","target":"/m"},{"Type":"bind","Source":"/volumes/job/_data/sub/../data","Target":"/dotted"},{"Type":"bind","Source":"/volumes/job/_data","Target":"/escaping"},{"Type":"BIND","Source":"/workspace/o/r"},{"Type":"volume","Source":"/workspace/o/r"}]},"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/containers/create", `{"HostConfig":{"Mounts":[{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind","device":"/workspace/o/r/data"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"unbindable","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind,remount","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Name":"plugin","Options":{"o":"bind","device":"/workspace/o/r"}}}}]}}`,
|
"/containers/create", `{"HostConfig":{"Mounts":[{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind","device":"/workspace/o/r/data"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"unbindable","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind,remount","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Name":"plugin","Options":{"o":"bind","device":"/workspace/o/r"}}}}]}}`,
|
||||||
`{"HostConfig":{"Mounts":[{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind","device":"/volumes/job/_data/data"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"unbindable","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind,remount","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Name":"plugin","Options":{"o":"bind","device":"/workspace/o/r"}}}}]},"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"HostConfig":{"Mounts":[{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind","device":"/volumes/job/_data/data"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"unbindable","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Options":{"o":"bind,remount","device":"/workspace/o/r"}}}},{"Type":"volume","VolumeOptions":{"DriverConfig":{"Name":"plugin","Options":{"o":"bind","device":"/workspace/o/r"}}}}]},"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/containers/create", `{"HostConfig":{"privileged":true,"Privileged":false,"Binds":["/workspace/o/r:/src"]},"hoſtconfig":null}`,
|
"/containers/create", `{"HostConfig":{"privileged":true,"Privileged":false,"Binds":["/workspace/o/r:/src"]},"hoſtconfig":null}`,
|
||||||
`{"HostConfig":{"privileged":true,"Privileged":false,"Binds":["/workspace/o/r:/src"]},"hoſtconfig":null,"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"HostConfig":{"privileged":true,"Privileged":false,"Binds":["/workspace/o/r:/src"]},"hoſtconfig":null,"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/networks/create", `{"Name":"n"}`,
|
"/networks/create", `{"Name":"n"}`,
|
||||||
`{"Name":"n","Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"Name":"n","Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", `{"Name":"v","labels":null,"DriverOpts":{"type":"none","o":"rbind,ro","device":"/workspace/o/r/"},"HostConfig":{"Binds":["/workspace/o/r:/src"]}}`,
|
"/volumes/create", `{"Name":"v","labels":null,"DriverOpts":{"type":"none","o":"rbind,ro","device":"/workspace/o/r/"},"HostConfig":{"Binds":["/workspace/o/r:/src"]}}`,
|
||||||
`{"Name":"v","Labels":{"com.gitea.runner.job":"job-1"},"DriverOpts":{"type":"none","o":"rbind,ro","device":"/volumes/job/_data/"},"HostConfig":{"Binds":["/workspace/o/r:/src"]}}`,
|
`{"Name":"v","Labels":{"com.gitea.runner.job.job-1":""},"DriverOpts":{"type":"none","o":"rbind,ro","device":"/volumes/job/_data/"},"HostConfig":{"Binds":["/workspace/o/r:/src"]}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", `{"Name":"d","DriverOpts":{"o":"bind","device":1,"device":"/workspace/o/r"}}`,
|
"/volumes/create", `{"Name":"d","DriverOpts":{"o":"bind","device":1,"device":"/workspace/o/r"}}`,
|
||||||
`{"Name":"d","DriverOpts":{"o":"bind","device":1,"device":"/workspace/o/r"},"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"Name":"d","DriverOpts":{"o":"bind","device":1,"device":"/workspace/o/r"},"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", `{"Name":"p","Driver":"plugin","DriverOpts":{"o":"bind","device":"/workspace/o/r"}}`,
|
"/volumes/create", `{"Name":"p","Driver":"plugin","DriverOpts":{"o":"bind","device":"/workspace/o/r"}}`,
|
||||||
`{"Name":"p","Driver":"plugin","DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"Name":"p","Driver":"plugin","DriverOpts":{"o":"bind","device":"/workspace/o/r"},"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", "",
|
"/volumes/create", "",
|
||||||
`{"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", "null",
|
"/volumes/create", "null",
|
||||||
`{"Labels":{"com.gitea.runner.job":"job-1"}}`,
|
`{"Labels":{"com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/volumes/create", `{"Labels":{"discard":"1"},"labels":null,"LABELS":{"own":"1"},"LABELS":{"extra":"2"}}`,
|
"/volumes/create", `{"Labels":{"discard":"1"},"labels":null,"LABELS":{"own":"1"},"LABELS":{"extra":"2"}}`,
|
||||||
`{"Labels":{"own":"1","extra":"2","com.gitea.runner.job":"job-1"}}`,
|
`{"Labels":{"own":"1","extra":"2","com.gitea.runner.job.job-1":""}}`,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
resp, err := client.Post("http://docker"+testCase.path, "application/json", strings.NewReader(testCase.body))
|
resp, err := client.Post("http://docker"+testCase.path, "application/json", strings.NewReader(testCase.body))
|
||||||
@@ -244,7 +244,7 @@ func TestDockerProxy(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, resp.Body.Close())
|
require.NoError(t, resp.Body.Close())
|
||||||
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
||||||
assert.JSONEq(t, `{"Labels":{"com.gitea.runner.job":"job-1"}}`, string(<-bodies))
|
assert.JSONEq(t, `{"Labels":{"com.gitea.runner.job.job-1":""}}`, string(<-bodies))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -276,7 +276,7 @@ func TestDockerProxy(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, resp.Body.Close())
|
require.NoError(t, resp.Body.Close())
|
||||||
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
||||||
assert.JSONEq(t, `{"Labels":{"com.gitea.runner.job":"job-1"}}`, string(<-bodies))
|
assert.JSONEq(t, `{"Labels":{"com.gitea.runner.job.job-1":""}}`, string(<-bodies))
|
||||||
require.NoError(t, <-closed)
|
require.NoError(t, <-closed)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -285,8 +285,8 @@ func TestRemoveLabelledRemovesContainersNetworksAndVolumes(t *testing.T) {
|
|||||||
containerFailure := errors.New("container removal failed")
|
containerFailure := errors.New("container removal failed")
|
||||||
listFailure := errors.New("listing failed")
|
listFailure := errors.New("listing failed")
|
||||||
volumeFailure := errors.New("volume removal failed")
|
volumeFailure := errors.New("volume removal failed")
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
filters := make(mobyclient.Filters).Add("label", jobLabel+"=job-1")
|
filters := make(mobyclient.Filters).Add("label", jobLabel+"job-1")
|
||||||
cli := &mockDockerClient{}
|
cli := &mockDockerClient{}
|
||||||
cli.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true, Filters: filters}).
|
cli.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true, Filters: filters}).
|
||||||
Return(mobyclient.ContainerListResult{Items: []container.Summary{{ID: "c1", Names: []string{"/app"}}}}, nil).Once()
|
Return(mobyclient.ContainerListResult{Items: []container.Summary{{ID: "c1", Names: []string{"/app"}}}}, nil).Once()
|
||||||
@@ -312,7 +312,8 @@ func TestRemoveLabelledRemovesContainersNetworksAndVolumes(t *testing.T) {
|
|||||||
|
|
||||||
func TestDockerProxyWithDaemon(t *testing.T) {
|
func TestDockerProxyWithDaemon(t *testing.T) {
|
||||||
requireDocker(t)
|
requireDocker(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
cleanupCtx := context.WithoutCancel(ctx)
|
||||||
require.NoError(t, NewDockerPullExecutor(NewDockerPullExecutorInput{Image: "alpine"})(ctx))
|
require.NoError(t, NewDockerPullExecutor(NewDockerPullExecutorInput{Image: "alpine"})(ctx))
|
||||||
direct, err := GetDockerClient(ctx)
|
direct, err := GetDockerClient(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -337,22 +338,24 @@ func TestDockerProxyWithDaemon(t *testing.T) {
|
|||||||
Config: &container.Config{Image: "alpine", Cmd: []string{"sleep", "300"}, Labels: map[string]string{"own": "1"}},
|
Config: &container.Config{Image: "alpine", Cmd: []string{"sleep", "300"}, Labels: map[string]string{"own": "1"}},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(func() { _, _ = direct.ContainerRemove(ctx, created.ID, mobyclient.ContainerRemoveOptions{Force: true}) })
|
t.Cleanup(func() {
|
||||||
|
_, _ = direct.ContainerRemove(cleanupCtx, created.ID, mobyclient.ContainerRemoveOptions{Force: true})
|
||||||
|
})
|
||||||
_, err = viaProxy.ContainerStart(ctx, created.ID, mobyclient.ContainerStartOptions{})
|
_, err = viaProxy.ContainerStart(ctx, created.ID, mobyclient.ContainerStartOptions{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
net, err := viaProxy.NetworkCreate(ctx, job, mobyclient.NetworkCreateOptions{})
|
net, err := viaProxy.NetworkCreate(ctx, job, mobyclient.NetworkCreateOptions{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(func() { _, _ = direct.NetworkRemove(ctx, net.ID, mobyclient.NetworkRemoveOptions{}) })
|
t.Cleanup(func() { _, _ = direct.NetworkRemove(cleanupCtx, net.ID, mobyclient.NetworkRemoveOptions{}) })
|
||||||
_, err = viaProxy.VolumeCreate(ctx, mobyclient.VolumeCreateOptions{Name: job})
|
_, err = viaProxy.VolumeCreate(ctx, mobyclient.VolumeCreateOptions{Name: job})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(func() { _, _ = direct.VolumeRemove(ctx, job, mobyclient.VolumeRemoveOptions{Force: true}) })
|
t.Cleanup(func() { _, _ = direct.VolumeRemove(cleanupCtx, job, mobyclient.VolumeRemoveOptions{Force: true}) })
|
||||||
|
|
||||||
inspected, err := direct.ContainerInspect(ctx, created.ID, mobyclient.ContainerInspectOptions{})
|
inspected, err := direct.ContainerInspect(ctx, created.ID, mobyclient.ContainerInspectOptions{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, map[string]string{"own": "1", jobLabel: job}, inspected.Container.Config.Labels)
|
assert.Subset(t, inspected.Container.Config.Labels, map[string]string{"own": "1", jobLabel + job: ""})
|
||||||
vol, err := direct.VolumeInspect(ctx, job, mobyclient.VolumeInspectOptions{})
|
vol, err := direct.VolumeInspect(ctx, job, mobyclient.VolumeInspectOptions{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, job, vol.Volume.Labels[jobLabel])
|
assert.Contains(t, vol.Volume.Labels, jobLabel+job)
|
||||||
|
|
||||||
exec, err := viaProxy.ExecCreate(ctx, created.ID, mobyclient.ExecCreateOptions{Cmd: []string{"cat"}, AttachStdin: true, AttachStdout: true, TTY: true})
|
exec, err := viaProxy.ExecCreate(ctx, created.ID, mobyclient.ExecCreateOptions{Cmd: []string{"cat"}, AttachStdin: true, AttachStdout: true, TTY: true})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -5,20 +5,20 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/config"
|
"github.com/docker/cli/cli/config"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCleanImage(t *testing.T) {
|
func TestCleanImage(t *testing.T) {
|
||||||
@@ -36,13 +36,13 @@ func TestCleanImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
imageOut := cleanImage(context.Background(), table.imageIn)
|
imageOut := cleanImage(t.Context(), table.imageIn)
|
||||||
assert.Equal(t, table.imageOut, imageOut)
|
assert.Equal(t, table.imageOut, imageOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetImagePullOptions(t *testing.T) {
|
func TestGetImagePullOptions(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
orig := config.Dir()
|
orig := config.Dir()
|
||||||
t.Cleanup(func() { config.SetDir(orig) })
|
t.Cleanup(func() { config.SetDir(orig) })
|
||||||
@@ -79,7 +79,7 @@ func TestIsPinnedImage(t *testing.T) {
|
|||||||
// The pull path reports a failure the daemon sent mid-stream, so it must carry the reason
|
// The pull path reports a failure the daemon sent mid-stream, so it must carry the reason
|
||||||
// whichever of the two shapes the daemon used.
|
// whichever of the two shapes the daemon used.
|
||||||
func TestLogDockerResponseError(t *testing.T) {
|
func TestLogDockerResponseError(t *testing.T) {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
streamErr := func(line string) error {
|
streamErr := func(line string) error {
|
||||||
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), false)
|
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), false)
|
||||||
}
|
}
|
||||||
|
|||||||
+107
-135
@@ -8,6 +8,7 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -24,13 +25,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"dario.cat/mergo"
|
"dario.cat/mergo"
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
cerrdefs "github.com/containerd/errdefs"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/docker/cli/cli/compose/loader"
|
"github.com/docker/cli/cli/compose/loader"
|
||||||
"github.com/docker/cli/cli/connhelper"
|
"github.com/docker/cli/cli/connhelper"
|
||||||
"github.com/joho/godotenv"
|
"github.com/docker/cli/opts"
|
||||||
"github.com/moby/moby/api/pkg/stdcopy"
|
"github.com/moby/moby/api/pkg/stdcopy"
|
||||||
"github.com/moby/moby/api/types/container"
|
"github.com/moby/moby/api/types/container"
|
||||||
"github.com/moby/moby/api/types/mount"
|
"github.com/moby/moby/api/types/mount"
|
||||||
@@ -39,7 +41,6 @@ import (
|
|||||||
"github.com/moby/moby/client"
|
"github.com/moby/moby/client"
|
||||||
"github.com/moby/moby/client/pkg/versions"
|
"github.com/moby/moby/client/pkg/versions"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// drainGracePeriod bounds how long we wait for an output-copy goroutine to
|
// drainGracePeriod bounds how long we wait for an output-copy goroutine to
|
||||||
@@ -94,8 +95,8 @@ func (cr *containerReference) Start(attach bool) common.Executor {
|
|||||||
cr.attach().IfBool(attach),
|
cr.attach().IfBool(attach),
|
||||||
cr.start(),
|
cr.start(),
|
||||||
cr.wait().IfBool(attach),
|
cr.wait().IfBool(attach),
|
||||||
cr.tryReadUID(),
|
cr.tryReadID("-u", func(id int) { cr.UID = id }),
|
||||||
cr.tryReadGID(),
|
cr.tryReadID("-g", func(id int) { cr.GID = id }),
|
||||||
func(ctx context.Context) error {
|
func(ctx context.Context) error {
|
||||||
// If this fails, then folders have wrong permissions on non root container
|
// If this fails, then folders have wrong permissions on non root container
|
||||||
if cr.UID != 0 || cr.GID != 0 {
|
if cr.UID != 0 || cr.GID != 0 {
|
||||||
@@ -304,7 +305,25 @@ func (cr *containerReference) UpdateFromEnv(srcPath string, env *map[string]stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cr *containerReference) UpdateFromImageEnv(env *map[string]string) common.Executor {
|
func (cr *containerReference) UpdateFromImageEnv(env *map[string]string) common.Executor {
|
||||||
return cr.extractFromImageEnv(env).IfNot(common.Dryrun)
|
envMap := *env
|
||||||
|
return common.Executor(func(ctx context.Context) error {
|
||||||
|
inspect, err := cr.cli.ContainerInspect(ctx, cr.id, client.ContainerInspectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
common.Logger(ctx).Error(err)
|
||||||
|
return fmt.Errorf("inspect container: %w", err)
|
||||||
|
}
|
||||||
|
if inspect.Container.Config == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for key, value := range opts.ConvertKVStringsToMap(inspect.Container.Config.Env) {
|
||||||
|
if envMap[key] == "" {
|
||||||
|
envMap[key] = value
|
||||||
|
} else if key == "PATH" {
|
||||||
|
envMap[key] += ":" + value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}).IfNot(common.Dryrun)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *containerReference) Exec(command []string, env map[string]string, user, workdir string) common.Executor {
|
func (cr *containerReference) Exec(command []string, env map[string]string, user, workdir string) common.Executor {
|
||||||
@@ -566,20 +585,6 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
|
|||||||
return nil, nil, fmt.Errorf("cannot process container options: '%s': '%w'", options, err)
|
return nil, nil, fmt.Errorf("cannot process container options: '%s': '%w'", options, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: If everything is fine after gitea/act v0.260.0, remove the following comment.
|
|
||||||
// In the old fork version, the code is
|
|
||||||
// if len(copts.netMode.Value()) == 0 {
|
|
||||||
// if err = copts.netMode.Set("host"); err != nil {
|
|
||||||
// return nil, nil, fmt.Errorf("cannot parse networkmode=host. This is an internal error and should not happen: '%w'", err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// And it has been commented with:
|
|
||||||
// If a service container's network is set to `host`, the container will not be able to
|
|
||||||
// connect to the specified network created for the job container and the service containers.
|
|
||||||
// So comment out the following code.
|
|
||||||
// Not the if it's necessary to comment it in the new version,
|
|
||||||
// since it's cr.input.NetworkMode now.
|
|
||||||
|
|
||||||
if len(copts.netMode.Value()) == 0 {
|
if len(copts.netMode.Value()) == 0 {
|
||||||
if err = copts.netMode.Set(cr.input.NetworkMode); err != nil {
|
if err = copts.netMode.Set(cr.input.NetworkMode); err != nil {
|
||||||
return nil, nil, fmt.Errorf("cannot parse networkmode=%s. This is an internal error and should not happen: '%w'", cr.input.NetworkMode, err)
|
return nil, nil, fmt.Errorf("cannot parse networkmode=%s. This is an internal error and should not happen: '%w'", cr.input.NetworkMode, err)
|
||||||
@@ -643,6 +648,9 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
|
|||||||
if flags.Changed("network") || flags.Changed("net") {
|
if flags.Changed("network") || flags.Changed("net") {
|
||||||
logger.Warn("--network and --net in the options will be ignored.")
|
logger.Warn("--network and --net in the options will be ignored.")
|
||||||
}
|
}
|
||||||
|
if len(cr.input.Entrypoint) != 0 && flags.Changed("entrypoint") {
|
||||||
|
logger.Warn("--entrypoint in the options will be ignored.")
|
||||||
|
}
|
||||||
hostConfig.NetworkMode = networkMode
|
hostConfig.NetworkMode = networkMode
|
||||||
logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig)
|
logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig)
|
||||||
|
|
||||||
@@ -672,17 +680,11 @@ func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
|
|||||||
ExposedPorts: exposedPorts,
|
ExposedPorts: exposedPorts,
|
||||||
Tty: input.AllocatePTY,
|
Tty: input.AllocatePTY,
|
||||||
}
|
}
|
||||||
// For Gitea, reduce log noise
|
|
||||||
// logger.Debugf("Common container.Config ==> %+v", config)
|
|
||||||
|
|
||||||
if len(input.Cmd) != 0 {
|
if len(input.Cmd) != 0 {
|
||||||
config.Cmd = input.Cmd
|
config.Cmd = input.Cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(input.Entrypoint) != 0 {
|
|
||||||
config.Entrypoint = input.Entrypoint
|
|
||||||
}
|
|
||||||
|
|
||||||
mounts := make([]mount.Mount, 0)
|
mounts := make([]mount.Mount, 0)
|
||||||
for mountSource, mountTarget := range input.Mounts {
|
for mountSource, mountTarget := range input.Mounts {
|
||||||
mounts = append(mounts, mount.Mount{
|
mounts = append(mounts, mount.Mount{
|
||||||
@@ -717,20 +719,20 @@ func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
|
|||||||
PortBindings: portBindings,
|
PortBindings: portBindings,
|
||||||
AutoRemove: input.AutoRemove,
|
AutoRemove: input.AutoRemove,
|
||||||
}
|
}
|
||||||
// For Gitea, reduce log noise
|
|
||||||
// logger.Debugf("Common container.HostConfig ==> %+v", hostConfig)
|
|
||||||
|
|
||||||
config, hostConfig, err = cr.mergeContainerConfigs(ctx, config, hostConfig)
|
config, hostConfig, err = cr.mergeContainerConfigs(ctx, config, hostConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(input.Entrypoint) != 0 {
|
||||||
|
config.Entrypoint = input.Entrypoint
|
||||||
|
}
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
config, hostConfig = cr.sanitizeConfig(ctx, config, hostConfig)
|
config, hostConfig = cr.sanitizeConfig(ctx, config, hostConfig)
|
||||||
|
|
||||||
var networkingConfig *network.NetworkingConfig
|
var networkingConfig *network.NetworkingConfig
|
||||||
// For Gitea, reduce log noise
|
|
||||||
// logger.Debugf("input.NetworkAliases ==> %v", input.NetworkAliases)
|
|
||||||
n := hostConfig.NetworkMode
|
n := hostConfig.NetworkMode
|
||||||
// IsUserDefined and IsHost are broken on windows
|
// IsUserDefined and IsHost are broken on windows
|
||||||
if n.IsUserDefined() && n != "host" && len(input.NetworkAliases) > 0 {
|
if n.IsUserDefined() && n != "host" && len(input.NetworkAliases) > 0 {
|
||||||
@@ -763,44 +765,6 @@ func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *containerReference) extractFromImageEnv(env *map[string]string) common.Executor {
|
|
||||||
envMap := *env
|
|
||||||
return func(ctx context.Context) error {
|
|
||||||
logger := common.Logger(ctx)
|
|
||||||
|
|
||||||
inspect, err := cr.cli.ImageInspect(ctx, cr.input.Image)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error(err)
|
|
||||||
return fmt.Errorf("inspect image: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if inspect.Config == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
imageEnv, err := godotenv.Unmarshal(strings.Join(inspect.Config.Env, "\n"))
|
|
||||||
if err != nil {
|
|
||||||
logger.Error(err)
|
|
||||||
return fmt.Errorf("unmarshal image env: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range imageEnv {
|
|
||||||
if k == "PATH" {
|
|
||||||
if envMap[k] == "" {
|
|
||||||
envMap[k] = v
|
|
||||||
} else {
|
|
||||||
envMap[k] += `:` + v
|
|
||||||
}
|
|
||||||
} else if envMap[k] == "" {
|
|
||||||
envMap[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
env = &envMap
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cr *containerReference) exec(cmd []string, env map[string]string, user, workdir string) common.Executor {
|
func (cr *containerReference) exec(cmd []string, env map[string]string, user, workdir string) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if cr.id == "" {
|
if cr.id == "" {
|
||||||
@@ -818,11 +782,6 @@ func (cr *containerReference) exec(cmd []string, env map[string]string, user, wo
|
|||||||
|
|
||||||
logger.Debugf("Exec command '%s'", cmd)
|
logger.Debugf("Exec command '%s'", cmd)
|
||||||
isTerminal := cr.input.AllocatePTY
|
isTerminal := cr.input.AllocatePTY
|
||||||
envList := make([]string, 0)
|
|
||||||
for k, v := range env {
|
|
||||||
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
var wd string
|
var wd string
|
||||||
if workdir != "" {
|
if workdir != "" {
|
||||||
if strings.HasPrefix(workdir, "/") {
|
if strings.HasPrefix(workdir, "/") {
|
||||||
@@ -839,7 +798,7 @@ func (cr *containerReference) exec(cmd []string, env map[string]string, user, wo
|
|||||||
User: user,
|
User: user,
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
WorkingDir: wd,
|
WorkingDir: wd,
|
||||||
Env: envList,
|
Env: getEnvListFromMap(env),
|
||||||
TTY: isTerminal,
|
TTY: isTerminal,
|
||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
@@ -856,7 +815,7 @@ func (cr *containerReference) exec(cmd []string, env map[string]string, user, wo
|
|||||||
}
|
}
|
||||||
defer resp.Close()
|
defer resp.Close()
|
||||||
|
|
||||||
err = cr.waitForCommand(ctx, resp.HijackedResponse, idResp, user, workdir)
|
err = cr.waitForCommand(ctx, resp.HijackedResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -906,15 +865,7 @@ func (cr *containerReference) tryReadID(opt string, cbk func(id int)) common.Exe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *containerReference) tryReadUID() common.Executor {
|
func (cr *containerReference) waitForCommand(ctx context.Context, resp client.HijackedResponse) error {
|
||||||
return cr.tryReadID("-u", func(id int) { cr.UID = id })
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cr *containerReference) tryReadGID() common.Executor {
|
|
||||||
return cr.tryReadID("-g", func(id int) { cr.GID = id })
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cr *containerReference) waitForCommand(ctx context.Context, resp client.HijackedResponse, _ client.ExecCreateResult, _, _ string) error {
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
// Buffered so the copy goroutine never blocks on send if the grace-period
|
// Buffered so the copy goroutine never blocks on send if the grace-period
|
||||||
@@ -956,6 +907,7 @@ func (cr *containerReference) waitForCommand(ctx context.Context, resp client.Hi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, skipGitDir bool) common.Executor {
|
func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, skipGitDir bool) common.Executor {
|
||||||
|
dstPath = path.Clean(dstPath)
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if cr.id == "" {
|
if cr.id == "" {
|
||||||
return cr.missingContainerError("copy directory to %s", dstPath)
|
return cr.missingContainerError("copy directory to %s", dstPath)
|
||||||
@@ -977,7 +929,11 @@ func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, ski
|
|||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
}
|
}
|
||||||
}(tarFile)
|
}(tarFile)
|
||||||
if err := writeDirTar(ctx, tarFile, dstPath, srcPath, useGitIgnore, skipGitDir, cr.UID, cr.GID); err != nil {
|
extractionRoot := "/"
|
||||||
|
if actPath := cr.GetActPath(); strings.HasPrefix(dstPath+"/", actPath+"/") {
|
||||||
|
extractionRoot = actPath // keeps tar entries from crossing the image's /var/run symlink
|
||||||
|
}
|
||||||
|
if err := writeDirTar(ctx, tarFile, path.Join("/", strings.TrimPrefix(dstPath, extractionRoot)), srcPath, useGitIgnore, skipGitDir, cr.UID, cr.GID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -986,14 +942,7 @@ func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, ski
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to seek tar archive: %w", err)
|
return fmt.Errorf("failed to seek tar archive: %w", err)
|
||||||
}
|
}
|
||||||
_, err = cr.cli.CopyToContainer(ctx, cr.id, client.CopyToContainerOptions{
|
return cr.upload(ctx, extractionRoot, tarFile)
|
||||||
DestinationPath: "/",
|
|
||||||
Content: tarFile,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to copy content to container: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1008,17 +957,17 @@ func (cr *containerReference) copyContent(dstPath string, files ...*FileEntry) c
|
|||||||
}
|
}
|
||||||
|
|
||||||
common.Logger(ctx).Debugf("Extracting content to '%s'", dstPath)
|
common.Logger(ctx).Debugf("Extracting content to '%s'", dstPath)
|
||||||
_, err := cr.cli.CopyToContainer(ctx, cr.id, client.CopyToContainerOptions{
|
return cr.upload(ctx, dstPath, &buf)
|
||||||
DestinationPath: dstPath,
|
|
||||||
Content: &buf,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to copy content to container: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cr *containerReference) upload(ctx context.Context, dstPath string, content io.Reader) error {
|
||||||
|
if _, err := cr.cli.CopyToContainer(ctx, cr.id, client.CopyToContainerOptions{DestinationPath: dstPath, Content: content}); err != nil {
|
||||||
|
return fmt.Errorf("failed to copy content to container: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (cr *containerReference) attach() common.Executor {
|
func (cr *containerReference) attach() common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
out, err := cr.cli.ContainerAttach(ctx, cr.id, client.ContainerAttachOptions{
|
out, err := cr.cli.ContainerAttach(ctx, cr.id, client.ContainerAttachOptions{
|
||||||
@@ -1097,7 +1046,7 @@ func (cr *containerReference) wait() common.Executor {
|
|||||||
// sanitizeOptionsHostConfig takes back everything a workflow could escape the container with,
|
// sanitizeOptionsHostConfig takes back everything a workflow could escape the container with,
|
||||||
// setting each field to trusted, which is what the runner's own options parse to on their own.
|
// setting each field to trusted, which is what the runner's own options parse to on their own.
|
||||||
// Only for unprivileged mode, since privileged mode grants host access anyway.
|
// Only for unprivileged mode, since privileged mode grants host access anyway.
|
||||||
func sanitizeOptionsHostConfig(logger logrus.FieldLogger, hostConfig, trusted *container.HostConfig) {
|
func sanitizeOptionsHostConfig(logger *logging.Logger, hostConfig, trusted *container.HostConfig) {
|
||||||
resetOption(logger, "--pid", &hostConfig.PidMode, trusted.PidMode)
|
resetOption(logger, "--pid", &hostConfig.PidMode, trusted.PidMode)
|
||||||
resetOption(logger, "--ipc", &hostConfig.IpcMode, trusted.IpcMode)
|
resetOption(logger, "--ipc", &hostConfig.IpcMode, trusted.IpcMode)
|
||||||
resetOption(logger, "--uts", &hostConfig.UTSMode, trusted.UTSMode)
|
resetOption(logger, "--uts", &hostConfig.UTSMode, trusted.UTSMode)
|
||||||
@@ -1117,6 +1066,10 @@ func sanitizeOptionsHostConfig(logger logrus.FieldLogger, hostConfig, trusted *c
|
|||||||
// systempaths=unconfined lands in these two rather than in SecurityOpt
|
// systempaths=unconfined lands in these two rather than in SecurityOpt
|
||||||
resetOption(logger, "--security-opt", &hostConfig.MaskedPaths, trusted.MaskedPaths)
|
resetOption(logger, "--security-opt", &hostConfig.MaskedPaths, trusted.MaskedPaths)
|
||||||
resetOption(logger, "--security-opt", &hostConfig.ReadonlyPaths, trusted.ReadonlyPaths)
|
resetOption(logger, "--security-opt", &hostConfig.ReadonlyPaths, trusted.ReadonlyPaths)
|
||||||
|
resetOption(logger, "--log-driver", &hostConfig.LogConfig.Type, trusted.LogConfig.Type) // drivers run in the daemon, egressing from its network
|
||||||
|
resetOption(logger, "--log-opt", &hostConfig.LogConfig.Config, trusted.LogConfig.Config)
|
||||||
|
resetOption(logger, "--oom-score-adj", &hostConfig.OomScoreAdj, trusted.OomScoreAdj) // -1000 exempts it from the host OOM killer
|
||||||
|
resetOption(logger, "--oom-kill-disable", &hostConfig.OomKillDisable, trusted.OomKillDisable)
|
||||||
|
|
||||||
// a driver mounts what it likes, e.g. local with device= binds any host path, which
|
// a driver mounts what it likes, e.g. local with device= binds any host path, which
|
||||||
// valid_volumes never gets to see
|
// valid_volumes never gets to see
|
||||||
@@ -1132,7 +1085,7 @@ func sanitizeOptionsHostConfig(logger logrus.FieldLogger, hostConfig, trusted *c
|
|||||||
|
|
||||||
// resetOption puts a field back to the runner's own value. It compares the values rather than
|
// resetOption puts a field back to the runner's own value. It compares the values rather than
|
||||||
// the flags, so a field that more than one option feeds cannot slip through.
|
// the flags, so a field that more than one option feeds cannot slip through.
|
||||||
func resetOption[T any](logger logrus.FieldLogger, option string, field *T, trusted T) {
|
func resetOption[T any](logger *logging.Logger, option string, field *T, trusted T) {
|
||||||
if reflect.DeepEqual(*field, trusted) {
|
if reflect.DeepEqual(*field, trusted) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1158,9 +1111,9 @@ func parseOptionsHostConfig(options string) (*container.HostConfig, error) {
|
|||||||
// sanitizeConfig remove the invalid configurations from `config` and `hostConfig`
|
// sanitizeConfig remove the invalid configurations from `config` and `hostConfig`
|
||||||
func (cr *containerReference) sanitizeConfig(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) (*container.Config, *container.HostConfig) {
|
func (cr *containerReference) sanitizeConfig(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) (*container.Config, *container.HostConfig) {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
|
matcher := newValidVolumeMatcher(ctx, cr.input.ValidVolumes)
|
||||||
|
|
||||||
if len(cr.input.ValidVolumes) > 0 {
|
if len(cr.input.ValidVolumes) > 0 {
|
||||||
matcher := newValidVolumeMatcher(ctx, cr.input.ValidVolumes)
|
|
||||||
// sanitize binds
|
// sanitize binds
|
||||||
sanitizedBinds := make([]string, 0, len(hostConfig.Binds))
|
sanitizedBinds := make([]string, 0, len(hostConfig.Binds))
|
||||||
for _, bind := range hostConfig.Binds {
|
for _, bind := range hostConfig.Binds {
|
||||||
@@ -1174,33 +1127,37 @@ func (cr *containerReference) sanitizeConfig(ctx context.Context, config *contai
|
|||||||
sanitizedBinds = append(sanitizedBinds, bind)
|
sanitizedBinds = append(sanitizedBinds, bind)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if matcher.isValid(parsed.Source, mount.Type(parsed.Type)) {
|
if matcher.isValid(parsed.Source, mount.Type(parsed.Type), parsed.ReadOnly) {
|
||||||
|
if matcher.needsNoCopy(parsed.Source, mount.Type(parsed.Type)) && (parsed.Volume == nil || !parsed.Volume.NoCopy) {
|
||||||
|
bind += ",nocopy"
|
||||||
|
}
|
||||||
sanitizedBinds = append(sanitizedBinds, bind)
|
sanitizedBinds = append(sanitizedBinds, bind)
|
||||||
} else {
|
} else {
|
||||||
logger.Warnf("[%s] is not a valid volume, will be ignored", parsed.Source)
|
logger.Warnf("[%s] is not a valid volume, will be ignored", parsed.Source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hostConfig.Binds = sanitizedBinds
|
hostConfig.Binds = sanitizedBinds
|
||||||
// sanitize mounts
|
|
||||||
sanitizedMounts := make([]mount.Mount, 0, len(hostConfig.Mounts))
|
|
||||||
for _, mt := range hostConfig.Mounts {
|
|
||||||
if matcher.isValid(mt.Source, mt.Type) {
|
|
||||||
sanitizedMounts = append(sanitizedMounts, mt)
|
|
||||||
} else {
|
|
||||||
logger.Warnf("[%s] is not a valid volume, will be ignored", mt.Source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hostConfig.Mounts = sanitizedMounts
|
|
||||||
} else {
|
} else {
|
||||||
for _, bind := range hostConfig.Binds {
|
for _, bind := range hostConfig.Binds {
|
||||||
logger.Warnf("[%s] is not a valid volume, will be ignored", bind)
|
logger.Warnf("[%s] is not a valid volume, will be ignored", bind)
|
||||||
}
|
}
|
||||||
for _, mt := range hostConfig.Mounts {
|
hostConfig.Binds = []string{}
|
||||||
|
}
|
||||||
|
// sanitize mounts
|
||||||
|
sanitizedMounts := make([]mount.Mount, 0, len(hostConfig.Mounts))
|
||||||
|
for _, mt := range hostConfig.Mounts {
|
||||||
|
readOnly := mt.ReadOnly && (mt.BindOptions == nil || !mt.BindOptions.ReadOnlyNonRecursive) // bind-recursive=writable leaves submounts writable
|
||||||
|
if matcher.isValid(mt.Source, mt.Type, readOnly) {
|
||||||
|
if matcher.needsNoCopy(mt.Source, mt.Type) {
|
||||||
|
mt.VolumeOptions = cmp.Or(mt.VolumeOptions, &mount.VolumeOptions{})
|
||||||
|
mt.VolumeOptions.NoCopy = true
|
||||||
|
}
|
||||||
|
sanitizedMounts = append(sanitizedMounts, mt)
|
||||||
|
} else {
|
||||||
logger.Warnf("[%s] is not a valid volume, will be ignored", mt.Source)
|
logger.Warnf("[%s] is not a valid volume, will be ignored", mt.Source)
|
||||||
}
|
}
|
||||||
hostConfig.Binds = []string{}
|
|
||||||
hostConfig.Mounts = []mount.Mount{}
|
|
||||||
}
|
}
|
||||||
|
hostConfig.Mounts = sanitizedMounts
|
||||||
|
|
||||||
return config, hostConfig
|
return config, hostConfig
|
||||||
}
|
}
|
||||||
@@ -1240,26 +1197,37 @@ func overlayVolumes(dst, src *container.HostConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type validVolumeMatcher struct {
|
type validVolumeMatcher struct {
|
||||||
allowAll bool
|
allowAll bool
|
||||||
named []string
|
allowAllReadOnly bool
|
||||||
host []string
|
named []validVolumeRule
|
||||||
|
host []validVolumeRule
|
||||||
|
}
|
||||||
|
|
||||||
|
type validVolumeRule struct {
|
||||||
|
pattern string
|
||||||
|
readOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newValidVolumeMatcher(ctx context.Context, validVolumes []string) validVolumeMatcher {
|
func newValidVolumeMatcher(ctx context.Context, validVolumes []string) validVolumeMatcher {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
ret := validVolumeMatcher{
|
ret := validVolumeMatcher{
|
||||||
named: make([]string, 0, len(validVolumes)),
|
named: make([]validVolumeRule, 0, len(validVolumes)),
|
||||||
host: make([]string, 0, len(validVolumes)),
|
host: make([]validVolumeRule, 0, len(validVolumes)),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range validVolumes {
|
for _, v := range validVolumes {
|
||||||
|
v, readOnly := strings.CutSuffix(v, ":ro")
|
||||||
if v == "**" {
|
if v == "**" {
|
||||||
ret.allowAll = true
|
if readOnly {
|
||||||
|
ret.allowAllReadOnly = true
|
||||||
|
} else {
|
||||||
|
ret.allowAll = true
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !isHostVolumePattern(v) {
|
if !isHostVolumePattern(v) {
|
||||||
if doublestar.ValidatePattern(v) {
|
if doublestar.ValidatePattern(v) {
|
||||||
ret.named = append(ret.named, v)
|
ret.named = append(ret.named, validVolumeRule{pattern: v, readOnly: readOnly})
|
||||||
} else {
|
} else {
|
||||||
logger.Errorf("invalid volume pattern %s", v)
|
logger.Errorf("invalid volume pattern %s", v)
|
||||||
}
|
}
|
||||||
@@ -1271,7 +1239,7 @@ func newValidVolumeMatcher(ctx context.Context, validVolumes []string) validVolu
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if doublestar.ValidatePathPattern(normalized) {
|
if doublestar.ValidatePathPattern(normalized) {
|
||||||
ret.host = append(ret.host, normalized)
|
ret.host = append(ret.host, validVolumeRule{pattern: normalized, readOnly: readOnly})
|
||||||
} else {
|
} else {
|
||||||
logger.Errorf("invalid volume pattern %s", normalized)
|
logger.Errorf("invalid volume pattern %s", normalized)
|
||||||
}
|
}
|
||||||
@@ -1280,30 +1248,34 @@ func newValidVolumeMatcher(ctx context.Context, validVolumes []string) validVolu
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m validVolumeMatcher) isValid(source string, sourceType mount.Type) bool {
|
func (m validVolumeMatcher) isValid(source string, sourceType mount.Type, readOnly bool) bool {
|
||||||
if m.allowAll {
|
if m.allowAll || (m.allowAllReadOnly && readOnly) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if sourceType == mount.TypeTmpfs && source == "" { // backed by memory, exposes nothing from the host, same as --tmpfs
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
rules, match := m.named, doublestar.MatchUnvalidated
|
||||||
if isHostVolumeSource(source, sourceType) {
|
if isHostVolumeSource(source, sourceType) {
|
||||||
normalized, err := normalizeHostVolumePath(source)
|
normalized, err := normalizeHostVolumePath(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, pattern := range m.host {
|
rules, match, source = m.host, doublestar.PathMatchUnvalidated, normalized
|
||||||
if doublestar.PathMatchUnvalidated(pattern, normalized) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
for _, pattern := range m.named {
|
for _, rule := range rules {
|
||||||
if doublestar.MatchUnvalidated(pattern, source) {
|
if (readOnly || !rule.readOnly) && match(rule.pattern, source) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// needsNoCopy reports a volume only `:ro` rules allow, as dockerd fills an empty volume from the image even when mounted read-only.
|
||||||
|
func (m validVolumeMatcher) needsNoCopy(source string, sourceType mount.Type) bool {
|
||||||
|
return sourceType == mount.TypeVolume && !m.isValid(source, sourceType, false)
|
||||||
|
}
|
||||||
|
|
||||||
func isHostVolumePattern(pattern string) bool {
|
func isHostVolumePattern(pattern string) bool {
|
||||||
return filepath.IsAbs(pattern) ||
|
return filepath.IsAbs(pattern) ||
|
||||||
strings.HasPrefix(pattern, "."+string(filepath.Separator)) ||
|
strings.HasPrefix(pattern, "."+string(filepath.Separator)) ||
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
@@ -18,6 +19,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
cerrdefs "github.com/containerd/errdefs"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/moby/moby/api/pkg/stdcopy"
|
"github.com/moby/moby/api/pkg/stdcopy"
|
||||||
@@ -25,50 +28,33 @@ import (
|
|||||||
"github.com/moby/moby/api/types/mount"
|
"github.com/moby/moby/api/types/mount"
|
||||||
"github.com/moby/moby/api/types/network"
|
"github.com/moby/moby/api/types/network"
|
||||||
mobyclient "github.com/moby/moby/client"
|
mobyclient "github.com/moby/moby/client"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDocker(t *testing.T) {
|
func TestDockerEnvironmentUsesContainerPath(t *testing.T) {
|
||||||
requireDocker(t)
|
client := &mockDockerClient{}
|
||||||
ctx := context.Background()
|
client.On("ContainerInspect", mock.Anything, "amd64-job", mobyclient.ContainerInspectOptions{}).
|
||||||
client, err := GetDockerClient(ctx)
|
Return(mobyclient.ContainerInspectResult{Container: container.InspectResponse{Config: &container.Config{Env: []string{
|
||||||
require.NoError(t, err)
|
"PATH=/opt/node/x64/bin:/usr/bin:/bin", "CONFLICT_VAR=container-value", "SOME_RANDOM_VAR=",
|
||||||
defer client.Close()
|
"ANOTHER_ONE=old-value", "ANOTHER_ONE=BUT_I_HAVE_VALUE", "LITERAL='quoted'=$HOME\nsecond line",
|
||||||
|
}}}}, nil)
|
||||||
dockerBuild := NewDockerBuildExecutor(NewDockerBuildExecutorInput{
|
|
||||||
ContextDir: "testdata",
|
|
||||||
ImageTag: "envmergetest",
|
|
||||||
})
|
|
||||||
|
|
||||||
err = dockerBuild(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
cr := &containerReference{
|
|
||||||
cli: client,
|
|
||||||
input: &NewContainerInput{
|
|
||||||
Image: "envmergetest",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
env := map[string]string{
|
env := map[string]string{
|
||||||
"PATH": "/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin",
|
"PATH": "/custom/bin",
|
||||||
"RANDOM_VAR": "WITH_VALUE",
|
"RANDOM_VAR": "WITH_VALUE",
|
||||||
"ANOTHER_VAR": "",
|
"ANOTHER_VAR": "",
|
||||||
"CONFLICT_VAR": "I_EXIST_IN_MULTIPLE_PLACES",
|
"CONFLICT_VAR": "step-value",
|
||||||
}
|
}
|
||||||
|
require.NoError(t, (&containerReference{cli: client, id: "amd64-job"}).UpdateFromImageEnv(&env)(t.Context()))
|
||||||
envExecutor := cr.extractFromImageEnv(&env)
|
|
||||||
err = envExecutor(ctx)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Equal(t, map[string]string{
|
assert.Equal(t, map[string]string{
|
||||||
"PATH": "/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/this/path/does/not/exists/anywhere:/this/either",
|
"PATH": "/custom/bin:/opt/node/x64/bin:/usr/bin:/bin",
|
||||||
"RANDOM_VAR": "WITH_VALUE",
|
"RANDOM_VAR": "WITH_VALUE",
|
||||||
"ANOTHER_VAR": "",
|
"ANOTHER_VAR": "",
|
||||||
"SOME_RANDOM_VAR": "",
|
"SOME_RANDOM_VAR": "",
|
||||||
"ANOTHER_ONE": "BUT_I_HAVE_VALUE",
|
"ANOTHER_ONE": "BUT_I_HAVE_VALUE",
|
||||||
"CONFLICT_VAR": "I_EXIST_IN_MULTIPLE_PLACES",
|
"CONFLICT_VAR": "step-value",
|
||||||
|
"LITERAL": "'quoted'=$HOME\nsecond line",
|
||||||
}, env)
|
}, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,6 +123,11 @@ func (m *mockDockerClient) NetworkList(ctx context.Context, opts mobyclient.Netw
|
|||||||
return args.Get(0).(mobyclient.NetworkListResult), args.Error(1)
|
return args.Get(0).(mobyclient.NetworkListResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockDockerClient) NetworkCreate(ctx context.Context, name string, opts mobyclient.NetworkCreateOptions) (mobyclient.NetworkCreateResult, error) {
|
||||||
|
args := m.Called(ctx, name, opts)
|
||||||
|
return args.Get(0).(mobyclient.NetworkCreateResult), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockDockerClient) NetworkInspect(ctx context.Context, id string, opts mobyclient.NetworkInspectOptions) (mobyclient.NetworkInspectResult, error) {
|
func (m *mockDockerClient) NetworkInspect(ctx context.Context, id string, opts mobyclient.NetworkInspectOptions) (mobyclient.NetworkInspectResult, error) {
|
||||||
args := m.Called(ctx, id, opts)
|
args := m.Called(ctx, id, opts)
|
||||||
return args.Get(0).(mobyclient.NetworkInspectResult), args.Error(1)
|
return args.Get(0).(mobyclient.NetworkInspectResult), args.Error(1)
|
||||||
@@ -175,7 +166,7 @@ func (m *mockConn) Close() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerExecAbort(t *testing.T) {
|
func TestDockerExecAbort(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
|
|
||||||
reader := &interruptReader{started: make(chan struct{}), interrupted: make(chan struct{}), stopped: make(chan struct{})}
|
reader := &interruptReader{started: make(chan struct{}), interrupted: make(chan struct{}), stopped: make(chan struct{})}
|
||||||
conn := &mockConn{}
|
conn := &mockConn{}
|
||||||
@@ -216,7 +207,7 @@ func TestDockerExecAbort(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerExecFailure(t *testing.T) {
|
func TestDockerExecFailure(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
conn := &mockConn{}
|
conn := &mockConn{}
|
||||||
|
|
||||||
@@ -263,7 +254,7 @@ func stdcopyFrame(stream stdcopy.StdType, payload string) []byte {
|
|||||||
// attach() streaming goroutine has drained and flushed the container's output,
|
// attach() streaming goroutine has drained and flushed the container's output,
|
||||||
// so a final line without a trailing newline is not lost.
|
// so a final line without a trailing newline is not lost.
|
||||||
func TestDockerAttachFlushesTrailingLine(t *testing.T) {
|
func TestDockerAttachFlushesTrailingLine(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
framed := bytes.NewBuffer(stdcopyFrame(stdcopy.Stdout, "line one\nlast line without newline"))
|
framed := bytes.NewBuffer(stdcopyFrame(stdcopy.Stdout, "line one\nlast line without newline"))
|
||||||
|
|
||||||
@@ -312,7 +303,7 @@ func TestDockerAttachFlushesTrailingLine(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerWaitFailure(t *testing.T) {
|
func TestDockerWaitFailure(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
statusCh := make(chan container.WaitResponse, 1)
|
statusCh := make(chan container.WaitResponse, 1)
|
||||||
statusCh <- container.WaitResponse{StatusCode: 2}
|
statusCh <- container.WaitResponse{StatusCode: 2}
|
||||||
@@ -363,8 +354,8 @@ func TestRemoveIgnoresAutoRemoveRace(t *testing.T) {
|
|||||||
{name: "real failure", err: removeFailure, wantErr: removeFailure},
|
{name: "real failure", err: removeFailure, wantErr: removeFailure},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerKill", ctx, "abc", killOpts).Return(mobyclient.ContainerKillResult{}, nil)
|
client.On("ContainerKill", ctx, "abc", killOpts).Return(mobyclient.ContainerKillResult{}, nil)
|
||||||
client.On("ContainerRemove", ctx, "abc", removeOpts).Return(mobyclient.ContainerRemoveResult{}, tc.err)
|
client.On("ContainerRemove", ctx, "abc", removeOpts).Return(mobyclient.ContainerRemoveResult{}, tc.err)
|
||||||
@@ -387,7 +378,7 @@ func TestRemoveIgnoresAutoRemoveRace(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
assert.Empty(t, cr.id)
|
assert.Empty(t, cr.id)
|
||||||
}
|
}
|
||||||
assert.Empty(t, hook.AllEntries())
|
assert.Empty(t, hook.AllRecords())
|
||||||
client.AssertExpectations(t)
|
client.AssertExpectations(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -397,7 +388,7 @@ func TestRemoveIgnoresAutoRemoveRace(t *testing.T) {
|
|||||||
// create() lost its reply, must still be removed rather than leaking with its network. It
|
// create() lost its reply, must still be removed rather than leaking with its network. It
|
||||||
// was never started here, so it is not worth a kill of its own.
|
// was never started here, so it is not worth a kill of its own.
|
||||||
func TestRemoveWithoutIDUsesName(t *testing.T) {
|
func TestRemoveWithoutIDUsesName(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerRemove", ctx, "job-1", mobyclient.ContainerRemoveOptions{RemoveVolumes: true, Force: true}).
|
client.On("ContainerRemove", ctx, "job-1", mobyclient.ContainerRemoveOptions{RemoveVolumes: true, Force: true}).
|
||||||
Return(mobyclient.ContainerRemoveResult{}, nil)
|
Return(mobyclient.ContainerRemoveResult{}, nil)
|
||||||
@@ -410,7 +401,7 @@ func TestRemoveWithoutIDUsesName(t *testing.T) {
|
|||||||
// find() must drop a stale cached id so later Copy/Exec don't hit the
|
// find() must drop a stale cached id so later Copy/Exec don't hit the
|
||||||
// daemon with a torn-down container.
|
// daemon with a torn-down container.
|
||||||
func TestFindRevalidatesStaleID(t *testing.T) {
|
func TestFindRevalidatesStaleID(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
notFound := cerrdefs.ErrNotFound.WithMessage("No such container")
|
notFound := cerrdefs.ErrNotFound.WithMessage("No such container")
|
||||||
boom := errors.New("daemon unreachable")
|
boom := errors.New("daemon unreachable")
|
||||||
newCR := func(id string) (*containerReference, *mockDockerClient) {
|
newCR := func(id string) (*containerReference, *mockDockerClient) {
|
||||||
@@ -468,7 +459,7 @@ func TestFindRevalidatesStaleID(t *testing.T) {
|
|||||||
// Every daemon entry point fails fast with a clear, container-named
|
// Every daemon entry point fails fast with a clear, container-named
|
||||||
// error when no live cr.id is known.
|
// error when no live cr.id is known.
|
||||||
func TestRejectsMissingContainer(t *testing.T) {
|
func TestRejectsMissingContainer(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
client := &mockDockerClient{}
|
client := &mockDockerClient{}
|
||||||
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{}, nil)
|
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{}, nil)
|
||||||
cr := &containerReference{cli: client, input: &NewContainerInput{Name: "job-1"}}
|
cr := &containerReference{cli: client, input: &NewContainerInput{Name: "job-1"}}
|
||||||
@@ -493,25 +484,45 @@ func TestRejectsMissingContainer(t *testing.T) {
|
|||||||
check("Inspect after removal", err)
|
check("Inspect after removal", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// End-to-end: a stale cr.id is cleared, repopulated from name lookup,
|
|
||||||
// and the Copy completes against the fresh id.
|
|
||||||
func TestPublicCopyPipelineHandlesStaleID(t *testing.T) {
|
func TestPublicCopyPipelineHandlesStaleID(t *testing.T) {
|
||||||
ctx := context.Background()
|
for _, testcase := range []struct {
|
||||||
client := &mockDockerClient{}
|
name, destination, extractionRoot, entry string
|
||||||
client.On("ContainerInspect", ctx, "stale", mobyclient.ContainerInspectOptions{}).
|
directory bool
|
||||||
Return(mobyclient.ContainerInspectResult{}, cerrdefs.ErrNotFound.WithMessage("gone"))
|
}{
|
||||||
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).
|
{name: "files", destination: "/var/run/act", extractionRoot: "/var/run/act", entry: "x"},
|
||||||
Return(mobyclient.ContainerListResult{Items: []container.Summary{
|
{name: "action below act volume", directory: true, destination: "/var/run/act/actions/checkout/", extractionRoot: "/var/run/act", entry: "actions/checkout/x"},
|
||||||
{ID: "fresh", Names: []string{"/job-1"}},
|
{name: "sibling prefix", directory: true, destination: "/var/run/actor/action", extractionRoot: "/", entry: "var/run/actor/action/x"},
|
||||||
}}, nil)
|
} {
|
||||||
client.On("CopyToContainer", ctx, "fresh", mock.MatchedBy(func(opts mobyclient.CopyToContainerOptions) bool {
|
t.Run(testcase.name, func(t *testing.T) {
|
||||||
return opts.DestinationPath == "/var/run/act"
|
ctx := t.Context()
|
||||||
})).Return(mobyclient.CopyToContainerResult{}, nil)
|
client := &mockDockerClient{}
|
||||||
|
client.On("ContainerInspect", ctx, "stale", mobyclient.ContainerInspectOptions{}).
|
||||||
|
Return(mobyclient.ContainerInspectResult{}, cerrdefs.ErrNotFound.WithMessage("gone"))
|
||||||
|
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).
|
||||||
|
Return(mobyclient.ContainerListResult{Items: []container.Summary{
|
||||||
|
{ID: "fresh", Names: []string{"/job-1"}},
|
||||||
|
}}, nil)
|
||||||
|
client.On("CopyToContainer", ctx, "fresh", mock.Anything).
|
||||||
|
Run(func(args mock.Arguments) {
|
||||||
|
opts := args.Get(2).(mobyclient.CopyToContainerOptions)
|
||||||
|
assert.Equal(t, testcase.extractionRoot, opts.DestinationPath)
|
||||||
|
header, err := tar.NewReader(opts.Content).Next()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, testcase.entry, header.Name)
|
||||||
|
}).Return(mobyclient.CopyToContainerResult{}, nil).Once()
|
||||||
|
|
||||||
cr := &containerReference{id: "stale", cli: client, input: &NewContainerInput{Name: "job-1"}}
|
cr := &containerReference{id: "stale", cli: client, input: &NewContainerInput{Name: "job-1"}}
|
||||||
require.NoError(t, cr.Copy("/var/run/act", &FileEntry{Name: "x", Mode: 0o644})(ctx))
|
if testcase.directory {
|
||||||
assert.Equal(t, "fresh", cr.id)
|
source := t.TempDir()
|
||||||
client.AssertExpectations(t)
|
require.NoError(t, os.WriteFile(filepath.Join(source, "x"), nil, 0o644))
|
||||||
|
require.NoError(t, cr.CopyDir(testcase.destination, source+string(filepath.Separator), false, true)(ctx))
|
||||||
|
} else {
|
||||||
|
require.NoError(t, cr.Copy(testcase.destination, &FileEntry{Name: "x", Mode: 0o644})(ctx))
|
||||||
|
}
|
||||||
|
assert.Equal(t, "fresh", cr.id)
|
||||||
|
client.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type assert containerReference implements ExecutionsEnvironment
|
// Type assert containerReference implements ExecutionsEnvironment
|
||||||
@@ -519,10 +530,12 @@ var _ ExecutionsEnvironment = &containerReference{}
|
|||||||
|
|
||||||
func TestCheckVolumes(t *testing.T) {
|
func TestCheckVolumes(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
validVolumes []string
|
validVolumes []string
|
||||||
binds []string
|
binds []string
|
||||||
expectedBinds []string
|
expectedBinds []string
|
||||||
|
mounts []mount.Mount
|
||||||
|
expectedMounts []mount.Mount
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "match all volumes",
|
desc: "match all volumes",
|
||||||
@@ -578,19 +591,41 @@ func TestCheckVolumes(t *testing.T) {
|
|||||||
"/opt/app/cache:/cache",
|
"/opt/app/cache:/cache",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "read-only rules allow only read-only mounts and copy nothing into volumes",
|
||||||
|
validVolumes: []string{"/etc/ssl:ro", "data:ro", "shared"},
|
||||||
|
binds: []string{"/etc/ssl:/ssl:ro", "/etc/ssl:/ssl", "data:/data:ro", "data:/data", "shared:/shared:ro"},
|
||||||
|
expectedBinds: []string{"/etc/ssl:/ssl:ro", "data:/data:ro,nocopy", "shared:/shared:ro"},
|
||||||
|
mounts: []mount.Mount{
|
||||||
|
{Type: mount.TypeBind, Source: "/etc/ssl", Target: "/ssl", ReadOnly: true},
|
||||||
|
{Type: mount.TypeBind, Source: "/etc/ssl", Target: "/ssl", ReadOnly: true, BindOptions: &mount.BindOptions{ReadOnlyNonRecursive: true}},
|
||||||
|
{Type: mount.TypeVolume, Source: "data", Target: "/data", ReadOnly: true},
|
||||||
|
},
|
||||||
|
expectedMounts: []mount.Mount{
|
||||||
|
{Type: mount.TypeBind, Source: "/etc/ssl", Target: "/ssl", ReadOnly: true},
|
||||||
|
{Type: mount.TypeVolume, Source: "data", Target: "/data", ReadOnly: true, VolumeOptions: &mount.VolumeOptions{NoCopy: true}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "read-only wildcard keeps writable rules writable",
|
||||||
|
validVolumes: []string{"**:ro", "shared"},
|
||||||
|
binds: []string{"/etc/ssl:/ssl:ro", "data:/data", "shared:/shared"},
|
||||||
|
expectedBinds: []string{"/etc/ssl:/ssl:ro", "shared:/shared"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
cr := &containerReference{
|
cr := &containerReference{
|
||||||
input: &NewContainerInput{
|
input: &NewContainerInput{
|
||||||
ValidVolumes: tc.validVolumes,
|
ValidVolumes: tc.validVolumes,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, hostConf := cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{Binds: tc.binds})
|
_, hostConf := cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{Binds: tc.binds, Mounts: tc.mounts})
|
||||||
assert.Equal(t, tc.expectedBinds, hostConf.Binds)
|
assert.Equal(t, tc.expectedBinds, hostConf.Binds)
|
||||||
assert.Len(t, hook.AllEntries(), len(tc.binds)-len(tc.expectedBinds)) // every drop is warned about
|
assert.ElementsMatch(t, tc.expectedMounts, hostConf.Mounts)
|
||||||
|
assert.Len(t, hook.AllRecords(), len(tc.binds)+len(tc.mounts)-len(tc.expectedBinds)-len(tc.expectedMounts)) // every drop is warned about
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -618,33 +653,33 @@ func TestMergeContainerConfigsKeepsTheRunnersFilesAndEnvToItself(t *testing.T) {
|
|||||||
t.Setenv("RUNNER_SECRET", "s3cr3t")
|
t.Setenv("RUNNER_SECRET", "s3cr3t")
|
||||||
|
|
||||||
for _, option := range []string{"--env-file " + hostFile, "--label-file " + hostFile} {
|
for _, option := range []string{"--env-file " + hostFile, "--label-file " + hostFile} {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
cr := &containerReference{input: &NewContainerInput{NetworkMode: "bridge", WorkflowOptions: option}}
|
cr := &containerReference{input: &NewContainerInput{NetworkMode: "bridge", WorkflowOptions: option}}
|
||||||
|
|
||||||
_, _, err := cr.mergeContainerConfigs(common.WithLogger(context.Background(), logger), &container.Config{}, &container.HostConfig{})
|
_, _, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{})
|
||||||
require.ErrorContains(t, err, "not allowed in a workflow")
|
require.ErrorContains(t, err, "not allowed in a workflow")
|
||||||
|
|
||||||
// the runner reading its own files is what those options are for
|
// the runner reading its own files is what those options are for
|
||||||
cr = &containerReference{input: &NewContainerInput{NetworkMode: "bridge", RunnerOptions: option}}
|
cr = &containerReference{input: &NewContainerInput{NetworkMode: "bridge", RunnerOptions: option}}
|
||||||
_, _, err = cr.mergeContainerConfigs(common.WithLogger(context.Background(), logger), &container.Config{}, &container.HostConfig{})
|
_, _, err = cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// a bare name is no longer resolved from the runner's environment, for either source
|
// a bare name is no longer resolved from the runner's environment, for either source
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
cr := &containerReference{input: &NewContainerInput{
|
cr := &containerReference{input: &NewContainerInput{
|
||||||
NetworkMode: "bridge",
|
NetworkMode: "bridge",
|
||||||
RunnerOptions: "--env RUNNER_SECRET",
|
RunnerOptions: "--env RUNNER_SECRET",
|
||||||
WorkflowOptions: "--env RUNNER_SECRET --env GIVEN=value",
|
WorkflowOptions: "--env RUNNER_SECRET --env GIVEN=value",
|
||||||
}}
|
}}
|
||||||
|
|
||||||
config, _, err := cr.mergeContainerConfigs(common.WithLogger(context.Background(), logger), &container.Config{}, &container.HostConfig{})
|
config, _, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, []string{"RUNNER_SECRET", "RUNNER_SECRET", "GIVEN=value"}, config.Env)
|
assert.Equal(t, []string{"RUNNER_SECRET", "RUNNER_SECRET", "GIVEN=value"}, config.Env)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSanitizeOptionsHostConfig(t *testing.T) {
|
func TestSanitizeOptionsHostConfig(t *testing.T) {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
|
|
||||||
// every field the sanitizer resets, so a reset dropped in a refactor fails here
|
// every field the sanitizer resets, so a reset dropped in a refactor fails here
|
||||||
hostConfig := &container.HostConfig{
|
hostConfig := &container.HostConfig{
|
||||||
@@ -666,6 +701,9 @@ func TestSanitizeOptionsHostConfig(t *testing.T) {
|
|||||||
DeviceCgroupRules: []string{"a *:* rwm"},
|
DeviceCgroupRules: []string{"a *:* rwm"},
|
||||||
DeviceRequests: []container.DeviceRequest{{Count: -1, Capabilities: [][]string{{"gpu"}}}},
|
DeviceRequests: []container.DeviceRequest{{Count: -1, Capabilities: [][]string{{"gpu"}}}},
|
||||||
Sysctls: map[string]string{"net.ipv4.ip_forward": "1"},
|
Sysctls: map[string]string{"net.ipv4.ip_forward": "1"},
|
||||||
|
LogConfig: container.LogConfig{Type: "syslog", Config: map[string]string{"syslog-address": "tcp://169.254.169.254:514"}},
|
||||||
|
OomScoreAdj: -1000,
|
||||||
|
OomKillDisable: new(true),
|
||||||
}
|
}
|
||||||
|
|
||||||
sanitizeOptionsHostConfig(logger, hostConfig, &container.HostConfig{})
|
sanitizeOptionsHostConfig(logger, hostConfig, &container.HostConfig{})
|
||||||
@@ -674,9 +712,9 @@ func TestSanitizeOptionsHostConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mergeOptions merges both option sources into a bare container, returning the result and its log.
|
// mergeOptions merges both option sources into a bare container, returning the result and its log.
|
||||||
func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privileged bool) (*container.HostConfig, *test.Hook) {
|
func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privileged bool) (*container.HostConfig, *logtest.Handler) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
cr := &containerReference{input: &NewContainerInput{
|
cr := &containerReference{input: &NewContainerInput{
|
||||||
RunnerOptions: runnerOptions,
|
RunnerOptions: runnerOptions,
|
||||||
WorkflowOptions: workflowOptions,
|
WorkflowOptions: workflowOptions,
|
||||||
@@ -685,7 +723,7 @@ func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privilege
|
|||||||
ValidVolumes: []string{"**"},
|
ValidVolumes: []string{"**"},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
_, hostConfig, err := cr.mergeContainerConfigs(common.WithLogger(context.Background(), logger), &container.Config{}, &container.HostConfig{
|
_, hostConfig, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{
|
||||||
Privileged: privileged,
|
Privileged: privileged,
|
||||||
UsernsMode: container.UsernsMode("private"),
|
UsernsMode: container.UsernsMode("private"),
|
||||||
NetworkMode: container.NetworkMode("bridge"),
|
NetworkMode: container.NetworkMode("bridge"),
|
||||||
@@ -699,7 +737,9 @@ func TestMergeContainerConfigsStripsDangerousOptionsWhenUnprivileged(t *testing.
|
|||||||
const dangerousOptions = "--pid=host --ipc=host --uts=host --cgroupns=host " +
|
const dangerousOptions = "--pid=host --ipc=host --uts=host --cgroupns=host " +
|
||||||
"--userns=host --cap-add=ALL --security-opt seccomp=unconfined " +
|
"--userns=host --cap-add=ALL --security-opt seccomp=unconfined " +
|
||||||
"--security-opt apparmor=unconfined --volumes-from other --isolation process " +
|
"--security-opt apparmor=unconfined --volumes-from other --isolation process " +
|
||||||
"--runtime runc --cgroup-parent /custom --sysctl net.ipv4.ip_forward=1"
|
"--runtime runc --cgroup-parent /custom --sysctl net.ipv4.ip_forward=1 " +
|
||||||
|
"--log-driver=syslog --log-opt syslog-address=tcp://169.254.169.254:514 " +
|
||||||
|
"--oom-score-adj=-1000 --oom-kill-disable"
|
||||||
|
|
||||||
// whatever the workflow adds, an unprivileged container comes out exactly as the runner's
|
// whatever the workflow adds, an unprivileged container comes out exactly as the runner's
|
||||||
// own options alone describe it, field for field
|
// own options alone describe it, field for field
|
||||||
@@ -726,8 +766,8 @@ func TestMergeContainerConfigsStripsDangerousOptionsWhenUnprivileged(t *testing.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckVolumesRejectsEscapingHostPaths(t *testing.T) {
|
func TestCheckVolumesRejectsEscapingHostPaths(t *testing.T) {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
|
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
allowed := filepath.Join(base, "allowed")
|
allowed := filepath.Join(base, "allowed")
|
||||||
@@ -762,6 +802,22 @@ func TestCheckVolumesRejectsEscapingHostPaths(t *testing.T) {
|
|||||||
assert.Empty(t, hostConf.Binds)
|
assert.Empty(t, hostConf.Binds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitizeConfigKeepsTmpfsMounts(t *testing.T) {
|
||||||
|
hostConfig, _ := mergeOptions(t, "", "--mount type=tmpfs,dst=/var/lib/mysql", false)
|
||||||
|
require.Len(t, hostConfig.Mounts, 1)
|
||||||
|
|
||||||
|
for _, validVolumes := range [][]string{nil, {"data"}, {"**:ro"}} {
|
||||||
|
logger, hook := logtest.NewLogger()
|
||||||
|
_, sanitized := (&containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}}).sanitizeConfig(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{Mounts: []mount.Mount{
|
||||||
|
hostConfig.Mounts[0],
|
||||||
|
{Type: mount.TypeVolume, Target: "/anonymous"},
|
||||||
|
{Type: mount.TypeTmpfs, Source: "/etc", Target: "/etc-copy"},
|
||||||
|
}})
|
||||||
|
assert.Equal(t, hostConfig.Mounts, sanitized.Mounts)
|
||||||
|
assert.Len(t, hook.AllRecords(), 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContainerInfoFromInspect(t *testing.T) {
|
func TestContainerInfoFromInspect(t *testing.T) {
|
||||||
t.Run("reports no healthcheck when the image declares none", func(t *testing.T) {
|
t.Run("reports no healthcheck when the image declares none", func(t *testing.T) {
|
||||||
info := containerInfoFromInspect(container.InspectResponse{
|
info := containerInfoFromInspect(container.InspectResponse{
|
||||||
@@ -841,8 +897,8 @@ func TestContainerInfoFromInspect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeContainerConfigsVolumesReplaceRunnerMounts(t *testing.T) {
|
func TestMergeContainerConfigsVolumesReplaceRunnerMounts(t *testing.T) {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
cr := &containerReference{
|
cr := &containerReference{
|
||||||
input: &NewContainerInput{
|
input: &NewContainerInput{
|
||||||
NetworkMode: "bridge",
|
NetworkMode: "bridge",
|
||||||
@@ -864,7 +920,7 @@ func TestMergeContainerConfigsVolumesReplaceRunnerMounts(t *testing.T) {
|
|||||||
func TestMergeContainerConfigsWarnsOnlyAboutOptionsThatWereGiven(t *testing.T) {
|
func TestMergeContainerConfigsWarnsOnlyAboutOptionsThatWereGiven(t *testing.T) {
|
||||||
warnings := func(runnerOptions, workflowOptions string) int {
|
warnings := func(runnerOptions, workflowOptions string) int {
|
||||||
_, hook := mergeOptions(t, runnerOptions, workflowOptions, false)
|
_, hook := mergeOptions(t, runnerOptions, workflowOptions, false)
|
||||||
return len(hook.AllEntries())
|
return len(hook.AllRecords())
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Zero(t, warnings("--volume /host/tools:/opt/hostedtoolcache", ""))
|
assert.Zero(t, warnings("--volume /host/tools:/opt/hostedtoolcache", ""))
|
||||||
@@ -873,8 +929,37 @@ func TestMergeContainerConfigsWarnsOnlyAboutOptionsThatWereGiven(t *testing.T) {
|
|||||||
assert.Equal(t, 1, warnings("", "--privileged"))
|
assert.Equal(t, 1, warnings("", "--privileged"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDockerCreateKeepsInputEntrypoint(t *testing.T) {
|
||||||
|
for _, entrypoint := range [][]string{nil, {"sleep", "3600"}} {
|
||||||
|
for _, optionEntrypoint := range []string{"/sbin/dumb-init", ""} {
|
||||||
|
logger, hook := logtest.NewLogger()
|
||||||
|
cr := &containerReference{
|
||||||
|
input: &NewContainerInput{
|
||||||
|
NetworkMode: "bridge",
|
||||||
|
Entrypoint: entrypoint,
|
||||||
|
RunnerOptions: "--entrypoint=runner-entrypoint",
|
||||||
|
WorkflowOptions: "--entrypoint=" + optionEntrypoint + " --init",
|
||||||
|
},
|
||||||
|
cli: &probeClient{create: func(opts mobyclient.ContainerCreateOptions) (mobyclient.ContainerCreateResult, error) {
|
||||||
|
if len(entrypoint) > 0 {
|
||||||
|
assert.Equal(t, entrypoint, opts.Config.Entrypoint)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, []string{optionEntrypoint}, opts.Config.Entrypoint)
|
||||||
|
}
|
||||||
|
require.NotNil(t, opts.HostConfig.Init)
|
||||||
|
assert.True(t, *opts.HostConfig.Init)
|
||||||
|
return mobyclient.ContainerCreateResult{ID: "created"}, nil
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
require.NoError(t, cr.create(nil, nil)(common.WithLogger(t.Context(), logger)))
|
||||||
|
assert.Equal(t, "created", cr.id)
|
||||||
|
assert.Equal(t, len(entrypoint) > 0, len(hook.AllRecords()) == 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMergeContainerConfigsKeepsNetworkAliasesFromOptions(t *testing.T) {
|
func TestMergeContainerConfigsKeepsNetworkAliasesFromOptions(t *testing.T) {
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
cr := &containerReference{input: &NewContainerInput{
|
cr := &containerReference{input: &NewContainerInput{
|
||||||
NetworkMode: "job-network", NetworkAliases: []string{"redis"}, WorkflowOptions: "--network-alias redis-primary",
|
NetworkMode: "job-network", NetworkAliases: []string{"redis"}, WorkflowOptions: "--network-alias redis-primary",
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
mobyclient "github.com/moby/moby/client"
|
mobyclient "github.com/moby/moby/client"
|
||||||
@@ -15,7 +14,7 @@ import (
|
|||||||
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
||||||
func requireDocker(t *testing.T) {
|
func requireDocker(t *testing.T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cli, err := GetDockerClient(ctx)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("skipping: docker client unavailable: %v", err)
|
t.Skipf("skipping: docker client unavailable: %v", err)
|
||||||
|
|||||||
@@ -27,9 +27,6 @@ import (
|
|||||||
"gitea.com/gitea/runner/internal/pkg/process"
|
"gitea.com/gitea/runner/internal/pkg/process"
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
"github.com/go-git/go-billy/v5/helper/polyfill"
|
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -100,25 +97,16 @@ func (e *HostEnvironment) CopyDir(destPath, srcPath string, useGitIgnore, skipGi
|
|||||||
srcPrefix += string(filepath.Separator)
|
srcPrefix += string(filepath.Separator)
|
||||||
}
|
}
|
||||||
logger.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
logger.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
||||||
var ignorer gitignore.Matcher
|
|
||||||
if useGitIgnore {
|
|
||||||
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Debugf("Error loading .gitignore: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ignorer = gitignore.NewMatcher(ps)
|
|
||||||
}
|
|
||||||
fc := &filecollector.FileCollector{
|
fc := &filecollector.FileCollector{
|
||||||
Ignorer: ignorer,
|
SrcPath: srcPath,
|
||||||
SrcPath: srcPath,
|
SrcPrefix: srcPrefix,
|
||||||
SrcPrefix: srcPrefix,
|
SkipGitDir: skipGitDir,
|
||||||
SkipGitDir: skipGitDir,
|
UseGitIgnore: useGitIgnore,
|
||||||
Handler: &filecollector.CopyCollector{
|
Handler: &filecollector.CopyCollector{
|
||||||
DstDir: destPath,
|
DstDir: destPath,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return filepath.Walk(srcPath, fc.CollectFiles(ctx, []string{}))
|
return filepath.Walk(srcPath, fc.CollectFiles(ctx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +140,7 @@ func (e *HostEnvironment) GetContainerArchive(ctx context.Context, srcPath strin
|
|||||||
SrcPrefix: srcPrefix,
|
SrcPrefix: srcPrefix,
|
||||||
Handler: tc,
|
Handler: tc,
|
||||||
}
|
}
|
||||||
err = filepath.Walk(srcPath, fc.CollectFiles(ctx, []string{}))
|
err = filepath.Walk(srcPath, fc.CollectFiles(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -33,38 +33,25 @@ func TestActionPlatformNames(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyDir(t *testing.T) {
|
func TestCopyDir(t *testing.T) {
|
||||||
dir := t.TempDir()
|
for _, test := range []struct {
|
||||||
ctx := context.Background()
|
name string
|
||||||
e := &HostEnvironment{
|
useGitIgnore, skipGitDir bool
|
||||||
Path: filepath.Join(dir, "path"),
|
}{
|
||||||
TmpDir: filepath.Join(dir, "tmp"),
|
{name: "git dir kept for a workspace copy"},
|
||||||
ToolCache: filepath.Join(dir, "tool_cache"),
|
{name: "git dir skipped for an action copy", skipGitDir: true},
|
||||||
ActPath: filepath.Join(dir, "act_path"),
|
{name: "unfiltered outside a git repository", useGitIgnore: true},
|
||||||
StdOut: os.Stdout,
|
} {
|
||||||
Workdir: path.Join("testdata", "scratch"),
|
t.Run(test.name, func(t *testing.T) {
|
||||||
}
|
|
||||||
_ = os.MkdirAll(e.Path, 0o700)
|
|
||||||
_ = os.MkdirAll(e.TmpDir, 0o700)
|
|
||||||
_ = os.MkdirAll(e.ToolCache, 0o700)
|
|
||||||
_ = os.MkdirAll(e.ActPath, 0o700)
|
|
||||||
err := e.CopyDir(e.Workdir, e.Path, true, false)(ctx)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyDirSkipGitDir(t *testing.T) {
|
|
||||||
for name, skipGitDir := range map[string]bool{"kept for a workspace copy": false, "skipped for an action copy": true} {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
src := filepath.Join(t.TempDir(), "action")
|
src := filepath.Join(t.TempDir(), "action")
|
||||||
require.NoError(t, os.MkdirAll(filepath.Join(src, ".git", "objects"), 0o700))
|
require.NoError(t, os.MkdirAll(filepath.Join(src, ".git", "objects"), 0o700))
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(src, ".git", "objects", "pack"), []byte("x"), 0o600))
|
require.NoError(t, os.WriteFile(filepath.Join(src, ".git", "objects", "pack"), []byte("x"), 0o600))
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(src, "action.yml"), []byte("name: x\n"), 0o600))
|
require.NoError(t, os.WriteFile(filepath.Join(src, "action.yml"), []byte("name: x\n"), 0o600))
|
||||||
|
|
||||||
dest := filepath.Join(t.TempDir(), "dest")
|
dest := filepath.Join(t.TempDir(), "dest")
|
||||||
e := &HostEnvironment{StdOut: os.Stdout}
|
require.NoError(t, (&HostEnvironment{}).CopyDir(dest, src+string(filepath.Separator), test.useGitIgnore, test.skipGitDir)(t.Context()))
|
||||||
require.NoError(t, e.CopyDir(dest, src+string(filepath.Separator), false, skipGitDir)(context.Background()))
|
|
||||||
|
|
||||||
assert.FileExists(t, filepath.Join(dest, "action.yml"))
|
assert.FileExists(t, filepath.Join(dest, "action.yml"))
|
||||||
if skipGitDir {
|
if test.skipGitDir {
|
||||||
assert.NoDirExists(t, filepath.Join(dest, ".git"))
|
assert.NoDirExists(t, filepath.Join(dest, ".git"))
|
||||||
} else {
|
} else {
|
||||||
assert.FileExists(t, filepath.Join(dest, ".git", "objects", "pack"))
|
assert.FileExists(t, filepath.Join(dest, ".git", "objects", "pack"))
|
||||||
@@ -75,7 +62,7 @@ func TestCopyDirSkipGitDir(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetContainerArchive(t *testing.T) {
|
func TestGetContainerArchive(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
e := &HostEnvironment{
|
e := &HostEnvironment{
|
||||||
Path: filepath.Join(dir, "path"),
|
Path: filepath.Join(dir, "path"),
|
||||||
TmpDir: filepath.Join(dir, "tmp"),
|
TmpDir: filepath.Join(dir, "tmp"),
|
||||||
@@ -110,7 +97,7 @@ func TestHostEnvironmentExecExitCode(t *testing.T) {
|
|||||||
t.Skip("uses POSIX shell")
|
t.Skip("uses POSIX shell")
|
||||||
}
|
}
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
e := &HostEnvironment{
|
e := &HostEnvironment{
|
||||||
Path: filepath.Join(dir, "path"),
|
Path: filepath.Join(dir, "path"),
|
||||||
TmpDir: filepath.Join(dir, "tmp"),
|
TmpDir: filepath.Join(dir, "tmp"),
|
||||||
@@ -161,7 +148,7 @@ func TestHostEnvironmentAllocatePTY(t *testing.T) {
|
|||||||
err := e.Exec(
|
err := e.Exec(
|
||||||
[]string{"sh", "-c", "[ -t 1 ] && printf TTY || printf NOTTY"},
|
[]string{"sh", "-c", "[ -t 1 ] && printf TTY || printf NOTTY"},
|
||||||
map[string]string{"PATH": os.Getenv("PATH")}, "", "",
|
map[string]string{"PATH": os.Getenv("PATH")}, "", "",
|
||||||
)(context.Background())
|
)(t.Context())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
got := strings.TrimSpace(strings.ReplaceAll(buf.String(), "\r", ""))
|
got := strings.TrimSpace(strings.ReplaceAll(buf.String(), "\r", ""))
|
||||||
assert.Equal(t, tc.expect, got)
|
assert.Equal(t, tc.expect, got)
|
||||||
@@ -170,8 +157,7 @@ func TestHostEnvironmentAllocatePTY(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHostEnvironmentRemovePreservesWorkdirByDefault(t *testing.T) {
|
func TestHostEnvironmentRemovePreservesWorkdirByDefault(t *testing.T) {
|
||||||
logger := logrus.New()
|
ctx := common.WithLogger(t.Context(), logging.Default())
|
||||||
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
miscRoot := filepath.Join(base, "misc")
|
miscRoot := filepath.Join(base, "misc")
|
||||||
path := filepath.Join(miscRoot, "hostexecutor")
|
path := filepath.Join(miscRoot, "hostexecutor")
|
||||||
@@ -193,8 +179,7 @@ func TestHostEnvironmentRemovePreservesWorkdirByDefault(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestHostEnvironmentRemoveCleansWorkdirWhenOwned(t *testing.T) {
|
func TestHostEnvironmentRemoveCleansWorkdirWhenOwned(t *testing.T) {
|
||||||
logger := logrus.New()
|
ctx := common.WithLogger(t.Context(), logging.Default())
|
||||||
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
miscRoot := filepath.Join(base, "misc")
|
miscRoot := filepath.Join(base, "misc")
|
||||||
path := filepath.Join(miscRoot, "hostexecutor")
|
path := filepath.Join(miscRoot, "hostexecutor")
|
||||||
@@ -235,7 +220,7 @@ func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
|
|||||||
removeAll = orig
|
removeAll = orig
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := removeAllWithContext(ctx, t.TempDir())
|
err := removeAllWithContext(ctx, t.TempDir())
|
||||||
@@ -252,8 +237,7 @@ func TestHostEnvironmentRemoveDoesNotHangOnStuckCleanUp(t *testing.T) {
|
|||||||
hostCleanupTimeout = 100 * time.Millisecond
|
hostCleanupTimeout = 100 * time.Millisecond
|
||||||
t.Cleanup(func() { hostCleanupTimeout = orig })
|
t.Cleanup(func() { hostCleanupTimeout = orig })
|
||||||
|
|
||||||
logger := logrus.New()
|
ctx := common.WithLogger(t.Context(), logging.Default())
|
||||||
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
path := filepath.Join(base, "misc", "hostexecutor")
|
path := filepath.Join(base, "misc", "hostexecutor")
|
||||||
require.NoError(t, os.MkdirAll(path, 0o700))
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
@@ -306,8 +290,7 @@ func TestHostEnvironmentRemoveDoesNotHangOnStuckPathRemoval(t *testing.T) {
|
|||||||
removeAll = origRemoveAll
|
removeAll = origRemoveAll
|
||||||
})
|
})
|
||||||
|
|
||||||
logger := logrus.New()
|
ctx := common.WithLogger(t.Context(), logging.Default())
|
||||||
ctx := common.WithLogger(context.Background(), logrus.NewEntry(logger))
|
|
||||||
base := t.TempDir()
|
base := t.TempDir()
|
||||||
path := filepath.Join(base, "misc", "hostexecutor")
|
path := filepath.Join(base, "misc", "hostexecutor")
|
||||||
require.NoError(t, os.MkdirAll(path, 0o700))
|
require.NoError(t, os.MkdirAll(path, 0o700))
|
||||||
|
|||||||
@@ -433,8 +433,8 @@ func (c *kubernetesContainer) Copy(destPath string, files ...*FileEntry) common.
|
|||||||
|
|
||||||
func (c *kubernetesContainer) CopyDir(destPath, srcPath string, useGitIgnore, skipGitDir bool) common.Executor {
|
func (c *kubernetesContainer) CopyDir(destPath, srcPath string, useGitIgnore, skipGitDir bool) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
return c.extract(ctx, "/", func(writer io.Writer) error {
|
return c.extract(ctx, destPath, func(writer io.Writer) error {
|
||||||
return writeDirTar(ctx, writer, destPath, srcPath, useGitIgnore, skipGitDir, 0, 0)
|
return writeDirTar(ctx, writer, "/", srcPath, useGitIgnore, skipGitDir, 0, 0)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -230,6 +231,15 @@ func TestKubernetesPodRunsJobThroughAPI(t *testing.T) {
|
|||||||
_, err = job.GetContainerArchive(ctx, workdir+"/missing")
|
_, err = job.GetContainerArchive(ctx, workdir+"/missing")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
|
actionDir := filepath.Join(t.TempDir(), "action")
|
||||||
|
require.NoError(t, os.MkdirAll(actionDir, 0o755))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(actionDir, "index.js"), []byte("main()"), 0o644))
|
||||||
|
require.NoError(t, job.CopyDir(workdir+"/act/actions/sha/", actionDir+"/", false, false)(ctx))
|
||||||
|
script, err := os.ReadFile(workdir + "/act/actions/sha/index.js")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "main()", string(script))
|
||||||
|
assert.Contains(t, cluster.requests["exec"][len(cluster.requests["exec"])-1], "command="+url.QueryEscape(workdir+"/act/actions/sha/"))
|
||||||
|
|
||||||
pathEnv := map[string]string{}
|
pathEnv := map[string]string{}
|
||||||
require.NoError(t, job.UpdateFromImageEnv(&pathEnv)(ctx))
|
require.NoError(t, job.UpdateFromImageEnv(&pathEnv)(ctx))
|
||||||
assert.Equal(t, os.Getenv("PATH"), pathEnv["PATH"])
|
assert.Equal(t, os.Getenv("PATH"), pathEnv["PATH"])
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinuxContainerEnvironmentExtensions struct{}
|
type LinuxContainerEnvironmentExtensions struct{}
|
||||||
@@ -21,13 +21,13 @@ type LinuxContainerEnvironmentExtensions struct{}
|
|||||||
// For use in docker volumes and binds
|
// For use in docker volumes and binds
|
||||||
func (*LinuxContainerEnvironmentExtensions) ToContainerPath(path string) string {
|
func (*LinuxContainerEnvironmentExtensions) ToContainerPath(path string) string {
|
||||||
if runtime.GOOS == "windows" && strings.Contains(path, "/") {
|
if runtime.GOOS == "windows" && strings.Contains(path, "/") {
|
||||||
log.Error("You cannot specify linux style local paths (/mnt/etc) on Windows as it does not understand them.")
|
logging.Error("You cannot specify linux style local paths (/mnt/etc) on Windows as it does not understand them.")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
abspath, err := filepath.Abs(path)
|
abspath, err := filepath.Abs(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
logging.Error(err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ func TestContainerPath(t *testing.T) {
|
|||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
logging.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rootDrive := os.Getenv("SystemDrive")
|
rootDrive := os.Getenv("SystemDrive")
|
||||||
@@ -49,7 +50,7 @@ func TestContainerPath(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
logging.Error(err)
|
||||||
}
|
}
|
||||||
for _, v := range []containerPathJob{
|
for _, v := range []containerPathJob{
|
||||||
{"/home/act/go/src/github.com/nektos/act", "/home/act/go/src/github.com/nektos/act", ""},
|
{"/home/act/go/src/github.com/nektos/act", "/home/act/go/src/github.com/nektos/act", ""},
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package container
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -28,7 +27,7 @@ func TestParseEnvFileSingleLine(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte("FOO=bar\nBAZ=qux\n"), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO=bar\nBAZ=qux\n"), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "bar", env["FOO"])
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
assert.Equal(t, "qux", env["BAZ"])
|
assert.Equal(t, "qux", env["BAZ"])
|
||||||
}
|
}
|
||||||
@@ -39,7 +38,7 @@ func TestParseEnvFileMultiLine(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "line1\nline2", env["FOO"])
|
assert.Equal(t, "line1\nline2", env["FOO"])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +49,7 @@ func TestParseEnvFileLargeValueWithinLimit(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, big, env["FOO"])
|
assert.Equal(t, big, env["FOO"])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +60,7 @@ func TestParseEnvFileLineExceedsBufferReportsScannerError(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
err := parseEnvFile(e, envPath, &env)(context.Background())
|
err := parseEnvFile(e, envPath, &env)(t.Context())
|
||||||
require.ErrorIs(t, err, bufio.ErrTooLong)
|
require.ErrorIs(t, err, bufio.ErrTooLong)
|
||||||
assert.Contains(t, err.Error(), "reading env file")
|
assert.Contains(t, err.Error(), "reading env file")
|
||||||
}
|
}
|
||||||
@@ -73,7 +72,7 @@ func TestParseEnvFileBlankLines(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte("\nFOO=bar\n\n \nBAZ=qux\n\n"), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte("\nFOO=bar\n\n \nBAZ=qux\n\n"), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "bar", env["FOO"])
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
assert.Equal(t, "qux", env["BAZ"])
|
assert.Equal(t, "qux", env["BAZ"])
|
||||||
}
|
}
|
||||||
@@ -84,12 +83,12 @@ func TestParseEnvFileMultiLineKeepsBlankLines(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\n\nline2\nEOF\n"), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\n\nline2\nEOF\n"), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "line1\n\nline2", env["FOO"])
|
assert.Equal(t, "line1\n\nline2", env["FOO"])
|
||||||
|
|
||||||
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\n\nline2\nEOF\n"), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\n\nline2\nEOF\n"), 0o600))
|
||||||
env = map[string]string{}
|
env = map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "\nline2", env["FOO"])
|
assert.Equal(t, "\nline2", env["FOO"])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +98,7 @@ func TestParseEnvFileUTF8BOM(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "bar", env["FOO"])
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +120,7 @@ func TestParseEnvFileUTF16(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
require.NoError(t, os.WriteFile(envPath, content, 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
require.NoError(t, parseEnvFile(e, envPath, &env)(context.Background()))
|
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
|
||||||
assert.Equal(t, "bar", env["FOO"])
|
assert.Equal(t, "bar", env["FOO"])
|
||||||
assert.Equal(t, "line1", env["MULTI"])
|
assert.Equal(t, "line1", env["MULTI"])
|
||||||
})
|
})
|
||||||
@@ -133,7 +132,7 @@ func TestParseEnvFileMissingDelimiter(t *testing.T) {
|
|||||||
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\nline2\n"), 0o600))
|
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\nline2\n"), 0o600))
|
||||||
|
|
||||||
env := map[string]string{}
|
env := map[string]string{}
|
||||||
err := parseEnvFile(e, envPath, &env)(context.Background())
|
err := parseEnvFile(e, envPath, &env)(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "delimiter")
|
assert.Contains(t, err.Error(), "delimiter")
|
||||||
}
|
}
|
||||||
+6
-20
@@ -13,10 +13,6 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/filecollector"
|
"gitea.com/gitea/runner/act/filecollector"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5/helper/polyfill"
|
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeFilesTar(ctx context.Context, w io.Writer, uid, gid int, files ...*FileEntry) error {
|
func writeFilesTar(ctx context.Context, w io.Writer, uid, gid int, files ...*FileEntry) error {
|
||||||
@@ -46,23 +42,13 @@ func writeDirTar(ctx context.Context, w io.Writer, dstPath, srcPath string, useG
|
|||||||
srcPrefix := strings.TrimSuffix(filepath.Dir(srcPath), string(filepath.Separator)) + string(filepath.Separator)
|
srcPrefix := strings.TrimSuffix(filepath.Dir(srcPath), string(filepath.Separator)) + string(filepath.Separator)
|
||||||
logger.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
logger.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
||||||
|
|
||||||
var ignorer gitignore.Matcher
|
|
||||||
if useGitIgnore {
|
|
||||||
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Debugf("Error loading .gitignore: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ignorer = gitignore.NewMatcher(ps)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := filepath.Walk(srcPath, (&filecollector.FileCollector{
|
if err := filepath.Walk(srcPath, (&filecollector.FileCollector{
|
||||||
Ignorer: ignorer,
|
SrcPath: srcPath,
|
||||||
SrcPath: srcPath,
|
SrcPrefix: srcPrefix,
|
||||||
SrcPrefix: srcPrefix,
|
SkipGitDir: skipGitDir,
|
||||||
SkipGitDir: skipGitDir,
|
UseGitIgnore: useGitIgnore,
|
||||||
Handler: &filecollector.TarCollector{TarWriter: tw, UID: uid, GID: gid, DstDir: dstPath[1:]},
|
Handler: &filecollector.TarCollector{TarWriter: tw, UID: uid, GID: gid, DstDir: dstPath[1:]},
|
||||||
}).CollectFiles(ctx, []string{})); err != nil {
|
}).CollectFiles(ctx)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tw.Close()
|
return tw.Close()
|
||||||
|
|||||||
Vendored
-5
@@ -1,5 +0,0 @@
|
|||||||
FROM scratch
|
|
||||||
ENV PATH="/this/path/does/not/exists/anywhere:/this/either"
|
|
||||||
ENV SOME_RANDOM_VAR=""
|
|
||||||
ENV ANOTHER_ONE="BUT_I_HAVE_VALUE"
|
|
||||||
ENV CONFLICT_VAR="I_EXIST_ONLY_HERE"
|
|
||||||
@@ -16,10 +16,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
git "github.com/go-git/go-git/v5"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"github.com/go-git/go-git/v5/plumbing/filemode"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/index"
|
"golang.org/x/text/unicode/norm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
@@ -94,73 +94,62 @@ func (cc *CopyCollector) WriteFile(fpath string, fi fs.FileInfo, linkName string
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FileCollector struct {
|
type FileCollector struct {
|
||||||
Ignorer gitignore.Matcher
|
SrcPath string
|
||||||
SrcPath string
|
SrcPrefix string
|
||||||
SrcPrefix string
|
SkipGitDir bool
|
||||||
SkipGitDir bool
|
UseGitIgnore bool
|
||||||
Handler Handler
|
Handler Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func openGitIndex(path string) (*index.Index, error) {
|
// foldPath compares git's spelling of a path with the filesystem's, which may differ in case or Unicode normalization.
|
||||||
repo, err := git.PlainOpen(path)
|
func foldPath(name string) string {
|
||||||
if err != nil {
|
return strings.ToLower(norm.NFC.String(name))
|
||||||
return nil, err
|
}
|
||||||
|
|
||||||
|
func (fc *FileCollector) gitFiles(ctx context.Context) map[string]bool {
|
||||||
|
if !fc.UseGitIgnore {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return repo.Storer.Index()
|
files, err := git.ListFiles(ctx, fc.SrcPath)
|
||||||
|
if err != nil {
|
||||||
|
common.Logger(ctx).Debugf("Copying %s without applying .gitignore: %v", fc.SrcPath, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
keep := map[string]bool{".": true}
|
||||||
|
for _, file := range files {
|
||||||
|
for file = foldPath(file); !keep[file]; file = path.Dir(file) {
|
||||||
|
keep[file] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc *FileCollector) CollectFiles(ctx context.Context, submodulePath []string) filepath.WalkFunc {
|
func (fc *FileCollector) CollectFiles(ctx context.Context) filepath.WalkFunc {
|
||||||
i, _ := openGitIndex(path.Join(fc.SrcPath, path.Join(submodulePath...)))
|
keep := fc.gitFiles(ctx)
|
||||||
return func(file string, fi os.FileInfo, err error) error {
|
return func(file string, fi os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ctx != nil && ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return errors.New("copy cancelled")
|
return errors.New("copy cancelled")
|
||||||
}
|
}
|
||||||
|
if fc.SkipGitDir && fi.Name() == ".git" {
|
||||||
sansPrefix := strings.TrimPrefix(file, fc.SrcPrefix)
|
|
||||||
split := strings.Split(sansPrefix, string(filepath.Separator))
|
|
||||||
// The root folders should be skipped, submodules only have the last path component set to "." by filepath.Walk
|
|
||||||
if fi.IsDir() && len(split) > 0 && split[len(split)-1] == "." {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if fc.SkipGitDir && split[len(split)-1] == ".git" {
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var entry *index.Entry
|
if keep != nil {
|
||||||
if i != nil {
|
rel, _ := filepath.Rel(fc.SrcPath, file)
|
||||||
entry, err = i.Entry(strings.Join(split[len(submodulePath):], "/"))
|
if rel = filepath.ToSlash(rel); !keep[foldPath(rel)] && !strings.Contains("/"+rel+"/", "/.git/") {
|
||||||
} else {
|
if fi.IsDir() {
|
||||||
err = index.ErrEntryNotFound
|
|
||||||
}
|
|
||||||
if err != nil && fc.Ignorer != nil && fc.Ignorer.Match(split, fi.IsDir()) {
|
|
||||||
if fi.IsDir() {
|
|
||||||
if i != nil {
|
|
||||||
ms, err := i.Glob(strings.Join(append(split[len(submodulePath):], "**"), "/"))
|
|
||||||
if err != nil || len(ms) == 0 {
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil && entry.Mode == filemode.Submodule {
|
path := filepath.ToSlash(strings.TrimPrefix(file, fc.SrcPrefix))
|
||||||
err = filepath.Walk(file, fc.CollectFiles(ctx, split))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
path := filepath.ToSlash(sansPrefix)
|
|
||||||
|
|
||||||
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
|
|
||||||
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||||
linkName, err := os.Readlink(file)
|
linkName, err := os.Readlink(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -171,17 +160,12 @@ func (fc *FileCollector) CollectFiles(ctx context.Context, submodulePath []strin
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// open file
|
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
defer context.AfterFunc(ctx, func() { _ = f.Close() })()
|
||||||
if ctx != nil {
|
|
||||||
stop := context.AfterFunc(ctx, func() { _ = f.Close() })
|
|
||||||
defer stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
return fc.Handler.WriteFile(path, fi, "", f)
|
return fc.Handler.WriteFile(path, fi, "", f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,106 +9,78 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
git "github.com/go-git/go-git/v5"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIgnoredTrackedfile(t *testing.T) {
|
func gitCmd(t *testing.T, args ...string) {
|
||||||
|
t.Helper()
|
||||||
|
out, err := exec.Command("git", args...).CombinedOutput()
|
||||||
|
require.NoError(t, err, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectFilesKeepsWhatGitTracksOrDoesNotIgnore(t *testing.T) {
|
||||||
repoDir := filepath.Join(t.TempDir(), "mygitrepo")
|
repoDir := filepath.Join(t.TempDir(), "mygitrepo")
|
||||||
repo, err := git.PlainInit(repoDir, false)
|
for _, dir := range []string{repoDir, filepath.Join(repoDir, "sub"), filepath.Join(repoDir, "nested")} {
|
||||||
require.NoError(t, err)
|
gitCmd(t, "init", "-q", dir)
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(repoDir, ".gitignore"), []byte(".*\n"), 0o644))
|
}
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(repoDir, ".env"), []byte("test=val1\n"), 0o644))
|
for name, body := range map[string]string{
|
||||||
worktree, err := repo.Worktree()
|
".gitignore": ".*\nign/\n",
|
||||||
require.NoError(t, err)
|
".env": "",
|
||||||
_, err = worktree.Add(".gitignore")
|
"Renamed": "",
|
||||||
require.NoError(t, err)
|
"ign/tracked": "",
|
||||||
|
"ign/untracked": "",
|
||||||
|
"sub/.gitignore": "dropped\n",
|
||||||
|
"sub/dropped": "",
|
||||||
|
"sub/kept": "",
|
||||||
|
"nested/kept": "",
|
||||||
|
} {
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(repoDir, name)), 0o755))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(repoDir, name), []byte(body), 0o644))
|
||||||
|
}
|
||||||
|
require.NoError(t, os.Mkdir(filepath.Join(repoDir, "uninitialized"), 0o755))
|
||||||
|
gitCmd(t, "-C", repoDir, "add", "-f", ".gitignore", "Renamed", "ign/tracked")
|
||||||
|
require.NoError(t, os.Rename(filepath.Join(repoDir, "Renamed"), filepath.Join(repoDir, "renamed")))
|
||||||
|
for _, gitlink := range []string{"sub", "uninitialized"} {
|
||||||
|
gitCmd(t, "-C", repoDir, "update-index", "--add", "--cacheinfo", "160000,4b825dc642cb6eb9a060e54bf8d69288fbee4904,"+gitlink)
|
||||||
|
}
|
||||||
|
expected := map[string]string{".gitignore": "", "ign/tracked": "", "nested/kept": "", "renamed": "", "sub/.gitignore": "", "sub/kept": ""}
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
require.NoError(t, os.Symlink(".env", filepath.Join(repoDir, "test.env")))
|
||||||
|
expected["test.env"] = ".env"
|
||||||
|
}
|
||||||
|
|
||||||
var archive bytes.Buffer
|
var archive bytes.Buffer
|
||||||
tw := tar.NewWriter(&archive)
|
tw := tar.NewWriter(&archive)
|
||||||
patterns, err := gitignore.ReadPatterns(worktree.Filesystem, nil)
|
fc := &FileCollector{SrcPath: repoDir, SrcPrefix: repoDir + string(filepath.Separator), UseGitIgnore: true, Handler: &TarCollector{TarWriter: tw}}
|
||||||
require.NoError(t, err)
|
require.NoError(t, filepath.Walk(repoDir, fc.CollectFiles(t.Context())))
|
||||||
ignorer := gitignore.NewMatcher(patterns)
|
|
||||||
fc := &FileCollector{
|
|
||||||
Ignorer: ignorer,
|
|
||||||
SrcPath: repoDir,
|
|
||||||
SrcPrefix: repoDir + string(filepath.Separator),
|
|
||||||
Handler: &TarCollector{
|
|
||||||
TarWriter: tw,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err = filepath.Walk(repoDir, fc.CollectFiles(context.Background(), nil))
|
|
||||||
assert.NoError(t, err, "successfully collect files")
|
|
||||||
require.NoError(t, tw.Close())
|
require.NoError(t, tw.Close())
|
||||||
|
|
||||||
|
files := map[string]string{}
|
||||||
tr := tar.NewReader(&archive)
|
tr := tar.NewReader(&archive)
|
||||||
h, err := tr.Next()
|
for header, err := tr.Next(); err != io.EOF; header, err = tr.Next() {
|
||||||
assert.NoError(t, err, "tar must not be empty") //nolint:testifylint // pre-existing issue from nektos/act
|
require.NoError(t, err)
|
||||||
assert.Equal(t, ".gitignore", h.Name)
|
files[header.Name] = header.Linkname
|
||||||
_, err = tr.Next()
|
}
|
||||||
assert.ErrorIs(t, err, io.EOF, "tar must only contain one element")
|
assert.Contains(t, files, ".git/HEAD")
|
||||||
|
maps.DeleteFunc(files, func(name, _ string) bool { return strings.Contains("/"+name, "/.git/") })
|
||||||
|
assert.Equal(t, expected, files)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSymlinks(t *testing.T) {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
t.Skip("creating symlinks requires elevated privileges on Windows")
|
|
||||||
}
|
|
||||||
repoDir := filepath.Join(t.TempDir(), "mygitrepo")
|
|
||||||
repo, err := git.PlainInit(repoDir, false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(repoDir, ".env"), []byte("test=val1\n"), 0o644))
|
|
||||||
require.NoError(t, os.Symlink(".env", filepath.Join(repoDir, "test.env")))
|
|
||||||
worktree, err := repo.Worktree()
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = worktree.Add("test.env")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var archive bytes.Buffer
|
|
||||||
tw := tar.NewWriter(&archive)
|
|
||||||
fc := &FileCollector{
|
|
||||||
SrcPath: repoDir,
|
|
||||||
SrcPrefix: repoDir + string(filepath.Separator),
|
|
||||||
Handler: &TarCollector{
|
|
||||||
TarWriter: tw,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err = filepath.Walk(repoDir, fc.CollectFiles(context.Background(), nil))
|
|
||||||
assert.NoError(t, err, "successfully collect files")
|
|
||||||
require.NoError(t, tw.Close())
|
|
||||||
tr := tar.NewReader(&archive)
|
|
||||||
h, err := tr.Next()
|
|
||||||
files := map[string]tar.Header{}
|
|
||||||
for err == nil {
|
|
||||||
files[h.Name] = *h
|
|
||||||
h, err = tr.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, ".env", files[".env"].Name)
|
|
||||||
assert.Equal(t, "test.env", files["test.env"].Name)
|
|
||||||
assert.Equal(t, ".env", files["test.env"].Linkname)
|
|
||||||
assert.ErrorIs(t, err, io.EOF, "tar must be read cleanly to EOF")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regression for https://gitea.com/gitea/runner/issues/876 and /941:
|
|
||||||
// re-copying an action directory must overwrite a pre-existing read-only
|
|
||||||
// file (e.g. a git pack .idx at mode 0444) instead of failing with EACCES
|
|
||||||
// on macOS or "Access is denied" on Windows.
|
|
||||||
func TestCopyCollectorWriteFileOverwritesReadOnlyFile(t *testing.T) {
|
func TestCopyCollectorWriteFileOverwritesReadOnlyFile(t *testing.T) {
|
||||||
dst := t.TempDir()
|
dst := t.TempDir()
|
||||||
target := filepath.Join(dst, "sub", "pack.idx")
|
target := filepath.Join(dst, "sub", "pack.idx")
|
||||||
require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
|
require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
|
||||||
require.NoError(t, os.WriteFile(target, []byte("old"), 0o444))
|
require.NoError(t, os.WriteFile(target, []byte("old"), 0o444))
|
||||||
|
fi, err := os.Stat(target)
|
||||||
src := filepath.Join(t.TempDir(), "pack.idx")
|
|
||||||
require.NoError(t, os.WriteFile(src, []byte("new"), 0o444))
|
|
||||||
fi, err := os.Stat(src)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
cc := &CopyCollector{DstDir: dst}
|
cc := &CopyCollector{DstDir: dst}
|
||||||
@@ -119,8 +91,6 @@ func TestCopyCollectorWriteFileOverwritesReadOnlyFile(t *testing.T) {
|
|||||||
assert.Equal(t, "new", string(got))
|
assert.Equal(t, "new", string(got))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Without the destination removal, os.Symlink fails with EEXIST when the
|
|
||||||
// path already holds a regular file from an earlier copy of the action.
|
|
||||||
func TestCopyCollectorWriteFileOverwritesFileWithSymlink(t *testing.T) {
|
func TestCopyCollectorWriteFileOverwritesFileWithSymlink(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("creating symlinks requires elevated privileges on Windows")
|
t.Skip("creating symlinks requires elevated privileges on Windows")
|
||||||
@@ -129,11 +99,8 @@ func TestCopyCollectorWriteFileOverwritesFileWithSymlink(t *testing.T) {
|
|||||||
target := filepath.Join(dst, "link")
|
target := filepath.Join(dst, "link")
|
||||||
require.NoError(t, os.WriteFile(target, []byte("stale"), 0o644))
|
require.NoError(t, os.WriteFile(target, []byte("stale"), 0o644))
|
||||||
|
|
||||||
fi, err := os.Lstat(target)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
cc := &CopyCollector{DstDir: dst}
|
cc := &CopyCollector{DstDir: dst}
|
||||||
require.NoError(t, cc.WriteFile("link", fi, "target", nil))
|
require.NoError(t, cc.WriteFile("link", nil, "target", nil))
|
||||||
|
|
||||||
resolved, err := os.Readlink(target)
|
resolved, err := os.Readlink(target)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -143,7 +110,7 @@ func TestCopyCollectorWriteFileOverwritesFileWithSymlink(t *testing.T) {
|
|||||||
func TestFileCollectorCancellationAndWalkError(t *testing.T) {
|
func TestFileCollectorCancellationAndWalkError(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(t.Context())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
cancel()
|
cancel()
|
||||||
walk := (&FileCollector{}).CollectFiles(ctx, nil)
|
walk := (&FileCollector{}).CollectFiles(ctx)
|
||||||
|
|
||||||
err := walk("file", nil, nil)
|
err := walk("file", nil, nil)
|
||||||
require.EqualError(t, err, "copy cancelled")
|
require.EqualError(t, err, "copy cancelled")
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func SetRef(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|||||||
if ghc.Ref == "" {
|
if ghc.Ref == "" {
|
||||||
ref, err := pinned(ctx, "ref:"+repoPath, func() (string, error) { return findGitRef(ctx, repoPath) })
|
ref, err := pinned(ctx, "ref:"+repoPath, func() (string, error) { return findGitRef(ctx, repoPath) })
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warningf("unable to get git ref: %v", err)
|
logger.Warnf("unable to get git ref: %v", err)
|
||||||
} else {
|
} else {
|
||||||
logger.Debugf("using github ref: %s", ref)
|
logger.Debugf("using github ref: %s", ref)
|
||||||
ghc.Ref = ref
|
ghc.Ref = ref
|
||||||
@@ -121,7 +121,7 @@ func SetSha(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|||||||
return revision, err
|
return revision, err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warningf("unable to get git revision: %v", err)
|
logger.Warnf("unable to get git revision: %v", err)
|
||||||
} else {
|
} else {
|
||||||
ghc.Sha = sha
|
ghc.Sha = sha
|
||||||
}
|
}
|
||||||
@@ -130,11 +130,11 @@ func SetSha(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
|||||||
|
|
||||||
// SetRepositoryAndOwner resolves the repository of the context from the git
|
// SetRepositoryAndOwner resolves the repository of the context from the git
|
||||||
// remote in repoPath when it is not set yet, and derives its owner.
|
// remote in repoPath when it is not set yet, and derives its owner.
|
||||||
func SetRepositoryAndOwner(ctx context.Context, ghc *model.GithubContext, githubInstance, repoPath string) {
|
func SetRepositoryAndOwner(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
||||||
if ghc.Repository == "" {
|
if ghc.Repository == "" {
|
||||||
repo, err := findGithubRepo(ctx, repoPath, githubInstance)
|
repo, err := findGithubRepo(ctx, repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.Logger(ctx).Warningf("unable to get git repo (githubInstance: %v, repoPath: %v): %v", githubInstance, repoPath, err)
|
common.Logger(ctx).Warnf("unable to get git repo (repoPath: %v): %v", repoPath, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ghc.Repository = repo
|
ghc.Repository = repo
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSetRef(t *testing.T) {
|
func TestSetRef(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
oldFindGitRef := findGitRef
|
oldFindGitRef := findGitRef
|
||||||
oldFindGitRevision := findGitRevision
|
oldFindGitRevision := findGitRevision
|
||||||
@@ -104,7 +105,7 @@ func TestSetRef(t *testing.T) {
|
|||||||
Event: table.event,
|
Event: table.event,
|
||||||
}
|
}
|
||||||
|
|
||||||
SetRef(context.Background(), ghc, "/some/dir")
|
SetRef(t.Context(), ghc, "/some/dir")
|
||||||
ghc.SetRefTypeAndName()
|
ghc.SetRefTypeAndName()
|
||||||
|
|
||||||
assert.Equal(t, table.ref, ghc.Ref)
|
assert.Equal(t, table.ref, ghc.Ref)
|
||||||
@@ -122,14 +123,14 @@ func TestSetRef(t *testing.T) {
|
|||||||
Event: map[string]any{},
|
Event: map[string]any{},
|
||||||
}
|
}
|
||||||
|
|
||||||
SetRef(context.Background(), ghc, "/some/dir")
|
SetRef(t.Context(), ghc, "/some/dir")
|
||||||
|
|
||||||
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetSha(t *testing.T) {
|
func TestSetSha(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
oldFindGitRef := findGitRef
|
oldFindGitRef := findGitRef
|
||||||
oldFindGitRevision := findGitRevision
|
oldFindGitRevision := findGitRevision
|
||||||
@@ -209,14 +210,14 @@ func TestSetSha(t *testing.T) {
|
|||||||
Event: table.event,
|
Event: table.event,
|
||||||
}
|
}
|
||||||
|
|
||||||
SetSha(context.Background(), ghc, "/some/dir")
|
SetSha(t.Context(), ghc, "/some/dir")
|
||||||
|
|
||||||
assert.Equal(t, table.sha, ghc.Sha)
|
assert.Equal(t, table.sha, ghc.Sha)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) {
|
t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) {
|
||||||
ctx := WithPinnedCheckout(context.Background())
|
ctx := WithPinnedCheckout(t.Context())
|
||||||
for _, head := range []string{"first", "moved"} {
|
for _, head := range []string{"first", "moved"} {
|
||||||
findGitRevision = func(context.Context, string) (string, string, error) { return "", head, nil }
|
findGitRevision = func(context.Context, string) (string, string, error) { return "", head, nil }
|
||||||
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }
|
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }
|
||||||
|
|||||||
+13
-26
@@ -145,17 +145,17 @@ func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actio
|
|||||||
containerActionDirCopy += `/`
|
containerActionDirCopy += `/`
|
||||||
}
|
}
|
||||||
|
|
||||||
defer git.AcquireCloneLock(actionDir)()
|
unlock, err := git.AcquireCloneLock(ctx, actionDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer unlock()
|
||||||
|
|
||||||
if !rc.Config.NoActionPatch {
|
if !rc.Config.NoActionPatch {
|
||||||
// A concurrent job's prepare resets this directory, so patch under the copy's lock.
|
// A concurrent job's prepare resets this directory, so patch under the copy's lock.
|
||||||
defer patchActions(ctx, actionScriptPaths(filepath.Join(actionDir, actionPath), step.getActionModel()))()
|
defer patchActions(ctx, actionScriptPaths(filepath.Join(actionDir, actionPath), step.getActionModel()))()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := removeGitIgnore(ctx, actionDir); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return rc.JobContainer.CopyDir(containerActionDirCopy, actionDir+"/", rc.Config.UseGitIgnore, true)(ctx)
|
return rc.JobContainer.CopyDir(containerActionDirCopy, actionDir+"/", rc.Config.UseGitIgnore, true)(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,23 +241,6 @@ func nodeActionCommand(script string) []string {
|
|||||||
return []string{"node", "--preserve-symlinks-main", script}
|
return []string{"node", "--preserve-symlinks-main", script}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/nektos/act/issues/228#issuecomment-629709055
|
|
||||||
// files in .gitignore are not copied in a Docker container
|
|
||||||
// this causes issues with actions that ignore other important resources
|
|
||||||
// such as `node_modules` for example
|
|
||||||
func removeGitIgnore(ctx context.Context, directory string) error {
|
|
||||||
gitIgnorePath := path.Join(directory, ".gitignore")
|
|
||||||
if _, err := os.Stat(gitIgnorePath); err == nil {
|
|
||||||
// .gitignore exists
|
|
||||||
common.Logger(ctx).Debugf("Removing %s before docker cp", gitIgnorePath)
|
|
||||||
err := os.Remove(gitIgnorePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// dockerActionImageTag derives the local docker image tag used when an action
|
// dockerActionImageTag derives the local docker image tag used when an action
|
||||||
// is built from a Dockerfile.
|
// is built from a Dockerfile.
|
||||||
//
|
//
|
||||||
@@ -366,7 +349,11 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, actionDir, b
|
|||||||
// Held across the whole build: the daemon drains contextDir lazily.
|
// Held across the whole build: the daemon drains contextDir lazily.
|
||||||
inner := prepImage
|
inner := prepImage
|
||||||
prepImage = func(ctx context.Context) error {
|
prepImage = func(ctx context.Context) error {
|
||||||
defer git.AcquireCloneLock(actionDir)()
|
unlock, err := git.AcquireCloneLock(ctx, actionDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer unlock()
|
||||||
return inner(ctx)
|
return inner(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -681,17 +668,17 @@ func shouldRunPostStep(step actionStep) common.Conditional {
|
|||||||
stepResult := stepResults[step.getStepModel().ID]
|
stepResult := stepResults[step.getStepModel().ID]
|
||||||
|
|
||||||
if stepResult == nil {
|
if stepResult == nil {
|
||||||
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; step was not executed", step.getStepModel())
|
log.With("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; step was not executed", step.getStepModel())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if stepResult.Conclusion == model.StepStatusSkipped {
|
if stepResult.Conclusion == model.StepStatusSkipped {
|
||||||
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; main step was skipped", step.getStepModel())
|
log.With("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; main step was skipped", step.getStepModel())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if step.getActionModel() == nil {
|
if step.getActionModel() == nil {
|
||||||
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s': no action model available", step.getStepModel())
|
log.With("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s': no action model available", step.getStepModel())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ runs:
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
action, err := readActionImpl(context.Background(), tt.step, "actionDir", "actionPath", readFile, writeFile)
|
action, err := readActionImpl(t.Context(), tt.step, "actionDir", "actionPath", readFile, writeFile)
|
||||||
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Equal(t, tt.expected, action)
|
assert.Equal(t, tt.expected, action)
|
||||||
@@ -143,6 +143,7 @@ runs:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestActionRunner(t *testing.T) {
|
func TestActionRunner(t *testing.T) {
|
||||||
|
t.Chdir(t.TempDir())
|
||||||
table := []struct {
|
table := []struct {
|
||||||
name string
|
name string
|
||||||
step actionStep
|
step actionStep
|
||||||
@@ -224,7 +225,7 @@ func TestActionRunner(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
cm.On("CopyDir", "/var/run/act/actions/dir/", "dir/", false, true).Return(func(ctx context.Context) error { return nil })
|
cm.On("CopyDir", "/var/run/act/actions/dir/", "dir/", false, true).Return(func(ctx context.Context) error { return nil })
|
||||||
@@ -289,7 +290,7 @@ func TestNewStepContainerDoesNotUseDockerSecrets(t *testing.T) {
|
|||||||
ContainerNewContainer = origContainerNewContainer
|
ContainerNewContainer = origContainerNewContainer
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := &RunContext{
|
rc := &RunContext{
|
||||||
Name: "job",
|
Name: "job",
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
@@ -326,7 +327,7 @@ func TestNewStepContainerDoesNotUseDockerSecrets(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMaybeCopyToActionDirHoldsCloneLock(t *testing.T) {
|
func TestMaybeCopyToActionDirHoldsCloneLock(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
actionDir := t.TempDir()
|
actionDir := t.TempDir()
|
||||||
|
|
||||||
@@ -366,7 +367,10 @@ func TestMaybeCopyToActionDirHoldsCloneLock(t *testing.T) {
|
|||||||
|
|
||||||
peerAcquired := make(chan struct{})
|
peerAcquired := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
unlock := git.AcquireCloneLock(actionDir)
|
unlock, err := git.AcquireCloneLock(t.Context(), actionDir)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
close(peerAcquired)
|
close(peerAcquired)
|
||||||
unlock()
|
unlock()
|
||||||
}()
|
}()
|
||||||
@@ -400,7 +404,9 @@ func TestMaybeCopyToActionDirHoldsCloneLock(t *testing.T) {
|
|||||||
func TestExecAsDockerHoldsCloneLockForRemoteUncached(t *testing.T) {
|
func TestExecAsDockerHoldsCloneLockForRemoteUncached(t *testing.T) {
|
||||||
actionDir := t.TempDir()
|
actionDir := t.TempDir()
|
||||||
|
|
||||||
unlockOnce := sync.OnceFunc(git.AcquireCloneLock(actionDir))
|
unlock, err := git.AcquireCloneLock(t.Context(), actionDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
unlockOnce := sync.OnceFunc(unlock)
|
||||||
defer unlockOnce()
|
defer unlockOnce()
|
||||||
|
|
||||||
innerEntered := make(chan struct{})
|
innerEntered := make(chan struct{})
|
||||||
@@ -441,7 +447,7 @@ func TestExecAsDockerHoldsCloneLockForRemoteUncached(t *testing.T) {
|
|||||||
env: map[string]string{},
|
env: map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
@@ -613,7 +619,7 @@ func TestExecAsDockerStageEntrypoint(t *testing.T) {
|
|||||||
cm.On("Start", true).Return(func(context.Context) error { return nil })
|
cm.On("Start", true).Return(func(context.Context) error { return nil })
|
||||||
cm.On("Close").Return(func(context.Context) error { return nil })
|
cm.On("Close").Return(func(context.Context) error { return nil })
|
||||||
|
|
||||||
require.NoError(t, execAsDocker(context.Background(), step, "action", t.TempDir(), t.TempDir(), false, tc.stage))
|
require.NoError(t, execAsDocker(t.Context(), step, "action", t.TempDir(), t.TempDir(), false, tc.stage))
|
||||||
require.NotNil(t, input)
|
require.NotNil(t, input)
|
||||||
assert.Equal(t, tc.wantCmd, input.Cmd)
|
assert.Equal(t, tc.wantCmd, input.Cmd)
|
||||||
assert.Equal(t, tc.wantEntrypoint, input.Entrypoint)
|
assert.Equal(t, tc.wantEntrypoint, input.Entrypoint)
|
||||||
@@ -628,7 +634,7 @@ func TestDockerActionHasPreAndPostStep(t *testing.T) {
|
|||||||
newStep := func(runs model.ActionRuns) actionStep {
|
newStep := func(runs model.ActionRuns) actionStep {
|
||||||
return &stepActionRemote{action: &model.Action{Runs: runs}}
|
return &stepActionRemote{action: &model.Action{Runs: runs}}
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
assert.False(t, hasPreStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
assert.False(t, hasPreStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
||||||
assert.False(t, hasPostStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
assert.False(t, hasPostStep(newStep(model.ActionRuns{Using: "docker", Image: "Dockerfile"}))(ctx))
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func TestCancelledJobStatusEnablesAlwaysAndCancelledSteps(t *testing.T) {
|
|||||||
enabled, err := interp.Evaluate("always()", exprparser.DefaultStatusCheckSuccess)
|
enabled, err := interp.Evaluate("always()", exprparser.DefaultStatusCheckSuccess)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, true, enabled, "`if: always()` step must run on a cancelled job")
|
assert.Equal(t, true, enabled, "`if: always()` step must run on a cancelled job")
|
||||||
setJobResult(context.Background(), rc, rc, true)
|
setJobResult(t.Context(), rc, rc, true)
|
||||||
assert.Equal(t, "cancelled", rc.Run.Job().Result)
|
assert.Equal(t, "cancelled", rc.Run.Job().Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ func TestMainStepsExecutorRunsAlwaysStepsAfterCancel(t *testing.T) {
|
|||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var ran []string
|
var ran []string
|
||||||
@@ -110,7 +110,7 @@ func TestMainStepsExecutorMarksFailedOnTimeoutBetweenSteps(t *testing.T) {
|
|||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx := newControllableDeadlineContext(context.Background())
|
ctx := newControllableDeadlineContext(t.Context())
|
||||||
|
|
||||||
var ran []string
|
var ran []string
|
||||||
var laterStepCtxErr error
|
var laterStepCtxErr error
|
||||||
@@ -149,7 +149,7 @@ func TestStepsExecutorRunsMainStepsAfterPreCancel(t *testing.T) {
|
|||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var ran []string
|
var ran []string
|
||||||
@@ -200,7 +200,7 @@ func TestStepsExecutorRunsMainStepsAfterPreFailure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := newStepsExecutor(rc, preSteps, steps)(context.Background())
|
err := newStepsExecutor(rc, preSteps, steps)(t.Context())
|
||||||
|
|
||||||
require.ErrorIs(t, err, assert.AnError, "the pre-step error is propagated")
|
require.ErrorIs(t, err, assert.AnError, "the pre-step error is propagated")
|
||||||
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-step failure")
|
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-step failure")
|
||||||
@@ -215,7 +215,7 @@ func TestPreStepFailureAffectsMainStepIfStatus(t *testing.T) {
|
|||||||
rc := createIfTestRunContext(map[string]*model.Job{
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
})
|
})
|
||||||
ctx := common.WithJobErrorContainer(context.Background())
|
ctx := common.WithJobErrorContainer(t.Context())
|
||||||
|
|
||||||
reportStepError(ctx, rc, assert.AnError)
|
reportStepError(ctx, rc, assert.AnError)
|
||||||
|
|
||||||
@@ -251,7 +251,7 @@ func TestPreStepFailureAffectsMainStepIfStatus(t *testing.T) {
|
|||||||
// from cancellation (so the steps run) yet still carry a usable error container.
|
// from cancellation (so the steps run) yet still carry a usable error container.
|
||||||
func TestPostStepsContextCancelledIsUsableForFailingStep(t *testing.T) {
|
func TestPostStepsContextCancelledIsUsableForFailingStep(t *testing.T) {
|
||||||
jobSpan := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID{1}, SpanID: trace.SpanID{1}})
|
jobSpan := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID{1}, SpanID: trace.SpanID{1}})
|
||||||
cancelled, cancel := context.WithCancel(trace.ContextWithSpanContext(common.WithJobErrorContainer(context.Background()), jobSpan))
|
cancelled, cancel := context.WithCancel(trace.ContextWithSpanContext(common.WithJobErrorContainer(t.Context()), jobSpan))
|
||||||
cancel()
|
cancel()
|
||||||
require.ErrorIs(t, cancelled.Err(), context.Canceled)
|
require.ErrorIs(t, cancelled.Err(), context.Canceled)
|
||||||
|
|
||||||
@@ -273,7 +273,7 @@ func TestPostStepsContextCancelledIsUsableForFailingStep(t *testing.T) {
|
|||||||
// job-error container (via context.WithoutCancel), so the timeout failure and any post-step error
|
// job-error container (via context.WithoutCancel), so the timeout failure and any post-step error
|
||||||
// survive into the post phase and the job is still reported as failed.
|
// survive into the post phase and the job is still reported as failed.
|
||||||
func TestPostStepsContextDeadlinePreservesJobError(t *testing.T) {
|
func TestPostStepsContextDeadlinePreservesJobError(t *testing.T) {
|
||||||
base := common.WithJobErrorContainer(context.Background())
|
base := common.WithJobErrorContainer(t.Context())
|
||||||
common.SetJobError(base, assert.AnError)
|
common.SetJobError(base, assert.AnError)
|
||||||
expired, cancel := context.WithDeadline(base, time.Now().Add(-time.Hour))
|
expired, cancel := context.WithDeadline(base, time.Now().Add(-time.Hour))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -292,14 +292,14 @@ func TestReportStepErrorTreatsCancelAsInterruption(t *testing.T) {
|
|||||||
rc := &RunContext{}
|
rc := &RunContext{}
|
||||||
|
|
||||||
// stray read cancellation while the job context is live: ignored, not a failure
|
// stray read cancellation while the job context is live: ignored, not a failure
|
||||||
live := common.WithJobErrorContainer(context.Background())
|
live := common.WithJobErrorContainer(t.Context())
|
||||||
reportStepError(live, rc, context.Canceled)
|
reportStepError(live, rc, context.Canceled)
|
||||||
require.NoError(t, common.JobError(live))
|
require.NoError(t, common.JobError(live))
|
||||||
assert.False(t, rc.jobFailed)
|
assert.False(t, rc.jobFailed)
|
||||||
assert.False(t, rc.jobCancelled)
|
assert.False(t, rc.jobCancelled)
|
||||||
|
|
||||||
// genuine job cancellation: recorded as cancelled, still not a failure
|
// genuine job cancellation: recorded as cancelled, still not a failure
|
||||||
cancelled, cancel := context.WithCancel(common.WithJobErrorContainer(context.Background()))
|
cancelled, cancel := context.WithCancel(common.WithJobErrorContainer(t.Context()))
|
||||||
cancel()
|
cancel()
|
||||||
reportStepError(cancelled, rc, context.Canceled)
|
reportStepError(cancelled, rc, context.Canceled)
|
||||||
require.NoError(t, common.JobError(cancelled))
|
require.NoError(t, common.JobError(cancelled))
|
||||||
@@ -307,7 +307,7 @@ func TestReportStepErrorTreatsCancelAsInterruption(t *testing.T) {
|
|||||||
assert.True(t, rc.jobCancelled)
|
assert.True(t, rc.jobCancelled)
|
||||||
|
|
||||||
// a real error still fails the job
|
// a real error still fails the job
|
||||||
failed := common.WithJobErrorContainer(context.Background())
|
failed := common.WithJobErrorContainer(t.Context())
|
||||||
reportStepError(failed, rc, assert.AnError)
|
reportStepError(failed, rc, assert.AnError)
|
||||||
require.ErrorIs(t, common.JobError(failed), assert.AnError)
|
require.ErrorIs(t, common.JobError(failed), assert.AnError)
|
||||||
assert.True(t, rc.jobFailed)
|
assert.True(t, rc.jobFailed)
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ func (rc *RunContext) refuseUnsecureCommand(ctx context.Context, command string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The step executor logs the failure itself, so keep this line's wording distinct.
|
// The step executor logs the failure itself, so keep this line's wording distinct.
|
||||||
common.Logger(ctx).WithField(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(fmt.Sprintf(
|
common.Logger(ctx).With(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(fmt.Sprintf(
|
||||||
"The `%s` command is disabled: it can set the environment of every later step from untrusted output. "+
|
"The `%s` command is disabled: it can set the environment of every later step from untrusted output. "+
|
||||||
"Write to $GITHUB_ENV or $GITHUB_PATH instead, or set ACTIONS_ALLOW_UNSECURE_COMMANDS to allow it",
|
"Write to $GITHUB_ENV or $GITHUB_PATH instead, or set ACTIONS_ALLOW_UNSECURE_COMMANDS to allow it",
|
||||||
command)))
|
command)))
|
||||||
@@ -159,7 +159,7 @@ func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg
|
|||||||
if fromCommand {
|
if fromCommand {
|
||||||
message = "Can't update NODE_OPTIONS environment variable using ::set-env:: command."
|
message = "Can't update NODE_OPTIONS environment variable using ::set-env:: command."
|
||||||
}
|
}
|
||||||
common.Logger(ctx).WithField(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(message))
|
common.Logger(ctx).With(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(message))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
common.Logger(ctx).Infof("::set-env:: %s=%s", name, arg)
|
common.Logger(ctx).Infof("::set-env:: %s=%s", name, arg)
|
||||||
|
|||||||
+18
-19
@@ -6,15 +6,14 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -26,8 +25,8 @@ func unsecureRC() *RunContext {
|
|||||||
|
|
||||||
func TestSetEnv(t *testing.T) {
|
func TestSetEnv(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
@@ -37,7 +36,7 @@ func TestSetEnv(t *testing.T) {
|
|||||||
rc.setEnvFile(ctx, map[string]string{"name": "node_options"}, "--require env.js")
|
rc.setEnvFile(ctx, map[string]string{"name": "node_options"}, "--require env.js")
|
||||||
a.NotContains(rc.Env, "NODE_OPTIONS")
|
a.NotContains(rc.Env, "NODE_OPTIONS")
|
||||||
a.NotContains(rc.Env, "node_options")
|
a.NotContains(rc.Env, "node_options")
|
||||||
entries := hook.AllEntries()
|
entries := hook.AllRecords()
|
||||||
require.Len(t, entries, 3)
|
require.Len(t, entries, 3)
|
||||||
a.Equal("##[error]Can't update NODE_OPTIONS environment variable using ::set-env:: command.", entries[1].Message)
|
a.Equal("##[error]Can't update NODE_OPTIONS environment variable using ::set-env:: command.", entries[1].Message)
|
||||||
a.Equal("##[error]Can't store NODE_OPTIONS output parameter using '$GITHUB_ENV' command.", entries[2].Message)
|
a.Equal("##[error]Can't store NODE_OPTIONS output parameter using '$GITHUB_ENV' command.", entries[2].Message)
|
||||||
@@ -45,7 +44,7 @@ func TestSetEnv(t *testing.T) {
|
|||||||
|
|
||||||
func TestStopCommandsKeepsSuppressedLinesInLog(t *testing.T) {
|
func TestStopCommandsKeepsSuppressedLinesInLog(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
@@ -68,7 +67,7 @@ func TestStopCommandsKeepsSuppressedLinesInLog(t *testing.T) {
|
|||||||
|
|
||||||
func TestSetOutput(t *testing.T) {
|
func TestSetOutput(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := new(RunContext)
|
rc := new(RunContext)
|
||||||
rc.StepResults = make(map[string]*model.StepResult)
|
rc.StepResults = make(map[string]*model.StepResult)
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
@@ -108,7 +107,7 @@ func TestSetOutput(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddpath(t *testing.T) {
|
func TestAddpath(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
@@ -120,10 +119,10 @@ func TestAddpath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStopCommands(t *testing.T) {
|
func TestStopCommands(t *testing.T) {
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
|
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
@@ -137,7 +136,7 @@ func TestStopCommands(t *testing.T) {
|
|||||||
a.Equal("abcd", rc.Env["x"])
|
a.Equal("abcd", rc.Env["x"])
|
||||||
|
|
||||||
messages := make([]string, 0)
|
messages := make([]string, 0)
|
||||||
for _, entry := range hook.AllEntries() {
|
for _, entry := range hook.AllRecords() {
|
||||||
messages = append(messages, entry.Message)
|
messages = append(messages, entry.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +148,7 @@ func TestStopCommands(t *testing.T) {
|
|||||||
func TestStopCommandsResumesOnCommandNamedToken(t *testing.T) {
|
func TestStopCommandsResumesOnCommandNamedToken(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(context.Background())
|
handler := rc.commandHandler(t.Context())
|
||||||
|
|
||||||
handler("::stop-commands::add-mask\n")
|
handler("::stop-commands::add-mask\n")
|
||||||
handler("::set-env name=x::suppressed\n")
|
handler("::set-env name=x::suppressed\n")
|
||||||
@@ -162,7 +161,7 @@ func TestStopCommandsResumesOnCommandNamedToken(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddpathADO(t *testing.T) {
|
func TestAddpathADO(t *testing.T) {
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := unsecureRC()
|
rc := unsecureRC()
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
|
|
||||||
@@ -174,17 +173,17 @@ func TestAddpathADO(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddmask(t *testing.T) {
|
func TestAddmask(t *testing.T) {
|
||||||
logger, hook := test.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
|
|
||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
loggerCtx := common.WithLogger(ctx, logger)
|
loggerCtx := common.WithLogger(ctx, logger)
|
||||||
|
|
||||||
rc := new(RunContext)
|
rc := new(RunContext)
|
||||||
handler := rc.commandHandler(loggerCtx)
|
handler := rc.commandHandler(loggerCtx)
|
||||||
handler("::ADD-MASK::my::secret")
|
handler("::ADD-MASK::my::secret")
|
||||||
|
|
||||||
a.Equal("***", hook.LastEntry().Message)
|
a.Equal("***", hook.LastRecord().Message)
|
||||||
a.Equal([]string{"my::secret"}, rc.Masks)
|
a.Equal([]string{"my::secret"}, rc.Masks)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +230,7 @@ func TestAddmaskUsemask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
re := captureOutput(t, func() {
|
re := captureOutput(t, func() {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks, map[string]any{})
|
ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks, map[string]any{})
|
||||||
|
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
@@ -248,7 +247,7 @@ func TestSaveState(t *testing.T) {
|
|||||||
StepResults: map[string]*model.StepResult{},
|
StepResults: map[string]*model.StepResult{},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
handler := rc.commandHandler(ctx)
|
handler := rc.commandHandler(ctx)
|
||||||
handler("::save-state name=state-name::state-value\n")
|
handler("::save-state name=state-name::state-value\n")
|
||||||
@@ -281,7 +280,7 @@ func TestUnsecureCommands(t *testing.T) {
|
|||||||
a := assert.New(t)
|
a := assert.New(t)
|
||||||
rc := &RunContext{Env: tt.jobEnv}
|
rc := &RunContext{Env: tt.jobEnv}
|
||||||
rc.setCurrentStepEnv(tt.stepEnv)
|
rc.setCurrentStepEnv(tt.stepEnv)
|
||||||
handler := rc.commandHandler(context.Background())
|
handler := rc.commandHandler(t.Context())
|
||||||
|
|
||||||
handler("::set-env name=x::valz\n")
|
handler("::set-env name=x::valz\n")
|
||||||
handler("::add-path::/opt/bin\n")
|
handler("::add-path::/opt/bin\n")
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
cerrdefs "github.com/containerd/errdefs"
|
cerrdefs "github.com/containerd/errdefs"
|
||||||
"github.com/moby/moby/client"
|
"github.com/moby/moby/client"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -83,10 +83,10 @@ func TestDockerProxyMountedJob(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
hook := &test.Hook{}
|
hook := logtest.New()
|
||||||
require.NoError(t, runner.NewPlanExecutor(plan)(common.WithLoggerHook(ctx, hook)))
|
require.NoError(t, runner.NewPlanExecutor(plan)(common.WithLoggerHandler(ctx, hook)))
|
||||||
var messages []string
|
var messages []string
|
||||||
for _, entry := range hook.AllEntries() {
|
for _, entry := range hook.AllRecords() {
|
||||||
messages = append(messages, strings.TrimSpace(entry.Message))
|
messages = append(messages, strings.TrimSpace(entry.Message))
|
||||||
}
|
}
|
||||||
require.Contains(t, messages, "docker proxy post verified")
|
require.Contains(t, messages, "docker proxy post verified")
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -82,7 +81,7 @@ func createRunContext(t *testing.T) *RunContext {
|
|||||||
|
|
||||||
func TestEvaluateRunContext(t *testing.T) {
|
func TestEvaluateRunContext(t *testing.T) {
|
||||||
rc := createRunContext(t)
|
rc := createRunContext(t)
|
||||||
ee := rc.NewExpressionEvaluator(context.Background())
|
ee := rc.NewExpressionEvaluator(t.Context())
|
||||||
|
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
in string
|
in string
|
||||||
@@ -142,7 +141,7 @@ func TestEvaluateRunContext(t *testing.T) {
|
|||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.in, func(t *testing.T) {
|
t.Run(table.in, func(t *testing.T) {
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
out, err := ee.evaluate(context.Background(), table.in, exprparser.DefaultStatusCheckNone)
|
out, err := ee.evaluate(t.Context(), table.in, exprparser.DefaultStatusCheckNone)
|
||||||
if table.errMesg == "" {
|
if table.errMesg == "" {
|
||||||
assertObject.NoError(err, table.in) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err, table.in) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assertObject.Equal(table.out, out, table.in)
|
assertObject.Equal(table.out, out, table.in)
|
||||||
@@ -162,7 +161,7 @@ func TestEvaluateStep(t *testing.T) {
|
|||||||
env: map[string]string{"INPUT_FORGED": "leaked"},
|
env: map[string]string{"INPUT_FORGED": "leaked"},
|
||||||
}
|
}
|
||||||
|
|
||||||
ee := rc.NewStepExpressionEvaluator(context.Background(), step)
|
ee := rc.NewStepExpressionEvaluator(t.Context(), step)
|
||||||
|
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
in string
|
in string
|
||||||
@@ -184,7 +183,7 @@ func TestEvaluateStep(t *testing.T) {
|
|||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.in, func(t *testing.T) {
|
t.Run(table.in, func(t *testing.T) {
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
out, err := ee.evaluate(context.Background(), table.in, exprparser.DefaultStatusCheckNone)
|
out, err := ee.evaluate(t.Context(), table.in, exprparser.DefaultStatusCheckNone)
|
||||||
if table.errMesg == "" {
|
if table.errMesg == "" {
|
||||||
assertObject.NoError(err, table.in) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err, table.in) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assertObject.Equal(table.out, out, table.in)
|
assertObject.Equal(table.out, out, table.in)
|
||||||
@@ -224,7 +223,7 @@ func TestInterpolate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ee := rc.NewExpressionEvaluator(context.Background())
|
ee := rc.NewExpressionEvaluator(t.Context())
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
in string
|
in string
|
||||||
out string
|
out string
|
||||||
@@ -270,14 +269,14 @@ func TestInterpolate(t *testing.T) {
|
|||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run("interpolate", func(t *testing.T) {
|
t.Run("interpolate", func(t *testing.T) {
|
||||||
out, err := ee.Interpolate(context.Background(), table.in)
|
out, err := ee.Interpolate(t.Context(), table.in)
|
||||||
require.NoError(t, err, table.in)
|
require.NoError(t, err, table.in)
|
||||||
assert.Equal(t, table.out, out, table.in)
|
assert.Equal(t, table.out, out, table.in)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, in := range []string{"${{ 1) && (2 }}", "run ${{ 1) && (2 }} now", "${{ 1"} {
|
for _, in := range []string{"${{ 1) && (2 }}", "run ${{ 1) && (2 }} now", "${{ 1"} {
|
||||||
_, err := ee.Interpolate(context.Background(), in)
|
_, err := ee.Interpolate(t.Context(), in)
|
||||||
assert.Error(t, err, in)
|
assert.Error(t, err, in)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -30,7 +29,7 @@ func requireLinuxDocker(t *testing.T) {
|
|||||||
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
||||||
func requireDocker(t *testing.T) {
|
func requireDocker(t *testing.T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cli, err := container.GetDockerClient(ctx)
|
cli, err := container.GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("skipping: docker client unavailable: %v", err)
|
t.Skipf("skipping: docker client unavailable: %v", err)
|
||||||
|
|||||||
+47
-21
@@ -29,7 +29,10 @@ import (
|
|||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxJobSummaryBytes = 1024 * 1024
|
const (
|
||||||
|
jobCleanupTimeout = time.Minute
|
||||||
|
maxJobSummaryBytes = 1024 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
// jobSummaryTruncationMarker is appended to a summary that exceeded the size limit
|
// jobSummaryTruncationMarker is appended to a summary that exceeded the size limit
|
||||||
// so the rendered output makes the truncation visible instead of silently cutting off.
|
// so the rendered output makes the truncation visible instead of silently cutting off.
|
||||||
@@ -89,14 +92,14 @@ func printPrepareActions(rc *RunContext, preparers []actionPreparer) common.Exec
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
rawLogger.Infof("Prepare all required actions")
|
rawLogger.Infof("Prepare all required actions")
|
||||||
|
|
||||||
for _, preparer := range preparers {
|
for _, preparer := range preparers {
|
||||||
if err := preparer.prepareActionExecutor()(ctx); err != nil {
|
if err := preparer.prepareActionExecutor()(ctx); err != nil {
|
||||||
// No step has run yet, so the failure belongs to the job.
|
// No step has run yet, so the failure belongs to the job.
|
||||||
reportStepError(ctx, rc, err)
|
reportStepError(ctx, rc, err)
|
||||||
return err
|
return setupError{err}
|
||||||
}
|
}
|
||||||
reference, sha, ok := preparer.actionDownloadInfo()
|
reference, sha, ok := preparer.actionDownloadInfo()
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -112,6 +115,11 @@ func printPrepareActions(rc *RunContext, preparers []actionPreparer) common.Exec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupError is a failure of the job setup, such as an action that cannot be downloaded.
|
||||||
|
type setupError struct{ error }
|
||||||
|
|
||||||
|
func (e setupError) Unwrap() error { return e.error }
|
||||||
|
|
||||||
// printCompleteJobName closes the setup section the way actions/runner ends its "Set up job" step.
|
// printCompleteJobName closes the setup section the way actions/runner ends its "Set up job" step.
|
||||||
func printCompleteJobName(rc *RunContext) common.Executor {
|
func printCompleteJobName(rc *RunContext) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
@@ -123,7 +131,7 @@ func printCompleteJobName(rc *RunContext) common.Executor {
|
|||||||
if name == "" && rc.Run != nil {
|
if name == "" && rc.Run != nil {
|
||||||
name = rc.Run.JobID
|
name = rc.Run.JobID
|
||||||
}
|
}
|
||||||
common.Logger(ctx).WithField(rawOutputField, true).Infof("Complete job name: %s", name)
|
common.Logger(ctx).With(rawOutputField, true).Infof("Complete job name: %s", name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,20 +144,18 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
preparers := make([]actionPreparer, 0)
|
preparers := make([]actionPreparer, 0)
|
||||||
var postExecutor common.Executor
|
var postExecutor common.Executor
|
||||||
|
|
||||||
steps = append(steps, func(ctx context.Context) error {
|
|
||||||
logger := common.Logger(ctx)
|
|
||||||
if len(info.matrix()) > 0 {
|
|
||||||
logger.Infof("Matrix: %v", info.matrix())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
infoSteps := info.steps()
|
infoSteps := info.steps()
|
||||||
|
|
||||||
if len(infoSteps) == 0 {
|
if len(infoSteps) == 0 {
|
||||||
return common.NewDebugExecutor("No steps found")
|
return common.NewDebugExecutor("No steps found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preSteps = append(preSteps, func(ctx context.Context) error {
|
||||||
|
if len(info.matrix()) > 0 {
|
||||||
|
common.Logger(ctx).Infof("Matrix: %v", info.matrix())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
preSteps = append(preSteps, func(ctx context.Context) error {
|
preSteps = append(preSteps, func(ctx context.Context) error {
|
||||||
// Have to be skipped for some Tests
|
// Have to be skipped for some Tests
|
||||||
if rc.Run == nil {
|
if rc.Run == nil {
|
||||||
@@ -228,14 +234,15 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
|
|
||||||
// The setup section of the job log. The started hook goes first, so what it sets up is
|
// The setup section of the job log. The started hook goes first, so what it sets up is
|
||||||
// in place for the first action download and the first step.
|
// in place for the first action download and the first step.
|
||||||
|
// The pre steps come after it, as steps of their own.
|
||||||
preSteps = append(preSteps, rc.runJobStartedHook)
|
preSteps = append(preSteps, rc.runJobStartedHook)
|
||||||
preSteps = append(preSteps, printPrepareActions(rc, preparers))
|
preSteps = append(preSteps, printPrepareActions(rc, preparers))
|
||||||
preSteps = append(preSteps, stepPreSteps...)
|
|
||||||
preSteps = append(preSteps, printCompleteJobName(rc))
|
preSteps = append(preSteps, printCompleteJobName(rc))
|
||||||
|
preSteps = append(preSteps, stepPreSteps...)
|
||||||
|
|
||||||
// Ahead of the teardown below, while the job environment is still up.
|
// Ahead of the teardown below, while the job environment is still up.
|
||||||
postExecutor = postExecutor.Finally(rc.runJobCompletedHook)
|
postExecutor = postExecutor.Finally(useJobStageLogger(StageComplete, rc.runJobCompletedHook))
|
||||||
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
postExecutor = postExecutor.Finally(useJobStageLogger(StageComplete, func(ctx context.Context) error {
|
||||||
// swallowed: a bad output fails this job, it must not abandon the rest of the plan
|
// swallowed: a bad output fails this job, it must not abandon the rest of the plan
|
||||||
if err := info.interpolateOutputs()(ctx); err != nil {
|
if err := info.interpolateOutputs()(ctx); err != nil {
|
||||||
reportStepError(ctx, rc, err)
|
reportStepError(ctx, rc, err)
|
||||||
@@ -243,13 +250,13 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
reportStepError(ctx, rc, err)
|
reportStepError(ctx, rc, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}))
|
||||||
|
|
||||||
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
postExecutor = postExecutor.Finally(useJobStageLogger(StageComplete, func(ctx context.Context) error {
|
||||||
jobError := common.JobError(ctx)
|
jobError := common.JobError(ctx)
|
||||||
var err error
|
var err error
|
||||||
// always allow 1 min for stopping and removing the runner, even if we were cancelled
|
// always allow 1 min for stopping and removing the runner, even if we were cancelled
|
||||||
ctx, cancel := context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), time.Minute)
|
ctx, cancel := context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), jobCleanupTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
@@ -262,7 +269,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
|
|||||||
rc.removeDockerActionImages(ctx)
|
rc.removeDockerActionImages(ctx)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
})
|
}))
|
||||||
|
|
||||||
return common.Executor(func(ctx context.Context) error {
|
return common.Executor(func(ctx context.Context) error {
|
||||||
if err := info.startContainer()(ctx); err != nil {
|
if err := info.startContainer()(ctx); err != nil {
|
||||||
@@ -316,6 +323,9 @@ func newStepsExecutor(rc *RunContext, preSteps, steps []common.Executor) common.
|
|||||||
mainExecutor := newMainStepsExecutor(rc, steps)
|
mainExecutor := newMainStepsExecutor(rc, steps)
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
preErr := preExecutor(ctx)
|
preErr := preExecutor(ctx)
|
||||||
|
if errors.As(preErr, new(setupError)) {
|
||||||
|
return preErr // as on GitHub, no step runs after the job setup failed
|
||||||
|
}
|
||||||
mainErr := mainExecutor(ctx)
|
mainErr := mainExecutor(ctx)
|
||||||
if preErr != nil {
|
if preErr != nil {
|
||||||
return preErr
|
return preErr
|
||||||
@@ -409,7 +419,7 @@ func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success boo
|
|||||||
jobResultMessage = "cancelled"
|
jobResultMessage = "cancelled"
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithField("jobResult", jobResult).Infof("Job %s", jobResultMessage)
|
logger.With("jobResult", jobResult).Infof("Job %s", jobResultMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// evaluateJobEnv returns the workflow and job env evaluated, as far as it got on error, leaving rc.Env raw.
|
// evaluateJobEnv returns the workflow and job env evaluated, as far as it got on error, leaving rc.Env raw.
|
||||||
@@ -532,7 +542,7 @@ func tryUploadJobSummary(ctx context.Context, rc *RunContext) {
|
|||||||
"/jobs/" + strconv.FormatInt(jobID, 10) + "/steps/"
|
"/jobs/" + strconv.FormatInt(jobID, 10) + "/steps/"
|
||||||
actPath := rc.JobContainer.GetActPath()
|
actPath := rc.JobContainer.GetActPath()
|
||||||
// Reuse a single client across all step uploads so connections can be pooled.
|
// Reuse a single client across all step uploads so connections can be pooled.
|
||||||
client := &http.Client{Timeout: jobSummaryUploadRequestTimeout}
|
client := &http.Client{Timeout: jobSummaryUploadRequestTimeout, Transport: rc.Config.instanceTransportFor(base)}
|
||||||
for i := range rc.Run.Job().Steps {
|
for i := range rc.Run.Job().Steps {
|
||||||
summaryPath := path.Join(actPath, "workflow", "step-summary-"+strconv.Itoa(i)+".md")
|
summaryPath := path.Join(actPath, "workflow", "step-summary-"+strconv.Itoa(i)+".md")
|
||||||
body, ok := readSingleFileFromContainerArchive(ctx, rc.JobContainer, summaryPath, maxJobSummaryBytes)
|
body, ok := readSingleFileFromContainerArchive(ctx, rc.JobContainer, summaryPath, maxJobSummaryBytes)
|
||||||
@@ -544,6 +554,15 @@ func tryUploadJobSummary(ctx context.Context, rc *RunContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For Gitea
|
||||||
|
// instanceTransportFor returns InstanceTransport only for URLs on the registered address, as steps can rewrite the summary URL through GITHUB_ENV.
|
||||||
|
func (c Config) instanceTransportFor(rawURL string) http.RoundTripper {
|
||||||
|
if !strings.HasPrefix(rawURL, c.GitHubInstance+"/") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.InstanceTransport
|
||||||
|
}
|
||||||
|
|
||||||
// extractJobIDFromRuntimeToken returns the JobID claim from an ACTIONS_RUNTIME_TOKEN JWT
|
// extractJobIDFromRuntimeToken returns the JobID claim from an ACTIONS_RUNTIME_TOKEN JWT
|
||||||
// without verifying its signature. Returns 0 if the token is unparseable or has no JobID.
|
// without verifying its signature. Returns 0 if the token is unparseable or has no JobID.
|
||||||
func extractJobIDFromRuntimeToken(token string) int64 {
|
func extractJobIDFromRuntimeToken(token string) int64 {
|
||||||
@@ -672,6 +691,13 @@ func archiveEntryMatchesPath(entryName, requestedPath string) bool {
|
|||||||
return entryName == requestedPath || entryName == path.Base(requestedPath)
|
return entryName == requestedPath || entryName == path.Base(requestedPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// useJobStageLogger tags the log entries of a job level executor with the stage they belong to.
|
||||||
|
func useJobStageLogger(stage string, executor common.Executor) common.Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
return executor(common.WithLogger(ctx, common.Logger(ctx).With("stage", stage)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, rc.ExprEval.InterpolateName(ctx, stepModel.String()), stage.String())
|
ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, rc.ExprEval.InterpolateName(ctx, stepModel.String()), stage.String())
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
logrustest "github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -44,7 +44,7 @@ func TestJobExecutor(t *testing.T) {
|
|||||||
{workdir, "job-nil-step", "push", "invalid Step 0: missing run or uses key", platforms, secrets},
|
{workdir, "job-nil-step", "push", "invalid Step 0: missing run or uses key", platforms, secrets},
|
||||||
}
|
}
|
||||||
// These tests are sufficient to only check syntax.
|
// These tests are sufficient to only check syntax.
|
||||||
ctx := common.WithDryrun(context.Background(), true)
|
ctx := common.WithDryrun(t.Context(), true)
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.workflowPath, func(t *testing.T) {
|
t.Run(table.workflowPath, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -136,11 +136,8 @@ func (apm *actionPreparerMock) actionDownloadInfo() (string, string, bool) {
|
|||||||
|
|
||||||
func TestPrintPrepareActionsGolden(t *testing.T) {
|
func TestPrintPrepareActionsGolden(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := log.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
ctx := common.WithLogger(t.Context(), logger.With("job", "j1"))
|
||||||
logger.SetLevel(log.InfoLevel)
|
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
|
||||||
|
|
||||||
preparers := []actionPreparer{
|
preparers := []actionPreparer{
|
||||||
&actionPreparerMock{reference: "actions/checkout@v7", sha: "9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0", ok: true},
|
&actionPreparerMock{reference: "actions/checkout@v7", sha: "9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0", ok: true},
|
||||||
@@ -162,10 +159,8 @@ func TestPrintPrepareActionsGolden(t *testing.T) {
|
|||||||
|
|
||||||
func TestPrintPrepareActionsSkipsWithoutActions(t *testing.T) {
|
func TestPrintPrepareActionsSkipsWithoutActions(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := log.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
ctx := common.WithLogger(t.Context(), logger.With("job", "j1"))
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
|
||||||
|
|
||||||
require.NoError(t, printPrepareActions(&RunContext{}, nil)(ctx))
|
require.NoError(t, printPrepareActions(&RunContext{}, nil)(ctx))
|
||||||
|
|
||||||
@@ -173,8 +168,8 @@ func TestPrintPrepareActionsSkipsWithoutActions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintPrepareActionsFailsJobOnDownloadError(t *testing.T) {
|
func TestPrintPrepareActionsFailsJobOnDownloadError(t *testing.T) {
|
||||||
logger, _ := logrustest.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
ctx := common.WithJobErrorContainer(common.WithLogger(context.Background(), logger.WithField("job", "j1")))
|
ctx := common.WithJobErrorContainer(common.WithLogger(t.Context(), logger.With("job", "j1")))
|
||||||
|
|
||||||
downloadErr := errors.New("failed to fetch \"actions/checkout\"")
|
downloadErr := errors.New("failed to fetch \"actions/checkout\"")
|
||||||
rc := &RunContext{}
|
rc := &RunContext{}
|
||||||
@@ -192,6 +187,27 @@ func TestPrintPrepareActionsFailsJobOnDownloadError(t *testing.T) {
|
|||||||
assert.Zero(t, remaining.prepared)
|
assert.Zero(t, remaining.prepared)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewStepsExecutorSkipsStepsAfterSetupFailure(t *testing.T) {
|
||||||
|
for name, tt := range map[string]struct {
|
||||||
|
preErr error
|
||||||
|
mainRan bool
|
||||||
|
}{
|
||||||
|
"setup failure": {preErr: setupError{assert.AnError}},
|
||||||
|
"pre step failure": {preErr: assert.AnError, mainRan: true}, // always() steps still run, as on GitHub
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
ctx := common.WithJobErrorContainer(common.WithLogger(context.Background(), logging.New(logtest.New())))
|
||||||
|
mainRan := false
|
||||||
|
err := newStepsExecutor(&RunContext{},
|
||||||
|
[]common.Executor{func(context.Context) error { return tt.preErr }},
|
||||||
|
[]common.Executor{func(context.Context) error { mainRan = true; return nil }},
|
||||||
|
)(ctx)
|
||||||
|
require.ErrorIs(t, err, assert.AnError)
|
||||||
|
assert.Equal(t, tt.mainRan, mainRan)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrintCompleteJobName(t *testing.T) {
|
func TestPrintCompleteJobName(t *testing.T) {
|
||||||
for name, tt := range map[string]struct {
|
for name, tt := range map[string]struct {
|
||||||
rc *RunContext
|
rc *RunContext
|
||||||
@@ -203,10 +219,8 @@ func TestPrintCompleteJobName(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := log.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
ctx := common.WithLogger(t.Context(), logger.With("job", "j1"))
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
ctx := common.WithLogger(context.Background(), logger.WithFields(log.Fields{"job": "j1"}))
|
|
||||||
|
|
||||||
require.NoError(t, printCompleteJobName(tt.rc)(ctx))
|
require.NoError(t, printCompleteJobName(tt.rc)(ctx))
|
||||||
|
|
||||||
@@ -225,7 +239,7 @@ type actionStepMock struct {
|
|||||||
// every action is downloaded before any step runs, and the job name closes the section. A pre
|
// every action is downloaded before any step runs, and the job name closes the section. A pre
|
||||||
// step that downloaded its own action would leave the log interleaved with the downloads.
|
// step that downloaded its own action would leave the log interleaved with the downloads.
|
||||||
func TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep(t *testing.T) {
|
func TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep(t *testing.T) {
|
||||||
ctx := common.WithJobErrorContainer(context.Background())
|
ctx := common.WithJobErrorContainer(t.Context())
|
||||||
jim := &jobInfoMock{}
|
jim := &jobInfoMock{}
|
||||||
sfm := &stepFactoryMock{}
|
sfm := &stepFactoryMock{}
|
||||||
rc := &RunContext{
|
rc := &RunContext{
|
||||||
@@ -256,8 +270,9 @@ func TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep(t *testing.T) {
|
|||||||
apm := &actionPreparerMock{reference: "actions/checkout@v" + stepModel.ID, ok: true}
|
apm := &actionPreparerMock{reference: "actions/checkout@v" + stepModel.ID, ok: true}
|
||||||
sfm.On("newStep", stepModel, rc).Return(&actionStepMock{stepMock: sm, actionPreparerMock: apm}, nil)
|
sfm.On("newStep", stepModel, rc).Return(&actionStepMock{stepMock: sm, actionPreparerMock: apm}, nil)
|
||||||
|
|
||||||
sm.On("pre").Return(func(context.Context) error {
|
sm.On("pre").Return(func(ctx context.Context) error {
|
||||||
executorOrder = append(executorOrder, "pre"+stepModel.ID)
|
executorOrder = append(executorOrder, "pre"+stepModel.ID)
|
||||||
|
common.Logger(ctx).Infof("Run Pre %s", stepModel.ID)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
sm.On("main").Return(func(context.Context) error {
|
sm.On("main").Return(func(context.Context) error {
|
||||||
@@ -269,24 +284,31 @@ func TestNewJobExecutorDownloadsAllActionsBeforeTheFirstStep(t *testing.T) {
|
|||||||
defer sm.AssertExpectations(t)
|
defer sm.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
err := newJobExecutor(jim, sfm, rc)(common.WithLogger(ctx, logger.WithField("job", "test")))
|
err := newJobExecutor(jim, sfm, rc)(common.WithLogger(ctx, logger.With("job", "test")))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, []string{"pre1", "pre2", "step1", "step2"}, executorOrder)
|
assert.Equal(t, []string{"pre1", "pre2", "step1", "step2"}, executorOrder)
|
||||||
|
|
||||||
|
// the setup is logged ahead of the pre steps and the cleanup is tagged, so the reporter can show both as steps of their own
|
||||||
setup := make([]string, 0)
|
setup := make([]string, 0)
|
||||||
for _, entry := range hook.AllEntries() {
|
for _, entry := range hook.AllRecords() {
|
||||||
if strings.HasPrefix(entry.Message, "Prepare all required actions") || strings.HasPrefix(entry.Message, "Download action") ||
|
if strings.HasPrefix(entry.Message, "Prepare all required actions") || strings.HasPrefix(entry.Message, "Download action") ||
|
||||||
strings.HasPrefix(entry.Message, "Complete job name") {
|
strings.HasPrefix(entry.Message, "Complete job name") || strings.HasPrefix(entry.Message, "Run Pre") {
|
||||||
setup = append(setup, entry.Message)
|
setup = append(setup, entry.Message)
|
||||||
}
|
}
|
||||||
|
if _, ok := entry.Attr("jobResult"); ok || strings.HasPrefix(entry.Message, "Cleaning up container") {
|
||||||
|
stage, _ := entry.Attr("stage")
|
||||||
|
assert.Equal(t, StageComplete, stage, entry.Message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assert.Equal(t, []string{
|
assert.Equal(t, []string{
|
||||||
"Prepare all required actions",
|
"Prepare all required actions",
|
||||||
"Download action repository 'actions/checkout@v1'",
|
"Download action repository 'actions/checkout@v1'",
|
||||||
"Download action repository 'actions/checkout@v2'",
|
"Download action repository 'actions/checkout@v2'",
|
||||||
"Complete job name: test",
|
"Complete job name: test",
|
||||||
|
"Run Pre 1",
|
||||||
|
"Run Pre 2",
|
||||||
}, setup)
|
}, setup)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,7 +484,7 @@ func TestNewJobExecutor(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fmt.Printf("::group::%s\n", tt.name) //nolint:forbidigo // pre-existing issue from nektos/act
|
fmt.Printf("::group::%s\n", tt.name) //nolint:forbidigo // pre-existing issue from nektos/act
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(common.WithJobErrorContainer(context.Background()))
|
ctx, cancel := context.WithCancel(common.WithJobErrorContainer(t.Context()))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
jim := &jobInfoMock{}
|
jim := &jobInfoMock{}
|
||||||
sfm := &stepFactoryMock{}
|
sfm := &stepFactoryMock{}
|
||||||
@@ -610,7 +632,7 @@ func (ctx *controllableDeadlineContext) expire() {
|
|||||||
// still run against a fresh, non-expired context, and the job must still be
|
// still run against a fresh, non-expired context, and the job must still be
|
||||||
// reported as failed.
|
// reported as failed.
|
||||||
func TestNewJobExecutorRunsPostStepsAfterTimeout(t *testing.T) {
|
func TestNewJobExecutorRunsPostStepsAfterTimeout(t *testing.T) {
|
||||||
ctx := newControllableDeadlineContext(common.WithJobErrorContainer(context.Background()))
|
ctx := newControllableDeadlineContext(common.WithJobErrorContainer(t.Context()))
|
||||||
|
|
||||||
jim := &jobInfoMock{}
|
jim := &jobInfoMock{}
|
||||||
sfm := &stepFactoryMock{}
|
sfm := &stepFactoryMock{}
|
||||||
@@ -691,7 +713,7 @@ func TestSetJobResultMatrixContinueOnError(t *testing.T) {
|
|||||||
return &model.Plan{Stages: []*model.Stage{{Runs: []*model.Run{{Workflow: wf, JobID: "job1"}}}}}
|
return &model.Plan{Stages: []*model.Stage{{Runs: []*model.Run{{Workflow: wf, JobID: "job1"}}}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
// fail drives a single matrix combination through the failure path; each
|
// fail drives a single matrix combination through the failure path; each
|
||||||
// RunContext is its own jobInfo (rc implements jobInfo) and shares the job.
|
// RunContext is its own jobInfo (rc implements jobInfo) and shares the job.
|
||||||
@@ -786,7 +808,7 @@ func TestTryUploadJobSummaryRetriesTransientFailure(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
||||||
@@ -830,7 +852,7 @@ func TestTryUploadJobSummaryStopsAtPhaseTimeout(t *testing.T) {
|
|||||||
defer server.Close()
|
defer server.Close()
|
||||||
defer close(release)
|
defer close(release)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "step-summary-0.md", body: "# summary"}))),
|
||||||
@@ -858,7 +880,7 @@ func TestTryUploadJobSummaryStopsAtPhaseTimeout(t *testing.T) {
|
|||||||
cm.AssertExpectations(t)
|
cm.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTryUploadJobSummaryUploadsEachStepIndependently(t *testing.T) {
|
func TestTryUploadJobSummaryUploadsEachStepIndependentlyWithInstanceTransport(t *testing.T) {
|
||||||
runtimeToken := fakeRuntimeToken(34)
|
runtimeToken := fakeRuntimeToken(34)
|
||||||
|
|
||||||
type upload struct {
|
type upload struct {
|
||||||
@@ -866,7 +888,7 @@ func TestTryUploadJobSummaryUploadsEachStepIndependently(t *testing.T) {
|
|||||||
body string
|
body string
|
||||||
}
|
}
|
||||||
var got []upload
|
var got []upload
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
got = append(got, upload{r.URL.Path, string(body)})
|
got = append(got, upload{r.URL.Path, string(body)})
|
||||||
@@ -874,7 +896,7 @@ func TestTryUploadJobSummaryUploadsEachStepIndependently(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
// Three steps: 0 has content, 1 has empty content (skipped), 2 has content.
|
// Three steps: 0 has content, 1 has empty content (skipped), 2 has content.
|
||||||
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
cm.On("GetContainerArchive", mock.Anything, "/var/run/act/workflow/step-summary-0.md").Return(
|
||||||
@@ -896,6 +918,7 @@ func TestTryUploadJobSummaryUploadsEachStepIndependently(t *testing.T) {
|
|||||||
"ACTIONS_RUNTIME_TOKEN": runtimeToken,
|
"ACTIONS_RUNTIME_TOKEN": runtimeToken,
|
||||||
"GITEA_RUN_ID": "12",
|
"GITEA_RUN_ID": "12",
|
||||||
}, cm, 3)
|
}, cm, 3)
|
||||||
|
rc.Config.GitHubInstance, rc.Config.InstanceTransport = server.URL, server.Client().Transport
|
||||||
|
|
||||||
tryUploadJobSummary(ctx, rc)
|
tryUploadJobSummary(ctx, rc)
|
||||||
|
|
||||||
@@ -921,7 +944,7 @@ func TestTryUploadJobSummaryRequiresExactCapability(t *testing.T) {
|
|||||||
"GITEA_RUN_ID": "12",
|
"GITEA_RUN_ID": "12",
|
||||||
}, &containerMock{}, 1)
|
}, &containerMock{}, 1)
|
||||||
|
|
||||||
tryUploadJobSummary(context.Background(), rc)
|
tryUploadJobSummary(t.Context(), rc)
|
||||||
|
|
||||||
assert.Equal(t, 0, requests)
|
assert.Equal(t, 0, requests)
|
||||||
}
|
}
|
||||||
@@ -941,7 +964,7 @@ func TestTryUploadJobSummarySkipsWhenJobIDMissingFromToken(t *testing.T) {
|
|||||||
"GITEA_RUN_ID": "12",
|
"GITEA_RUN_ID": "12",
|
||||||
}, &containerMock{}, 1)
|
}, &containerMock{}, 1)
|
||||||
|
|
||||||
tryUploadJobSummary(context.Background(), rc)
|
tryUploadJobSummary(t.Context(), rc)
|
||||||
|
|
||||||
assert.Equal(t, 0, requests)
|
assert.Equal(t, 0, requests)
|
||||||
}
|
}
|
||||||
@@ -954,7 +977,7 @@ func TestExtractJobIDFromRuntimeToken(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReadSingleFileFromContainerArchiveFindsMatchingRegularFile(t *testing.T) {
|
func TestReadSingleFileFromContainerArchiveFindsMatchingRegularFile(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
io.NopCloser(bytes.NewReader(tarArchive(t,
|
io.NopCloser(bytes.NewReader(tarArchive(t,
|
||||||
@@ -973,8 +996,8 @@ func TestReadSingleFileFromContainerArchiveFindsMatchingRegularFile(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReadSingleFileFromContainerArchiveTruncatesWhenTooLarge(t *testing.T) {
|
func TestReadSingleFileFromContainerArchiveTruncatesWhenTooLarge(t *testing.T) {
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
content := strings.Repeat("a", 300)
|
content := strings.Repeat("a", 300)
|
||||||
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
@@ -991,15 +1014,15 @@ func TestReadSingleFileFromContainerArchiveTruncatesWhenTooLarge(t *testing.T) {
|
|||||||
assert.LessOrEqual(t, len(body), maxBytes)
|
assert.LessOrEqual(t, len(body), maxBytes)
|
||||||
keep := maxBytes - len(jobSummaryTruncationMarker)
|
keep := maxBytes - len(jobSummaryTruncationMarker)
|
||||||
assert.Equal(t, []byte(content[:keep]+jobSummaryTruncationMarker), body)
|
assert.Equal(t, []byte(content[:keep]+jobSummaryTruncationMarker), body)
|
||||||
if assert.Len(t, hook.Entries, 1) {
|
if assert.Len(t, hook.AllRecords(), 1) {
|
||||||
assert.Contains(t, hook.Entries[0].Message, "job summary truncated")
|
assert.Contains(t, hook.AllRecords()[0].Message, "job summary truncated")
|
||||||
}
|
}
|
||||||
cm.AssertExpectations(t)
|
cm.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadSingleFileFromContainerArchiveKeepsExactLimitWithoutWarning(t *testing.T) {
|
func TestReadSingleFileFromContainerArchiveKeepsExactLimitWithoutWarning(t *testing.T) {
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(context.Background(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(
|
||||||
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "SUMMARY.md", body: "abc"}))),
|
io.NopCloser(bytes.NewReader(tarArchive(t, tarEntry{name: "SUMMARY.md", body: "abc"}))),
|
||||||
@@ -1011,7 +1034,7 @@ func TestReadSingleFileFromContainerArchiveKeepsExactLimitWithoutWarning(t *test
|
|||||||
// A summary that is exactly at the limit is kept whole and not flagged as truncated.
|
// A summary that is exactly at the limit is kept whole and not flagged as truncated.
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, []byte("abc"), body)
|
assert.Equal(t, []byte("abc"), body)
|
||||||
assert.Empty(t, hook.Entries)
|
assert.Empty(t, hook.AllRecords())
|
||||||
cm.AssertExpectations(t)
|
cm.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1069,7 +1092,7 @@ func makeTestRC(t *testing.T, jobYAML string) *RunContext {
|
|||||||
Name: "workflow1",
|
Name: "workflow1",
|
||||||
Jobs: map[string]*model.Job{"job1": job},
|
Jobs: map[string]*model.Job{"job1": job},
|
||||||
}, nil)
|
}, nil)
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
rc.ExprEval = rc.NewExpressionEvaluator(t.Context())
|
||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1087,7 +1110,7 @@ func TestApplyJobTimeout(t *testing.T) {
|
|||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
rc := makeTestRC(t, tc.yaml)
|
rc := makeTestRC(t, tc.yaml)
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
newCtx, cancel := applyJobTimeout(ctx, rc, rc.Run.Job())
|
newCtx, cancel := applyJobTimeout(ctx, rc, rc.Run.Job())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, hasDeadline := newCtx.Deadline()
|
_, hasDeadline := newCtx.Deadline()
|
||||||
@@ -1111,7 +1134,7 @@ func TestEvaluateJobContinueOnError(t *testing.T) {
|
|||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
rc := makeTestRC(t, tc.yaml)
|
rc := makeTestRC(t, tc.yaml)
|
||||||
got := evaluateJobContinueOnError(context.Background(), rc, rc.Run.Job())
|
got := evaluateJobContinueOnError(t.Context(), rc, rc.Run.Job())
|
||||||
assert.Equal(t, tc.want, got)
|
assert.Equal(t, tc.want, got)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1176,7 +1199,7 @@ func TestTryUploadJobSummaryMasksSecrets(t *testing.T) {
|
|||||||
rc.Config.ExtraMasks = []string{"pr0xypw"}
|
rc.Config.ExtraMasks = []string{"pr0xypw"}
|
||||||
rc.Masks = []string{"runtime-added"}
|
rc.Masks = []string{"runtime-added"}
|
||||||
|
|
||||||
tryUploadJobSummary(context.Background(), rc)
|
tryUploadJobSummary(t.Context(), rc)
|
||||||
|
|
||||||
assert.Equal(t, "deployed true with *** and *** via ***", got)
|
assert.Equal(t, "deployed true with *** and *** via ***", got)
|
||||||
cm.AssertExpectations(t)
|
cm.AssertExpectations(t)
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (rc *RunContext) runJobHook(ctx context.Context, hookPath, name string) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd, shell := hookCommand(hookPath)
|
cmd, shell := hookCommand(hookPath)
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
defer rawLogger.Infof("::endgroup::")
|
defer rawLogger.Infof("::endgroup::")
|
||||||
rawLogger.Infof("::group::Run '%s'", EscapeCommandData(hookPath))
|
rawLogger.Infof("::group::Run '%s'", EscapeCommandData(hookPath))
|
||||||
rawLogger.Infof("A %s hook has been configured by the runner administrator", name)
|
rawLogger.Infof("A %s hook has been configured by the runner administrator", name)
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -64,8 +65,8 @@ func newHookRunContext(jobContainer *hookContainer, config *Config) (*RunContext
|
|||||||
Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {}}}},
|
Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {}}}},
|
||||||
JobContainer: jobContainer,
|
JobContainer: jobContainer,
|
||||||
}
|
}
|
||||||
logger, _ := test.NewNullLogger()
|
logger := logging.New(logtest.New())
|
||||||
ctx := common.WithJobErrorContainer(common.WithLogger(context.Background(), logger.WithField("test", true)))
|
ctx := common.WithJobErrorContainer(common.WithLogger(context.Background(), logger.With("test", true)))
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
||||||
return rc, ctx
|
return rc, ctx
|
||||||
}
|
}
|
||||||
|
|||||||
+136
-99
@@ -19,8 +19,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,6 +35,16 @@ const (
|
|||||||
gray = 37
|
gray = 37
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// The "stage" of the log entries of a step, and of the job level entries that finalize the job after its steps.
|
||||||
|
StagePre = "Pre"
|
||||||
|
StageMain = "Main"
|
||||||
|
StagePost = "Post"
|
||||||
|
StageComplete = "Complete"
|
||||||
|
// StepStartedField marks the log entry of a step that is about to run, unlike one that is skipped.
|
||||||
|
StepStartedField = "stepStarted"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
rawOutputField = "raw_output"
|
rawOutputField = "raw_output"
|
||||||
scriptLineCyanField = "script_line_cyan"
|
scriptLineCyanField = "script_line_cyan"
|
||||||
@@ -73,8 +83,9 @@ func WithMasks(ctx context.Context, masks *[]string) context.Context {
|
|||||||
return context.WithValue(ctx, masksContextKeyVal, masks)
|
return context.WithValue(ctx, masksContextKeyVal, masks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JobLoggerFactory supplies the handler a job's own log output goes to, in place of stdout.
|
||||||
type JobLoggerFactory interface {
|
type JobLoggerFactory interface {
|
||||||
WithJobLogger() *logrus.Logger
|
WithJobLogger() logging.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
type jobLoggerFactoryContextKey string
|
type jobLoggerFactoryContextKey string
|
||||||
@@ -89,84 +100,70 @@ func WithJobLoggerFactory(ctx context.Context, factory JobLoggerFactory) context
|
|||||||
func WithJobLogger(ctx context.Context, jobID, jobName string, config *Config, masks *[]string, matrix map[string]any) context.Context {
|
func WithJobLogger(ctx context.Context, jobID, jobName string, config *Config, masks *[]string, matrix map[string]any) context.Context {
|
||||||
ctx = WithMasks(ctx, masks)
|
ctx = WithMasks(ctx, masks)
|
||||||
|
|
||||||
var logger *logrus.Logger
|
var output logging.Handler
|
||||||
if jobLoggerFactory, ok := ctx.Value(jobLoggerFactoryContextKeyVal).(JobLoggerFactory); ok && jobLoggerFactory != nil {
|
if jobLoggerFactory, ok := ctx.Value(jobLoggerFactoryContextKeyVal).(JobLoggerFactory); ok && jobLoggerFactory != nil {
|
||||||
logger = jobLoggerFactory.WithJobLogger()
|
output = jobLoggerFactory.WithJobLogger()
|
||||||
|
} else if config.JSONLogger {
|
||||||
|
output = newJobLogJSONHandler(os.Stdout)
|
||||||
} else {
|
} else {
|
||||||
var formatter logrus.Formatter
|
mux.Lock()
|
||||||
if config.JSONLogger {
|
nextColor++
|
||||||
formatter = &logrus.JSONFormatter{}
|
color := colors[nextColor%len(colors)]
|
||||||
} else {
|
mux.Unlock()
|
||||||
mux.Lock()
|
output = newJobLogHandler(os.Stdout, color)
|
||||||
defer mux.Unlock()
|
|
||||||
nextColor++
|
|
||||||
formatter = &jobLogFormatter{color: colors[nextColor%len(colors)]}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger = logrus.New()
|
|
||||||
logger.SetOutput(os.Stdout)
|
|
||||||
logger.SetLevel(logrus.GetLevel())
|
|
||||||
logger.SetFormatter(formatter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{ // Adapt to Gitea
|
var handlers []logging.Handler
|
||||||
if hook := common.LoggerHook(ctx); hook != nil {
|
if h := common.LoggerHandler(ctx); h != nil { // Adapt to Gitea
|
||||||
logger.AddHook(hook)
|
handlers = append(handlers, h)
|
||||||
}
|
|
||||||
if config.JobLoggerLevel != nil {
|
|
||||||
logger.SetLevel(*config.JobLoggerLevel)
|
|
||||||
} else {
|
|
||||||
logger.SetLevel(logrus.TraceLevel)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
handlers = append(handlers, &maskedHandler{
|
||||||
logger.SetFormatter(&maskedFormatter{
|
Handler: output,
|
||||||
Formatter: logger.Formatter,
|
masker: valueMasker(config.InsecureSecrets, config.maskers()),
|
||||||
masker: valueMasker(config.InsecureSecrets, config.maskers()),
|
|
||||||
})
|
})
|
||||||
rtn := logger.WithFields(logrus.Fields{
|
|
||||||
"job": jobName,
|
level := logging.LevelTrace
|
||||||
"jobID": jobID,
|
if config.JobLoggerLevel != nil {
|
||||||
"dryrun": common.Dryrun(ctx),
|
level = *config.JobLoggerLevel
|
||||||
"matrix": matrix,
|
}
|
||||||
}).WithContext(ctx)
|
|
||||||
|
rtn := logging.New(logging.NewLevelHandler(logging.NewMultiHandler(handlers...), level)).With(
|
||||||
|
"job", jobName,
|
||||||
|
"jobID", jobID,
|
||||||
|
"dryrun", common.Dryrun(ctx),
|
||||||
|
"matrix", matrix,
|
||||||
|
).WithContext(ctx)
|
||||||
|
|
||||||
return common.WithLogger(ctx, rtn)
|
return common.WithLogger(ctx, rtn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context {
|
func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context {
|
||||||
ctx = WithMasks(ctx, masks)
|
ctx = WithMasks(ctx, masks)
|
||||||
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{}).WithContext(ctx))
|
return common.WithLogger(ctx, common.Logger(ctx).WithContext(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithCompositeStepLogger(ctx context.Context, stepID string) context.Context {
|
func WithCompositeStepLogger(ctx context.Context, stepID string) context.Context {
|
||||||
val := common.Logger(ctx)
|
stepIDs := append(slices.Clone(common.StepIDs(ctx)), stepID)
|
||||||
stepIDs := make([]string, 0)
|
ctx = common.WithStepIDs(ctx, stepIDs)
|
||||||
|
|
||||||
if logger, ok := val.(*logrus.Entry); ok {
|
return common.WithLogger(ctx, common.Logger(ctx).With("stepID", stepIDs).WithContext(ctx))
|
||||||
if oldStepIDs, ok := logger.Data["stepID"].([]string); ok {
|
|
||||||
stepIDs = append(stepIDs, oldStepIDs...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stepIDs = append(stepIDs, stepID)
|
|
||||||
|
|
||||||
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{
|
|
||||||
"stepID": stepIDs,
|
|
||||||
}).WithContext(ctx))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func withStepLogger(ctx context.Context, stepNumber int, stepID, stepName, stageName string) context.Context {
|
func withStepLogger(ctx context.Context, stepNumber int, stepID, stepName, stageName string) context.Context {
|
||||||
rtn := common.Logger(ctx).WithFields(logrus.Fields{
|
stepIDs := []string{stepID}
|
||||||
"stepNumber": stepNumber,
|
ctx = common.WithStepIDs(ctx, stepIDs)
|
||||||
"step": stepName,
|
|
||||||
"stepID": []string{stepID},
|
rtn := common.Logger(ctx).With(
|
||||||
"stage": stageName,
|
"stepNumber", stepNumber,
|
||||||
})
|
"step", stepName,
|
||||||
|
"stepID", stepIDs,
|
||||||
|
"stage", stageName,
|
||||||
|
)
|
||||||
return common.WithLogger(ctx, rtn)
|
return common.WithLogger(ctx, rtn)
|
||||||
}
|
}
|
||||||
|
|
||||||
type entryProcessor func(entry *logrus.Entry) *logrus.Entry
|
// entryProcessor rewrites a record before the output handler sees it.
|
||||||
|
type entryProcessor func(ctx context.Context, r logging.Record) logging.Record
|
||||||
|
|
||||||
var secretValueEncoders = []func(string) string{
|
var secretValueEncoders = []func(string) string{
|
||||||
func(v string) string { return base64.StdEncoding.EncodeToString([]byte(v)) },
|
func(v string) string { return base64.StdEncoding.EncodeToString([]byte(v)) },
|
||||||
@@ -321,16 +318,16 @@ func valueMasker(insecureSecrets bool, oldnew []string) entryProcessor {
|
|||||||
replacer *strings.Replacer
|
replacer *strings.Replacer
|
||||||
)
|
)
|
||||||
|
|
||||||
return func(entry *logrus.Entry) *logrus.Entry {
|
return func(ctx context.Context, r logging.Record) logging.Record {
|
||||||
if insecureSecrets {
|
if insecureSecrets {
|
||||||
return entry
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
masks := Masks(entry.Context)
|
masks := Masks(ctx)
|
||||||
|
|
||||||
if len(*masks) == 0 {
|
if len(*masks) == 0 {
|
||||||
entry.Message = defReplacer.Replace(entry.Message)
|
r.Message = defReplacer.Replace(r.Message)
|
||||||
return entry
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
@@ -349,89 +346,129 @@ func valueMasker(insecureSecrets bool, oldnew []string) entryProcessor {
|
|||||||
cmasker := replacer
|
cmasker := replacer
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
entry.Message = cmasker.Replace(entry.Message)
|
r.Message = cmasker.Replace(r.Message)
|
||||||
|
|
||||||
return entry
|
return r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type maskedFormatter struct {
|
type maskedHandler struct {
|
||||||
logrus.Formatter
|
logging.Handler
|
||||||
masker entryProcessor
|
masker entryProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *maskedFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
func (m *maskedHandler) Handle(ctx context.Context, r logging.Record) error {
|
||||||
return f.Formatter.Format(f.masker(entry))
|
return m.Handler.Handle(ctx, m.masker(ctx, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
type jobLogFormatter struct {
|
func (m *maskedHandler) WithAttrs(attrs []logging.Attr) logging.Handler {
|
||||||
|
return &maskedHandler{Handler: m.Handler.WithAttrs(attrs), masker: m.masker}
|
||||||
|
}
|
||||||
|
|
||||||
|
// jobLogHandler writes a job's log to w, one record per line in the shape format gives it.
|
||||||
|
type jobLogHandler struct {
|
||||||
|
w io.Writer
|
||||||
|
mu *sync.Mutex // shared by every handler derived with WithAttrs, as they share w
|
||||||
|
attrs []logging.Attr
|
||||||
|
format func(logging.Record) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newJobLogHandler writes readable text.
|
||||||
|
func newJobLogHandler(w io.Writer, color int) *jobLogHandler {
|
||||||
|
return &jobLogHandler{w: w, mu: &sync.Mutex{}, format: textFormatter{w: w, color: color}.format}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *jobLogHandler) Enabled(context.Context, logging.Level) bool { return true }
|
||||||
|
|
||||||
|
func (h *jobLogHandler) WithAttrs(attrs []logging.Attr) logging.Handler {
|
||||||
|
c := *h
|
||||||
|
c.attrs = slices.Concat(h.attrs, attrs)
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *jobLogHandler) Handle(_ context.Context, r logging.Record) error {
|
||||||
|
r.Attrs = slices.Concat(h.attrs, r.Attrs)
|
||||||
|
line, err := h.format(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
_, err = h.w.Write(line)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type textFormatter struct {
|
||||||
|
w io.Writer
|
||||||
color int
|
color int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *jobLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
func (f textFormatter) format(r logging.Record) ([]byte, error) {
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
|
|
||||||
// the web renderer decodes command data, so this local view has to as well
|
// the web renderer decodes command data, so this local view has to as well
|
||||||
if _, _, _, ok := tryParseRawActionCommand(entry.Message + "\n"); ok {
|
if _, _, _, ok := tryParseRawActionCommand(r.Message + "\n"); ok {
|
||||||
entry.Message = UnescapeCommandData(entry.Message)
|
r.Message = UnescapeCommandData(r.Message)
|
||||||
}
|
}
|
||||||
|
r.Message = strings.TrimSuffix(r.Message, "\n")
|
||||||
|
|
||||||
if f.isColored(entry) {
|
if f.isColored() {
|
||||||
f.printColored(b, entry)
|
f.printColored(b, r)
|
||||||
} else {
|
} else {
|
||||||
f.print(b, entry)
|
f.print(b, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteByte('\n')
|
b.WriteByte('\n')
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *jobLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
func attrIsTrue(r logging.Record, key string) bool {
|
||||||
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
v, _ := r.Attr(key)
|
||||||
|
return v == true
|
||||||
|
}
|
||||||
|
|
||||||
job := entry.Data["job"]
|
func (f textFormatter) printColored(b *bytes.Buffer, r logging.Record) {
|
||||||
|
job, _ := r.Attr("job")
|
||||||
|
|
||||||
debugFlag := ""
|
debugFlag := ""
|
||||||
if entry.Level == logrus.DebugLevel {
|
if r.Level == logging.LevelDebug {
|
||||||
debugFlag = "[DEBUG] "
|
debugFlag = "[DEBUG] "
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case entry.Data[rawOutputField] == true:
|
case attrIsTrue(r, rawOutputField):
|
||||||
if entry.Data[scriptLineCyanField] == true {
|
if attrIsTrue(r, scriptLineCyanField) {
|
||||||
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m \x1b[36;1m%s\x1b[0m", f.color, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m \x1b[36;1m%s\x1b[0m", f.color, r.Message)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m %s", f.color, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m %s", f.color, r.Message)
|
||||||
}
|
}
|
||||||
case entry.Data["dryrun"] == true:
|
case attrIsTrue(r, "dryrun"):
|
||||||
fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s%s", gray, f.color, job, debugFlag, entry.Message)
|
fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s%s", gray, f.color, job, debugFlag, r.Message)
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s%s", f.color, job, debugFlag, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s%s", f.color, job, debugFlag, r.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *jobLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
func (f textFormatter) print(b *bytes.Buffer, r logging.Record) {
|
||||||
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
job, _ := r.Attr("job")
|
||||||
|
|
||||||
job := entry.Data["job"]
|
|
||||||
|
|
||||||
debugFlag := ""
|
debugFlag := ""
|
||||||
if entry.Level == logrus.DebugLevel {
|
if r.Level == logging.LevelDebug {
|
||||||
debugFlag = "[DEBUG] "
|
debugFlag = "[DEBUG] "
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case entry.Data[rawOutputField] == true:
|
case attrIsTrue(r, rawOutputField):
|
||||||
fmt.Fprintf(b, "[%s] | %s", job, entry.Message)
|
fmt.Fprintf(b, "[%s] | %s", job, r.Message)
|
||||||
case entry.Data["dryrun"] == true:
|
case attrIsTrue(r, "dryrun"):
|
||||||
fmt.Fprintf(b, "*DRYRUN* [%s] %s%s", job, debugFlag, entry.Message)
|
fmt.Fprintf(b, "*DRYRUN* [%s] %s%s", job, debugFlag, r.Message)
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(b, "[%s] %s%s", job, debugFlag, entry.Message)
|
fmt.Fprintf(b, "[%s] %s%s", job, debugFlag, r.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *jobLogFormatter) isColored(entry *logrus.Entry) bool {
|
func (f textFormatter) isColored() bool {
|
||||||
isColored := checkIfTerminal(entry.Logger.Out)
|
isColored := checkIfTerminal(f.w)
|
||||||
|
|
||||||
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
|
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
|
||||||
isColored = true
|
isColored = true
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
// newJobLogJSONHandler writes one JSON object per record, in the shape of logrus's JSONFormatter.
|
||||||
|
func newJobLogJSONHandler(w io.Writer) *jobLogHandler {
|
||||||
|
return &jobLogHandler{w: w, mu: &sync.Mutex{}, format: formatJSON}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatJSON(r logging.Record) ([]byte, error) {
|
||||||
|
data := make(map[string]any, len(r.Attrs)+3)
|
||||||
|
for _, a := range r.Attrs {
|
||||||
|
switch v := a.Value.(type) {
|
||||||
|
case error:
|
||||||
|
data[a.Key] = v.Error() // an error marshals to {}
|
||||||
|
default:
|
||||||
|
data[a.Key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, key := range []string{"time", "msg", "level"} {
|
||||||
|
if v, ok := data[key]; ok { // keep the fixed keys from being overwritten
|
||||||
|
data["fields."+key] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data["time"] = r.Time.Format(time.RFC3339)
|
||||||
|
data["msg"] = r.Message
|
||||||
|
data["level"] = jsonLevel(r.Level)
|
||||||
|
|
||||||
|
line, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
|
||||||
|
}
|
||||||
|
return append(line, '\n'), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonLevel(l logging.Level) string {
|
||||||
|
if l.String() == "warn" {
|
||||||
|
return "warning" // what logrus calls it
|
||||||
|
}
|
||||||
|
return l.String()
|
||||||
|
}
|
||||||
+64
-15
@@ -4,13 +4,19 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -49,10 +55,7 @@ func TestValueMasker(t *testing.T) {
|
|||||||
ctx := WithMasks(t.Context(), &entry.masks)
|
ctx := WithMasks(t.Context(), &entry.masks)
|
||||||
masker := valueMasker(false, AppendSecretMaskers(nil, entry.secrets))
|
masker := valueMasker(false, AppendSecretMaskers(nil, entry.secrets))
|
||||||
for line := range strings.SplitSeq(entry.lines, "\n") {
|
for line := range strings.SplitSeq(entry.lines, "\n") {
|
||||||
lentry := masker(&logrus.Entry{
|
lentry := masker(ctx, logging.Record{Message: line})
|
||||||
Context: ctx,
|
|
||||||
Message: line,
|
|
||||||
})
|
|
||||||
for _, line := range entry.disallowed {
|
for _, line := range entry.disallowed {
|
||||||
assert.NotContains(t, lentry.Message, line)
|
assert.NotContains(t, lentry.Message, line)
|
||||||
}
|
}
|
||||||
@@ -79,7 +82,7 @@ func TestValueMaskerEncodedSecrets(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
masker := valueMasker(false, AppendSecretMaskers(nil, map[string]string{"TOKEN": tc.secret}))
|
masker := valueMasker(false, AppendSecretMaskers(nil, map[string]string{"TOKEN": tc.secret}))
|
||||||
entry := masker(&logrus.Entry{Context: t.Context(), Message: strings.Join(tc.encoded, " ")})
|
entry := masker(t.Context(), logging.Record{Message: strings.Join(tc.encoded, " ")})
|
||||||
|
|
||||||
assert.Contains(t, entry.Message, "***")
|
assert.Contains(t, entry.Message, "***")
|
||||||
for _, disallowed := range tc.encoded {
|
for _, disallowed := range tc.encoded {
|
||||||
@@ -105,7 +108,7 @@ func TestValueMaskerJSONEscapesBothWays(t *testing.T) {
|
|||||||
{"literal (JS JSON.stringify / .NET)", jsonStringEscapeNoHTML(secret)},
|
{"literal (JS JSON.stringify / .NET)", jsonStringEscapeNoHTML(secret)},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
entry := masker(&logrus.Entry{Context: t.Context(), Message: `{"t":"` + tc.form + `"}`})
|
entry := masker(t.Context(), logging.Record{Message: `{"t":"` + tc.form + `"}`})
|
||||||
|
|
||||||
assert.Contains(t, entry.Message, "***")
|
assert.Contains(t, entry.Message, "***")
|
||||||
assert.NotContains(t, entry.Message, tc.form)
|
assert.NotContains(t, entry.Message, tc.form)
|
||||||
@@ -118,7 +121,7 @@ func TestValueMaskerJSONEscapesBothWays(t *testing.T) {
|
|||||||
func TestValueMaskerHidesExtraMasks(t *testing.T) {
|
func TestValueMaskerHidesExtraMasks(t *testing.T) {
|
||||||
masker := valueMasker(false, (&Config{ExtraMasks: []string{"pr0xypw"}}).maskers())
|
masker := valueMasker(false, (&Config{ExtraMasks: []string{"pr0xypw"}}).maskers())
|
||||||
|
|
||||||
entry := masker(&logrus.Entry{Context: t.Context(), Message: "proxy is http://user:pr0xypw@proxy:3128"})
|
entry := masker(t.Context(), logging.Record{Message: "proxy is http://user:pr0xypw@proxy:3128"})
|
||||||
|
|
||||||
assert.Equal(t, "proxy is http://user:***@proxy:3128", entry.Message)
|
assert.Equal(t, "proxy is http://user:***@proxy:3128", entry.Message)
|
||||||
}
|
}
|
||||||
@@ -137,7 +140,7 @@ func TestValueMaskerEncodedMasks(t *testing.T) {
|
|||||||
{"encoded: " + base64.StdEncoding.EncodeToString([]byte("s3cr3t")), "encoded: ***"},
|
{"encoded: " + base64.StdEncoding.EncodeToString([]byte("s3cr3t")), "encoded: ***"},
|
||||||
{"encoded: " + base64.StdEncoding.EncodeToString([]byte("first")), "encoded: ***"},
|
{"encoded: " + base64.StdEncoding.EncodeToString([]byte("first")), "encoded: ***"},
|
||||||
} {
|
} {
|
||||||
entry := masker(&logrus.Entry{Context: WithMasks(t.Context(), &masks), Message: tc.line})
|
entry := masker(WithMasks(t.Context(), &masks), logging.Record{Message: tc.line})
|
||||||
assert.Equal(t, tc.want, entry.Message, tc.line)
|
assert.Equal(t, tc.want, entry.Message, tc.line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,7 +157,7 @@ func TestValueMaskerBase64Alignments(t *testing.T) {
|
|||||||
for _, prefix := range []string{"x-access-token:", "user:", "ab:"} {
|
for _, prefix := range []string{"x-access-token:", "user:", "ab:"} {
|
||||||
t.Run(prefix, func(t *testing.T) {
|
t.Run(prefix, func(t *testing.T) {
|
||||||
encoded := base64.StdEncoding.EncodeToString([]byte(prefix + secret))
|
encoded := base64.StdEncoding.EncodeToString([]byte(prefix + secret))
|
||||||
entry := masker(&logrus.Entry{Context: t.Context(), Message: "Authorization: Basic " + encoded})
|
entry := masker(t.Context(), logging.Record{Message: "Authorization: Basic " + encoded})
|
||||||
|
|
||||||
assert.Contains(t, entry.Message, "***")
|
assert.Contains(t, entry.Message, "***")
|
||||||
assert.NotEqual(t, "Authorization: Basic "+encoded, entry.Message)
|
assert.NotEqual(t, "Authorization: Basic "+encoded, entry.Message)
|
||||||
@@ -172,7 +175,7 @@ func TestValueMaskerBase64Alignments(t *testing.T) {
|
|||||||
func TestValueMaskerCachedReplacerSeesNewMasks(t *testing.T) {
|
func TestValueMaskerCachedReplacerSeesNewMasks(t *testing.T) {
|
||||||
masker := valueMasker(false, AppendSecretMaskers(nil, map[string]string{"TOKEN": "secret-token"}))
|
masker := valueMasker(false, AppendSecretMaskers(nil, map[string]string{"TOKEN": "secret-token"}))
|
||||||
mask := func(masks *[]string, message string) string {
|
mask := func(masks *[]string, message string) string {
|
||||||
return masker(&logrus.Entry{Context: WithMasks(t.Context(), masks), Message: message}).Message
|
return masker(WithMasks(t.Context(), masks), logging.Record{Message: message}).Message
|
||||||
}
|
}
|
||||||
|
|
||||||
job := []string{"first mask"}
|
job := []string{"first mask"}
|
||||||
@@ -206,15 +209,61 @@ func TestAppendSecretMaskerSkipsUselessEncodings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestJobLogFormatterDecodesCommandData(t *testing.T) {
|
func TestJobLogFormatterDecodesCommandData(t *testing.T) {
|
||||||
logger := logrus.New()
|
|
||||||
logger.Out = io.Discard
|
|
||||||
format := func(message string) string {
|
format := func(message string) string {
|
||||||
out, err := (&jobLogFormatter{}).Format(&logrus.Entry{Logger: logger, Message: message, Data: logrus.Fields{rawOutputField: true}})
|
line, err := textFormatter{w: io.Discard}.format(logging.Record{Message: message, Attrs: []logging.Attr{{Key: rawOutputField, Value: true}}})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return string(out)
|
return string(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Contains(t, format("##[error]deploy 50%25 traffic"), "##[error]deploy 50% traffic")
|
assert.Contains(t, format("##[error]deploy 50%25 traffic"), "##[error]deploy 50% traffic")
|
||||||
// a plain line is not command data and keeps its literal escapes
|
// a plain line is not command data and keeps its literal escapes
|
||||||
assert.Contains(t, format("progress 50%25 done"), "progress 50%25 done")
|
assert.Contains(t, format("progress 50%25 done"), "progress 50%25 done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type factoryFunc func() logging.Handler
|
||||||
|
|
||||||
|
func (f factoryFunc) WithJobLogger() logging.Handler { return f() }
|
||||||
|
|
||||||
|
func TestWithJobLoggerFansOutAndMasksOnlyOutput(t *testing.T) {
|
||||||
|
output, hook := logtest.NewWithLevel(logging.LevelTrace), logtest.NewWithLevel(logging.LevelTrace)
|
||||||
|
ctx := WithJobLoggerFactory(t.Context(), factoryFunc(func() logging.Handler { return output }))
|
||||||
|
ctx = common.WithLoggerHandler(ctx, hook)
|
||||||
|
level := logging.LevelInfo
|
||||||
|
cfg := &Config{Secrets: map[string]string{"TOKEN": "s3cr3t-value"}, JobLoggerLevel: &level}
|
||||||
|
|
||||||
|
ctx = WithJobLogger(ctx, "j1", "build", cfg, &[]string{}, nil)
|
||||||
|
ctx = withStepLogger(ctx, 2, "s2", "step two", "Main")
|
||||||
|
ctx = WithCompositeStepLogger(ctx, "inner")
|
||||||
|
common.Logger(ctx).Infof("token is %s", "s3cr3t-value")
|
||||||
|
common.Logger(ctx).Debugf("below the job logger level")
|
||||||
|
|
||||||
|
// the reporter masks for itself, so its record keeps the original
|
||||||
|
require.Len(t, hook.AllRecords(), 1)
|
||||||
|
assert.Equal(t, "token is s3cr3t-value", hook.LastRecord().Message)
|
||||||
|
require.Len(t, output.AllRecords(), 1)
|
||||||
|
assert.Equal(t, "token is ***", output.LastRecord().Message)
|
||||||
|
|
||||||
|
for _, want := range []struct {
|
||||||
|
key string
|
||||||
|
val any
|
||||||
|
}{{"job", "build"}, {"jobID", "j1"}, {"stepNumber", 2}, {"stage", "Main"}, {"stepID", []string{"s2", "inner"}}} {
|
||||||
|
got, ok := hook.LastRecord().Attr(want.key)
|
||||||
|
assert.True(t, ok, want.key)
|
||||||
|
assert.Equal(t, want.val, got, want.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJobLogJSONHandler(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
logger := logging.New(newJobLogJSONHandler(buf)).With("job", "build", "msg", "clash").WithError(errors.New("boom"))
|
||||||
|
logger.Warnf("careful %d", 1)
|
||||||
|
|
||||||
|
var got map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(buf.Bytes(), &got))
|
||||||
|
assert.Equal(t, "careful 1", got["msg"])
|
||||||
|
assert.Equal(t, "warning", got["level"])
|
||||||
|
assert.Equal(t, "build", got["job"])
|
||||||
|
assert.Equal(t, "boom", got["error"])
|
||||||
|
assert.Equal(t, "clash", got["fields.msg"])
|
||||||
|
assert.NotEmpty(t, got["time"])
|
||||||
|
}
|
||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
@@ -27,7 +26,7 @@ func newLocalReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
|||||||
if !rc.Config.NoSkipCheckout {
|
if !rc.Config.NoSkipCheckout {
|
||||||
// resolve the local workflow against the workspace root, not the process
|
// resolve the local workflow against the workspace root, not the process
|
||||||
// working directory, so it is found regardless of where the runner is invoked
|
// working directory, so it is found regardless of where the runner is invoked
|
||||||
return newReusableWorkflowExecutor(rc, rc.Config.Workdir, localWorkflow.Path)
|
return newReusableWorkflowExecutor(rc, rc.Config.Workdir, localWorkflow.Path, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
uses := fmt.Sprintf("%s/%s@%s", rc.Config.PresetGitHubContext.Repository, localWorkflow.Path, rc.Config.PresetGitHubContext.Sha)
|
uses := fmt.Sprintf("%s/%s@%s", rc.Config.PresetGitHubContext.Repository, localWorkflow.Path, rc.Config.PresetGitHubContext.Sha)
|
||||||
@@ -39,12 +38,9 @@ func newLocalReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
|||||||
|
|
||||||
workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(uses))
|
workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(uses))
|
||||||
|
|
||||||
// If the repository is private, we need a token to clone it
|
|
||||||
token := rc.Config.GetToken()
|
|
||||||
|
|
||||||
return common.NewPipelineExecutor(
|
return common.NewPipelineExecutor(
|
||||||
cloneRemoteReusableWorkflow(rc, remoteReusableWorkflow.CloneURL(), remoteReusableWorkflow.Ref, workflowDir, token),
|
cloneRemoteReusableWorkflow(rc, remoteReusableWorkflow.CloneURL(), remoteReusableWorkflow.Ref, workflowDir),
|
||||||
newReusableWorkflowExecutor(rc, workflowDir, remoteReusableWorkflow.Path),
|
newReusableWorkflowExecutor(rc, workflowDir, remoteReusableWorkflow.Path, true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,11 +56,9 @@ func newRemoteReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
|||||||
filename := fmt.Sprintf("%s/%s@%s", remoteReusableWorkflow.Owner, remoteReusableWorkflow.Repo, remoteReusableWorkflow.Ref)
|
filename := fmt.Sprintf("%s/%s@%s", remoteReusableWorkflow.Owner, remoteReusableWorkflow.Repo, remoteReusableWorkflow.Ref)
|
||||||
workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(filename))
|
workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(filename))
|
||||||
|
|
||||||
token := getGitCloneToken(rc.Config, remoteReusableWorkflow.CloneURL())
|
|
||||||
|
|
||||||
return common.NewPipelineExecutor(
|
return common.NewPipelineExecutor(
|
||||||
cloneRemoteReusableWorkflow(rc, remoteReusableWorkflow.CloneURL(), remoteReusableWorkflow.Ref, workflowDir, token),
|
cloneRemoteReusableWorkflow(rc, remoteReusableWorkflow.CloneURL(), remoteReusableWorkflow.Ref, workflowDir),
|
||||||
newReusableWorkflowExecutor(rc, workflowDir, remoteReusableWorkflow.Path),
|
newReusableWorkflowExecutor(rc, workflowDir, remoteReusableWorkflow.Path, true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,28 +70,32 @@ func newRemoteReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
|||||||
// 2. Gitea has already full URL with rc.Config.GitHubInstance when calling newRemoteReusableWorkflow
|
// 2. Gitea has already full URL with rc.Config.GitHubInstance when calling newRemoteReusableWorkflow
|
||||||
//
|
//
|
||||||
// remoteReusableWorkflow.URL = rc.getGithubContext(ctx).ServerURL
|
// remoteReusableWorkflow.URL = rc.getGithubContext(ctx).ServerURL
|
||||||
func cloneRemoteReusableWorkflow(rc *RunContext, cloneURL, ref, targetDirectory, token string) common.Executor {
|
func cloneRemoteReusableWorkflow(rc *RunContext, cloneURL, ref, targetDirectory string) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
interpolatedURL, err := rc.NewExpressionEvaluator(ctx).Interpolate(ctx, cloneURL)
|
interpolatedURL, err := rc.NewExpressionEvaluator(ctx).Interpolate(ctx, cloneURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to interpolate the workflow clone URL: %w", err)
|
return fmt.Errorf("unable to interpolate the workflow clone URL: %w", err)
|
||||||
}
|
}
|
||||||
return git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{
|
return git.NewGitCloneExecutor(rc.Config.withInstanceAuth(git.NewGitCloneExecutorInput{
|
||||||
URL: interpolatedURL,
|
URL: interpolatedURL,
|
||||||
Ref: ref,
|
Ref: ref,
|
||||||
Dir: targetDirectory,
|
Dir: targetDirectory,
|
||||||
Token: token,
|
|
||||||
OfflineMode: rc.Config.ActionOfflineMode,
|
OfflineMode: rc.Config.ActionOfflineMode,
|
||||||
Depth: rc.Config.ActionCloneDepth,
|
Depth: rc.Config.ActionCloneDepth,
|
||||||
})(ctx)
|
}, rc.Config.GetToken()))(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReusableWorkflowExecutor(rc *RunContext, directory, workflow string) common.Executor {
|
func newReusableWorkflowExecutor(rc *RunContext, directory, workflow string, cached bool) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
// Serialize workflow reads with cache updates.
|
|
||||||
planner, err := func() (model.WorkflowPlanner, error) {
|
planner, err := func() (model.WorkflowPlanner, error) {
|
||||||
defer git.AcquireCloneLock(directory)()
|
if cached {
|
||||||
|
unlock, err := git.AcquireCloneLock(ctx, directory)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer unlock()
|
||||||
|
}
|
||||||
return model.NewWorkflowPlanner(path.Join(directory, workflow), true)
|
return model.NewWorkflowPlanner(path.Join(directory, workflow), true)
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -141,11 +139,7 @@ type remoteReusableWorkflow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *remoteReusableWorkflow) CloneURL() string {
|
func (r *remoteReusableWorkflow) CloneURL() string {
|
||||||
// In Gitea, r.URL always has the protocol prefix, we don't need to add extra prefix in this case.
|
return fmt.Sprintf("%s/%s/%s", withHTTPS(r.URL), r.Owner, r.Repo)
|
||||||
if strings.HasPrefix(r.URL, "http://") || strings.HasPrefix(r.URL, "https://") {
|
|
||||||
return fmt.Sprintf("%s/%s/%s", r.URL, r.Owner, r.Repo)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("https://%s/%s/%s", r.URL, r.Owner, r.Repo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var absoluteReusableWorkflowURLRegex = regexp.MustCompile(`^(https?://.*)/([^/]+/[^/]+/\.[^/]+/workflows/[^@]+@.*)$`)
|
var absoluteReusableWorkflowURLRegex = regexp.MustCompile(`^(https?://.*)/([^/]+/[^/]+/\.[^/]+/workflows/[^@]+@.*)$`)
|
||||||
@@ -199,7 +193,7 @@ func setReusedWorkflowCallerResult(rc *RunContext, runner *runnerImpl) common.Ex
|
|||||||
unlock := lockJob(rc.Run.Job())
|
unlock := lockJob(rc.Run.Job())
|
||||||
rc.result(reusedWorkflowJobResult)
|
rc.result(reusedWorkflowJobResult)
|
||||||
unlock()
|
unlock()
|
||||||
common.Logger(ctx).WithField("jobResult", reusedWorkflowJobResult).Infof("Job %s", reusedWorkflowJobResultMessage)
|
common.Logger(ctx).With("jobResult", reusedWorkflowJobResult).Infof("Job %s", reusedWorkflowJobResultMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,13 +202,18 @@ func setReusedWorkflowCallerResult(rc *RunContext, runner *runnerImpl) common.Ex
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
// getGitCloneToken returns GITEA_TOKEN when shouldCloneURLUseToken returns true,
|
// withInstanceAuth passes instance credentials only to clones from this Gitea instance, and runner.insecure only to its registered address.
|
||||||
// otherwise returns an empty string
|
func (c Config) withInstanceAuth(input git.NewGitCloneExecutorInput, token string) git.NewGitCloneExecutorInput {
|
||||||
func getGitCloneToken(conf *Config, cloneURL string) string {
|
u, err := url.Parse(input.URL)
|
||||||
if !shouldCloneURLUseToken(conf.GitHubInstance, conf.trustedActionInstance(), cloneURL) {
|
if err != nil || !onInstanceHost(u.Host, c.GitHubInstance, c.trustedActionInstance()) {
|
||||||
return ""
|
return input
|
||||||
}
|
}
|
||||||
return conf.GetToken()
|
input.ClientCertFile, input.ClientKeyFile = c.ClientCertFile, c.ClientKeyFile
|
||||||
|
input.InsecureSkipTLS = c.InsecureSkipTLS && onInstanceHost(u.Host, c.GitHubInstance)
|
||||||
|
if u.User == nil {
|
||||||
|
input.Token = token
|
||||||
|
}
|
||||||
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
@@ -228,26 +227,13 @@ func (c Config) trustedActionInstance() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For Gitea
|
// For Gitea
|
||||||
// shouldCloneURLUseToken returns true when the following conditions are met:
|
// onInstanceHost reports whether host is that of one of the instance URLs, which may lack a scheme.
|
||||||
// 1. cloneURL's host matches this Gitea instance: either the registered instance
|
func onInstanceHost(host string, instanceURLs ...string) bool {
|
||||||
// (instanceURL) or, for DEFAULT_ACTIONS_URL=self on a different hostname, the
|
for _, candidate := range instanceURLs {
|
||||||
// self-hosted action instance (trustedActionInstance, "" when not trusted)
|
|
||||||
// 2. the cloneURL does not have basic auth embedded
|
|
||||||
func shouldCloneURLUseToken(instanceURL, trustedActionInstance, cloneURL string) bool {
|
|
||||||
u2, err := url.Parse(cloneURL)
|
|
||||||
if err != nil || u2.User != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, candidate := range []string{instanceURL, trustedActionInstance} {
|
|
||||||
if candidate == "" {
|
if candidate == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(candidate, "http://") &&
|
if u, err := url.Parse(withHTTPS(candidate)); err == nil && u.Host == host {
|
||||||
!strings.HasPrefix(candidate, "https://") {
|
|
||||||
candidate = "https://" + candidate
|
|
||||||
}
|
|
||||||
if u1, err := url.Parse(candidate); err == nil && u1.Host == u2.Host {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/fs"
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -59,7 +63,7 @@ func TestReusableWorkflowCachedBranchRefRefreshes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
cacheDir := t.TempDir()
|
cacheDir := t.TempDir()
|
||||||
|
|
||||||
require.NoError(t, cloneRemoteReusableWorkflow(rc, remoteDir, "master", cacheDir, "")(context.Background()))
|
require.NoError(t, cloneRemoteReusableWorkflow(rc, remoteDir, "master", cacheDir)(t.Context()))
|
||||||
got, err := os.ReadFile(filepath.Join(cacheDir, workflowPath))
|
got, err := os.ReadFile(filepath.Join(cacheDir, workflowPath))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, tmpl("v1"), string(got))
|
require.Equal(t, tmpl("v1"), string(got))
|
||||||
@@ -69,7 +73,7 @@ func TestReusableWorkflowCachedBranchRefRefreshes(t *testing.T) {
|
|||||||
gitMust(t, workDir, "commit", "-am", "v2")
|
gitMust(t, workDir, "commit", "-am", "v2")
|
||||||
gitMust(t, workDir, "push", "origin", "master")
|
gitMust(t, workDir, "push", "origin", "master")
|
||||||
|
|
||||||
require.NoError(t, cloneRemoteReusableWorkflow(rc, remoteDir, "master", cacheDir, "")(context.Background()))
|
require.NoError(t, cloneRemoteReusableWorkflow(rc, remoteDir, "master", cacheDir)(t.Context()))
|
||||||
got, err = os.ReadFile(filepath.Join(cacheDir, workflowPath))
|
got, err = os.ReadFile(filepath.Join(cacheDir, workflowPath))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, tmpl("v2"), string(got), "cached workflow file must reflect the updated branch tip")
|
require.Equal(t, tmpl("v2"), string(got), "cached workflow file must reflect the updated branch tip")
|
||||||
@@ -79,14 +83,16 @@ func TestNewReusableWorkflowExecutorHoldsCloneLock(t *testing.T) {
|
|||||||
workflowDir := t.TempDir()
|
workflowDir := t.TempDir()
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(workflowDir, "reusable.yml"), []byte(":"), 0o644))
|
require.NoError(t, os.WriteFile(filepath.Join(workflowDir, "reusable.yml"), []byte(":"), 0o644))
|
||||||
|
|
||||||
unlockOnce := sync.OnceFunc(git.AcquireCloneLock(workflowDir))
|
unlock, err := git.AcquireCloneLock(t.Context(), workflowDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
unlockOnce := sync.OnceFunc(unlock)
|
||||||
defer unlockOnce()
|
defer unlockOnce()
|
||||||
|
|
||||||
rc := &RunContext{
|
rc := &RunContext{
|
||||||
Config: &Config{},
|
Config: &Config{},
|
||||||
Run: &model.Run{Workflow: &model.Workflow{Jobs: map[string]*model.Job{}}},
|
Run: &model.Run{Workflow: &model.Workflow{Jobs: map[string]*model.Job{}}},
|
||||||
}
|
}
|
||||||
exec := newReusableWorkflowExecutor(rc, workflowDir, "reusable.yml")
|
exec := newReusableWorkflowExecutor(rc, workflowDir, "reusable.yml", true)
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
go func() { done <- exec(context.Background()) }()
|
go func() { done <- exec(context.Background()) }()
|
||||||
@@ -108,8 +114,9 @@ func TestNewReusableWorkflowExecutorHoldsCloneLock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewLocalReusableWorkflowExecutorFindsSameRepositoryPaths(t *testing.T) {
|
func TestNewLocalReusableWorkflowExecutorFindsSameRepositoryPaths(t *testing.T) {
|
||||||
workdir := t.TempDir()
|
workdir := filepath.Join(t.TempDir(), "workspace")
|
||||||
require.NoError(t, os.MkdirAll(filepath.Join(workdir, ".gitea", "workflows"), 0o755))
|
require.NoError(t, os.MkdirAll(filepath.Join(workdir, ".gitea", "workflows"), 0o755))
|
||||||
|
require.NoError(t, os.Mkdir(workdir+".lock", 0o755))
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(workdir, ".gitea", "workflows", "reusable.yml"), []byte(":"), 0o644))
|
require.NoError(t, os.WriteFile(filepath.Join(workdir, ".gitea", "workflows", "reusable.yml"), []byte(":"), 0o644))
|
||||||
|
|
||||||
for _, uses := range []string{"./.gitea/workflows/reusable.yml", "$/.gitea/workflows/reusable.yml"} {
|
for _, uses := range []string{"./.gitea/workflows/reusable.yml", "$/.gitea/workflows/reusable.yml"} {
|
||||||
@@ -118,112 +125,83 @@ func TestNewLocalReusableWorkflowExecutorFindsSameRepositoryPaths(t *testing.T)
|
|||||||
Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {Uses: uses}}}},
|
Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {Uses: uses}}}},
|
||||||
}
|
}
|
||||||
err := newLocalReusableWorkflowExecutor(rc)(t.Context())
|
err := newLocalReusableWorkflowExecutor(rc)(t.Context())
|
||||||
require.Error(t, err)
|
require.ErrorContains(t, err, "workflow is not valid")
|
||||||
require.NotErrorIs(t, err, fs.ErrNotExist)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitCloneTokenWithSchemalessGiteaInstance(t *testing.T) {
|
func TestCloneRemoteReusableWorkflowSendsInstanceTokenAndClientCert(t *testing.T) {
|
||||||
conf := &Config{
|
var gotCert bool
|
||||||
GitHubInstance: "gitea.example.net",
|
var gotToken string
|
||||||
Secrets: map[string]string{
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
"GITEA_TOKEN": "token-value",
|
gotCert = len(r.TLS.PeerCertificates) > 0
|
||||||
|
_, gotToken, _ = r.BasicAuth()
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
}))
|
||||||
|
server.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert}
|
||||||
|
server.StartTLS()
|
||||||
|
defer server.Close()
|
||||||
|
pair := server.TLS.Certificates[0]
|
||||||
|
keyDER, err := x509.MarshalPKCS8PrivateKey(pair.PrivateKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
dir := t.TempDir()
|
||||||
|
certFile, keyFile := filepath.Join(dir, "client.crt"), filepath.Join(dir, "client.key")
|
||||||
|
require.NoError(t, os.WriteFile(certFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: pair.Certificate[0]}), 0o600))
|
||||||
|
require.NoError(t, os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}), 0o600))
|
||||||
|
rc := &RunContext{
|
||||||
|
Config: &Config{
|
||||||
|
GitHubInstance: server.URL,
|
||||||
|
Secrets: map[string]string{"GITEA_TOKEN": "token"},
|
||||||
|
InsecureSkipTLS: true,
|
||||||
|
ClientCertFile: certFile,
|
||||||
|
ClientKeyFile: keyFile,
|
||||||
},
|
},
|
||||||
|
Run: &model.Run{JobID: "j1", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"j1": {}}}},
|
||||||
}
|
}
|
||||||
|
|
||||||
token := getGitCloneToken(conf, "https://gitea.example.net/actions/tools")
|
require.Error(t, cloneRemoteReusableWorkflow(rc, server.URL+"/owner/repo", "main", t.TempDir())(t.Context()))
|
||||||
|
require.True(t, gotCert)
|
||||||
require.Equal(t, "token-value", token)
|
require.Equal(t, "token", gotToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitCloneTokenSelfHostedActionsDifferentHost(t *testing.T) {
|
func TestInstanceAuthAndTransportOnlyApplyToTheInstance(t *testing.T) {
|
||||||
// The runner registered with one hostname while DEFAULT_ACTIONS_URL=self resolves
|
for _, tt := range []struct {
|
||||||
// actions against AppURL on a different hostname for the same instance.
|
name, instanceURL, trustedActionInstance, cloneURL string
|
||||||
conf := &Config{
|
token, cert, insecure bool
|
||||||
GitHubInstance: "gitea.local",
|
|
||||||
DefaultActionInstance: "https://gitea.my-nas.lan",
|
|
||||||
DefaultActionInstanceIsSelfHosted: true,
|
|
||||||
Secrets: map[string]string{
|
|
||||||
"GITEA_TOKEN": "token-value",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
token := getGitCloneToken(conf, "https://gitea.my-nas.lan/owner/action")
|
|
||||||
|
|
||||||
require.Equal(t, "token-value", token)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestShouldCloneURLUseToken(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
instanceURL string
|
|
||||||
trustedActionInstance string
|
|
||||||
cloneURL string
|
|
||||||
want bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{"same host with schemaless instance", "gitea.example.net", "", "https://gitea.example.net/actions/tools", true, true, true},
|
||||||
name: "same host with schemaless instance",
|
{"same host with schemaless instance and port", "gitea.example.net:3000", "", "https://gitea.example.net:3000/actions/tools", true, true, true},
|
||||||
instanceURL: "gitea.example.net",
|
{"different host", "gitea.example.net", "", "https://github.com/actions/tools", false, false, false},
|
||||||
cloneURL: "https://gitea.example.net/actions/tools",
|
{"embedded basic auth keeps its own credentials", "gitea.example.net", "", "https://user:pass@gitea.example.net/actions/tools", false, true, true},
|
||||||
want: true,
|
{"invalid clone URL", "gitea.example.net", "", "://gitea.example.net/actions/tools", false, false, false},
|
||||||
},
|
{"self-hosted action instance on different host skips no verification", "gitea.local", "https://gitea.my-nas.lan", "https://gitea.my-nas.lan/owner/action", true, true, false},
|
||||||
{
|
{"self-hosted action instance with embedded basic auth", "gitea.local", "https://gitea.my-nas.lan", "https://user:[email protected]/owner/action", false, true, false},
|
||||||
name: "same host with schemaless instance and port",
|
{"github mode does not trust mirror host", "gitea.local", "", "https://mirror.example.com/owner/action", false, false, false},
|
||||||
instanceURL: "gitea.example.net:3000",
|
} {
|
||||||
cloneURL: "https://gitea.example.net:3000/actions/tools",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "different host",
|
|
||||||
instanceURL: "gitea.example.net",
|
|
||||||
cloneURL: "https://github.com/actions/tools",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "embedded basic auth",
|
|
||||||
instanceURL: "gitea.example.net",
|
|
||||||
cloneURL: "https://user:[email protected]/actions/tools",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid clone URL",
|
|
||||||
instanceURL: "gitea.example.net",
|
|
||||||
cloneURL: "://gitea.example.net/actions/tools",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// self-hosted DEFAULT_ACTIONS_URL on a different hostname than the
|
|
||||||
// registered instance: the token must still be attached.
|
|
||||||
name: "self-hosted action instance on different host",
|
|
||||||
instanceURL: "gitea.local",
|
|
||||||
trustedActionInstance: "https://gitea.my-nas.lan",
|
|
||||||
cloneURL: "https://gitea.my-nas.lan/owner/action",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// embedded basic auth must still be rejected even when the host matches
|
|
||||||
// the trusted action instance.
|
|
||||||
name: "self-hosted action instance with embedded basic auth",
|
|
||||||
instanceURL: "gitea.local",
|
|
||||||
trustedActionInstance: "https://gitea.my-nas.lan",
|
|
||||||
cloneURL: "https://user:[email protected]/owner/action",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// github.com / mirror hosts are never trusted: trustedActionInstance is
|
|
||||||
// empty in github mode, so an off-instance clone URL gets no token.
|
|
||||||
name: "github mode does not trust mirror host",
|
|
||||||
instanceURL: "gitea.local",
|
|
||||||
cloneURL: "https://mirror.example.com/owner/action",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
require.Equal(t, tt.want, shouldCloneURLUseToken(tt.instanceURL, tt.trustedActionInstance, tt.cloneURL))
|
conf := Config{
|
||||||
|
GitHubInstance: tt.instanceURL,
|
||||||
|
DefaultActionInstance: tt.trustedActionInstance,
|
||||||
|
DefaultActionInstanceIsSelfHosted: tt.trustedActionInstance != "",
|
||||||
|
InsecureSkipTLS: true,
|
||||||
|
ClientCertFile: "/cert",
|
||||||
|
ClientKeyFile: "/key",
|
||||||
|
}
|
||||||
|
want := git.NewGitCloneExecutorInput{URL: tt.cloneURL, InsecureSkipTLS: tt.insecure}
|
||||||
|
if tt.token {
|
||||||
|
want.Token = "token"
|
||||||
|
}
|
||||||
|
if tt.cert {
|
||||||
|
want.ClientCertFile, want.ClientKeyFile = conf.ClientCertFile, conf.ClientKeyFile
|
||||||
|
}
|
||||||
|
require.Equal(t, want, conf.withInstanceAuth(git.NewGitCloneExecutorInput{URL: tt.cloneURL}, "token"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transport := &http.Transport{}
|
||||||
|
conf := Config{GitHubInstance: "https://gitea.local", InstanceTransport: transport}
|
||||||
|
require.Same(t, transport, conf.instanceTransportFor("https://gitea.local/api/actions_pipeline/"))
|
||||||
|
require.Nil(t, conf.instanceTransportFor("https://gitea.local.example/api/actions_pipeline/"))
|
||||||
|
require.Nil(t, conf.instanceTransportFor("https://other.example/x#/api/actions_pipeline/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitMust(t *testing.T, dir string, args ...string) {
|
func gitMust(t *testing.T, dir string, args ...string) {
|
||||||
|
|||||||
+31
-53
@@ -511,7 +511,7 @@ func (rc *RunContext) startKubernetesPod() common.Executor {
|
|||||||
// printStartJobContainerGroup mirrors actions/runner's "Starting job container"
|
// printStartJobContainerGroup mirrors actions/runner's "Starting job container"
|
||||||
// section: emit the group header and summary, return a closer for ::endgroup::.
|
// section: emit the group header and summary, return a closer for ::endgroup::.
|
||||||
func printStartJobContainerGroup(ctx context.Context, image, name, network string) func() {
|
func printStartJobContainerGroup(ctx context.Context, image, name, network string) func() {
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
rawLogger.Infof("::group::Starting job container")
|
rawLogger.Infof("::group::Starting job container")
|
||||||
rawLogger.Infof("image: %s", image)
|
rawLogger.Infof("image: %s", image)
|
||||||
rawLogger.Infof("name: %s", name)
|
rawLogger.Infof("name: %s", name)
|
||||||
@@ -706,7 +706,7 @@ func (rc *RunContext) commandLogWriter(ctx context.Context) io.Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func rawLogLine(ctx context.Context) common.LineHandler {
|
func rawLogLine(ctx context.Context) common.LineHandler {
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
return func(line string) bool {
|
return func(line string) bool {
|
||||||
rawLogger.Infof("%s", line)
|
rawLogger.Infof("%s", line)
|
||||||
return true
|
return true
|
||||||
@@ -725,7 +725,7 @@ func (rc *RunContext) cleanupJobResources(networkName string, createAndDeleteNet
|
|||||||
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
||||||
errs = append(errs, rc.stopServiceContainers()(ctx))
|
errs = append(errs, rc.stopServiceContainers()(ctx))
|
||||||
}
|
}
|
||||||
if !common.Dryrun(ctx) && (preclean || rc.hadDockerProxy) {
|
if !common.Dryrun(ctx) && !rc.Config.NoSweep && (preclean || rc.hadDockerProxy) {
|
||||||
errs = append(errs, container.RemoveDockerJobResources(ctx, rc.jobContainerName()))
|
errs = append(errs, container.RemoveDockerJobResources(ctx, rc.jobContainerName()))
|
||||||
}
|
}
|
||||||
if preclean || rc.deferVolumeCleanup == nil {
|
if preclean || rc.deferVolumeCleanup == nil {
|
||||||
@@ -957,7 +957,7 @@ func (rc *RunContext) waitForServiceContainers() common.Executor {
|
|||||||
// waitUntilHealthy waits on the healthcheck alone, so a container that declares none is
|
// waitUntilHealthy waits on the healthcheck alone, so a container that declares none is
|
||||||
// ready at once and one that exited is left to the steps that need it.
|
// ready at once and one that exited is left to the steps that need it.
|
||||||
func (svc *serviceContainer) waitUntilHealthy(ctx context.Context, timeout time.Duration) error {
|
func (svc *serviceContainer) waitUntilHealthy(ctx context.Context, timeout time.Duration) error {
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
loggedStarting := false
|
loggedStarting := false
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
@@ -1015,7 +1015,7 @@ const serviceLogsTimeout = 15 * time.Second // cleanup gets one minute in total
|
|||||||
func (rc *RunContext) printServiceLogs(ctx context.Context) {
|
func (rc *RunContext) printServiceLogs(ctx context.Context) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, serviceLogsTimeout)
|
ctx, cancel := context.WithTimeout(ctx, serviceLogsTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
for _, svc := range rc.serviceContainers {
|
for _, svc := range rc.serviceContainers {
|
||||||
if !svc.created || svc.logsDumped {
|
if !svc.created || svc.logsDumped {
|
||||||
continue
|
continue
|
||||||
@@ -1236,10 +1236,6 @@ func (rc *RunContext) Executor() (common.Executor, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RunContext) runsOnImage(ctx context.Context) (string, error) {
|
func (rc *RunContext) runsOnImage(ctx context.Context) (string, error) {
|
||||||
if rc.Run.Job().RunsOn() == nil && rc.containerSpec.Image == "" {
|
|
||||||
common.Logger(ctx).Errorf("'runs-on' key not defined in %s", rc.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
runsOn := rc.Run.Job().RunsOn()
|
runsOn := rc.Run.Job().RunsOn()
|
||||||
for i, v := range runsOn {
|
for i, v := range runsOn {
|
||||||
var err error
|
var err error
|
||||||
@@ -1342,7 +1338,7 @@ func (rc *RunContext) isEnabled(ctx context.Context) (bool, error) {
|
|||||||
rc.caller.setReusedWorkflowJobResult(rc.Run.JobID, "skipped")
|
rc.caller.setReusedWorkflowJobResult(rc.Run.JobID, "skipped")
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
l.WithField("jobResult", "skipped").Debugf("Skipping job '%s' due to '%s'", job.Name, job.If.Value)
|
l.With("jobResult", "skipped").Debugf("Skipping job '%s' due to '%s'", job.Name, job.If.Value)
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1545,14 +1541,8 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
|||||||
merged.RunnerPerflog, merged.RunnerTrackingID = ghc.RunnerPerflog, ghc.RunnerTrackingID
|
merged.RunnerPerflog, merged.RunnerTrackingID = ghc.RunnerPerflog, ghc.RunnerTrackingID
|
||||||
*ghc = merged
|
*ghc = merged
|
||||||
|
|
||||||
instance := rc.Config.GitHubInstance
|
rc.setInstanceURLs(ghc)
|
||||||
if !strings.HasPrefix(instance, "http://") &&
|
ghc.GraphQLURL = "" // Gitea doesn't support graphql
|
||||||
!strings.HasPrefix(instance, "https://") {
|
|
||||||
instance = "https://" + instance
|
|
||||||
}
|
|
||||||
ghc.ServerURL = instance
|
|
||||||
ghc.APIURL = instance + "/api/v1" // the version of Gitea is v1
|
|
||||||
ghc.GraphQLURL = "" // Gitea doesn't support graphql
|
|
||||||
return ghc
|
return ghc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1566,7 +1556,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
|||||||
|
|
||||||
ghc.SetBaseAndHeadRef()
|
ghc.SetBaseAndHeadRef()
|
||||||
repoPath := rc.Config.Workdir
|
repoPath := rc.Config.Workdir
|
||||||
ghcontext.SetRepositoryAndOwner(ctx, ghc, rc.Config.GitHubInstance, repoPath)
|
ghcontext.SetRepositoryAndOwner(ctx, ghc, repoPath)
|
||||||
if ghc.Ref == "" {
|
if ghc.Ref == "" {
|
||||||
ghcontext.SetRef(ctx, ghc, repoPath)
|
ghcontext.SetRef(ctx, ghc, repoPath)
|
||||||
}
|
}
|
||||||
@@ -1576,27 +1566,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
|||||||
|
|
||||||
ghc.SetRefTypeAndName()
|
ghc.SetRefTypeAndName()
|
||||||
|
|
||||||
// defaults
|
rc.setInstanceURLs(ghc)
|
||||||
ghc.ServerURL = "https://github.com"
|
|
||||||
ghc.APIURL = "https://api.github.com"
|
|
||||||
ghc.GraphQLURL = "https://api.github.com/graphql"
|
|
||||||
// per GHES
|
|
||||||
if rc.Config.GitHubInstance != "github.com" {
|
|
||||||
ghc.ServerURL = "https://" + rc.Config.GitHubInstance
|
|
||||||
ghc.APIURL = fmt.Sprintf("https://%s/api/v3", rc.Config.GitHubInstance)
|
|
||||||
ghc.GraphQLURL = fmt.Sprintf("https://%s/api/graphql", rc.Config.GitHubInstance)
|
|
||||||
}
|
|
||||||
|
|
||||||
{ // Adapt to Gitea
|
|
||||||
instance := rc.Config.GitHubInstance
|
|
||||||
if !strings.HasPrefix(instance, "http://") &&
|
|
||||||
!strings.HasPrefix(instance, "https://") {
|
|
||||||
instance = "https://" + instance
|
|
||||||
}
|
|
||||||
ghc.ServerURL = instance
|
|
||||||
ghc.APIURL = instance + "/api/v1" // the version of Gitea is v1
|
|
||||||
ghc.GraphQLURL = "" // Gitea doesn't support graphql
|
|
||||||
}
|
|
||||||
|
|
||||||
// allow to be overridden by user
|
// allow to be overridden by user
|
||||||
if rc.Config.Env["GITHUB_SERVER_URL"] != "" {
|
if rc.Config.Env["GITHUB_SERVER_URL"] != "" {
|
||||||
@@ -1612,6 +1582,20 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
|||||||
return ghc
|
return ghc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) setInstanceURLs(ghc *model.GithubContext) {
|
||||||
|
if instance := strings.TrimRight(rc.Config.GitHubInstance, "/"); instance != "" {
|
||||||
|
ghc.ServerURL = withHTTPS(instance)
|
||||||
|
ghc.APIURL = ghc.ServerURL + "/api/v1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withHTTPS(u string) string {
|
||||||
|
if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") {
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
return "https://" + u
|
||||||
|
}
|
||||||
|
|
||||||
func isLocalCheckout(ghc *model.GithubContext, step *model.Step) bool {
|
func isLocalCheckout(ghc *model.GithubContext, step *model.Step) bool {
|
||||||
if step.Type() == model.StepTypeInvalid {
|
if step.Type() == model.StepTypeInvalid {
|
||||||
// This will be errored out by the executor later, we need this here to avoid a null panic though
|
// This will be errored out by the executor later, we need this here to avoid a null panic though
|
||||||
@@ -1661,20 +1645,16 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
|
|||||||
env["GITHUB_HEAD_REF"] = github.HeadRef
|
env["GITHUB_HEAD_REF"] = github.HeadRef
|
||||||
env["GITHUB_SERVER_URL"] = github.ServerURL
|
env["GITHUB_SERVER_URL"] = github.ServerURL
|
||||||
env["GITHUB_API_URL"] = github.APIURL
|
env["GITHUB_API_URL"] = github.APIURL
|
||||||
env["GITHUB_GRAPHQL_URL"] = github.GraphQLURL
|
|
||||||
|
|
||||||
{ // Adapt to Gitea
|
for _, name := range []string{"GITHUB_SERVER_URL", "GITHUB_API_URL"} {
|
||||||
instance := rc.Config.GitHubInstance
|
if env[name] == "" {
|
||||||
if !strings.HasPrefix(instance, "http://") &&
|
delete(env, name)
|
||||||
!strings.HasPrefix(instance, "https://") {
|
|
||||||
instance = "https://" + instance
|
|
||||||
}
|
}
|
||||||
env["GITHUB_SERVER_URL"] = instance
|
|
||||||
env["GITHUB_API_URL"] = instance + "/api/v1" // the version of Gitea is v1
|
|
||||||
env["GITHUB_GRAPHQL_URL"] = "" // Gitea doesn't support graphql
|
|
||||||
env["GITHUB_RUN_ATTEMPT"] = github.RunAttempt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
env["GITHUB_GRAPHQL_URL"] = ""
|
||||||
|
env["GITHUB_RUN_ATTEMPT"] = github.RunAttempt
|
||||||
|
|
||||||
env["RUNNER_NAME"] = rc.Config.RunnerName
|
env["RUNNER_NAME"] = rc.Config.RunnerName
|
||||||
env["RUNNER_ENVIRONMENT"] = "self-hosted"
|
env["RUNNER_ENVIRONMENT"] = "self-hosted"
|
||||||
if workspace := parentDir(github.Workspace); workspace != "" {
|
if workspace := parentDir(github.Workspace); workspace != "" {
|
||||||
@@ -1708,9 +1688,7 @@ func parentDir(p string) string {
|
|||||||
// still names a release when the label is a rolling one such as ubuntu-latest.
|
// still names a release when the label is a rolling one such as ubuntu-latest.
|
||||||
func (rc *RunContext) imageOS(ctx context.Context) string {
|
func (rc *RunContext) imageOS(ctx context.Context) string {
|
||||||
if rc.Run.Job().RunsOn() == nil {
|
if rc.Run.Job().RunsOn() == nil {
|
||||||
// A composite action runs on a synthetic job, and resolving its image would only
|
return "" // a composite action runs on a synthetic job without runs-on
|
||||||
// log that runs-on is missing.
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
if imageOS := imageOSFromImage(rc.platformImage); imageOS != "" {
|
if imageOS := imageOSFromImage(rc.platformImage); imageOS != "" {
|
||||||
return imageOS
|
return imageOS
|
||||||
|
|||||||
@@ -25,14 +25,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/exprparser"
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/docker/cli/cli/compose/loader"
|
"github.com/docker/cli/cli/compose/loader"
|
||||||
"github.com/moby/moby/api/types/volume"
|
"github.com/moby/moby/api/types/volume"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
logrustest "github.com/sirupsen/logrus/hooks/test"
|
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
require "github.com/stretchr/testify/require"
|
require "github.com/stretchr/testify/require"
|
||||||
@@ -83,7 +83,7 @@ func TestRunContext_EvalBool(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
rc.ExprEval = rc.NewExpressionEvaluator(t.Context())
|
||||||
|
|
||||||
tables := []struct {
|
tables := []struct {
|
||||||
in string
|
in string
|
||||||
@@ -176,7 +176,7 @@ func TestRunContext_EvalBool(t *testing.T) {
|
|||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.in, func(t *testing.T) {
|
t.Run(table.in, func(t *testing.T) {
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
b, err := EvalBool(context.Background(), rc.ExprEval, table.in, exprparser.DefaultStatusCheckSuccess)
|
b, err := EvalBool(t.Context(), rc.ExprEval, table.in, exprparser.DefaultStatusCheckSuccess)
|
||||||
if table.wantErr {
|
if table.wantErr {
|
||||||
assertObject.Error(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.Error(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
}
|
}
|
||||||
@@ -563,8 +563,8 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc.Run.JobID = "job1"
|
rc.Run.JobID = "job1"
|
||||||
rc.Run.Workflow.Jobs = map[string]*model.Job{"job1": job}
|
rc.Run.Workflow.Jobs = map[string]*model.Job{"job1": job}
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
rc.ExprEval = rc.NewExpressionEvaluator(t.Context())
|
||||||
require.NoError(t, rc.resolvePlatformImage(context.Background()))
|
require.NoError(t, rc.resolvePlatformImage(t.Context()))
|
||||||
|
|
||||||
gotbind, gotmount := rc.GetBindsAndMounts(t.Context())
|
gotbind, gotmount := rc.GetBindsAndMounts(t.Context())
|
||||||
assert.Contains(t, gotbind, "/host/mame/roms:/root/.mame/roms:ro")
|
assert.Contains(t, gotbind, "/host/mame/roms:/root/.mame/roms:ro")
|
||||||
@@ -778,9 +778,10 @@ func TestRemoveDockerActionImagesByTag(t *testing.T) {
|
|||||||
func TestCleanupJobResourcesContinuesAfterFailure(t *testing.T) {
|
func TestCleanupJobResourcesContinuesAfterFailure(t *testing.T) {
|
||||||
t.Setenv("TMPDIR", "/tmp")
|
t.Setenv("TMPDIR", "/tmp")
|
||||||
proxyDir := t.TempDir()
|
proxyDir := t.TempDir()
|
||||||
for _, name := range []string{"synchronous", "proxy", "closed proxy", "deferred", "preclean deferred", "canceled"} {
|
for _, name := range []string{"synchronous", "proxy", "closed proxy", "kept proxy", "deferred", "preclean deferred", "canceled"} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
proxy, preclean, deferred := strings.HasSuffix(name, "proxy"), strings.HasPrefix(name, "preclean"), strings.HasSuffix(name, "deferred")
|
proxy, preclean, deferred := strings.HasSuffix(name, "proxy"), strings.HasPrefix(name, "preclean"), strings.HasSuffix(name, "deferred")
|
||||||
|
removesLabelled := (proxy || preclean) && name != "kept proxy"
|
||||||
if proxy && runtime.GOOS == "windows" {
|
if proxy && runtime.GOOS == "windows" {
|
||||||
t.Skip("Unix socket ownership is unavailable on Windows")
|
t.Skip("Unix socket ownership is unavailable on Windows")
|
||||||
}
|
}
|
||||||
@@ -790,7 +791,7 @@ func TestCleanupJobResourcesContinuesAfterFailure(t *testing.T) {
|
|||||||
service.On("Remove").Return(func(context.Context) error { return removeError }).Once()
|
service.On("Remove").Return(func(context.Context) error { return removeError }).Once()
|
||||||
service.On("Close").Return(func(context.Context) error { return closeError }).Once()
|
service.On("Close").Return(func(context.Context) error { return closeError }).Once()
|
||||||
rc := &RunContext{
|
rc := &RunContext{
|
||||||
Config: &Config{CacheContainer: "cache-container"},
|
Config: &Config{CacheContainer: "cache-container", NoSweep: name == "kept proxy"},
|
||||||
Run: &model.Run{Workflow: &model.Workflow{Name: "wf"}, JobID: "job"},
|
Run: &model.Run{Workflow: &model.Workflow{Name: "wf"}, JobID: "job"},
|
||||||
JobContainer: job,
|
JobContainer: job,
|
||||||
serviceContainers: []*serviceContainer{{name: "svc", container: service}},
|
serviceContainers: []*serviceContainer{{name: "svc", container: service}},
|
||||||
@@ -846,8 +847,8 @@ func TestCleanupJobResourcesContinuesAfterFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Equal(t, 2, volumeRemovals)
|
assert.Equal(t, 2, volumeRemovals)
|
||||||
}
|
}
|
||||||
assert.Equal(t, proxy || preclean, strings.Contains(err.Error(), "labelled GET /containers/json"))
|
assert.Equal(t, removesLabelled, strings.Contains(err.Error(), "labelled GET /containers/json"))
|
||||||
if proxy || preclean {
|
if removesLabelled {
|
||||||
require.ErrorContains(t, err, "labelled GET /networks")
|
require.ErrorContains(t, err, "labelled GET /networks")
|
||||||
require.ErrorContains(t, err, "labelled GET /volumes")
|
require.ErrorContains(t, err, "labelled GET /volumes")
|
||||||
}
|
}
|
||||||
@@ -863,7 +864,7 @@ func TestInterpolateOutputsIsPerMatrixComboKeepingNeedsOutputs(t *testing.T) {
|
|||||||
needed := &model.Job{Outputs: map[string]string{"o": "from-gitea"}}
|
needed := &model.Job{Outputs: map[string]string{"o": "from-gitea"}}
|
||||||
run := &model.Run{JobID: "j", Workflow: &model.Workflow{Name: "w", Jobs: map[string]*model.Job{"j": job, "needed": needed}}}
|
run := &model.Run{JobID: "j", Workflow: &model.Workflow{Name: "w", Jobs: map[string]*model.Job{"j": job, "needed": needed}}}
|
||||||
r := &runnerImpl{config: &Config{}}
|
r := &runnerImpl{config: &Config{}}
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
_, err := r.newRunContext(ctx, &model.Run{JobID: "needed", Workflow: run.Workflow}, nil)
|
_, err := r.newRunContext(ctx, &model.Run{JobID: "needed", Workflow: run.Workflow}, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -898,7 +899,7 @@ func TestInterpolateOutputsFromExpression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitHubContext(t *testing.T) {
|
func TestGetGitHubContext(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
@@ -914,6 +915,7 @@ func TestGetGitHubContext(t *testing.T) {
|
|||||||
Run: &model.Run{
|
Run: &model.Run{
|
||||||
Workflow: &model.Workflow{
|
Workflow: &model.Workflow{
|
||||||
Name: "GitHubContextTest",
|
Name: "GitHubContextTest",
|
||||||
|
Jobs: map[string]*model.Job{"job1": {}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "GitHubContextTest",
|
Name: "GitHubContextTest",
|
||||||
@@ -925,9 +927,9 @@ func TestGetGitHubContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc.Run.JobID = "job1"
|
rc.Run.JobID = "job1"
|
||||||
|
|
||||||
ghc := rc.getGithubContext(context.Background())
|
ghc := rc.getGithubContext(t.Context())
|
||||||
|
|
||||||
log.Debugf("%v", ghc)
|
logging.Debugf("%v", ghc)
|
||||||
|
|
||||||
actor := "nektos/act"
|
actor := "nektos/act"
|
||||||
if a := os.Getenv("ACT_ACTOR"); a != "" {
|
if a := os.Getenv("ACT_ACTOR"); a != "" {
|
||||||
@@ -955,10 +957,40 @@ func TestGetGitHubContext(t *testing.T) {
|
|||||||
assert.Equal(t, ghc.Job, "job1") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Equal(t, ghc.Job, "job1") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
rc.Config.PresetGitHubContext = &model.GithubContext{Actor: "preset-actor", TriggeringActor: "triggerer", Job: "preset-job"}
|
rc.Config.PresetGitHubContext = &model.GithubContext{Actor: "preset-actor", TriggeringActor: "triggerer", Job: "preset-job"}
|
||||||
ghc = rc.getGithubContext(context.Background())
|
ghc = rc.getGithubContext(t.Context())
|
||||||
assert.Equal(t, "preset-actor", ghc.Actor)
|
assert.Equal(t, "preset-actor", ghc.Actor)
|
||||||
assert.Equal(t, "triggerer", ghc.TriggeringActor)
|
assert.Equal(t, "triggerer", ghc.TriggeringActor)
|
||||||
assert.Equal(t, "job1", ghc.Job)
|
assert.Equal(t, "job1", ghc.Job)
|
||||||
|
|
||||||
|
preset := &model.GithubContext{ServerURL: "https://preset.test", APIURL: "https://preset.test/api/v1"}
|
||||||
|
for _, testcase := range []struct {
|
||||||
|
name, instance, serverURL, apiURL string
|
||||||
|
preset *model.GithubContext
|
||||||
|
env map[string]string
|
||||||
|
}{
|
||||||
|
{name: "unknown instance"},
|
||||||
|
{name: "bare host with trailing slash", instance: "gitea.test/", serverURL: "https://gitea.test", apiURL: "https://gitea.test/api/v1"},
|
||||||
|
{name: "scheme port and base path", instance: "http://gitea.test:3000/gitea/", serverURL: "http://gitea.test:3000/gitea", apiURL: "http://gitea.test:3000/gitea/api/v1"},
|
||||||
|
{name: "daemon instance replaces preset endpoints", instance: "gitea.test", preset: preset, serverURL: "https://gitea.test", apiURL: "https://gitea.test/api/v1"},
|
||||||
|
{name: "daemon keeps preset endpoints without instance", preset: preset, serverURL: "https://preset.test", apiURL: "https://preset.test/api/v1"},
|
||||||
|
{name: "environment overrides", env: map[string]string{"GITHUB_SERVER_URL": "https://override.test", "GITHUB_API_URL": "https://override.test/api"}, serverURL: "https://override.test", apiURL: "https://override.test/api"},
|
||||||
|
} {
|
||||||
|
t.Run(testcase.name, func(t *testing.T) {
|
||||||
|
rc.Config.GitHubInstance = testcase.instance
|
||||||
|
rc.Config.PresetGitHubContext = testcase.preset
|
||||||
|
rc.Config.Env = map[string]string{"GITHUB_REPOSITORY": "owner/repo", "GITHUB_REF": "refs/heads/main", "SHA_REF": "1234567890"}
|
||||||
|
maps.Copy(rc.Config.Env, testcase.env)
|
||||||
|
github := rc.getGithubContext(t.Context())
|
||||||
|
assert.Equal(t, testcase.serverURL, github.ServerURL)
|
||||||
|
assert.Equal(t, testcase.apiURL, github.APIURL)
|
||||||
|
env := map[string]string{}
|
||||||
|
rc.withGithubEnv(t.Context(), github, env)
|
||||||
|
_, exported := env["GITHUB_SERVER_URL"]
|
||||||
|
assert.Equal(t, testcase.serverURL != "", exported)
|
||||||
|
assert.Equal(t, testcase.serverURL, env["GITHUB_SERVER_URL"])
|
||||||
|
assert.Equal(t, testcase.apiURL, env["GITHUB_API_URL"])
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGithubContextRef(t *testing.T) {
|
func TestGetGithubContextRef(t *testing.T) {
|
||||||
@@ -995,7 +1027,7 @@ func TestGetGithubContextRef(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc := rc.getGithubContext(context.Background())
|
ghc := rc.getGithubContext(t.Context())
|
||||||
|
|
||||||
assert.Equal(t, data.ref, ghc.Ref)
|
assert.Equal(t, data.ref, ghc.Ref)
|
||||||
})
|
})
|
||||||
@@ -1028,54 +1060,54 @@ func createJob(t *testing.T, input, result string) *model.Job {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunContextRunsOnPlatformNames(t *testing.T) {
|
func TestRunContextRunsOnPlatformNames(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
|
|
||||||
rc := createIfTestRunContext(map[string]*model.Job{
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ${{ 'ubuntu-latest' }}`, ""),
|
"job1": createJob(t, `runs-on: ${{ 'ubuntu-latest' }}`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: [self-hosted, my-runner]`, ""),
|
"job1": createJob(t, `runs-on: [self-hosted, my-runner]`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: [self-hosted, "${{ 'my-runner' }}"]`, ""),
|
"job1": createJob(t, `runs-on: [self-hosted, "${{ 'my-runner' }}"]`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ${{ fromJSON('["ubuntu-latest"]') }}`, ""),
|
"job1": createJob(t, `runs-on: ${{ fromJSON('["ubuntu-latest"]') }}`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
// test missing / invalid runs-on
|
// test missing / invalid runs-on
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `name: something`, ""),
|
"job1": createJob(t, `name: something`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on:
|
"job1": createJob(t, `runs-on:
|
||||||
mapping: value`, ""),
|
mapping: value`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ${{ invalid expression }}`, ""),
|
"job1": createJob(t, `runs-on: ${{ invalid expression }}`, ""),
|
||||||
})
|
})
|
||||||
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(t.Context()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunContextIsEnabled(t *testing.T) {
|
func TestRunContextIsEnabled(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
|
|
||||||
// success()
|
// success()
|
||||||
@@ -1085,8 +1117,8 @@ env:
|
|||||||
BROKEN: ${{ fromJSON('invalid') }}
|
BROKEN: ${{ fromJSON('invalid') }}
|
||||||
if: success()`, ""),
|
if: success()`, ""),
|
||||||
})
|
})
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
require.Error(t, evaluateJobEnvAndDefaults(context.Background(), rc))
|
require.Error(t, evaluateJobEnvAndDefaults(t.Context(), rc))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
||||||
@@ -1095,7 +1127,7 @@ needs: [job1]
|
|||||||
if: success()`, ""),
|
if: success()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.False(rc.isEnabled(context.Background()))
|
assertObject.False(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
||||||
@@ -1104,7 +1136,7 @@ needs: [job1]
|
|||||||
if: success()`, ""),
|
if: success()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
||||||
@@ -1112,14 +1144,14 @@ if: success()`, ""),
|
|||||||
if: success()`, ""),
|
if: success()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
// failure()
|
// failure()
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest
|
"job1": createJob(t, `runs-on: ubuntu-latest
|
||||||
if: failure()`, ""),
|
if: failure()`, ""),
|
||||||
})
|
})
|
||||||
assertObject.False(rc.isEnabled(context.Background()))
|
assertObject.False(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
||||||
@@ -1128,7 +1160,7 @@ needs: [job1]
|
|||||||
if: failure()`, ""),
|
if: failure()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
||||||
@@ -1137,7 +1169,7 @@ needs: [job1]
|
|||||||
if: failure()`, ""),
|
if: failure()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.False(rc.isEnabled(context.Background()))
|
assertObject.False(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
||||||
@@ -1145,14 +1177,14 @@ if: failure()`, ""),
|
|||||||
if: failure()`, ""),
|
if: failure()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.False(rc.isEnabled(context.Background()))
|
assertObject.False(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
// always()
|
// always()
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest
|
"job1": createJob(t, `runs-on: ubuntu-latest
|
||||||
if: always()`, ""),
|
if: always()`, ""),
|
||||||
})
|
})
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "failure"),
|
||||||
@@ -1161,7 +1193,7 @@ needs: [job1]
|
|||||||
if: always()`, ""),
|
if: always()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
||||||
@@ -1170,7 +1202,7 @@ needs: [job1]
|
|||||||
if: always()`, ""),
|
if: always()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
"job1": createJob(t, `runs-on: ubuntu-latest`, "success"),
|
||||||
@@ -1178,23 +1210,23 @@ if: always()`, ""),
|
|||||||
if: always()`, ""),
|
if: always()`, ""),
|
||||||
})
|
})
|
||||||
rc.Run.JobID = "job2"
|
rc.Run.JobID = "job2"
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml`, ""),
|
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml`, ""),
|
||||||
})
|
})
|
||||||
assertObject.True(rc.isEnabled(context.Background()))
|
assertObject.True(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml
|
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml
|
||||||
if: false`, ""),
|
if: false`, ""),
|
||||||
})
|
})
|
||||||
assertObject.False(rc.isEnabled(context.Background()))
|
assertObject.False(rc.isEnabled(t.Context()))
|
||||||
|
|
||||||
rc = createIfTestRunContext(map[string]*model.Job{
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
"job1": createJob(t, `if: ${{ fromJSON('not-json') }}`, ""),
|
"job1": createJob(t, `if: ${{ fromJSON('not-json') }}`, ""),
|
||||||
})
|
})
|
||||||
enabled, err := rc.isEnabled(context.Background())
|
enabled, err := rc.isEnabled(t.Context())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assertObject.False(enabled)
|
assertObject.False(enabled)
|
||||||
}
|
}
|
||||||
@@ -1274,12 +1306,9 @@ func TestCreateContainerNameBoundedForLongMatrixInput(t *testing.T) {
|
|||||||
|
|
||||||
func TestPrintStartJobContainerGroupGolden(t *testing.T) {
|
func TestPrintStartJobContainerGroupGolden(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := log.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
entry := logger.With("job", "j1")
|
||||||
logger.SetLevel(log.InfoLevel)
|
ctx := common.WithLogger(t.Context(), entry)
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
entry := logger.WithFields(log.Fields{"job": "j1"})
|
|
||||||
ctx := common.WithLogger(context.Background(), entry)
|
|
||||||
|
|
||||||
printStartJobContainerGroup(ctx, "node:20", "GITEA-WORKFLOW-build-JOB-test", "gitea-runner-network")()
|
printStartJobContainerGroup(ctx, "node:20", "GITEA-WORKFLOW-build-JOB-test", "gitea-runner-network")()
|
||||||
|
|
||||||
@@ -1370,7 +1399,7 @@ func TestRunContext_cleanupFailedStart(t *testing.T) {
|
|||||||
Run: &model.Run{JobID: "job", Workflow: workflow},
|
Run: &model.Run{JobID: "job", Workflow: workflow},
|
||||||
platformImage: "node:20",
|
platformImage: "node:20",
|
||||||
}
|
}
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
ctx := common.WithLogger(common.WithDryrun(t.Context(), true), logger)
|
ctx := common.WithLogger(common.WithDryrun(t.Context(), true), logger)
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
||||||
err = rc.startContainer().Then(rc.stopContainer()).Finally(rc.closeContainer())(ctx)
|
err = rc.startContainer().Then(rc.stopContainer()).Finally(rc.closeContainer())(ctx)
|
||||||
@@ -1391,7 +1420,7 @@ func TestRunContext_cleanupFailedStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Equal(t, strings.Fields(want+" job.Remove service.Remove service.Close job.Close"), operations)
|
assert.Equal(t, strings.Fields(want+" job.Remove service.Remove service.Close job.Close"), operations)
|
||||||
assert.Empty(t, rc.Masks)
|
assert.Empty(t, rc.Masks)
|
||||||
assert.Equal(t, testcase.pullError == nil && testcase.health != container.HealthUnhealthy, slices.ContainsFunc(hook.AllEntries(), func(entry *log.Entry) bool {
|
assert.Equal(t, testcase.pullError == nil && testcase.health != container.HealthUnhealthy, slices.ContainsFunc(hook.AllRecords(), func(entry logging.Record) bool {
|
||||||
return strings.HasPrefix(entry.Message, "::group::Print service container logs")
|
return strings.HasPrefix(entry.Message, "::group::Print service container logs")
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
@@ -1414,7 +1443,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return(&container.Info{ID: "id", State: "running", Health: container.HealthNone}, nil).Once()
|
Return(&container.Info{ID: "id", State: "running", Health: container.HealthNone}, nil).Once()
|
||||||
|
|
||||||
rc := newRunContext(0, &serviceContainer{name: "redis", container: service})
|
rc := newRunContext(0, &serviceContainer{name: "redis", container: service})
|
||||||
require.NoError(t, rc.waitForServiceContainers()(context.Background()))
|
require.NoError(t, rc.waitForServiceContainers()(t.Context()))
|
||||||
service.AssertExpectations(t)
|
service.AssertExpectations(t)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1431,9 +1460,8 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return(&container.Info{ID: "id", State: "running", Health: container.HealthHealthy}, nil).Once()
|
Return(&container.Info{ID: "id", State: "running", Health: container.HealthHealthy}, nil).Once()
|
||||||
|
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
logger := log.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(&output, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(&output)
|
require.NoError(t, newRunContext(0, &serviceContainer{name: "postgres", container: service}).waitForServiceContainers()(common.WithLogger(t.Context(), logger)))
|
||||||
require.NoError(t, newRunContext(0, &serviceContainer{name: "postgres", container: service}).waitForServiceContainers()(common.WithLogger(t.Context(), logger.WithFields(nil))))
|
|
||||||
assert.Equal(t, []time.Duration{0, time.Second, 2 * time.Second}, pollTimes)
|
assert.Equal(t, []time.Duration{0, time.Second, 2 * time.Second}, pollTimes)
|
||||||
assert.Equal(t, 1, strings.Count(output.String(), "postgres service is starting."))
|
assert.Equal(t, 1, strings.Count(output.String(), "postgres service is starting."))
|
||||||
assert.Contains(t, output.String(), "postgres service is healthy.")
|
assert.Contains(t, output.String(), "postgres service is healthy.")
|
||||||
@@ -1450,7 +1478,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
service.On("DumpLogs", mock.Anything).Return(nil).Once()
|
service.On("DumpLogs", mock.Anything).Return(nil).Once()
|
||||||
|
|
||||||
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
||||||
err := rc.waitForServiceContainers()(context.Background())
|
err := rc.waitForServiceContainers()(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "the service 'postgres' is unhealthy: connection refused")
|
assert.Contains(t, err.Error(), "the service 'postgres' is unhealthy: connection refused")
|
||||||
service.AssertExpectations(t)
|
service.AssertExpectations(t)
|
||||||
@@ -1462,7 +1490,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return(&container.Info{State: "exited", ExitCode: 2, Health: container.HealthNone}, nil).Once()
|
Return(&container.Info{State: "exited", ExitCode: 2, Health: container.HealthNone}, nil).Once()
|
||||||
|
|
||||||
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
||||||
require.NoError(t, rc.waitForServiceContainers()(context.Background()))
|
require.NoError(t, rc.waitForServiceContainers()(t.Context()))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("proceeds when the container is gone", func(t *testing.T) {
|
t.Run("proceeds when the container is gone", func(t *testing.T) {
|
||||||
@@ -1471,7 +1499,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return((*container.Info)(nil), container.ErrContainerNotFound).Once()
|
Return((*container.Info)(nil), container.ErrContainerNotFound).Once()
|
||||||
|
|
||||||
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(0, &serviceContainer{name: "postgres", container: service})
|
||||||
require.NoError(t, rc.waitForServiceContainers()(context.Background()))
|
require.NoError(t, rc.waitForServiceContainers()(t.Context()))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("fails right away when one service fails while another is still starting", func(t *testing.T) {
|
t.Run("fails right away when one service fails while another is still starting", func(t *testing.T) {
|
||||||
@@ -1505,7 +1533,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return(&container.Info{State: "running", Health: container.HealthStarting}, nil)
|
Return(&container.Info{State: "running", Health: container.HealthStarting}, nil)
|
||||||
|
|
||||||
rc := newRunContext(20*time.Millisecond, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(20*time.Millisecond, &serviceContainer{name: "postgres", container: service})
|
||||||
err := rc.waitForServiceContainers()(context.Background())
|
err := rc.waitForServiceContainers()(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "did not become healthy within")
|
assert.Contains(t, err.Error(), "did not become healthy within")
|
||||||
})
|
})
|
||||||
@@ -1517,7 +1545,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
Return((*container.Info)(nil), errors.New("inspect aborted"))
|
Return((*container.Info)(nil), errors.New("inspect aborted"))
|
||||||
|
|
||||||
rc := newRunContext(20*time.Millisecond, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(20*time.Millisecond, &serviceContainer{name: "postgres", container: service})
|
||||||
err := rc.waitForServiceContainers()(context.Background())
|
err := rc.waitForServiceContainers()(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "did not become healthy within")
|
assert.Contains(t, err.Error(), "did not become healthy within")
|
||||||
})
|
})
|
||||||
@@ -1530,7 +1558,7 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
|
|
||||||
svc := &serviceContainer{name: "postgres", container: service}
|
svc := &serviceContainer{name: "postgres", container: service}
|
||||||
rc := newRunContext(-1, svc)
|
rc := newRunContext(-1, svc)
|
||||||
require.NoError(t, rc.waitForServiceContainers()(context.Background()))
|
require.NoError(t, rc.waitForServiceContainers()(t.Context()))
|
||||||
service.AssertExpectations(t)
|
service.AssertExpectations(t)
|
||||||
assert.Equal(t, "id", svc.info.ID)
|
assert.Equal(t, "id", svc.info.ID)
|
||||||
})
|
})
|
||||||
@@ -1540,13 +1568,13 @@ func TestWaitForServiceContainers(t *testing.T) {
|
|||||||
service.On("Inspect", mock.Anything).Return((*container.Info)(nil), errors.New("daemon is gone")).Once()
|
service.On("Inspect", mock.Anything).Return((*container.Info)(nil), errors.New("daemon is gone")).Once()
|
||||||
|
|
||||||
rc := newRunContext(-1, &serviceContainer{name: "postgres", container: service})
|
rc := newRunContext(-1, &serviceContainer{name: "postgres", container: service})
|
||||||
err := rc.waitForServiceContainers()(context.Background())
|
err := rc.waitForServiceContainers()(t.Context())
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "failed to inspect service 'postgres'")
|
assert.Contains(t, err.Error(), "failed to inspect service 'postgres'")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("is a no-op without services", func(t *testing.T) {
|
t.Run("is a no-op without services", func(t *testing.T) {
|
||||||
require.NoError(t, newRunContext(0).waitForServiceContainers()(context.Background()))
|
require.NoError(t, newRunContext(0).waitForServiceContainers()(t.Context()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1562,7 +1590,7 @@ func TestReportUnstartedServices(t *testing.T) {
|
|||||||
{name: "redis", container: running},
|
{name: "redis", container: running},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
require.NoError(t, rc.reportUnstartedServices()(context.Background()))
|
require.NoError(t, rc.reportUnstartedServices()(t.Context()))
|
||||||
dead.AssertExpectations(t)
|
dead.AssertExpectations(t)
|
||||||
running.AssertExpectations(t)
|
running.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
@@ -1602,14 +1630,14 @@ func TestCaptureJobContainerInfoExportsDockerWorkspace(t *testing.T) {
|
|||||||
JobContainer: job,
|
JobContainer: job,
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, rc.captureJobContainerInfo()(context.Background()))
|
require.NoError(t, rc.captureJobContainerInfo()(t.Context()))
|
||||||
|
|
||||||
assert.Equal(t, "job-container-id", rc.jobContainerID)
|
assert.Equal(t, "job-container-id", rc.jobContainerID)
|
||||||
assert.Equal(t, "/var/lib/docker/volumes/job/_data/owner/repo", rc.Env["GITEA_DOCKER_WORKSPACE"])
|
assert.Equal(t, "/var/lib/docker/volumes/job/_data/owner/repo", rc.Env["GITEA_DOCKER_WORKSPACE"])
|
||||||
|
|
||||||
rc.Config.Workdir = "/elsewhere"
|
rc.Config.Workdir = "/elsewhere"
|
||||||
rc.Env = map[string]string{}
|
rc.Env = map[string]string{}
|
||||||
require.NoError(t, rc.captureJobContainerInfo()(context.Background()))
|
require.NoError(t, rc.captureJobContainerInfo()(t.Context()))
|
||||||
assert.NotContains(t, rc.Env, "GITEA_DOCKER_WORKSPACE")
|
assert.NotContains(t, rc.Env, "GITEA_DOCKER_WORKSPACE")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1686,7 +1714,7 @@ func TestRunContextContainerImageCannotSelectBackend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunContextImageOS(t *testing.T) {
|
func TestRunContextImageOS(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
t.Run("prefers the release in the resolved image tag", func(t *testing.T) {
|
t.Run("prefers the release in the resolved image tag", func(t *testing.T) {
|
||||||
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
||||||
@@ -1713,7 +1741,7 @@ func TestRunContextImageOS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunContextGetRunnerContext(t *testing.T) {
|
func TestRunContextGetRunnerContext(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
t.Run("adds the runner values the container cannot know", func(t *testing.T) {
|
t.Run("adds the runner values the container cannot know", func(t *testing.T) {
|
||||||
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
||||||
@@ -1765,8 +1793,7 @@ func TestRunContextUpdateExtraPath(t *testing.T) {
|
|||||||
{name: "over 16 MiB", content: []byte(strings.Repeat("x", 16*1024*1024+1)), wantErr: true},
|
{name: "over 16 MiB", content: []byte(strings.Repeat("x", 16*1024*1024+1)), wantErr: true},
|
||||||
} {
|
} {
|
||||||
t.Run(testcase.name, func(t *testing.T) {
|
t.Run(testcase.name, func(t *testing.T) {
|
||||||
logger := log.New()
|
logger := logging.New(logging.DiscardHandler)
|
||||||
logger.SetOutput(io.Discard)
|
|
||||||
ctx := common.WithLogger(t.Context(), logger)
|
ctx := common.WithLogger(t.Context(), logger)
|
||||||
jobContainer := &containerMock{}
|
jobContainer := &containerMock{}
|
||||||
jobContainer.On("GetContainerArchive", mock.Anything, "/github/path").
|
jobContainer.On("GetContainerArchive", mock.Anything, "/github/path").
|
||||||
@@ -1794,7 +1821,7 @@ func TestParentDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunContextWithGithubEnvRunnerValues(t *testing.T) {
|
func TestRunContextWithGithubEnvRunnerValues(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
||||||
rc.Config.RunnerName = "runner-1"
|
rc.Config.RunnerName = "runner-1"
|
||||||
rc.Config.Secrets = map[string]string{"ACTIONS_STEP_DEBUG": "true"}
|
rc.Config.Secrets = map[string]string{"ACTIONS_STEP_DEBUG": "true"}
|
||||||
|
|||||||
+59
-50
@@ -5,20 +5,23 @@
|
|||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/ghcontext"
|
"gitea.com/gitea/runner/act/ghcontext"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
docker_container "github.com/moby/moby/api/types/container"
|
docker_container "github.com/moby/moby/api/types/container"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,7 +50,7 @@ type Config struct {
|
|||||||
ContainerDaemonSocket string // Path to Docker daemon socket
|
ContainerDaemonSocket string // Path to Docker daemon socket
|
||||||
ContainerOptions string // Options for the job container
|
ContainerOptions string // Options for the job container
|
||||||
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
|
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
|
||||||
GitHubInstance string // GitHub instance to use, default "github.com"
|
GitHubInstance string // instance hostname or URL, empty when unknown
|
||||||
ContainerCapAdd []string // list of kernel capabilities to add to the containers
|
ContainerCapAdd []string // list of kernel capabilities to add to the containers
|
||||||
ContainerCapDrop []string // list of kernel capabilities to remove from the containers
|
ContainerCapDrop []string // list of kernel capabilities to remove from the containers
|
||||||
ArtifactServerPath string // the path where the artifact server stores uploads
|
ArtifactServerPath string // the path where the artifact server stores uploads
|
||||||
@@ -76,16 +79,19 @@ type Config struct {
|
|||||||
DefaultActionInstanceIsSelfHosted bool
|
DefaultActionInstanceIsSelfHosted bool
|
||||||
PlatformPicker func(labels []string) string
|
PlatformPicker func(labels []string) string
|
||||||
KubernetesPicker func(labels []string) container.KubernetesOptions
|
KubernetesPicker func(labels []string) container.KubernetesOptions
|
||||||
JobLoggerLevel *log.Level // the level of job logger
|
InstanceTransport http.RoundTripper
|
||||||
ValidVolumes []string // only volumes (and bind mounts) in this slice can be mounted on the job container or service containers
|
JobLoggerLevel *logging.Level // the level of job logger
|
||||||
SharedToolCache bool // one tool cache for all jobs instead of one per job
|
ValidVolumes []string // only volumes (and bind mounts) in this slice can be mounted on the job container or service containers
|
||||||
InsecureSkipTLS bool // whether to skip verifying TLS certificate of the Gitea instance
|
SharedToolCache bool // one tool cache for all jobs instead of one per job
|
||||||
MaxParallel int // max parallel jobs to run across all workflows (0 = no limit, uses CPU count)
|
InsecureSkipTLS bool // whether to skip verifying TLS certificate of the Gitea instance
|
||||||
AllocatePTY bool // allocate a pseudo-TTY for each step's process
|
ClientCertFile, ClientKeyFile string // PEM client certificate for the Gitea instance
|
||||||
ServiceReadyTimeout time.Duration // how long a job waits for its service containers to report healthy (0 uses the default)
|
MaxParallel int // max parallel jobs to run across all workflows (0 = no limit, uses CPU count)
|
||||||
RunnerName string // name this runner registered with, reported as `runner.name`, defaults to the hostname
|
AllocatePTY bool // allocate a pseudo-TTY for each step's process
|
||||||
JobStartedHook string // script run inside the job environment before the job's first step; ACTIONS_RUNNER_HOOK_JOB_STARTED is read from Env when empty
|
ServiceReadyTimeout time.Duration // how long a job waits for its service containers to report healthy (0 uses the default)
|
||||||
JobCompletedHook string // script run inside the job environment after the job's last step; ACTIONS_RUNNER_HOOK_JOB_COMPLETED is read from Env when empty
|
NoSweep bool // keep the containers, networks and volumes a job creates through its docker proxy
|
||||||
|
RunnerName string // name this runner registered with, reported as `runner.name`, defaults to the hostname
|
||||||
|
JobStartedHook string // script run inside the job environment before the job's first step; ACTIONS_RUNNER_HOOK_JOB_STARTED is read from Env when empty
|
||||||
|
JobCompletedHook string // script run inside the job environment after the job's last step; ACTIONS_RUNNER_HOOK_JOB_COMPLETED is read from Env when empty
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunnerDebug reports whether debug logging is on, exposed as `runner.debug` and
|
// RunnerDebug reports whether debug logging is on, exposed as `runner.debug` and
|
||||||
@@ -106,13 +112,7 @@ func (c Config) GetToken() string {
|
|||||||
|
|
||||||
// DefaultActionURL returns the host used for implicit remote actions.
|
// DefaultActionURL returns the host used for implicit remote actions.
|
||||||
func (c Config) DefaultActionURL() string {
|
func (c Config) DefaultActionURL() string {
|
||||||
if c.DefaultActionInstance != "" {
|
return cmp.Or(c.DefaultActionInstance, c.GitHubInstance, "github.com")
|
||||||
return c.DefaultActionInstance
|
|
||||||
}
|
|
||||||
if c.GitHubInstance != "" {
|
|
||||||
return c.GitHubInstance
|
|
||||||
}
|
|
||||||
return "github.com"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type caller struct {
|
type caller struct {
|
||||||
@@ -149,7 +149,7 @@ func (runner *runnerImpl) configure() (*runnerImpl, error) {
|
|||||||
if runner.config.EventJSON != "" {
|
if runner.config.EventJSON != "" {
|
||||||
runner.eventJSON = runner.config.EventJSON
|
runner.eventJSON = runner.config.EventJSON
|
||||||
} else if runner.config.EventPath != "" {
|
} else if runner.config.EventPath != "" {
|
||||||
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
logging.Debugf("Reading event.json from %s", runner.config.EventPath)
|
||||||
eventJSONBytes, err := os.ReadFile(runner.config.EventPath)
|
eventJSONBytes, err := os.ReadFile(runner.config.EventPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -163,7 +163,7 @@ func maxParallelFor(strategy *model.Strategy, combinations int) int {
|
|||||||
maxParallel := 4 // actionslib has no default for an undeclared max-parallel
|
maxParallel := 4 // actionslib has no default for an undeclared max-parallel
|
||||||
if strategy != nil {
|
if strategy != nil {
|
||||||
if limit, err := strategy.MaxParallel(); err != nil {
|
if limit, err := strategy.MaxParallel(); err != nil {
|
||||||
log.Errorf("Ignoring invalid max-parallel: %v", err)
|
logging.Errorf("Ignoring invalid max-parallel: %v", err)
|
||||||
} else if limit > 0 {
|
} else if limit > 0 {
|
||||||
maxParallel = limit
|
maxParallel = limit
|
||||||
}
|
}
|
||||||
@@ -173,39 +173,48 @@ func maxParallelFor(strategy *model.Strategy, combinations int) int {
|
|||||||
|
|
||||||
// NewPlanExecutor ...
|
// NewPlanExecutor ...
|
||||||
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
||||||
|
for _, stage := range plan.Stages {
|
||||||
|
for _, run := range stage.Runs {
|
||||||
|
job := run.Job()
|
||||||
|
if runsOn := job.RunsOn(); job.Uses == "" && len(job.Steps) > 0 && (len(runsOn) == 0 || slices.Contains(runsOn, "")) { // Gitea's needs placeholders carry no steps
|
||||||
|
return common.NewErrorExecutor(fmt.Errorf("job %q requires a non-empty 'runs-on'", run.JobID))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
maxJobNameLen := 0
|
maxJobNameLen := 0
|
||||||
|
|
||||||
stagePipeline := make([]common.Executor, 0)
|
stagePipeline := make([]common.Executor, 0)
|
||||||
log.Debugf("Plan Stages: %v", plan.Stages)
|
logging.Debugf("Plan Stages: %v", plan.Stages)
|
||||||
|
|
||||||
for i := range plan.Stages {
|
for i := range plan.Stages {
|
||||||
stage := plan.Stages[i]
|
stage := plan.Stages[i]
|
||||||
stagePipeline = append(stagePipeline, func(ctx context.Context) error {
|
stagePipeline = append(stagePipeline, func(ctx context.Context) error {
|
||||||
pipeline := make([]common.Executor, 0)
|
pipeline := make([]common.Executor, 0)
|
||||||
for _, run := range stage.Runs {
|
for _, run := range stage.Runs {
|
||||||
log.Debugf("Stages Runs: %v", stage.Runs)
|
logging.Debugf("Stages Runs: %v", stage.Runs)
|
||||||
stageExecutor := make([]common.Executor, 0)
|
stageExecutor := make([]common.Executor, 0)
|
||||||
job := run.Job()
|
job := run.Job()
|
||||||
log.Debugf("Job.Name: %v", job.Name)
|
logging.Debugf("Job.Name: %v", job.Name)
|
||||||
log.Debugf("Job.RawNeeds: %v", job.RawNeeds)
|
logging.Debugf("Job.RawNeeds: %v", job.RawNeeds)
|
||||||
log.Debugf("Job.RawRunsOn: %v", job.RawRunsOn)
|
logging.Debugf("Job.RawRunsOn: %v", job.RawRunsOn)
|
||||||
log.Debugf("Job.Env: %v", job.Env)
|
logging.Debugf("Job.Env: %v", job.Env)
|
||||||
log.Debugf("Job.If: %v", job.If)
|
logging.Debugf("Job.If: %v", job.If)
|
||||||
for step := range job.Steps {
|
for step := range job.Steps {
|
||||||
if nil != job.Steps[step] {
|
if nil != job.Steps[step] {
|
||||||
log.Debugf("Job.Steps: %v", job.Steps[step].String())
|
logging.Debugf("Job.Steps: %v", job.Steps[step].String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Debugf("Job.TimeoutMinutes: %v", job.TimeoutMinutes)
|
logging.Debugf("Job.TimeoutMinutes: %v", job.TimeoutMinutes)
|
||||||
log.Debugf("Job.Services: %v", job.Services)
|
logging.Debugf("Job.Services: %v", job.Services)
|
||||||
log.Debugf("Job.Strategy: %v", job.Strategy)
|
logging.Debugf("Job.Strategy: %v", job.Strategy)
|
||||||
log.Debugf("Job.RawContainer: %v", job.RawContainer)
|
logging.Debugf("Job.RawContainer: %v", job.RawContainer)
|
||||||
log.Debugf("Job.Defaults.Run.Shell: %v", job.Defaults.Run.Shell)
|
logging.Debugf("Job.Defaults.Run.Shell: %v", job.Defaults.Run.Shell)
|
||||||
log.Debugf("Job.Defaults.Run.WorkingDirectory: %v", job.Defaults.Run.WorkingDirectory)
|
logging.Debugf("Job.Defaults.Run.WorkingDirectory: %v", job.Defaults.Run.WorkingDirectory)
|
||||||
log.Debugf("Job.Outputs: %v", job.Outputs)
|
logging.Debugf("Job.Outputs: %v", job.Outputs)
|
||||||
log.Debugf("Job.Uses: %v", job.Uses)
|
logging.Debugf("Job.Uses: %v", job.Uses)
|
||||||
log.Debugf("Job.With: %v", job.With)
|
logging.Debugf("Job.With: %v", job.With)
|
||||||
log.Debugf("Job.Result: %v", job.Result)
|
logging.Debugf("Job.Result: %v", job.Result)
|
||||||
|
|
||||||
if job.Strategy != nil || job.RawStrategy.Kind == yaml.ScalarNode {
|
if job.Strategy != nil || job.RawStrategy.Kind == yaml.ScalarNode {
|
||||||
strategyRc, err := runner.newRunContext(ctx, run, nil)
|
strategyRc, err := runner.newRunContext(ctx, run, nil)
|
||||||
@@ -217,13 +226,13 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Job.Strategy.FailFast: %v", job.Strategy.GetFailFast())
|
logging.Debugf("Job.Strategy.FailFast: %v", job.Strategy.GetFailFast())
|
||||||
log.Debugf("Job.Strategy.FailFastString: %v", job.Strategy.FailFastString)
|
logging.Debugf("Job.Strategy.FailFastString: %v", job.Strategy.FailFastString)
|
||||||
log.Debugf("Job.Strategy.MaxParallelString: %v", job.Strategy.MaxParallelString)
|
logging.Debugf("Job.Strategy.MaxParallelString: %v", job.Strategy.MaxParallelString)
|
||||||
log.Debugf("Job.Strategy.RawMatrix: %v", job.Strategy.RawMatrix)
|
logging.Debugf("Job.Strategy.RawMatrix: %v", job.Strategy.RawMatrix)
|
||||||
// An unevaluated expression is left in place, which GetMatrixes below rejects.
|
// An unevaluated expression is left in place, which GetMatrixes below rejects.
|
||||||
if err := strategyRc.NewExpressionEvaluator(ctx).EvaluateYamlNode(ctx, &job.Strategy.RawMatrix); err != nil {
|
if err := strategyRc.NewExpressionEvaluator(ctx).EvaluateYamlNode(ctx, &job.Strategy.RawMatrix); err != nil {
|
||||||
log.Errorf("Error while evaluating matrix: %v", err)
|
logging.Errorf("Error while evaluating matrix: %v", err)
|
||||||
}
|
}
|
||||||
for _, value := range []*string{&job.Strategy.FailFastString, &job.Strategy.MaxParallelString} {
|
for _, value := range []*string{&job.Strategy.FailFastString, &job.Strategy.MaxParallelString} {
|
||||||
if evaluated, err := strategyRc.ExprEval.Interpolate(ctx, *value); err == nil {
|
if evaluated, err := strategyRc.ExprEval.Interpolate(ctx, *value); err == nil {
|
||||||
@@ -237,11 +246,11 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get job matrix: %w", err)
|
return fmt.Errorf("could not get job matrix: %w", err)
|
||||||
}
|
}
|
||||||
log.Debugf("Job Matrices: %v", matrixes)
|
logging.Debugf("Job Matrices: %v", matrixes)
|
||||||
|
|
||||||
maxParallel := maxParallelFor(job.Strategy, len(matrixes))
|
maxParallel := maxParallelFor(job.Strategy, len(matrixes))
|
||||||
|
|
||||||
log.Infof("Running job with maxParallel=%d for %d matrix combinations", maxParallel, len(matrixes))
|
logging.Infof("Running job with maxParallel=%d for %d matrix combinations", maxParallel, len(matrixes))
|
||||||
|
|
||||||
for i, matrix := range matrixes {
|
for i, matrix := range matrixes {
|
||||||
index, total := i, len(matrixes)
|
index, total := i, len(matrixes)
|
||||||
@@ -300,7 +309,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
|||||||
if len(pipeline) == 1 {
|
if len(pipeline) == 1 {
|
||||||
// Single run/job: execute directly without additional parallelization wrapper
|
// Single run/job: execute directly without additional parallelization wrapper
|
||||||
// This ensures max-parallel is the only limiting factor
|
// This ensures max-parallel is the only limiting factor
|
||||||
log.Debugf("Single pipeline element, executing directly")
|
logging.Debugf("Single pipeline element, executing directly")
|
||||||
return pipeline[0](ctx)
|
return pipeline[0](ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,9 +319,9 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
|||||||
// If MaxParallel is set in config, use it
|
// If MaxParallel is set in config, use it
|
||||||
if runner.config.MaxParallel > 0 {
|
if runner.config.MaxParallel > 0 {
|
||||||
parallelism = runner.config.MaxParallel
|
parallelism = runner.config.MaxParallel
|
||||||
log.Debugf("Using configured max-parallel: %d", parallelism)
|
logging.Debugf("Using configured max-parallel: %d", parallelism)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Using CPU count for parallelism: %d", parallelism)
|
logging.Debugf("Using CPU count for parallelism: %d", parallelism)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't exceed the number of pipeline elements
|
// Don't exceed the number of pipeline elements
|
||||||
@@ -320,7 +329,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
|||||||
parallelism = len(pipeline)
|
parallelism = len(pipeline)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Executing %d pipeline elements with parallelism %d", len(pipeline), parallelism)
|
logging.Infof("Executing %d pipeline elements with parallelism %d", len(pipeline), parallelism)
|
||||||
return common.NewParallelExecutor(parallelism, pipeline...)(ctx)
|
return common.NewParallelExecutor(parallelism, pipeline...)(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-28
@@ -17,17 +17,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
baseImage = "node:24-bookworm-slim"
|
baseImage = "node:24-bookworm-slim"
|
||||||
platforms map[string]string
|
platforms map[string]string
|
||||||
logLevel = log.DebugLevel
|
logLevel = logging.LevelDebug
|
||||||
workdir = "testdata"
|
workdir = "testdata"
|
||||||
secrets map[string]string
|
secrets map[string]string
|
||||||
testRunID = strings.ToLower(rand.Text()[:8]) // keeps docker names apart from concurrent test processes on one daemon
|
testRunID = strings.ToLower(rand.Text()[:8]) // keeps docker names apart from concurrent test processes on one daemon
|
||||||
@@ -54,7 +55,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if l := os.Getenv("ACT_TEST_LOG_LEVEL"); l != "" {
|
if l := os.Getenv("ACT_TEST_LOG_LEVEL"); l != "" {
|
||||||
if lvl, err := log.ParseLevel(l); err == nil {
|
if lvl, err := logging.ParseLevel(l); err == nil {
|
||||||
logLevel = lvl
|
logLevel = lvl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,10 +71,11 @@ func TestNoWorkflowsFoundByPlanner(t *testing.T) {
|
|||||||
planner, err := model.NewWorkflowPlanner("res", true)
|
planner, err := model.NewWorkflowPlanner("res", true)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
out := log.StandardLogger().Out
|
out := logging.Output()
|
||||||
|
t.Cleanup(func() { logging.SetOutput(out) })
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
logging.SetOutput(&buf)
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
plan, err := planner.PlanEvent("pull_request")
|
plan, err := planner.PlanEvent("pull_request")
|
||||||
assert.NotNil(t, plan)
|
assert.NotNil(t, plan)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
@@ -83,25 +85,24 @@ func TestNoWorkflowsFoundByPlanner(t *testing.T) {
|
|||||||
assert.NotNil(t, plan)
|
assert.NotNil(t, plan)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Contains(t, buf.String(), "no workflows found by planner")
|
assert.Contains(t, buf.String(), "no workflows found by planner")
|
||||||
log.SetOutput(out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGraphMissingEvent(t *testing.T) {
|
func TestGraphMissingEvent(t *testing.T) {
|
||||||
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/no-event.yml", true)
|
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/no-event.yml", true)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
out := log.StandardLogger().Out
|
out := logging.Output()
|
||||||
|
t.Cleanup(func() { logging.SetOutput(out) })
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
logging.SetOutput(&buf)
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
plan, err := planner.PlanEvent("push")
|
plan, err := planner.PlanEvent("push")
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.NotNil(t, plan)
|
assert.NotNil(t, plan)
|
||||||
assert.Empty(t, plan.Stages)
|
assert.Empty(t, plan.Stages)
|
||||||
|
|
||||||
assert.Contains(t, buf.String(), "no events found for workflow: no-event.yml")
|
assert.Contains(t, buf.String(), `msg="no events found for workflow" file=no-event.yml`)
|
||||||
log.SetOutput(out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGraphMissingFirst(t *testing.T) {
|
func TestGraphMissingFirst(t *testing.T) {
|
||||||
@@ -118,29 +119,30 @@ func TestGraphWithMissing(t *testing.T) {
|
|||||||
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/missing.yml", true)
|
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/missing.yml", true)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
out := log.StandardLogger().Out
|
out := logging.Output()
|
||||||
|
t.Cleanup(func() { logging.SetOutput(out) })
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
logging.SetOutput(&buf)
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
plan, err := planner.PlanEvent("push")
|
plan, err := planner.PlanEvent("push")
|
||||||
assert.NotNil(t, plan)
|
assert.NotNil(t, plan)
|
||||||
assert.Empty(t, plan.Stages)
|
assert.Empty(t, plan.Stages)
|
||||||
assert.EqualError(t, err, "unable to build dependency graph for missing (missing.yml)") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.EqualError(t, err, "unable to build dependency graph for missing (missing.yml)") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
assert.Contains(t, buf.String(), "unable to build dependency graph for missing (missing.yml)")
|
assert.Contains(t, buf.String(), "unable to build dependency graph for missing (missing.yml)")
|
||||||
log.SetOutput(out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGraphWithSomeMissing(t *testing.T) {
|
func TestGraphWithSomeMissing(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/", true)
|
planner, err := model.NewWorkflowPlanner("testdata/issue-1595/", true)
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
out := log.StandardLogger().Out
|
out := logging.Output()
|
||||||
|
t.Cleanup(func() { logging.SetOutput(out) })
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
logging.SetOutput(&buf)
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
|
|
||||||
plan, err := planner.PlanAll()
|
plan, err := planner.PlanAll()
|
||||||
assert.Error(t, err, "unable to build dependency graph for no first (no-first.yml)") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.Error(t, err, "unable to build dependency graph for no first (no-first.yml)") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
@@ -148,7 +150,21 @@ func TestGraphWithSomeMissing(t *testing.T) {
|
|||||||
assert.Len(t, plan.Stages, 1)
|
assert.Len(t, plan.Stages, 1)
|
||||||
assert.Contains(t, buf.String(), "unable to build dependency graph for missing (missing.yml)")
|
assert.Contains(t, buf.String(), "unable to build dependency graph for missing (missing.yml)")
|
||||||
assert.Contains(t, buf.String(), "unable to build dependency graph for no first (no-first.yml)")
|
assert.Contains(t, buf.String(), "unable to build dependency graph for no first (no-first.yml)")
|
||||||
log.SetOutput(out)
|
}
|
||||||
|
|
||||||
|
func TestNewPlanExecutorRequiresRunsOnBeforeRunningAnyJob(t *testing.T) {
|
||||||
|
for _, runsOn := range []string{"", "if: false", "runs-on: [ubuntu-latest, '']"} {
|
||||||
|
workflow, err := model.ReadWorkflow(strings.NewReader("jobs:\n first:\n runs-on: ubuntu-latest\n steps: [run: echo]\n test:\n needs: first\n " + runsOn + "\n steps: [run: echo]\n"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
plan, err := model.CombineWorkflowPlanner(workflow).PlanJob("test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.ErrorContains(t, (&runnerImpl{config: &Config{}}).NewPlanExecutor(plan)(t.Context()), `job "test" requires a non-empty 'runs-on'`, runsOn)
|
||||||
|
assert.Empty(t, workflow.Jobs["first"].Result, runsOn)
|
||||||
|
}
|
||||||
|
|
||||||
|
workflow := &model.Workflow{Jobs: map[string]*model.Job{"build": {Result: "success"}}}
|
||||||
|
require.NoError(t, (&runnerImpl{config: &Config{}}).NewPlanExecutor(&model.Plan{Stages: []*model.Stage{{Runs: []*model.Run{{Workflow: workflow, JobID: "build"}}}}})(t.Context()))
|
||||||
|
assert.Equal(t, "success", workflow.Jobs["build"].Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGraphEvent(t *testing.T) {
|
func TestGraphEvent(t *testing.T) {
|
||||||
@@ -205,7 +221,7 @@ type TestJobFileInfo struct {
|
|||||||
func (j *TestJobFileInfo) runTest(ctx context.Context, t *testing.T, cfg *Config) {
|
func (j *TestJobFileInfo) runTest(ctx context.Context, t *testing.T, cfg *Config) {
|
||||||
fmt.Printf("::group::%s\n", j.workflowPath) //nolint:forbidigo // pre-existing issue from nektos/act
|
fmt.Printf("::group::%s\n", j.workflowPath) //nolint:forbidigo // pre-existing issue from nektos/act
|
||||||
|
|
||||||
log.SetLevel(logLevel)
|
logging.SetLevel(logLevel)
|
||||||
|
|
||||||
workdir, err := filepath.Abs(j.workdir)
|
workdir, err := filepath.Abs(j.workdir)
|
||||||
assert.NoError(t, err, workdir) //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, workdir) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
@@ -265,7 +281,7 @@ func TestRunEvent(t *testing.T) {
|
|||||||
requireDocker(t)
|
requireDocker(t)
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
tables := []TestJobFileInfo{
|
tables := []TestJobFileInfo{
|
||||||
// Shells
|
// Shells
|
||||||
@@ -368,7 +384,7 @@ func TestRunEventHostEnvironment(t *testing.T) {
|
|||||||
// nix-specific cases gate on their own tool below so a missing pwsh/nix skips just those.
|
// nix-specific cases gate on their own tool below so a missing pwsh/nix skips just those.
|
||||||
requireHostTools(t, "bash", "node")
|
requireHostTools(t, "bash", "node")
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
tables := []TestJobFileInfo{}
|
tables := []TestJobFileInfo{}
|
||||||
|
|
||||||
@@ -466,7 +482,7 @@ func TestRunEventHostEnvironment(t *testing.T) {
|
|||||||
func TestDryrunEvent(t *testing.T) {
|
func TestDryrunEvent(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
// Dryrun plans without containers or network (shells and local actions only).
|
// Dryrun plans without containers or network (shells and local actions only).
|
||||||
ctx := common.WithDryrun(context.Background(), true)
|
ctx := common.WithDryrun(t.Context(), true)
|
||||||
|
|
||||||
tables := []TestJobFileInfo{
|
tables := []TestJobFileInfo{
|
||||||
// Shells
|
// Shells
|
||||||
@@ -497,7 +513,7 @@ func TestReusableWorkflowCaller(t *testing.T) {
|
|||||||
requireDocker(t)
|
requireDocker(t)
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
table := TestJobFileInfo{workdir, "uses-workflow", "push", "", platforms, map[string]string{"secret": "keep_it_private"}}
|
table := TestJobFileInfo{workdir, "uses-workflow", "push", "", platforms, map[string]string{"secret": "keep_it_private"}}
|
||||||
table.runTest(context.Background(), t, &Config{Secrets: table.secrets})
|
table.runTest(t.Context(), t, &Config{Secrets: table.secrets})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunEventSecrets(t *testing.T) {
|
func TestRunEventSecrets(t *testing.T) {
|
||||||
@@ -518,7 +534,7 @@ func TestRunEventSecrets(t *testing.T) {
|
|||||||
secrets, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".secrets"))
|
secrets, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".secrets"))
|
||||||
assert.NoError(t, err, "Failed to read .secrets") //nolint:testifylint // pre-existing issue from nektos/act
|
assert.NoError(t, err, "Failed to read .secrets") //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
tjfi.runTest(context.Background(), t, &Config{Secrets: secrets, Env: env})
|
tjfi.runTest(t.Context(), t, &Config{Secrets: secrets, Env: env})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunEventPullRequest(t *testing.T) {
|
func TestRunEventPullRequest(t *testing.T) {
|
||||||
@@ -535,5 +551,5 @@ func TestRunEventPullRequest(t *testing.T) {
|
|||||||
platforms: platforms,
|
platforms: platforms,
|
||||||
}
|
}
|
||||||
|
|
||||||
tjfi.runTest(context.Background(), t, &Config{EventPath: filepath.Join(workdir, workflowPath, "event.json")})
|
tjfi.runTest(t.Context(), t, &Config{EventPath: filepath.Join(workdir, workflowPath, "event.json")})
|
||||||
}
|
}
|
||||||
+22
-15
@@ -47,11 +47,11 @@ const maxSymlinkDepth = 10
|
|||||||
func (s stepStage) String() string {
|
func (s stepStage) String() string {
|
||||||
switch s {
|
switch s {
|
||||||
case stepStagePre:
|
case stepStagePre:
|
||||||
return "Pre"
|
return StagePre
|
||||||
case stepStageMain:
|
case stepStageMain:
|
||||||
return "Main"
|
return StageMain
|
||||||
case stepStagePost:
|
case stepStagePost:
|
||||||
return "Post"
|
return StagePost
|
||||||
}
|
}
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
@@ -97,14 +97,14 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
stepResult.Conclusion = model.StepStatusFailure
|
stepResult.Conclusion = model.StepStatusFailure
|
||||||
stepResult.Outcome = model.StepStatusFailure
|
stepResult.Outcome = model.StepStatusFailure
|
||||||
logger.WithField("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepModel)
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepModel)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !runStep {
|
if !runStep {
|
||||||
stepResult.Conclusion = model.StepStatusSkipped
|
stepResult.Conclusion = model.StepStatusSkipped
|
||||||
stepResult.Outcome = model.StepStatusSkipped
|
stepResult.Outcome = model.StepStatusSkipped
|
||||||
logger.WithField("stepResult", stepResult.Conclusion).Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
|
logger.With("stepResult", stepResult.Conclusion).Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,12 +112,13 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
|||||||
if strings.Contains(stepString, "::add-mask::") {
|
if strings.Contains(stepString, "::add-mask::") {
|
||||||
stepString = "add-mask command"
|
stepString = "add-mask command"
|
||||||
}
|
}
|
||||||
|
startLogger := logger.With(StepStartedField, true)
|
||||||
if stage == stepStageMain {
|
if stage == stepStageMain {
|
||||||
// Main steps print their own raw "Run <title>" header, so this line is redundant and
|
// Main steps print their own raw "Run <title>" header, so this line is redundant and
|
||||||
// only leaks into the "Set up job" section for the first step; keep it as a debug trace.
|
// only leaks into the "Set up job" section for the first step; keep it as a debug trace.
|
||||||
logger.Debugf("Run %s %s", stage, stepString)
|
startLogger.Debugf("Run %s %s", stage, stepString)
|
||||||
} else {
|
} else {
|
||||||
logger.Infof("Run %s %s", stage, stepString)
|
startLogger.Infof("Run %s %s", stage, stepString)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare and clean Runner File Commands
|
// Prepare and clean Runner File Commands
|
||||||
@@ -186,29 +187,35 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
err = insecureErr
|
err = insecureErr
|
||||||
}
|
}
|
||||||
if fileErr := processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnvFile); fileErr != nil && err == nil {
|
fileCommandCtx := ctx
|
||||||
|
if ctx.Err() != nil { // an interrupted step still records its file commands, a live deadline stays in force
|
||||||
|
var cancelFileCommands context.CancelFunc
|
||||||
|
fileCommandCtx, cancelFileCommands = context.WithTimeout(context.WithoutCancel(ctx), jobCleanupTimeout)
|
||||||
|
defer cancelFileCommands()
|
||||||
|
}
|
||||||
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, envFileCommand, rc, rc.setEnvFile); fileErr != nil && err == nil {
|
||||||
err = fileErr
|
err = fileErr
|
||||||
}
|
}
|
||||||
if fileErr := processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState); fileErr != nil && err == nil {
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, stateFileCommand, rc, rc.saveState); fileErr != nil && err == nil {
|
||||||
err = fileErr
|
err = fileErr
|
||||||
}
|
}
|
||||||
if fileErr := processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput); fileErr != nil && err == nil {
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, outputFileCommand, rc, rc.setOutput); fileErr != nil && err == nil {
|
||||||
err = fileErr
|
err = fileErr
|
||||||
}
|
}
|
||||||
if fileErr := rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand)); fileErr != nil && err == nil {
|
if fileErr := rc.UpdateExtraPath(fileCommandCtx, path.Join(actPath, pathFileCommand)); fileErr != nil && err == nil {
|
||||||
err = fileErr
|
err = fileErr
|
||||||
}
|
}
|
||||||
_ = rc.JobContainer.Copy(actPath, files...)(ctx)
|
_ = rc.JobContainer.Copy(actPath, files...)(fileCommandCtx)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logger.WithField("stepResult", stepResult.Conclusion).Infof("Success - %s %s", stage, stepString)
|
logger.With("stepResult", stepResult.Conclusion).Infof("Success - %s %s", stage, stepString)
|
||||||
} else {
|
} else {
|
||||||
stepResult.Outcome = model.StepStatusFailure
|
stepResult.Outcome = model.StepStatusFailure
|
||||||
|
|
||||||
continueOnError, parseErr := isContinueOnError(ctx, stepModel.RawContinueOnError, step, stage)
|
continueOnError, parseErr := isContinueOnError(ctx, stepModel.RawContinueOnError, step, stage)
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
stepResult.Conclusion = model.StepStatusFailure
|
stepResult.Conclusion = model.StepStatusFailure
|
||||||
logger.WithField("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
||||||
return parseErr
|
return parseErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +230,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
|||||||
|
|
||||||
// Infof: Errorf entries are promoted to the user log by the reporter,
|
// Infof: Errorf entries are promoted to the user log by the reporter,
|
||||||
// which would duplicate the ##[error] annotation emitted elsewhere.
|
// which would duplicate the ##[error] annotation emitted elsewhere.
|
||||||
logger.WithField("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ func (sab *stepActionBuiltin) main() common.Executor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printRunActionHeader(ctx, sab.Step, sab.env, sab.RunContext)
|
printRunActionHeader(ctx, sab.Step, sab.env, sab.RunContext)
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
defer rawLogger.Infof("::endgroup::")
|
defer rawLogger.Infof("::endgroup::")
|
||||||
|
|
||||||
rc := sab.RunContext
|
rc := sab.RunContext
|
||||||
|
rc.ApplyExtraPath(ctx, &sab.env)
|
||||||
return sab.run(ctx, &action.Context{
|
return sab.run(ctx, &action.Context{
|
||||||
Container: rc.JobContainer,
|
Container: rc.JobContainer,
|
||||||
Github: rc.getGithubContext(ctx),
|
Github: rc.getGithubContext(ctx),
|
||||||
@@ -48,6 +49,9 @@ func (sab *stepActionBuiltin) main() common.Executor {
|
|||||||
NoSkipCheckout: rc.Config.NoSkipCheckout,
|
NoSkipCheckout: rc.Config.NoSkipCheckout,
|
||||||
UseGitIgnore: rc.Config.UseGitIgnore,
|
UseGitIgnore: rc.Config.UseGitIgnore,
|
||||||
InsecureSkipTLS: rc.Config.InsecureSkipTLS,
|
InsecureSkipTLS: rc.Config.InsecureSkipTLS,
|
||||||
|
ClientCertFile: rc.Config.ClientCertFile,
|
||||||
|
ClientKeyFile: rc.Config.ClientKeyFile,
|
||||||
|
Env: sab.env,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ func (sal *stepActionLocal) main() common.Executor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printRunActionHeader(ctx, sal.Step, sal.env, sal.getRunContext())
|
printRunActionHeader(ctx, sal.Step, sal.env, sal.getRunContext())
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
defer rawLogger.Infof("::endgroup::")
|
defer rawLogger.Infof("::endgroup::")
|
||||||
|
|
||||||
actionDir := filepath.Join(sal.getRunContext().Config.Workdir, sal.Step.Uses)
|
actionDir := filepath.Join(sal.getRunContext().Config.Workdir, sal.Step.Uses)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func (salm *stepActionLocalMocks) readAction(_ context.Context, step *model.Step
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStepActionLocalTest(t *testing.T) {
|
func TestStepActionLocalTest(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
salm := &stepActionLocalMocks{}
|
salm := &stepActionLocalMocks{}
|
||||||
@@ -225,7 +225,7 @@ func TestStepActionLocalPost(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
gogit "github.com/go-git/go-git/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepActionRemote struct {
|
type stepActionRemote struct {
|
||||||
@@ -68,41 +67,33 @@ func (sar *stepActionRemote) prepareActionExecutor() common.Executor {
|
|||||||
actionDir := sar.actionDir()
|
actionDir := sar.actionDir()
|
||||||
defaultActionURL := sar.RunContext.Config.DefaultActionURL()
|
defaultActionURL := sar.RunContext.Config.DefaultActionURL()
|
||||||
// For Gitea
|
// For Gitea
|
||||||
// A composite RunContext nils Config.Secrets, so getGitCloneToken would yield an
|
// github.Token, unlike Config.GetToken, survives the composite config copy, which nils Config.Secrets.
|
||||||
// empty token and clone the action anonymously (401 against the authenticated
|
gitClone := stepActionRemoteNewCloneExecutor(sar.RunContext.Config.withInstanceAuth(git.NewGitCloneExecutorInput{
|
||||||
// instance). github.Token survives the composite config copy and matches the
|
URL: sar.remoteAction.CloneURL(defaultActionURL),
|
||||||
// top-level token; keep the shouldCloneURLUseToken host gate to avoid leaking it.
|
|
||||||
cloneURL := sar.remoteAction.CloneURL(defaultActionURL)
|
|
||||||
token := ""
|
|
||||||
if shouldCloneURLUseToken(sar.RunContext.Config.GitHubInstance, sar.RunContext.Config.trustedActionInstance(), cloneURL) {
|
|
||||||
token = github.Token
|
|
||||||
}
|
|
||||||
gitClone := stepActionRemoteNewCloneExecutor(git.NewGitCloneExecutorInput{
|
|
||||||
URL: cloneURL,
|
|
||||||
Ref: sar.remoteAction.Ref,
|
Ref: sar.remoteAction.Ref,
|
||||||
Dir: actionDir,
|
Dir: actionDir,
|
||||||
Token: token,
|
|
||||||
OfflineMode: sar.RunContext.Config.ActionOfflineMode,
|
OfflineMode: sar.RunContext.Config.ActionOfflineMode,
|
||||||
Depth: sar.RunContext.Config.ActionCloneDepth,
|
Depth: sar.RunContext.Config.ActionCloneDepth,
|
||||||
// printPrepareActions reports the download with its resolved commit.
|
// printPrepareActions reports the download with its resolved commit.
|
||||||
Quiet: true,
|
Quiet: true,
|
||||||
|
}, github.Token))
|
||||||
InsecureSkipTLS: sar.cloneSkipTLS(), // For Gitea
|
|
||||||
})
|
|
||||||
var ntErr common.Executor
|
|
||||||
if err := gitClone(ctx); err != nil {
|
if err := gitClone(ctx); err != nil {
|
||||||
var refErr *git.Error
|
var refErr *git.Error
|
||||||
switch {
|
switch {
|
||||||
case errors.As(err, &refErr) && errors.Is(err, git.ErrShortRef):
|
case errors.As(err, &refErr) && errors.Is(err, git.ErrShortRef):
|
||||||
return fmt.Errorf("unable to resolve action `%s`, the provided ref `%s` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `%s` instead",
|
return fmt.Errorf("unable to resolve action `%s`, the provided ref `%s` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `%s` instead",
|
||||||
sar.Step.Uses, sar.remoteAction.Ref, refErr.Commit())
|
sar.Step.Uses, sar.remoteAction.Ref, refErr.Commit())
|
||||||
case errors.Is(err, gogit.ErrForceNeeded): // TODO: figure out if it will be easy to shadow/alias go-git err's
|
|
||||||
ntErr = common.NewInfoExecutor("Non-terminating error while running 'git clone': %v", err)
|
|
||||||
default:
|
default:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unlock, err := git.AcquireCloneLock(ctx, actionDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer unlock()
|
||||||
|
|
||||||
// Best effort: the download report falls back to the ref alone when the commit is unknown.
|
// Best effort: the download report falls back to the ref alone when the commit is unknown.
|
||||||
if _, sha, err := git.FindGitRevision(ctx, actionDir); err != nil {
|
if _, sha, err := git.FindGitRevision(ctx, actionDir); err != nil {
|
||||||
common.Logger(ctx).Debugf("unable to resolve the commit of %s: %v", sar.remoteAction.Reference(), err)
|
common.Logger(ctx).Debugf("unable to resolve the commit of %s: %v", sar.remoteAction.Reference(), err)
|
||||||
@@ -115,15 +106,9 @@ func (sar *stepActionRemote) prepareActionExecutor() common.Executor {
|
|||||||
return f, f, err
|
return f, f, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return common.NewPipelineExecutor(
|
actionModel, err := sar.readAction(ctx, sar.Step, actionDir, sar.remoteAction.Path, remoteReader, os.WriteFile)
|
||||||
ntErr,
|
sar.action = actionModel
|
||||||
func(ctx context.Context) error {
|
return err
|
||||||
defer git.AcquireCloneLock(actionDir)()
|
|
||||||
actionModel, err := sar.readAction(ctx, sar.Step, actionDir, sar.remoteAction.Path, remoteReader, os.WriteFile)
|
|
||||||
sar.action = actionModel
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
)(ctx)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +134,7 @@ func (sar *stepActionRemote) main() common.Executor {
|
|||||||
sar.prepareActionExecutor(),
|
sar.prepareActionExecutor(),
|
||||||
runStepExecutor(sar, stepStageMain, func(ctx context.Context) error {
|
runStepExecutor(sar, stepStageMain, func(ctx context.Context) error {
|
||||||
printRunActionHeader(ctx, sar.Step, sar.env, sar.RunContext)
|
printRunActionHeader(ctx, sar.Step, sar.env, sar.RunContext)
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
defer rawLogger.Infof("::endgroup::")
|
defer rawLogger.Infof("::endgroup::")
|
||||||
|
|
||||||
github := sar.getGithubContext(ctx)
|
github := sar.getGithubContext(ctx)
|
||||||
@@ -259,22 +244,6 @@ func (sar *stepActionRemote) getCompositeSteps() *compositeSteps {
|
|||||||
return sar.compositeSteps
|
return sar.compositeSteps
|
||||||
}
|
}
|
||||||
|
|
||||||
// For Gitea
|
|
||||||
// cloneSkipTLS returns true if the runner can clone an action from the Gitea instance
|
|
||||||
func (sar *stepActionRemote) cloneSkipTLS() bool {
|
|
||||||
if !sar.RunContext.Config.InsecureSkipTLS {
|
|
||||||
// Return false if the Gitea instance is not an insecure instance
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if sar.remoteAction.URL == "" {
|
|
||||||
// Empty URL means the default action instance should be used
|
|
||||||
// Return true if the URL of the Gitea instance is the same as the URL of the default action instance
|
|
||||||
return sar.RunContext.Config.DefaultActionURL() == sar.RunContext.Config.GitHubInstance
|
|
||||||
}
|
|
||||||
// Return true if the URL of the remote action is the same as the URL of the Gitea instance
|
|
||||||
return sar.remoteAction.URL == sar.RunContext.Config.GitHubInstance
|
|
||||||
}
|
|
||||||
|
|
||||||
type remoteAction struct {
|
type remoteAction struct {
|
||||||
URL string
|
URL string
|
||||||
Org string
|
Org string
|
||||||
@@ -287,8 +256,8 @@ func (ra *remoteAction) CloneURL(u string) string {
|
|||||||
if ra.URL == "" {
|
if ra.URL == "" {
|
||||||
// keep an absolute local path as-is (used by tests to resolve actions from a local
|
// keep an absolute local path as-is (used by tests to resolve actions from a local
|
||||||
// repo); only bare host names get the https:// scheme prepended
|
// repo); only bare host names get the https:// scheme prepended
|
||||||
if !strings.HasPrefix(u, "http://") && !strings.HasPrefix(u, "https://") && !filepath.IsAbs(u) {
|
if !filepath.IsAbs(u) {
|
||||||
u = "https://" + u
|
u = withHTTPS(u)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
u = ra.URL
|
u = ra.URL
|
||||||
@@ -337,6 +306,9 @@ func newRemoteAction(action string, github *model.GithubContext, instanceURL str
|
|||||||
}
|
}
|
||||||
ra.Org, ra.Repo, _ = strings.Cut(repo, "/")
|
ra.Org, ra.Repo, _ = strings.Cut(repo, "/")
|
||||||
ra.URL, ra.Ref = github.ServerURL, ref
|
ra.URL, ra.Ref = github.ServerURL, ref
|
||||||
|
if ra.URL == "" {
|
||||||
|
return nil, fmt.Errorf("unable to resolve %q without a Gitea instance", action)
|
||||||
|
}
|
||||||
if ra.Org == "" || ra.Repo == "" || ra.Ref == "" {
|
if ra.Org == "" || ra.Repo == "" || ra.Ref == "" {
|
||||||
return nil, fmt.Errorf("unable to resolve %q without a repository and commit", action)
|
return nil, fmt.Errorf("unable to resolve %q without a repository and commit", action)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ func TestStepActionRemote(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
sarm := &stepActionRemoteMocks{}
|
sarm := &stepActionRemoteMocks{}
|
||||||
@@ -193,7 +193,7 @@ func TestStepActionRemote(t *testing.T) {
|
|||||||
RunContext: &RunContext{
|
RunContext: &RunContext{
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
GitHubInstance: "github.com",
|
GitHubInstance: "github.com",
|
||||||
ActionCacheDir: "/tmp/test-cache",
|
ActionCacheDir: t.TempDir(),
|
||||||
},
|
},
|
||||||
Run: &model.Run{
|
Run: &model.Run{
|
||||||
JobID: "1",
|
JobID: "1",
|
||||||
@@ -438,7 +438,7 @@ func TestStepActionRemotePost(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm := &containerMock{}
|
cm := &containerMock{}
|
||||||
|
|
||||||
@@ -447,7 +447,7 @@ func TestStepActionRemotePost(t *testing.T) {
|
|||||||
RunContext: &RunContext{
|
RunContext: &RunContext{
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
GitHubInstance: "https://github.com",
|
GitHubInstance: "https://github.com",
|
||||||
ActionCacheDir: "/tmp/test-cache",
|
ActionCacheDir: t.TempDir(),
|
||||||
},
|
},
|
||||||
JobContainer: cm,
|
JobContainer: cm,
|
||||||
Run: &model.Run{
|
Run: &model.Run{
|
||||||
@@ -688,6 +688,7 @@ func Test_newRemoteActionSelfRepo(t *testing.T) {
|
|||||||
Ref: "v1",
|
Ref: "v1",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{name: "unknown instance", action: "$/.gitea/actions/build", github: &model.GithubContext{Repository: "owner/workflow-repo", Sha: "abc123"}},
|
||||||
{name: "empty path", action: "$/", github: workflow},
|
{name: "empty path", action: "$/", github: workflow},
|
||||||
{name: "ref suffix is not allowed", action: "$/.gitea/actions/build@v1", github: workflow},
|
{name: "ref suffix is not allowed", action: "$/.gitea/actions/build@v1", github: workflow},
|
||||||
{name: "path traversal", action: "$/../escape", github: workflow},
|
{name: "path traversal", action: "$/../escape", github: workflow},
|
||||||
@@ -771,7 +772,7 @@ func TestStepActionRemotePreResolvesDownloadedCommit(t *testing.T) {
|
|||||||
readAction: readActionImpl,
|
readAction: readActionImpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, sar.prepareActionExecutor()(context.Background()))
|
require.NoError(t, sar.prepareActionExecutor()(t.Context()))
|
||||||
|
|
||||||
reference, sha, ok := sar.actionDownloadInfo()
|
reference, sha, ok := sar.actionDownloadInfo()
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
@@ -852,7 +853,7 @@ func TestStepActionRemoteCloneTokenSurvivesNilSecrets(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range table {
|
for _, tt := range table {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
var capturedToken string
|
var capturedToken string
|
||||||
setCloneExecutor(t, func(input git.NewGitCloneExecutorInput) common.Executor {
|
setCloneExecutor(t, func(input git.NewGitCloneExecutorInput) common.Executor {
|
||||||
@@ -867,7 +868,7 @@ func TestStepActionRemoteCloneTokenSurvivesNilSecrets(t *testing.T) {
|
|||||||
Config: &Config{
|
Config: &Config{
|
||||||
GitHubInstance: tt.gitHubInstance,
|
GitHubInstance: tt.gitHubInstance,
|
||||||
DefaultActionInstance: tt.defaultActionInstance,
|
DefaultActionInstance: tt.defaultActionInstance,
|
||||||
ActionCacheDir: "/tmp/test-cache",
|
ActionCacheDir: t.TempDir(),
|
||||||
// Mirrors the state of a composite RunContext: job secrets are
|
// Mirrors the state of a composite RunContext: job secrets are
|
||||||
// stripped, but the job token is still reachable via Config.Token.
|
// stripped, but the job token is still reachable via Config.Token.
|
||||||
Secrets: nil,
|
Secrets: nil,
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"io"
|
"io"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -35,7 +34,7 @@ func TestStepDockerMain(t *testing.T) {
|
|||||||
ContainerNewContainer = origContainerNewContainer
|
ContainerNewContainer = origContainerNewContainer
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
sd := &stepDocker{
|
sd := &stepDocker{
|
||||||
RunContext: &RunContext{
|
RunContext: &RunContext{
|
||||||
@@ -124,7 +123,7 @@ func TestStepDockerNewStepContainerAllocatePTY(t *testing.T) {
|
|||||||
ContainerNewContainer = origContainerNewContainer
|
ContainerNewContainer = origContainerNewContainer
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
sd := &stepDocker{
|
sd := &stepDocker{
|
||||||
RunContext: &RunContext{
|
RunContext: &RunContext{
|
||||||
StepResults: map[string]*model.StepResult{},
|
StepResults: map[string]*model.StepResult{},
|
||||||
@@ -197,7 +196,7 @@ func TestStepDockerNewStepContainerNetworkModeAndPath(t *testing.T) {
|
|||||||
ContainerNewContainer = origContainerNewContainer
|
ContainerNewContainer = origContainerNewContainer
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
platform := tc.platform
|
platform := tc.platform
|
||||||
sd := &stepDocker{
|
sd := &stepDocker{
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/lookpath"
|
"gitea.com/gitea/runner/act/lookpath"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/kballard/go-shellquote"
|
"github.com/kballard/go-shellquote"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
yaml "go.yaml.in/yaml/v4"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,8 +58,8 @@ func (sr *stepRun) main() common.Executor {
|
|||||||
// printRunScriptActionDetails mirrors actions/runner ScriptHandler.PrintActionDetails
|
// printRunScriptActionDetails mirrors actions/runner ScriptHandler.PrintActionDetails
|
||||||
// for script steps.
|
// for script steps.
|
||||||
func (sr *stepRun) printRunScriptActionDetails(ctx context.Context) {
|
func (sr *stepRun) printRunScriptActionDetails(ctx context.Context) {
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
scriptLineLogger := rawLogger.WithField(scriptLineCyanField, true)
|
scriptLineLogger := rawLogger.With(scriptLineCyanField, true)
|
||||||
|
|
||||||
normalized := strings.TrimRight(strings.ReplaceAll(sr.interpolatedScript, "\r\n", "\n"), "\n")
|
normalized := strings.TrimRight(strings.ReplaceAll(sr.interpolatedScript, "\r\n", "\n"), "\n")
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ func printRunActionHeader(ctx context.Context, step *model.Step, env map[string]
|
|||||||
if step == nil {
|
if step == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
|
|
||||||
title := step.Uses
|
title := step.Uses
|
||||||
if step.Name != "" {
|
if step.Name != "" {
|
||||||
@@ -105,7 +105,7 @@ func printRunActionHeader(ctx context.Context, step *model.Step, env map[string]
|
|||||||
// printStepEnvBlock emits the declared-env block (YAML order, internal vars filtered)
|
// printStepEnvBlock emits the declared-env block (YAML order, internal vars filtered)
|
||||||
// shared by the run: and uses: "Run" headers.
|
// shared by the run: and uses: "Run" headers.
|
||||||
func printStepEnvBlock(ctx context.Context, step *model.Step, env map[string]string, rc *RunContext) {
|
func printStepEnvBlock(ctx context.Context, step *model.Step, env map[string]string, rc *RunContext) {
|
||||||
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||||
caseInsensitive := rc != nil && rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive()
|
caseInsensitive := rc != nil && rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive()
|
||||||
var visible []string
|
var visible []string
|
||||||
for _, k := range stepDeclaredEnvKeysInOrder(step) {
|
for _, k := range stepDeclaredEnvKeysInOrder(step) {
|
||||||
@@ -135,7 +135,7 @@ func printStepEnvBlock(ctx context.Context, step *model.Step, env map[string]str
|
|||||||
|
|
||||||
// logKeyedValue prints one row per line of value: Gitea stores one log row per line, so an
|
// logKeyedValue prints one row per line of value: Gitea stores one log row per line, so an
|
||||||
// embedded newline would reach the user as a literal "\n".
|
// embedded newline would reach the user as a literal "\n".
|
||||||
func logKeyedValue(rawLogger *logrus.Entry, key, value string) {
|
func logKeyedValue(rawLogger *logging.Logger, key, value string) {
|
||||||
lines := strings.Split(strings.ReplaceAll(value, "\r\n", "\n"), "\n")
|
lines := strings.Split(strings.ReplaceAll(value, "\r\n", "\n"), "\n")
|
||||||
rawLogger.Infof(" %s: %s", key, lines[0])
|
rawLogger.Infof(" %s: %s", key, lines[0])
|
||||||
for _, line := range lines[1:] {
|
for _, line := range lines[1:] {
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ package runner
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
yaml "go.yaml.in/yaml/v4"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
@@ -84,12 +84,9 @@ env:
|
|||||||
require.NoError(t, yaml.Unmarshal([]byte(raw), &step))
|
require.NoError(t, yaml.Unmarshal([]byte(raw), &step))
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := logrus.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
entry := logger.With("job", "j1")
|
||||||
logger.SetLevel(logrus.InfoLevel)
|
ctx := common.WithLogger(t.Context(), entry)
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
entry := logger.WithFields(logrus.Fields{"job": "j1"})
|
|
||||||
ctx := common.WithLogger(context.Background(), entry)
|
|
||||||
|
|
||||||
sr := &stepRun{
|
sr := &stepRun{
|
||||||
Step: &step,
|
Step: &step,
|
||||||
@@ -133,12 +130,9 @@ env:
|
|||||||
require.NoError(t, yaml.Unmarshal([]byte(raw), &step))
|
require.NoError(t, yaml.Unmarshal([]byte(raw), &step))
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := logrus.New()
|
logger := logging.New(logging.NewLevelHandler(newJobLogHandler(buf, cyan), logging.LevelInfo))
|
||||||
logger.SetOutput(buf)
|
entry := logger.With("job", "j1")
|
||||||
logger.SetLevel(logrus.InfoLevel)
|
ctx := common.WithLogger(t.Context(), entry)
|
||||||
logger.SetFormatter(&jobLogFormatter{color: cyan})
|
|
||||||
entry := logger.WithFields(logrus.Fields{"job": "j1"})
|
|
||||||
ctx := common.WithLogger(context.Background(), entry)
|
|
||||||
|
|
||||||
printRunActionHeader(ctx, &step, map[string]string{"CUSTOM": "value", "GITHUB_TOKEN": "tok"}, &RunContext{})
|
printRunActionHeader(ctx, &step, map[string]string{"CUSTOM": "value", "GITHUB_TOKEN": "tok"}, &RunContext{})
|
||||||
|
|
||||||
@@ -166,17 +160,17 @@ func TestIsInternalEnvKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrintColoredScriptLineCyan(t *testing.T) {
|
func TestPrintColoredScriptLineCyan(t *testing.T) {
|
||||||
f := &jobLogFormatter{color: cyan}
|
f := textFormatter{w: io.Discard, color: cyan}
|
||||||
entry := &logrus.Entry{
|
record := logging.Record{
|
||||||
Level: logrus.InfoLevel,
|
Level: logging.LevelInfo,
|
||||||
Message: "echo one",
|
Message: "echo one",
|
||||||
Data: logrus.Fields{
|
Attrs: []logging.Attr{
|
||||||
"job": "j1",
|
{Key: "job", Value: "j1"},
|
||||||
rawOutputField: true,
|
{Key: rawOutputField, Value: true},
|
||||||
scriptLineCyanField: true,
|
{Key: scriptLineCyanField, Value: true},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
f.printColored(buf, entry)
|
f.printColored(buf, record)
|
||||||
assert.Equal(t, "\x1b[36m|\x1b[0m \x1b[36;1mecho one\x1b[0m", buf.String())
|
assert.Equal(t, "\x1b[36m|\x1b[0m \x1b[36;1mecho one\x1b[0m", buf.String())
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ func TestStepRun(t *testing.T) {
|
|||||||
|
|
||||||
cm.On("UpdateFromEnv", "/var/run/act/workflow/outputcmd.txt", mock.AnythingOfType("*map[string]string")).Return(noopExecutor)
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/outputcmd.txt", mock.AnythingOfType("*map[string]string")).Return(noopExecutor)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := t.Context()
|
||||||
|
|
||||||
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
|
||||||
|
|
||||||
|
|||||||
+106
-39
@@ -7,18 +7,20 @@ package runner
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/internal/pkg/telemetry"
|
"gitea.com/gitea/runner/internal/pkg/telemetry"
|
||||||
|
|
||||||
"gitea.dev/actionslib/pkg/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
runnerv1 "gitea.dev/actionslib/runner/v1"
|
runnerv1 "gitea.dev/actionslib/runner/v1"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
logrustest "github.com/sirupsen/logrus/hooks/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -164,8 +166,8 @@ func TestSetupEnv(t *testing.T) {
|
|||||||
sm.On("getStepModel").Return(step)
|
sm.On("getStepModel").Return(step)
|
||||||
sm.On("getEnv").Return(&env)
|
sm.On("getEnv").Return(&env)
|
||||||
|
|
||||||
require.NoError(t, setupEnv(context.Background(), sm))
|
require.NoError(t, setupEnv(t.Context(), sm))
|
||||||
require.NoError(t, setupInputs(context.Background(), sm))
|
require.NoError(t, setupInputs(t.Context(), sm))
|
||||||
|
|
||||||
// These are commit or system specific
|
// These are commit or system specific
|
||||||
delete(env, "GITHUB_REF")
|
delete(env, "GITHUB_REF")
|
||||||
@@ -189,7 +191,6 @@ func TestSetupEnv(t *testing.T) {
|
|||||||
"GITHUB_ACTION_PATH": "",
|
"GITHUB_ACTION_PATH": "",
|
||||||
"GITHUB_ACTION_REF": "",
|
"GITHUB_ACTION_REF": "",
|
||||||
"GITHUB_ACTION_REPOSITORY": "",
|
"GITHUB_ACTION_REPOSITORY": "",
|
||||||
"GITHUB_API_URL": "https:///api/v1", // Gitea uses api/v1 (upstream GitHub: api/v3)
|
|
||||||
"GITHUB_BASE_REF": "",
|
"GITHUB_BASE_REF": "",
|
||||||
"GITHUB_EVENT_NAME": "",
|
"GITHUB_EVENT_NAME": "",
|
||||||
"GITHUB_EVENT_PATH": "/var/run/act/workflow/event.json",
|
"GITHUB_EVENT_PATH": "/var/run/act/workflow/event.json",
|
||||||
@@ -200,7 +201,6 @@ func TestSetupEnv(t *testing.T) {
|
|||||||
"GITHUB_RUN_ATTEMPT": "",
|
"GITHUB_RUN_ATTEMPT": "",
|
||||||
"GITHUB_RUN_ID": "runId",
|
"GITHUB_RUN_ID": "runId",
|
||||||
"GITHUB_RUN_NUMBER": "1",
|
"GITHUB_RUN_NUMBER": "1",
|
||||||
"GITHUB_SERVER_URL": "https://",
|
|
||||||
"GITHUB_WORKFLOW": "",
|
"GITHUB_WORKFLOW": "",
|
||||||
"INPUT_ID": "1234567",
|
"INPUT_ID": "1234567",
|
||||||
"INPUT_STEP_WITH": "with-value",
|
"INPUT_STEP_WITH": "with-value",
|
||||||
@@ -299,63 +299,63 @@ func TestIsStepEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
|
|
||||||
// success()
|
// success()
|
||||||
step := createTestStep(t, "if: success()")
|
step := createTestStep(t, "if: success()")
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getIfExpression(context.Background(), stepStageMain), step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getIfExpression(t.Context(), stepStageMain), step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: success()")
|
step = createTestStep(t, "if: success()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusSuccess,
|
Conclusion: model.StepStatusSuccess,
|
||||||
}
|
}
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: success()")
|
step = createTestStep(t, "if: success()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusFailure,
|
Conclusion: model.StepStatusFailure,
|
||||||
}
|
}
|
||||||
assertObject.False(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.False(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
// failure()
|
// failure()
|
||||||
step = createTestStep(t, "if: failure()")
|
step = createTestStep(t, "if: failure()")
|
||||||
assertObject.False(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.False(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: failure()")
|
step = createTestStep(t, "if: failure()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusSuccess,
|
Conclusion: model.StepStatusSuccess,
|
||||||
}
|
}
|
||||||
assertObject.False(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.False(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: failure()")
|
step = createTestStep(t, "if: failure()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusFailure,
|
Conclusion: model.StepStatusFailure,
|
||||||
}
|
}
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
// always()
|
// always()
|
||||||
step = createTestStep(t, "if: always()")
|
step = createTestStep(t, "if: always()")
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: always()")
|
step = createTestStep(t, "if: always()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusSuccess,
|
Conclusion: model.StepStatusSuccess,
|
||||||
}
|
}
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
step = createTestStep(t, "if: always()")
|
step = createTestStep(t, "if: always()")
|
||||||
step.getRunContext().StepResults["a"] = &model.StepResult{
|
step.getRunContext().StepResults["a"] = &model.StepResult{
|
||||||
Conclusion: model.StepStatusFailure,
|
Conclusion: model.StepStatusFailure,
|
||||||
}
|
}
|
||||||
assertObject.True(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.True(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
|
|
||||||
// neither env nor the step's own with: values are inputs, at any stage
|
// neither env nor the step's own with: values are inputs, at any stage
|
||||||
step = createTestStep(t, "if: inputs.forged")
|
step = createTestStep(t, "if: inputs.forged")
|
||||||
step.getRunContext().Env["INPUT_FORGED"] = "leaked"
|
step.getRunContext().Env["INPUT_FORGED"] = "leaked"
|
||||||
*step.getEnv() = map[string]string{"INPUT_FORGED": "leaked"}
|
*step.getEnv() = map[string]string{"INPUT_FORGED": "leaked"}
|
||||||
assertObject.False(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStageMain))
|
assertObject.False(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStageMain))
|
||||||
assertObject.False(isStepEnabled(context.Background(), step.getStepModel().If.Value, step, stepStagePost))
|
assertObject.False(isStepEnabled(t.Context(), step.getStepModel().If.Value, step, stepStagePost))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsContinueOnError(t *testing.T) {
|
func TestIsContinueOnError(t *testing.T) {
|
||||||
@@ -383,49 +383,49 @@ func TestIsContinueOnError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetLevel(log.DebugLevel)
|
logging.SetLevel(logging.LevelDebug)
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
|
|
||||||
// absent
|
// absent
|
||||||
step := createTestStep(t, "name: test")
|
step := createTestStep(t, "name: test")
|
||||||
continueOnError, err := isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err := isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.False(continueOnError)
|
assertObject.False(continueOnError)
|
||||||
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// explcit true
|
// explcit true
|
||||||
step = createTestStep(t, "continue-on-error: true")
|
step = createTestStep(t, "continue-on-error: true")
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.True(continueOnError)
|
assertObject.True(continueOnError)
|
||||||
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// explicit false
|
// explicit false
|
||||||
step = createTestStep(t, "continue-on-error: false")
|
step = createTestStep(t, "continue-on-error: false")
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.False(continueOnError)
|
assertObject.False(continueOnError)
|
||||||
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// expression true
|
// expression true
|
||||||
step = createTestStep(t, "continue-on-error: ${{ 'test' == 'test' }}")
|
step = createTestStep(t, "continue-on-error: ${{ 'test' == 'test' }}")
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.True(continueOnError)
|
assertObject.True(continueOnError)
|
||||||
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// expression false
|
// expression false
|
||||||
step = createTestStep(t, "continue-on-error: ${{ 'test' != 'test' }}")
|
step = createTestStep(t, "continue-on-error: ${{ 'test' != 'test' }}")
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.False(continueOnError)
|
assertObject.False(continueOnError)
|
||||||
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
assertObject.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act
|
||||||
|
|
||||||
// the step's own with: values are not inputs
|
// the step's own with: values are not inputs
|
||||||
step = createTestStep(t, "continue-on-error: ${{ inputs.forged }}")
|
step = createTestStep(t, "continue-on-error: ${{ inputs.forged }}")
|
||||||
*step.getEnv() = map[string]string{"INPUT_FORGED": "true"}
|
*step.getEnv() = map[string]string{"INPUT_FORGED": "true"}
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.False(continueOnError)
|
assertObject.False(continueOnError)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// expression parse error
|
// expression parse error
|
||||||
step = createTestStep(t, "continue-on-error: ${{ 'test' != test }}")
|
step = createTestStep(t, "continue-on-error: ${{ 'test' != test }}")
|
||||||
continueOnError, err = isContinueOnError(context.Background(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
continueOnError, err = isContinueOnError(t.Context(), step.getStepModel().RawContinueOnError, step, stepStageMain)
|
||||||
assertObject.False(continueOnError)
|
assertObject.False(continueOnError)
|
||||||
assertObject.Error(err)
|
assertObject.Error(err)
|
||||||
}
|
}
|
||||||
@@ -449,9 +449,9 @@ func TestRunStepExecutorDoesNotLeakRefusalToNextStep(t *testing.T) {
|
|||||||
StepResults: map[string]*model.StepResult{},
|
StepResults: map[string]*model.StepResult{},
|
||||||
JobContainer: cm,
|
JobContainer: cm,
|
||||||
}
|
}
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
rc.ExprEval = rc.NewExpressionEvaluator(t.Context())
|
||||||
// Dryrun skips reading the path file back from the (mocked) container.
|
// Dryrun skips reading the path file back from the (mocked) container.
|
||||||
ctx := common.WithDryrun(context.Background(), true)
|
ctx := common.WithDryrun(t.Context(), true)
|
||||||
|
|
||||||
// A refusal parsed out of the job container's own output belongs to no step, so the
|
// A refusal parsed out of the job container's own output belongs to no step, so the
|
||||||
// first step must not be failed by it.
|
// first step must not be failed by it.
|
||||||
@@ -475,11 +475,31 @@ func TestRunStepExecutorDoesNotLeakRefusalToNextStep(t *testing.T) {
|
|||||||
require.NoError(t, errB)
|
require.NoError(t, errB)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fileCommandTestContainer struct {
|
||||||
|
*container.HostEnvironment
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fileCommandTestContainer) UpdateFromEnv(srcPath string, env *map[string]string) common.Executor {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HostEnvironment.UpdateFromEnv(srcPath, env)(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fileCommandTestContainer) GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c.HostEnvironment.GetContainerArchive(ctx, srcPath)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunStepExecutorParity(t *testing.T) {
|
func TestRunStepExecutorParity(t *testing.T) {
|
||||||
newStep := func(t *testing.T, stepModel *model.Step) *stepRun {
|
newStep := func(t *testing.T, stepModel *model.Step) *stepRun {
|
||||||
rc := createRunContext(t)
|
rc := createRunContext(t)
|
||||||
rc.JobContainer = &container.HostEnvironment{ActPath: t.TempDir()}
|
rc.JobContainer = &container.HostEnvironment{ActPath: t.TempDir()}
|
||||||
rc.ExprEval = rc.NewExpressionEvaluator(context.Background())
|
rc.ExprEval = rc.NewExpressionEvaluator(t.Context())
|
||||||
return &stepRun{RunContext: rc, Step: stepModel, env: map[string]string{}}
|
return &stepRun{RunContext: rc, Step: stepModel, env: map[string]string{}}
|
||||||
}
|
}
|
||||||
badExpression := "${{ 'test' != test }}"
|
badExpression := "${{ 'test' != test }}"
|
||||||
@@ -493,30 +513,48 @@ func TestRunStepExecutorParity(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
step := newStep(t, test.step)
|
step := newStep(t, test.step)
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
err := runStepExecutor(step, stepStageMain, test.executor)(common.WithLogger(context.Background(), logger))
|
err := runStepExecutor(step, stepStageMain, test.executor)(common.WithLogger(t.Context(), logger))
|
||||||
|
|
||||||
require.ErrorContains(t, err, test.wantError)
|
require.ErrorContains(t, err, test.wantError)
|
||||||
assert.Equal(t, model.StepStatusFailure, step.RunContext.StepResults[test.step.ID].Conclusion)
|
assert.Equal(t, model.StepStatusFailure, step.RunContext.StepResults[test.step.ID].Conclusion)
|
||||||
assert.Equal(t, model.StepStatusFailure, hook.LastEntry().Data["stepResult"])
|
assert.Equal(t, model.StepStatusFailure, lastRecordAttr(hook, "stepResult"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Run("only a step that runs is marked started", func(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
condition string
|
||||||
|
started int
|
||||||
|
}{{"true", 1}, {"false", 0}} {
|
||||||
|
logger, hook := logtest.NewLogger()
|
||||||
|
step := newStep(t, &model.Step{ID: "pre", If: yaml.Node{Value: test.condition}})
|
||||||
|
require.NoError(t, runStepExecutor(step, stepStagePre, noopExecutor)(common.WithLogger(context.Background(), logger)))
|
||||||
|
started := 0
|
||||||
|
for _, entry := range hook.AllRecords() {
|
||||||
|
if v, _ := entry.Attr(StepStartedField); v == true {
|
||||||
|
started++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(t, test.started, started, test.condition)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("file command error honors continue-on-error", func(t *testing.T) {
|
t.Run("file command error honors continue-on-error", func(t *testing.T) {
|
||||||
step := newStep(t, &model.Step{ID: "commands", RawContinueOnError: "true"})
|
step := newStep(t, &model.Step{ID: "commands", RawContinueOnError: "true"})
|
||||||
logger, hook := logrustest.NewNullLogger()
|
logger, hook := logtest.NewLogger()
|
||||||
err := runStepExecutor(step, stepStageMain, func(context.Context) error {
|
err := runStepExecutor(step, stepStageMain, func(context.Context) error {
|
||||||
require.NoError(t, os.WriteFile(step.env["GITHUB_ENV"], []byte("GOOD=1\nmalformed\n"), 0o600))
|
require.NoError(t, os.WriteFile(step.env["GITHUB_ENV"], []byte("GOOD=1\nmalformed\n"), 0o600))
|
||||||
require.NoError(t, os.WriteFile(step.env["GITHUB_OUTPUT"], []byte("kept=value\n"), 0o600))
|
require.NoError(t, os.WriteFile(step.env["GITHUB_OUTPUT"], []byte("kept=value\n"), 0o600))
|
||||||
require.NoError(t, os.WriteFile(step.env["GITHUB_STATE"], []byte("saved=value\n"), 0o600))
|
require.NoError(t, os.WriteFile(step.env["GITHUB_STATE"], []byte("saved=value\n"), 0o600))
|
||||||
return nil
|
return nil
|
||||||
})(common.WithLogger(context.Background(), logger))
|
})(common.WithLogger(t.Context(), logger))
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
result := step.RunContext.StepResults[step.Step.ID]
|
result := step.RunContext.StepResults[step.Step.ID]
|
||||||
assert.Equal(t, model.StepStatusFailure, result.Outcome)
|
assert.Equal(t, model.StepStatusFailure, result.Outcome)
|
||||||
assert.Equal(t, model.StepStatusSuccess, result.Conclusion)
|
assert.Equal(t, model.StepStatusSuccess, result.Conclusion)
|
||||||
assert.Equal(t, model.StepStatusSuccess, hook.LastEntry().Data["stepResult"])
|
assert.Equal(t, model.StepStatusSuccess, lastRecordAttr(hook, "stepResult"))
|
||||||
assert.Equal(t, "1", step.RunContext.Env["GOOD"])
|
assert.Equal(t, "1", step.RunContext.Env["GOOD"])
|
||||||
assert.Equal(t, "value", result.Outputs["kept"])
|
assert.Equal(t, "value", result.Outputs["kept"])
|
||||||
assert.Equal(t, "value", step.RunContext.IntraActionState[step.Step.ID]["saved"])
|
assert.Equal(t, "value", step.RunContext.IntraActionState[step.Step.ID]["saved"])
|
||||||
@@ -527,13 +565,37 @@ func TestRunStepExecutorParity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("file commands survive a job timeout", func(t *testing.T) {
|
||||||
|
synctest.Test(t, func(t *testing.T) {
|
||||||
|
step := newStep(t, &model.Step{ID: "interrupted"})
|
||||||
|
ctx, cancel := applyJobTimeout(t.Context(), step.RunContext, &model.Job{TimeoutMinutes: "1"})
|
||||||
|
defer cancel()
|
||||||
|
step.RunContext.JobContainer = &fileCommandTestContainer{step.RunContext.JobContainer.(*container.HostEnvironment)}
|
||||||
|
err := runStepExecutor(step, stepStageMain, func(execCtx context.Context) error {
|
||||||
|
for name, content := range map[string]string{
|
||||||
|
"GITHUB_ENV": "KEPT_ENV=value\n", "GITHUB_STATE": "saved=value\n",
|
||||||
|
"GITHUB_OUTPUT": "kept=value\n", "GITHUB_PATH": "/kept/bin\n",
|
||||||
|
} {
|
||||||
|
require.NoError(t, os.WriteFile(step.env[name], []byte(content), 0o600))
|
||||||
|
}
|
||||||
|
<-execCtx.Done()
|
||||||
|
return execCtx.Err()
|
||||||
|
})(ctx)
|
||||||
|
require.ErrorIs(t, err, context.DeadlineExceeded)
|
||||||
|
assert.Equal(t, "value", step.RunContext.Env["KEPT_ENV"])
|
||||||
|
assert.Equal(t, "value", step.RunContext.IntraActionState[step.Step.ID]["saved"])
|
||||||
|
assert.Equal(t, "value", step.RunContext.StepResults[step.Step.ID].Outputs["kept"])
|
||||||
|
assert.Contains(t, step.RunContext.ExtraPath, "/kept/bin")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("composite child files do not leak", func(t *testing.T) {
|
t.Run("composite child files do not leak", func(t *testing.T) {
|
||||||
outer := newStep(t, &model.Step{ID: "outer"})
|
outer := newStep(t, &model.Step{ID: "outer"})
|
||||||
childRC := &RunContext{
|
childRC := &RunContext{
|
||||||
Config: outer.RunContext.Config, Run: outer.RunContext.Run, Env: map[string]string{}, StepResults: map[string]*model.StepResult{},
|
Config: outer.RunContext.Config, Run: outer.RunContext.Run, Env: map[string]string{}, StepResults: map[string]*model.StepResult{},
|
||||||
JobContainer: outer.RunContext.JobContainer, Parent: outer.RunContext,
|
JobContainer: outer.RunContext.JobContainer, Parent: outer.RunContext,
|
||||||
}
|
}
|
||||||
childRC.ExprEval = childRC.NewExpressionEvaluator(context.Background())
|
childRC.ExprEval = childRC.NewExpressionEvaluator(t.Context())
|
||||||
child := &stepRun{RunContext: childRC, Step: &model.Step{ID: "child"}, env: map[string]string{}}
|
child := &stepRun{RunContext: childRC, Step: &model.Step{ID: "child"}, env: map[string]string{}}
|
||||||
|
|
||||||
err := runStepExecutor(outer, stepStageMain, func(ctx context.Context) error {
|
err := runStepExecutor(outer, stepStageMain, func(ctx context.Context) error {
|
||||||
@@ -544,7 +606,7 @@ func TestRunStepExecutorParity(t *testing.T) {
|
|||||||
})(ctx))
|
})(ctx))
|
||||||
outer.RunContext.setOutput(ctx, map[string]string{"name": "declared"}, "outer")
|
outer.RunContext.setOutput(ctx, map[string]string{"name": "declared"}, "outer")
|
||||||
return nil
|
return nil
|
||||||
})(context.Background())
|
})(t.Context())
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, map[string]string{"declared": "outer"}, outer.RunContext.StepResults[outer.Step.ID].Outputs)
|
assert.Equal(t, map[string]string{"declared": "outer"}, outer.RunContext.StepResults[outer.Step.ID].Outputs)
|
||||||
@@ -577,12 +639,17 @@ func TestRunStepExecutorParity(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("timeout must be positive", func(t *testing.T) {
|
t.Run("timeout must be positive", func(t *testing.T) {
|
||||||
exprEval := createRunContext(t).NewExpressionEvaluator(context.Background())
|
exprEval := createRunContext(t).NewExpressionEvaluator(t.Context())
|
||||||
for timeout, wantDeadline := range map[string]bool{"-1": false, "0": false, "1": true} {
|
for timeout, wantDeadline := range map[string]bool{"-1": false, "0": false, "1": true} {
|
||||||
ctx, cancel := evaluateStepTimeout(context.Background(), exprEval, &model.Step{TimeoutMinutes: timeout})
|
ctx, cancel := evaluateStepTimeout(t.Context(), exprEval, &model.Step{TimeoutMinutes: timeout})
|
||||||
_, hasDeadline := ctx.Deadline()
|
_, hasDeadline := ctx.Deadline()
|
||||||
cancel()
|
cancel()
|
||||||
assert.Equal(t, wantDeadline, hasDeadline, timeout)
|
assert.Equal(t, wantDeadline, hasDeadline, timeout)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lastRecordAttr(h *logtest.Handler, key string) any {
|
||||||
|
v, _ := h.LastRecord().Attr(key)
|
||||||
|
return v
|
||||||
|
}
|
||||||
+3
-3
@@ -37,10 +37,10 @@ async function main() {
|
|||||||
await request('POST', `${api}/networks/${name}/connect`, {Container: job});
|
await request('POST', `${api}/networks/${name}/connect`, {Container: job});
|
||||||
fs.appendFileSync(process.env.GITHUB_STATE, 'post=true\n');
|
fs.appendFileSync(process.env.GITHUB_STATE, 'post=true\n');
|
||||||
}
|
}
|
||||||
const label = process.env.PROXY_TEST_MODE === 'proxy' ? job : undefined;
|
const label = process.env.PROXY_TEST_MODE === 'proxy' ? '' : undefined;
|
||||||
assert.equal(JSON.parse(await request('GET', `${api}/volumes/${name}`)).Labels?.['com.gitea.runner.job'], label);
|
assert.equal(JSON.parse(await request('GET', `${api}/volumes/${name}`)).Labels?.[`com.gitea.runner.job.${job}`], label);
|
||||||
const network = JSON.parse(await request('GET', `${api}/networks/${name}`));
|
const network = JSON.parse(await request('GET', `${api}/networks/${name}`));
|
||||||
assert.equal(network.Labels?.['com.gitea.runner.job'], label);
|
assert.equal(network.Labels?.[`com.gitea.runner.job.${job}`], label);
|
||||||
assert(Object.values(network.Containers).some((container) => container.Name === job));
|
assert(Object.values(network.Containers).some((container) => container.Name === job));
|
||||||
if (post) {
|
if (post) {
|
||||||
console.log('docker proxy post verified');
|
console.log('docker proxy post verified');
|
||||||
|
|||||||
+1
-1
@@ -52,5 +52,5 @@ jobs:
|
|||||||
- run: |
|
- run: |
|
||||||
docker run --rm --user 1001 --group-add 2375 -v /var/run/docker.sock:/var/run/docker.sock -v /usr/local/bin/docker:/usr/local/bin/docker:ro -v "${GITEA_DOCKER_WORKSPACE:?}/data:/data:ro" \
|
docker run --rm --user 1001 --group-add 2375 -v /var/run/docker.sock:/var/run/docker.sock -v /usr/local/bin/docker:/usr/local/bin/docker:ro -v "${GITEA_DOCKER_WORKSPACE:?}/data:/data:ro" \
|
||||||
"$PROXY_TEST_IMAGE" sh -c 'grep -qx from-job /data/marker && docker volume create "$0"' "$PROXY_TEST_RESOURCE-nested"
|
"$PROXY_TEST_IMAGE" sh -c 'grep -qx from-job /data/marker && docker volume create "$0"' "$PROXY_TEST_RESOURCE-nested"
|
||||||
test "$(docker volume inspect -f '{{ index .Labels "com.gitea.runner.job" }}' "$PROXY_TEST_RESOURCE-nested")" = "$JOB_CONTAINER_NAME"
|
test "$(docker volume ls -q -f "label=com.gitea.runner.job.$JOB_CONTAINER_NAME" -f "name=$PROXY_TEST_RESOURCE-nested")" = "$PROXY_TEST_RESOURCE-nested"
|
||||||
echo "docker binds verified"
|
echo "docker binds verified"
|
||||||
+22
-4
@@ -8,14 +8,32 @@ Built-in actions ship with the runner, so a job needs no action download and no
|
|||||||
|
|
||||||
## checkout
|
## checkout
|
||||||
|
|
||||||
Works like `actions/checkout` with these inputs. Any other input fails the step.
|
Works like `actions/checkout` with these inputs. Any other input fails the step. It runs `git` where the job runs, see the [prerequisites](../README.md#prerequisites).
|
||||||
|
|
||||||
| Input | Default |
|
| Input | Default |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `repository` | `github.repository` |
|
| `repository` | `github.repository` |
|
||||||
| `ref` | the triggering commit, or the default branch of another repository |
|
| `ref` | the triggering ref and commit, or the default branch of another repository |
|
||||||
| `token` | `github.token` |
|
| `token` | `github.token` |
|
||||||
|
| `ssh-key` | empty, a private key fetches over SSH instead |
|
||||||
|
| `ssh-known-hosts` | empty, host keys trusted besides `~/.ssh/known_hosts` |
|
||||||
|
| `ssh-strict` | `true`, rejects unknown host keys |
|
||||||
|
| `ssh-user` | the user of the repository's SSH URL, else `git` |
|
||||||
|
| `persist-credentials` | `true`, later steps of the job can fetch and push with `token` or `ssh-key` |
|
||||||
| `path` | workspace root |
|
| `path` | workspace root |
|
||||||
| `fetch-depth` | `1`, `0` for full history |
|
| `clean` | `true`, removes untracked files, also in submodules |
|
||||||
|
| `filter` | empty, a partial clone filter such as `blob:none` |
|
||||||
|
| `sparse-checkout` | empty, the paths to check out, one per line |
|
||||||
|
| `sparse-checkout-cone-mode` | `true`, `false` reads `sparse-checkout` as patterns |
|
||||||
|
| `fetch-depth` | `1`, `0` for the full history of every branch and tag |
|
||||||
|
| `fetch-tags` | `false`, `true` fetches every tag |
|
||||||
|
| `show-progress` | `true`, shows the fetch progress |
|
||||||
|
| `lfs` | `false`, `true` downloads Git LFS files |
|
||||||
|
| `submodules` | `false`, `true` checks out submodules, `recursive` also theirs |
|
||||||
|
| `set-safe-directory` | `true`, lets checkout's `git` work in a directory another user owns |
|
||||||
|
|
||||||
With `gitea-runner exec`, checking out the workflow's own repository copies your local working directory, unless `--no-skip-checkout` is set.
|
A branch is checked out as a local branch tracking `origin`, a tag or commit as a detached `HEAD`. A triggering tag that moved since the event fails the step. The step outputs `ref` and `commit` like `actions/checkout`.
|
||||||
|
|
||||||
|
Persisted credentials end with the job. `token` is only sent to `github.server_url`, while `ssh-key` is offered to any SSH host, as with `actions/checkout`. The runner's `client_cert_file` is only offered to `github.server_url` and only during the step, so later steps do not get it. Without `ssh-key`, submodule URLs of the form `git@<server host>:` are fetched from `github.server_url` with `token`. With `ssh-key`, the repository is fetched from the SSH URL in the event's `repository.ssh_url`, or from the server's host on port 22.
|
||||||
|
|
||||||
|
With `gitea-runner exec`, checking out the workflow's own repository copies your local working directory, unless `--no-skip-checkout` or `ssh-key` is set.
|
||||||
+1
-1
@@ -45,7 +45,7 @@ kubernetes:
|
|||||||
nvidia.com/gpu.present: "true"
|
nvidia.com/gpu.present: "true"
|
||||||
```
|
```
|
||||||
|
|
||||||
A job with `runs-on: [linux, gpu]` gets the `linux` and then the `gpu` entry of `pod_templates` merged over `pod_template`, while its image comes from the first of its labels the runner has. A template volume named `workspace` or `act` replaces the `emptyDir` the runner mounts at the workspace or at `/var/run/act`, for example to limit its size. Images are pulled by the cluster's `imagePullPolicy` default, a template's, or always with `container.force_pull`.
|
A job with `runs-on: [linux, gpu]` gets the `linux` and then the `gpu` entry of `pod_templates` merged over `pod_template`, while its image comes from the first of its labels the runner has. A template volume named `workspace` or `act` replaces the `emptyDir` the runner mounts at the workspace or at `/var/run/act`, for example to limit its size. Images are pulled by the cluster's `imagePullPolicy` default, a template's, or always with `container.force_pull`. Like every config value, templates expand `${NAME}` from the runner's environment, so a shell command in a template writes `$${NAME}` for the pod's own variable.
|
||||||
|
|
||||||
Job pods mount no service account token unless the template sets `automountServiceAccountToken: true`. Job pods and their Secrets are labeled `app.kubernetes.io/managed-by: gitea-runner` and `com.gitea.runner.uuid: <runner UUID>`. The idle cleanup removes those the runner did not get to remove, once they are older than `runner.workdir_cleanup_age`.
|
Job pods mount no service account token unless the template sets `automountServiceAccountToken: true`. Job pods and their Secrets are labeled `app.kubernetes.io/managed-by: gitea-runner` and `com.gitea.runner.uuid: <runner UUID>`. The idle cleanup removes those the runner did not get to remove, once they are older than `runner.workdir_cleanup_age`.
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ The script environment is **not** a full copy of the job container environment.
|
|||||||
|
|
||||||
## Output and errors
|
## Output and errors
|
||||||
|
|
||||||
- **Stdout/stderr** are written to the **runner process log** (logrus), prefixed with `post-task script stdout:` / `post-task script stderr:`.
|
- **Stdout/stderr** are written to the **runner process log**, prefixed with `post-task script stdout:` / `post-task script stderr:`.
|
||||||
- **Non-zero exit codes** are logged as warnings only. They do **not** change the job result already reported to Gitea.
|
- **Non-zero exit codes** are logged as warnings only. They do **not** change the job result already reported to Gitea.
|
||||||
- **Timeouts and start failures** are logged as warnings; the runner still completes the task acknowledgement.
|
- **Timeouts and start failures** are logged as warnings; the runner still completes the task acknowledgement.
|
||||||
|
|
||||||
|
|||||||
+18
-2
@@ -31,7 +31,7 @@ Except for the endpoint, each `OTEL_EXPORTER_OTLP_*` variable has `TRACES` and `
|
|||||||
|
|
||||||
- A span per job, named `RUN {workflow} {job}`, with the repository, ref, commit, run URL, run number, attempt and result.
|
- A span per job, named `RUN {workflow} {job}`, with the repository, ref, commit, run URL, run number, attempt and result.
|
||||||
- A span per step, named as in Gitea's step list, including `Pre` and `Post` stages and the steps of composite actions, with the step's result.
|
- A span per step, named as in Gitea's step list, including `Pre` and `Post` stages and the steps of composite actions, with the step's result.
|
||||||
- The runner's name, UUID and version.
|
- The runner's name, UUID, version, process ID and start time, and the Go version.
|
||||||
|
|
||||||
Logs, step output and secrets are never sent. Step names are taken from the workflow file as written, without expanding `${{ }}` expressions.
|
Logs, step output and secrets are never sent. Step names are taken from the workflow file as written, without expanding `${{ }}` expressions.
|
||||||
|
|
||||||
@@ -41,7 +41,23 @@ Jobs receive `TRACEPARENT`, so tools in a job that use OpenTelemetry can add the
|
|||||||
|
|
||||||
## Metrics
|
## Metrics
|
||||||
|
|
||||||
The metrics of the Prometheus endpoint are also exported, under the same names and whether or not `metrics.enabled` is set. They are sent every export interval and once more on shutdown. If your collector only accepts traces, set `OTEL_METRICS_EXPORTER=none`.
|
The metrics of the Prometheus endpoint are also exported, whether or not `metrics.enabled` is set, except `gitea_runner_info`, `go_info` and `process_start_time_seconds`, which are sent as resource attributes. They are sent every export interval and once more on shutdown. To turn them off, set `OTEL_METRICS_EXPORTER=none`.
|
||||||
|
|
||||||
|
Metrics are named after the [OpenTelemetry semantic conventions](https://opentelemetry.io/docs/specs/semconv/) where one exists, and under `gitea.runner.*` otherwise. Other Go and process metrics, such as `go_memstats_heap_alloc_bytes`, keep their Prometheus names.
|
||||||
|
|
||||||
|
| Metric | Attributes |
|
||||||
|
| --- | --- |
|
||||||
|
| `cicd.worker.count` | `cicd.worker.state` |
|
||||||
|
| `cicd.system.errors` | `cicd.system.component`, `rpc.method`, `error.type` |
|
||||||
|
| `gitea.runner.jobs` | `cicd.pipeline.result` |
|
||||||
|
| `gitea.runner.job.active`, `gitea.runner.job.limit`, `gitea.runner.job.utilization`, `gitea.runner.job.duration` | |
|
||||||
|
| `gitea.runner.poll.fetches` | `gitea.runner.poll.result` |
|
||||||
|
| `gitea.runner.uptime`, `gitea.runner.poll.backoff`, `gitea.runner.poll.fetch.duration` | |
|
||||||
|
| `gitea.runner.report.log.requests`, `gitea.runner.report.state.requests` | `gitea.runner.report.result` |
|
||||||
|
| `gitea.runner.report.log.buffer`, `gitea.runner.report.log.duration`, `gitea.runner.report.state.duration` | |
|
||||||
|
| `go.memory.used` | `go.memory.type` |
|
||||||
|
| `go.goroutine.count`, `go.memory.allocated`, `go.memory.gc.goal`, `go.memory.gc.cycles`, `go.memory.limit`, `go.config.gogc`, `go.processor.limit` | |
|
||||||
|
| `process.memory.usage`, `process.memory.virtual`, `process.unix.file_descriptor.count` (`process.windows.handle.count` on Windows) | |
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
//go:build e2e
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func testBuiltinCheckout(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
api, repo := newScenario(t)
|
||||||
|
pushWorkflow(t, api, repo, "checkout.yml")
|
||||||
|
branchRun := waitForRun(t, api, repo)
|
||||||
|
if err := api.CreateTag(t.Context(), repo, "v1"); err != nil {
|
||||||
|
t.Fatalf("create tag: %v", err)
|
||||||
|
}
|
||||||
|
tagRun := waitForRunAfter(t, api, repo, branchRun.ID)
|
||||||
|
requireSuccess(t, api, repo, branchRun.ID)
|
||||||
|
requireSuccess(t, api, repo, tagRun.ID)
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ func TestCompatibility(t *testing.T) {
|
|||||||
t.Run("artifact", testArtifactRoundTrip)
|
t.Run("artifact", testArtifactRoundTrip)
|
||||||
t.Run("cancellation_and_log_streaming", testRunCancellation)
|
t.Run("cancellation_and_log_streaming", testRunCancellation)
|
||||||
t.Run("dispatch", testWorkflowDispatch)
|
t.Run("dispatch", testWorkflowDispatch)
|
||||||
|
t.Run("builtin_checkout", testBuiltinCheckout)
|
||||||
t.Run("ephemeral", testEphemeralRunner)
|
t.Run("ephemeral", testEphemeralRunner)
|
||||||
t.Run("kubernetes", testKubernetes)
|
t.Run("kubernetes", testKubernetes)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ func (a *GiteaAPI) CreateFile(ctx context.Context, repo, path, content, message
|
|||||||
return a.doJSON(ctx, http.MethodPost, url, body, nil)
|
return a.doJSON(ctx, http.MethodPost, url, body, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *GiteaAPI) CreateTag(ctx context.Context, repo, name string) error {
|
||||||
|
body := map[string]any{"tag_name": name}
|
||||||
|
url := fmt.Sprintf("/api/v1/repos/%s/%s/tags", giteaAdminUser, repo)
|
||||||
|
return a.doJSON(ctx, http.MethodPost, url, body, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *GiteaAPI) CreateSecret(ctx context.Context, repo, name, value string) error {
|
func (a *GiteaAPI) CreateSecret(ctx context.Context, repo, name, value string) error {
|
||||||
body := map[string]any{"data": value}
|
body := map[string]any{"data": value}
|
||||||
url := fmt.Sprintf("/api/v1/repos/%s/%s/actions/secrets/%s", giteaAdminUser, repo, name)
|
url := fmt.Sprintf("/api/v1/repos/%s/%s/actions/secrets/%s", giteaAdminUser, repo, name)
|
||||||
|
|||||||
@@ -188,6 +188,7 @@ func StartGitea(ctx context.Context, cli mobyclient.APIClient) (*GiteaFixture, e
|
|||||||
"GITEA__security__INSTALL_LOCK=true",
|
"GITEA__security__INSTALL_LOCK=true",
|
||||||
"GITEA__database__DB_TYPE=sqlite3",
|
"GITEA__database__DB_TYPE=sqlite3",
|
||||||
"GITEA__actions__ENABLED=true",
|
"GITEA__actions__ENABLED=true",
|
||||||
|
"GITEA__server__LFS_START_SERVER=true",
|
||||||
"GITEA__server__ROOT_URL=" + baseURL + "/",
|
"GITEA__server__ROOT_URL=" + baseURL + "/",
|
||||||
},
|
},
|
||||||
ExposedPorts: network.PortSet{containerPort: struct{}{}},
|
ExposedPorts: network.PortSet{containerPort: struct{}{}},
|
||||||
|
|||||||
+6
-1
@@ -141,6 +141,11 @@ func dumpRunLogs(t *testing.T, api *GiteaAPI, repo string, runID int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func waitForRun(t *testing.T, api *GiteaAPI, repo string) *ActionRun {
|
func waitForRun(t *testing.T, api *GiteaAPI, repo string) *ActionRun {
|
||||||
|
t.Helper()
|
||||||
|
return waitForRunAfter(t, api, repo, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForRunAfter(t *testing.T, api *GiteaAPI, repo string, previousID int64) *ActionRun {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -150,7 +155,7 @@ func waitForRun(t *testing.T, api *GiteaAPI, repo string) *ActionRun {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("get latest run: %v", err)
|
t.Fatalf("get latest run: %v", err)
|
||||||
}
|
}
|
||||||
if len(runs) > 0 {
|
if len(runs) > 0 && runs[0].ID > previousID {
|
||||||
return &runs[0]
|
return &runs[0]
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func startRunner(t *testing.T, repo, labelName string, options runnerOptions) *p
|
|||||||
}
|
}
|
||||||
labelNames := []string{label.Name}
|
labelNames := []string{label.Name}
|
||||||
|
|
||||||
pingCli := client.New(fixture.baseURL, cfg.Runner.Insecure, "", "", config.RequestTimeout)
|
pingCli := client.New(fixture.baseURL, cfg.Runner.TLSConfig(), "", "", config.RequestTimeout, cfg.Runner.ExtraHeaders)
|
||||||
if _, err := pingCli.Ping(ctx, connect.NewRequest(&pingv1.PingRequest{Data: t.Name()})); err != nil {
|
if _, err := pingCli.Ping(ctx, connect.NewRequest(&pingv1.PingRequest{Data: t.Name()})); err != nil {
|
||||||
t.Fatalf("ping %s: %v", fixture.baseURL, err)
|
t.Fatalf("ping %s: %v", fixture.baseURL, err)
|
||||||
}
|
}
|
||||||
@@ -105,7 +105,7 @@ func startRunner(t *testing.T, repo, labelName string, options runnerOptions) *p
|
|||||||
Ephemeral: regResp.Msg.Runner.Ephemeral,
|
Ephemeral: regResp.Msg.Runner.Ephemeral,
|
||||||
}
|
}
|
||||||
|
|
||||||
cli := client.New(fixture.baseURL, cfg.Runner.Insecure, reg.UUID, reg.Token, config.RequestTimeout)
|
cli := client.New(fixture.baseURL, cfg.Runner.TLSConfig(), reg.UUID, reg.Token, config.RequestTimeout, cfg.Runner.ExtraHeaders)
|
||||||
|
|
||||||
runner := run.NewRunner(cfg, reg, cli)
|
runner := run.NewRunner(cfg, reg, cli)
|
||||||
declResp, err := runner.Declare(ctx, labelNames)
|
declResp, err := runner.Declare(ctx, labelNames)
|
||||||
|
|||||||
Vendored
+61
@@ -0,0 +1,61 @@
|
|||||||
|
name: checkout
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
checkout:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- id: checkout
|
||||||
|
uses: builtin:checkout
|
||||||
|
- uses: builtin:checkout
|
||||||
|
with:
|
||||||
|
ref: ${{ github.ref_name }}
|
||||||
|
path: short
|
||||||
|
- run: test "$(git -C short rev-parse HEAD)" = "$GITHUB_SHA"
|
||||||
|
- if: github.ref_type == 'branch'
|
||||||
|
run: |
|
||||||
|
test "$(git symbolic-ref HEAD)" = "$GITHUB_REF"
|
||||||
|
test "${{ steps.checkout.outputs.ref }}" = "$GITHUB_REF"
|
||||||
|
test "${{ steps.checkout.outputs.commit }}" = "$GITHUB_SHA"
|
||||||
|
git -c user.name=e2e -c [email protected] commit --allow-empty -m pushed
|
||||||
|
git push
|
||||||
|
git tag moved
|
||||||
|
git push --force origin moved
|
||||||
|
git switch --orphan child
|
||||||
|
echo child > child.txt
|
||||||
|
git add child.txt
|
||||||
|
git -c user.name=e2e -c [email protected] commit -m child
|
||||||
|
git push origin child
|
||||||
|
git switch -c content main
|
||||||
|
git submodule add -b child "../${{ github.event.repository.name }}.git" sub
|
||||||
|
git lfs install --local
|
||||||
|
git lfs track '*.bin'
|
||||||
|
mkdir keep drop
|
||||||
|
echo big > keep/big.bin
|
||||||
|
echo big > drop/big.bin
|
||||||
|
git add .gitattributes keep drop
|
||||||
|
git -c user.name=e2e -c [email protected] commit -m content
|
||||||
|
git push origin content
|
||||||
|
- if: github.ref_type == 'branch'
|
||||||
|
uses: builtin:checkout
|
||||||
|
with:
|
||||||
|
ref: content
|
||||||
|
path: full
|
||||||
|
submodules: true
|
||||||
|
lfs: true
|
||||||
|
- if: github.ref_type == 'branch'
|
||||||
|
uses: builtin:checkout
|
||||||
|
with:
|
||||||
|
ref: content
|
||||||
|
path: sparse
|
||||||
|
sparse-checkout: keep
|
||||||
|
lfs: true
|
||||||
|
- if: github.ref_type == 'branch'
|
||||||
|
run: |
|
||||||
|
test "$(cat full/sub/child.txt)" = child
|
||||||
|
test "$(cat full/keep/big.bin)" = big
|
||||||
|
test "$(cat sparse/keep/big.bin)" = big
|
||||||
|
test ! -e sparse/drop
|
||||||
|
- if: github.ref_type == 'tag'
|
||||||
|
run: test "$(git describe --tags)" = "$GITHUB_REF_NAME"
|
||||||
@@ -2,23 +2,21 @@ module gitea.com/gitea/runner
|
|||||||
|
|
||||||
go 1.27
|
go 1.27
|
||||||
|
|
||||||
toolchain go1.27.1
|
toolchain go1.27.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
connectrpc.com/connect v1.21.0
|
connectrpc.com/connect v1.21.0
|
||||||
dario.cat/mergo v1.0.2
|
dario.cat/mergo v1.0.2
|
||||||
gitea.dev/actionslib v1.2.1
|
gitea.dev/actionslib v1.4.1
|
||||||
github.com/avast/retry-go/v5 v5.0.0
|
github.com/avast/retry-go/v5 v5.0.0
|
||||||
github.com/bmatcuk/doublestar/v4 v4.10.2
|
github.com/bmatcuk/doublestar/v4 v4.10.2
|
||||||
github.com/coder/websocket v1.8.15
|
github.com/coder/websocket v1.8.15
|
||||||
github.com/containerd/errdefs v1.0.0
|
github.com/containerd/errdefs v1.0.0
|
||||||
github.com/creack/pty v1.1.24
|
github.com/creack/pty v1.1.24
|
||||||
github.com/distribution/reference v0.6.0
|
github.com/distribution/reference v0.6.0
|
||||||
github.com/docker/cli v29.8.1+incompatible
|
github.com/docker/cli v29.8.2+incompatible
|
||||||
github.com/docker/go-connections v0.8.1
|
github.com/docker/go-connections v0.8.1
|
||||||
github.com/docker/go-units v0.5.0
|
github.com/docker/go-units v0.5.0
|
||||||
github.com/go-git/go-billy/v5 v5.9.1
|
|
||||||
github.com/go-git/go-git/v5 v5.19.2
|
|
||||||
github.com/google/go-cmp v0.7.0
|
github.com/google/go-cmp v0.7.0
|
||||||
github.com/johannesboyne/gofakes3 v1.2.0
|
github.com/johannesboyne/gofakes3 v1.2.0
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
@@ -26,8 +24,8 @@ require (
|
|||||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
|
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
|
||||||
github.com/mattn/go-isatty v0.0.24
|
github.com/mattn/go-isatty v0.0.24
|
||||||
github.com/moby/go-archive v0.3.3
|
github.com/moby/go-archive v0.3.3
|
||||||
github.com/moby/moby/api v1.56.0
|
github.com/moby/moby/api v1.56.1
|
||||||
github.com/moby/moby/client v0.6.0
|
github.com/moby/moby/client v0.6.1
|
||||||
github.com/moby/patternmatcher v0.6.1
|
github.com/moby/patternmatcher v0.6.1
|
||||||
github.com/opencontainers/image-spec v1.1.1
|
github.com/opencontainers/image-spec v1.1.1
|
||||||
github.com/opencontainers/selinux v1.15.1
|
github.com/opencontainers/selinux v1.15.1
|
||||||
@@ -39,15 +37,15 @@ require (
|
|||||||
github.com/stretchr/testify v1.12.1
|
github.com/stretchr/testify v1.12.1
|
||||||
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928
|
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928
|
||||||
go.etcd.io/bbolt v1.5.0
|
go.etcd.io/bbolt v1.5.0
|
||||||
go.opentelemetry.io/otel v1.46.0
|
go.opentelemetry.io/otel v1.47.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0
|
||||||
go.opentelemetry.io/otel/sdk v1.46.0
|
go.opentelemetry.io/otel/sdk v1.47.0
|
||||||
go.opentelemetry.io/otel/trace v1.46.0
|
go.opentelemetry.io/otel/trace v1.47.0
|
||||||
go.opentelemetry.io/proto/otlp v1.11.0
|
go.opentelemetry.io/proto/otlp v1.11.1
|
||||||
go.yaml.in/yaml/v4 v4.0.0-rc.6
|
go.yaml.in/yaml/v4 v4.0.0-rc.6
|
||||||
golang.org/x/net v0.59.0
|
golang.org/x/net v0.60.0
|
||||||
golang.org/x/sync v0.23.0
|
golang.org/x/sync v0.24.0
|
||||||
golang.org/x/sys v0.48.0
|
golang.org/x/sys v0.49.0
|
||||||
golang.org/x/term v0.46.0
|
golang.org/x/term v0.46.0
|
||||||
golang.org/x/text v0.42.0
|
golang.org/x/text v0.42.0
|
||||||
google.golang.org/protobuf v1.36.12
|
google.golang.org/protobuf v1.36.12
|
||||||
@@ -58,49 +56,36 @@ require (
|
|||||||
require (
|
require (
|
||||||
cyphar.com/go-pathrs v0.2.5 // indirect
|
cyphar.com/go-pathrs v0.2.5 // indirect
|
||||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||||
github.com/ProtonMail/go-crypto v1.4.1 // indirect
|
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/cloudflare/circl v1.6.5 // indirect
|
|
||||||
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
||||||
github.com/containerd/log v0.1.0 // indirect
|
github.com/containerd/log v0.1.0 // indirect
|
||||||
github.com/cyphar/filepath-securejoin v0.7.0 // indirect
|
github.com/cyphar/filepath-securejoin v0.7.0 // indirect
|
||||||
github.com/docker/docker-credential-helpers v0.9.8 // indirect
|
github.com/docker/docker-credential-helpers v0.9.8 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
|
||||||
github.com/felixge/httpsnoop v1.1.0 // indirect
|
github.com/felixge/httpsnoop v1.1.0 // indirect
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
|
||||||
github.com/go-logr/logr v1.4.4 // indirect
|
github.com/go-logr/logr v1.4.4 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
|
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
|
||||||
github.com/kevinburke/ssh_config v1.6.0 // indirect
|
|
||||||
github.com/klauspost/compress v1.19.2 // indirect
|
github.com/klauspost/compress v1.19.2 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
|
||||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||||
github.com/moby/sys/sequential v0.7.0 // indirect
|
github.com/moby/sys/sequential v0.7.0 // indirect
|
||||||
github.com/moby/sys/user v0.4.1 // indirect
|
github.com/moby/sys/user v0.4.1 // indirect
|
||||||
github.com/moby/sys/userns v0.2.0 // indirect
|
github.com/moby/sys/userns v0.2.0 // indirect
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.6.0 // indirect
|
|
||||||
github.com/prometheus/common v0.70.1 // indirect
|
github.com/prometheus/common v0.70.1 // indirect
|
||||||
github.com/prometheus/procfs v0.21.1 // indirect
|
github.com/prometheus/procfs v0.21.1 // indirect
|
||||||
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
|
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
|
||||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect
|
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect
|
||||||
github.com/sergi/go-diff v1.4.0 // indirect
|
|
||||||
github.com/skeema/knownhosts v1.3.2 // indirect
|
|
||||||
github.com/stretchr/objx v0.5.3 // indirect
|
github.com/stretchr/objx v0.5.3 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
|
||||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 // indirect
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 // indirect
|
||||||
go.opentelemetry.io/otel/metric v1.46.0 // indirect
|
go.opentelemetry.io/otel/log v1.47.0 // indirect
|
||||||
|
go.opentelemetry.io/otel/metric v1.47.0 // indirect
|
||||||
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d // indirect
|
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d // indirect
|
||||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||||
golang.org/x/crypto v0.57.0 // indirect
|
|
||||||
golang.org/x/tools v0.49.0 // indirect
|
golang.org/x/tools v0.49.0 // indirect
|
||||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
|
||||||
)
|
)
|
||||||
@@ -4,19 +4,12 @@ cyphar.com/go-pathrs v0.2.5 h1:SnX9FBvnoyn3lUs1dkMgZ52bAETpirNu3FTRh5HlRik=
|
|||||||
cyphar.com/go-pathrs v0.2.5/go.mod h1:y8f1EMG7r+hCuFf/rXsKqMJrJAUoADZGNh5/vZPKcGc=
|
cyphar.com/go-pathrs v0.2.5/go.mod h1:y8f1EMG7r+hCuFf/rXsKqMJrJAUoADZGNh5/vZPKcGc=
|
||||||
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||||
gitea.dev/actionslib v1.2.1 h1:GL//K/0zIZV6h1OOksv1awvFVg0rg7fug2xWcG7mkG0=
|
gitea.dev/actionslib v1.4.1 h1:DWlUnzDfuuMArQWS6yZ+vwJu9av7sZVMOox7li/kyu4=
|
||||||
gitea.dev/actionslib v1.2.1/go.mod h1:1+gqOKGSEPn2IFHgY8S3GC5Ld+Hn8jF3OxiFHQ/fpx4=
|
gitea.dev/actionslib v1.4.1/go.mod h1:RbXaZudZ04YndjBBxZJF9rZAqU6SKGkaSD1sob5lQbE=
|
||||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
||||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
|
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
|
||||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
|
||||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||||
github.com/ProtonMail/go-crypto v1.4.1 h1:9RfcZHqEQUvP8RzecWEUafnZVtEvrBVL9BiF67IQOfM=
|
|
||||||
github.com/ProtonMail/go-crypto v1.4.1/go.mod h1:e1OaTyu5SYVrO9gKOEhTc+5UcXtTUa+P3uLudwcgPqo=
|
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
|
||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
|
|
||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
|
||||||
github.com/avast/retry-go/v5 v5.0.0 h1:kf1Qc2UsTZ4qq8elDymqfbISvkyMuhgRxuJqX2NHP7k=
|
github.com/avast/retry-go/v5 v5.0.0 h1:kf1Qc2UsTZ4qq8elDymqfbISvkyMuhgRxuJqX2NHP7k=
|
||||||
github.com/avast/retry-go/v5 v5.0.0/go.mod h1://d+usmKWio1agtZfS1H/ltTqwtIfBnRq9zEwjc3eH8=
|
github.com/avast/retry-go/v5 v5.0.0/go.mod h1://d+usmKWio1agtZfS1H/ltTqwtIfBnRq9zEwjc3eH8=
|
||||||
github.com/aws/aws-sdk-go-v2 v1.41.5 h1:dj5kopbwUsVUVFgO4Fi5BIT3t4WyqIDjGKCangnV/yY=
|
github.com/aws/aws-sdk-go-v2 v1.41.5 h1:dj5kopbwUsVUVFgO4Fi5BIT3t4WyqIDjGKCangnV/yY=
|
||||||
@@ -53,8 +46,6 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
|
|||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/cevatbarisyilmaz/ara v0.0.4 h1:SGH10hXpBJhhTlObuZzTuFn1rrdmjQImITXnZVPSodc=
|
github.com/cevatbarisyilmaz/ara v0.0.4 h1:SGH10hXpBJhhTlObuZzTuFn1rrdmjQImITXnZVPSodc=
|
||||||
github.com/cevatbarisyilmaz/ara v0.0.4/go.mod h1:BfFOxnUd6Mj6xmcvRxHN3Sr21Z1T3U2MYkYOmoQe4Ts=
|
github.com/cevatbarisyilmaz/ara v0.0.4/go.mod h1:BfFOxnUd6Mj6xmcvRxHN3Sr21Z1T3U2MYkYOmoQe4Ts=
|
||||||
github.com/cloudflare/circl v1.6.5 h1:O64F26HEqNhznd/hrC5KZXVKYuKM2rx4deZDTc4ihQA=
|
|
||||||
github.com/cloudflare/circl v1.6.5/go.mod h1:h5LNyxAc5nTue9DS5jT+48en2PSDYt3zdGnz5OstK6c=
|
|
||||||
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
||||||
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||||
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
|
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
|
||||||
@@ -74,30 +65,16 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
|
|||||||
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||||
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
||||||
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
github.com/docker/cli v29.8.1+incompatible h1:qYL1bCp6cRw2SB1xmLlIOPyV171dilw9W2Jew38vy9c=
|
github.com/docker/cli v29.8.2+incompatible h1:2zgdFuoFst2T80oS42vhKGxkd0JRo305eIEKTsxR7pQ=
|
||||||
github.com/docker/cli v29.8.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
github.com/docker/cli v29.8.2+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
||||||
github.com/docker/docker-credential-helpers v0.9.8 h1:bIREROb7So6PRlq6KTtdS9MPEjC29OQRkFNlvK2OX8Q=
|
github.com/docker/docker-credential-helpers v0.9.8 h1:bIREROb7So6PRlq6KTtdS9MPEjC29OQRkFNlvK2OX8Q=
|
||||||
github.com/docker/docker-credential-helpers v0.9.8/go.mod h1:v1S+hepowrQXITkEfw6o4+BMbGot02wiKpzWhGUZK6c=
|
github.com/docker/docker-credential-helpers v0.9.8/go.mod h1:v1S+hepowrQXITkEfw6o4+BMbGot02wiKpzWhGUZK6c=
|
||||||
github.com/docker/go-connections v0.8.1 h1:JibmG5hULs5qXSr/cp/w3Pw5fZuStt4MOHMUExb29/M=
|
github.com/docker/go-connections v0.8.1 h1:JibmG5hULs5qXSr/cp/w3Pw5fZuStt4MOHMUExb29/M=
|
||||||
github.com/docker/go-connections v0.8.1/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q=
|
github.com/docker/go-connections v0.8.1/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q=
|
||||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||||
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
|
|
||||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
|
|
||||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
|
||||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
|
||||||
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
|
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
|
||||||
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
|
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
|
||||||
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
|
|
||||||
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
|
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
|
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
|
|
||||||
github.com/go-git/go-billy/v5 v5.9.1 h1:8U73XiOTfINdItHVa6z4Gv7ToObcZ6grkqQbLryLCdA=
|
|
||||||
github.com/go-git/go-billy/v5 v5.9.1/go.mod h1:ExsU+jcGwXTBOnyilvAnEM1wug1IxHr4yP2ZXsNRtV0=
|
|
||||||
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
|
|
||||||
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
|
|
||||||
github.com/go-git/go-git/v5 v5.19.2 h1:wkfn7vOlUBu8ivAWKBWisTiwJK4jYHzTF8Ndv1LyGqY=
|
|
||||||
github.com/go-git/go-git/v5 v5.19.2/go.mod h1:QqCBE1EFN5ddFmrliLQ3/ntRCUjZU3EJuwuB/jWEHjk=
|
|
||||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
@@ -105,8 +82,6 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
|||||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||||
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
|
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
|
||||||
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
|
|
||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
|
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||||
@@ -115,8 +90,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
|
||||||
github.com/johannesboyne/gofakes3 v1.2.0 h1:I9VEzPWvvAUAGzDlhYFoZjF0AXMlkcEyZlmBwiI6Oms=
|
github.com/johannesboyne/gofakes3 v1.2.0 h1:I9VEzPWvvAUAGzDlhYFoZjF0AXMlkcEyZlmBwiI6Oms=
|
||||||
github.com/johannesboyne/gofakes3 v1.2.0/go.mod h1:UHhRZRod9rENGFrUWTYnQHZqlNgSmjOq8DaD/ATQYRM=
|
github.com/johannesboyne/gofakes3 v1.2.0/go.mod h1:UHhRZRod9rENGFrUWTYnQHZqlNgSmjOq8DaD/ATQYRM=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
@@ -125,19 +98,8 @@ github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4d
|
|||||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
|
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
|
||||||
github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY=
|
|
||||||
github.com/kevinburke/ssh_config v1.6.0/go.mod h1:q2RIzfka+BXARoNexmF9gkxEX7DmvbW9P4hIVx2Kg4M=
|
|
||||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
|
||||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||||
@@ -146,10 +108,10 @@ github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3N
|
|||||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||||
github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME=
|
github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME=
|
||||||
github.com/moby/go-archive v0.3.3/go.mod h1:Npdv43fFqlhZW7Xo8fbm3ZMYFvAGNviUPqX21VERbcE=
|
github.com/moby/go-archive v0.3.3/go.mod h1:Npdv43fFqlhZW7Xo8fbm3ZMYFvAGNviUPqX21VERbcE=
|
||||||
github.com/moby/moby/api v1.56.0 h1:GQzua3NA599ASSIICx0iFgiJeO9YkdDARvQsm23ZZuQ=
|
github.com/moby/moby/api v1.56.1 h1:PpWkvVPB7Fr/No8w+TfyJ/I6rWZ2YPZhT3JorIg+06c=
|
||||||
github.com/moby/moby/api v1.56.0/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk=
|
github.com/moby/moby/api v1.56.1/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk=
|
||||||
github.com/moby/moby/client v0.6.0 h1:AJjEB21QPbXSXjDsZorFBoDZPhMrfbpaPLgSMAW9Bgs=
|
github.com/moby/moby/client v0.6.1 h1:gvfKmdcWw+9MzXYP0vAUKQtBTGu1L8475K2nsmL7O98=
|
||||||
github.com/moby/moby/client v0.6.0/go.mod h1:OCo00wNRyA3m4lmJ228W3JbyCN4ZNNYjpOXiJydBdcQ=
|
github.com/moby/moby/client v0.6.1/go.mod h1:XHgTFqz9NCgS/VuoxXMmEDVmVgXQ4vFEc15wsCIwb24=
|
||||||
github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U=
|
github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U=
|
||||||
github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
|
github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
|
||||||
github.com/moby/sys/mount v0.3.5 h1:eS3fsZTjHaBihwjp4/+5Z3jxqLXYsbwxqpVSfFv3M00=
|
github.com/moby/sys/mount v0.3.5 h1:eS3fsZTjHaBihwjp4/+5Z3jxqLXYsbwxqpVSfFv3M00=
|
||||||
@@ -164,18 +126,12 @@ github.com/moby/sys/userns v0.2.0 h1:nEtDtp7NCV/6dutSklNe8FrENPwFdc4mXnZqC/JWgXM
|
|||||||
github.com/moby/sys/userns v0.2.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
github.com/moby/sys/userns v0.2.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
|
|
||||||
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
|
|
||||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||||
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||||
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||||
github.com/opencontainers/selinux v1.15.1 h1:ERxeh5caJvCzNAKdI8WQbJmB1LDTn4BuaAg8wihLBpA=
|
github.com/opencontainers/selinux v1.15.1 h1:ERxeh5caJvCzNAKdI8WQbJmB1LDTn4BuaAg8wihLBpA=
|
||||||
github.com/opencontainers/selinux v1.15.1/go.mod h1:LenyElirjUHszfxrjuFqC85HIeXZKumHcKMQtnaDlQQ=
|
github.com/opencontainers/selinux v1.15.1/go.mod h1:LenyElirjUHszfxrjuFqC85HIeXZKumHcKMQtnaDlQQ=
|
||||||
github.com/pjbgf/sha1cd v0.6.0 h1:3WJ8Wz8gvDz29quX1OcEmkAlUg9diU4GxJHqs0/XiwU=
|
|
||||||
github.com/pjbgf/sha1cd v0.6.0/go.mod h1:lhpGlyHLpQZoxMv8HcgXvZEhcGs0PG/vsZnEJ7H0iCM=
|
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/prometheus/client_golang v1.24.1 h1:JnJkREXzWxUdCuPFpIWZiPispT9xVV59uiuyR2bPlnU=
|
github.com/prometheus/client_golang v1.24.1 h1:JnJkREXzWxUdCuPFpIWZiPispT9xVV59uiuyR2bPlnU=
|
||||||
github.com/prometheus/client_golang v1.24.1/go.mod h1:F+oSRECHg4sse5ucfYpYDeIv/hu68Zo0uoHKetWnzcE=
|
github.com/prometheus/client_golang v1.24.1/go.mod h1:F+oSRECHg4sse5ucfYpYDeIv/hu68Zo0uoHKetWnzcE=
|
||||||
@@ -185,20 +141,13 @@ github.com/prometheus/common v0.70.1 h1:1HvjP4D5oL3t8RsPlwxA9onvvStjtIHYE5XuuwOi
|
|||||||
github.com/prometheus/common v0.70.1/go.mod h1:VdFUQDMZK3VLkurFUVhia6uys/0suUp86TJz5qbJRhc=
|
github.com/prometheus/common v0.70.1/go.mod h1:VdFUQDMZK3VLkurFUVhia6uys/0suUp86TJz5qbJRhc=
|
||||||
github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI=
|
github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI=
|
||||||
github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
|
github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
|
||||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
|
||||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
|
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
|
||||||
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
|
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
|
||||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 h1:1EYB5IzjZawrrnELUi78f9fPu57HuXjmddZPjrls/28=
|
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 h1:1EYB5IzjZawrrnELUi78f9fPu57HuXjmddZPjrls/28=
|
||||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
|
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
|
||||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
|
||||||
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
|
|
||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
|
||||||
github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
|
github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
|
||||||
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
|
github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
|
||||||
github.com/skeema/knownhosts v1.3.2 h1:EDL9mgf4NzwMXCTfaxSD/o/a5fxDw/xL9nkU28JjdBg=
|
|
||||||
github.com/skeema/knownhosts v1.3.2/go.mod h1:bEg3iQAuw+jyiw+484wwFJoKSLwcfd7fqRy+N0QTiow=
|
|
||||||
github.com/spf13/afero v1.2.1 h1:qgMbHoJbPbw579P+1zVY+6n4nIFuIchaIjzZ/I/Yq8M=
|
github.com/spf13/afero v1.2.1 h1:qgMbHoJbPbw579P+1zVY+6n4nIFuIchaIjzZ/I/Yq8M=
|
||||||
github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
|
github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
|
||||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||||
@@ -211,8 +160,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
|
|||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
|
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
|
||||||
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
|
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
@@ -220,8 +167,6 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD
|
|||||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||||
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928 h1:zjNCuOOhh1TKRU0Ru3PPPJt80z7eReswCao91gBLk00=
|
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928 h1:zjNCuOOhh1TKRU0Ru3PPPJt80z7eReswCao91gBLk00=
|
||||||
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928/go.mod h1:PCFYfAEfKT+Nd6zWvUpsXduMR1bXFLf0uGSlEF05MCI=
|
github.com/timshannon/bolthold v0.0.0-20240314194003-30aac6950928/go.mod h1:PCFYfAEfKT+Nd6zWvUpsXduMR1bXFLf0uGSlEF05MCI=
|
||||||
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
|
|
||||||
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
|
|
||||||
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
||||||
go.etcd.io/bbolt v1.5.0 h1:S7GAl7Fxv12yohbwFfIbQCGDWbQbtDGPET4P/bD4lxU=
|
go.etcd.io/bbolt v1.5.0 h1:S7GAl7Fxv12yohbwFfIbQCGDWbQbtDGPET4P/bD4lxU=
|
||||||
go.etcd.io/bbolt v1.5.0/go.mod h1:mkltfYE5aUHQxUct9N9V+Kp7aSjFqjgrhcXIS70Lrdk=
|
go.etcd.io/bbolt v1.5.0/go.mod h1:mkltfYE5aUHQxUct9N9V+Kp7aSjFqjgrhcXIS70Lrdk=
|
||||||
@@ -230,20 +175,22 @@ go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ
|
|||||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 h1:LMuyCAyfalSjDyjdC65nK6N0zoTT63+E/u95X0JovZI=
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0 h1:LMuyCAyfalSjDyjdC65nK6N0zoTT63+E/u95X0JovZI=
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0/go.mod h1:085m8qbm4hgc8rZWGDEa4vmyyo2c3nPxUslYUKUIU04=
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.70.0/go.mod h1:085m8qbm4hgc8rZWGDEa4vmyyo2c3nPxUslYUKUIU04=
|
||||||
go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc=
|
go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM=
|
||||||
go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE=
|
go.opentelemetry.io/otel v1.47.0/go.mod h1:8wS9O2qfXrYrzp6hIF/HOYJJf/wIhFPhR2xLuP+iXQU=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 h1:OFnwLJr+pF3iHrlGSzbxyuo6/6HyBlnlN1CWEJmBVcw=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 h1:julhjPeUH/q/7hinbSdDdqt5h7Zw9YWmRlWRhI0jd54=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0/go.mod h1:716wFneO0ov19A2beH5hjfh9AK5z/VWNAtDijp1Y0/g=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0/go.mod h1:Ao2mz688LH/tFf0yMAenidq6k2YNSx6SIY2q6jDACck=
|
||||||
go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8=
|
go.opentelemetry.io/otel/log v1.47.0 h1:cOTS1CcLbSQeZKanGJ+0JpF/+t4PELi3O3bbl2lqCcI=
|
||||||
go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o=
|
go.opentelemetry.io/otel/log v1.47.0/go.mod h1:9byitSQ5pLC6PpqwGXjqdMKya6ZTswHRZh2vvXT33nw=
|
||||||
go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI=
|
go.opentelemetry.io/otel/metric v1.47.0 h1:4PptaldXx3Eat1XjMZ68pPJEs5wrhlemctZE9a3UdWY=
|
||||||
go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM=
|
go.opentelemetry.io/otel/metric v1.47.0/go.mod h1:ADGSXxRrXM6bjbvLo535EstVFlPpPYZm4LBKixjDHwU=
|
||||||
go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE=
|
go.opentelemetry.io/otel/sdk v1.47.0 h1:zWXEr4j2lFefG87TU6Yg8a7ngfohIKFZHKp0Hf5hC6I=
|
||||||
go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4=
|
go.opentelemetry.io/otel/sdk v1.47.0/go.mod h1:VUc24kiOeoGsxG8G9ULx3fWKvB7jMhnGE8Oi607lgR0=
|
||||||
go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c=
|
go.opentelemetry.io/otel/sdk/metric v1.47.0 h1:lfISg2j93VT6yqdk9OfUaZmw/GfcZqCCV3jdXtsPnKw=
|
||||||
go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI=
|
go.opentelemetry.io/otel/sdk/metric v1.47.0/go.mod h1:ypLp+mW1Nt2x+Szt3b5/i1syodyts49lMOwxpDI3VGw=
|
||||||
go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk=
|
go.opentelemetry.io/otel/trace v1.47.0 h1:JOjX/Oci8K94QHddo+bbfya/Ai/nf6/dt9ZfrFNWSrM=
|
||||||
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
|
go.opentelemetry.io/otel/trace v1.47.0/go.mod h1:jNaSLa2PZEYFG6fRjJABAu+bw4FS08uDmPg28lTghu0=
|
||||||
|
go.opentelemetry.io/proto/otlp v1.11.1 h1:SCrPqH9NE5CmBrOqpjNCiT0X5mGwMyGERXdogfR7Yjw=
|
||||||
|
go.opentelemetry.io/proto/otlp v1.11.1/go.mod h1:wq8W4aSf6bOy8RZyESOeQFr9Pu7wydWX/TY4sgvrTPg=
|
||||||
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d h1:Ns9kd1Rwzw7t0BR8XMphenji4SmIoNZPn8zhYmaVKP8=
|
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d h1:Ns9kd1Rwzw7t0BR8XMphenji4SmIoNZPn8zhYmaVKP8=
|
||||||
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d/go.mod h1:92Uoe3l++MlthCm+koNi0tcUCX3anayogF0Pa/sp24k=
|
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d/go.mod h1:92Uoe3l++MlthCm+koNi0tcUCX3anayogF0Pa/sp24k=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
@@ -255,49 +202,26 @@ go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
|||||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||||
go.yaml.in/yaml/v4 v4.0.0-rc.6 h1:1h7H1ohdUh93/FyE4YaDa1Zh64K6VVbjF4K6WUxMtH4=
|
go.yaml.in/yaml/v4 v4.0.0-rc.6 h1:1h7H1ohdUh93/FyE4YaDa1Zh64K6VVbjF4K6WUxMtH4=
|
||||||
go.yaml.in/yaml/v4 v4.0.0-rc.6/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
|
go.yaml.in/yaml/v4 v4.0.0-rc.6/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
|
||||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
|
||||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM=
|
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80=
|
|
||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
|
||||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
|
||||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
|
||||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
|
||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
|
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
|
||||||
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
|
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
|
||||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
|
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
|
||||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
||||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
|
||||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
||||||
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/artifactcache"
|
"gitea.com/gitea/runner/act/artifactcache"
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/internal/app/run"
|
"gitea.com/gitea/runner/internal/app/run"
|
||||||
"gitea.com/gitea/runner/internal/pkg/config"
|
"gitea.com/gitea/runner/internal/pkg/config"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,13 +60,13 @@ func runCacheServer(configFile *string, cacheArgs *cacheServerArgs) func(cmd *co
|
|||||||
Port: port,
|
Port: port,
|
||||||
InternalSecret: secret,
|
InternalSecret: secret,
|
||||||
Policy: run.CachePolicy(cfg),
|
Policy: run.CachePolicy(cfg),
|
||||||
Logger: log.StandardLogger().WithField("module", "cache_request"),
|
Logger: logging.Default().With("module", "cache_request"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("cache server is listening on %v", cacheHandler.ExternalURL())
|
logging.Infof("cache server is listening on %v", cacheHandler.ExternalURL())
|
||||||
|
|
||||||
<-cmd.Context().Done()
|
<-cmd.Context().Done()
|
||||||
|
|
||||||
|
|||||||
+84
-39
@@ -5,8 +5,10 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -16,6 +18,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common/logging"
|
||||||
"gitea.com/gitea/runner/internal/app/poll"
|
"gitea.com/gitea/runner/internal/app/poll"
|
||||||
"gitea.com/gitea/runner/internal/app/run"
|
"gitea.com/gitea/runner/internal/app/run"
|
||||||
"gitea.com/gitea/runner/internal/pkg/client"
|
"gitea.com/gitea/runner/internal/pkg/client"
|
||||||
@@ -29,8 +32,9 @@ import (
|
|||||||
"gitea.com/gitea/runner/internal/pkg/ver"
|
"gitea.com/gitea/runner/internal/pkg/ver"
|
||||||
|
|
||||||
"connectrpc.com/connect"
|
"connectrpc.com/connect"
|
||||||
|
runnerv1 "gitea.dev/actionslib/runner/v1"
|
||||||
|
"github.com/avast/retry-go/v5"
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,11 +46,11 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
}
|
}
|
||||||
|
|
||||||
initLogging(cfg)
|
initLogging(cfg)
|
||||||
log.Infoln("Starting runner daemon")
|
logging.Info("Starting runner daemon")
|
||||||
|
|
||||||
reg, err := config.LoadRegistration(cfg.Runner.File)
|
reg, err := config.LoadRegistration(cfg.Runner.File)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
log.Error("registration file not found, please register the runner first")
|
logging.Error("registration file not found, please register the runner first")
|
||||||
return err
|
return err
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("failed to load registration file: %w", err)
|
return fmt.Errorf("failed to load registration file: %w", err)
|
||||||
@@ -58,13 +62,13 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
releaseLock, err := lock.TryLock(cfg.Runner.File)
|
releaseLock, err := lock.TryLock(cfg.Runner.File)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, lock.ErrLocked):
|
case errors.Is(err, lock.ErrLocked):
|
||||||
log.Errorf("another gitea-runner process is already using %q; each runner process needs its own runner file (runner.file)", cfg.Runner.File)
|
logging.Errorf("another gitea-runner process is already using %q; each runner process needs its own runner file (runner.file)", cfg.Runner.File)
|
||||||
return err
|
return err
|
||||||
case err != nil:
|
case err != nil:
|
||||||
// Best-effort guard: if the lock file can't be created (e.g. a
|
// Best-effort guard: if the lock file can't be created (e.g. a
|
||||||
// read-only runner-file mount), warn and start anyway rather than
|
// read-only runner-file mount), warn and start anyway rather than
|
||||||
// refusing to run.
|
// refusing to run.
|
||||||
log.Warnf("could not lock runner file %q, continuing without the single-process guard: %v", cfg.Runner.File, err)
|
logging.Warnf("could not lock runner file %q, continuing without the single-process guard: %v", cfg.Runner.File, err)
|
||||||
default:
|
default:
|
||||||
// Held until shutdown finishes: the draining runner still owns this
|
// Held until shutdown finishes: the draining runner still owns this
|
||||||
// identity on the server, so releasing early would let a restart
|
// identity on the server, so releasing early would let a restart
|
||||||
@@ -74,13 +78,13 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
|
|
||||||
shutdownTelemetry, err := telemetry.Setup(ctx, reg.UUID, reg.Name, metrics.Registry)
|
shutdownTelemetry, err := telemetry.Setup(ctx, reg.UUID, reg.Name, metrics.Registry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Warn("OpenTelemetry export failed to start")
|
logging.WithError(err).Warn("OpenTelemetry export failed to start")
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := shutdownTelemetry(ctx); err != nil {
|
if err := shutdownTelemetry(ctx); err != nil {
|
||||||
log.WithError(err).Warn("OpenTelemetry export failed to flush")
|
logging.WithError(err).Warn("OpenTelemetry export failed to flush")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -90,13 +94,13 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
for _, l := range lbls {
|
for _, l := range lbls {
|
||||||
label, err := labels.Parse(l)
|
label, err := labels.Parse(l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Warnf("ignored invalid label %q", l)
|
logging.WithError(err).Warnf("ignored invalid label %q", l)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ls = append(ls, label)
|
ls = append(ls, label)
|
||||||
}
|
}
|
||||||
if len(ls) == 0 {
|
if len(ls) == 0 {
|
||||||
log.Warn("no labels configured, runner may not be able to pick up jobs")
|
logging.Warn("no labels configured, runner may not be able to pick up jobs")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before the first Docker API call: the standard library resolves the proxy
|
// Before the first Docker API call: the standard library resolves the proxy
|
||||||
@@ -104,7 +108,7 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
if dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost); err == nil {
|
if dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost); err == nil {
|
||||||
run.BypassProxyForDockerHost(dockerSocketPath)
|
run.BypassProxyForDockerHost(dockerSocketPath)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("cannot resolve the docker socket path, so Docker API calls are not exempted from the proxy: %v", err)
|
logging.Debugf("cannot resolve the docker socket path, so Docker API calls are not exempted from the proxy: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ls.RequireDocker() || cfg.Container.RequireDocker {
|
if ls.RequireDocker() || cfg.Container.RequireDocker {
|
||||||
@@ -116,20 +120,20 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
for keepRunning {
|
for keepRunning {
|
||||||
dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost)
|
dockerSocketPath, err := getDockerSocketPath(cfg.Container.DockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to get socket path: %s", err.Error())
|
logging.Errorf("Failed to get socket path: %s", err.Error())
|
||||||
} else if err = envcheck.CheckIfDockerRunning(tctx, dockerSocketPath); errors.Is(err, context.Canceled) {
|
} else if err = envcheck.CheckIfDockerRunning(tctx, dockerSocketPath); errors.Is(err, context.Canceled) {
|
||||||
log.Infof("Docker wait timeout of %s expired", timeout.String())
|
logging.Infof("Docker wait timeout of %s expired", timeout.String())
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Errorf("Docker connection failed: %s", err.Error())
|
logging.Errorf("Docker connection failed: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Infof("Docker is ready")
|
logging.Infof("Docker is ready")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
case <-tctx.Done():
|
case <-tctx.Done():
|
||||||
log.Infof("Docker wait timeout of %s expired", timeout.String())
|
logging.Infof("Docker wait timeout of %s expired", timeout.String())
|
||||||
keepRunning = false
|
keepRunning = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,38 +169,56 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
|
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
|
||||||
return fmt.Errorf("failed to save runner config: %w", err)
|
return fmt.Errorf("failed to save runner config: %w", err)
|
||||||
}
|
}
|
||||||
log.Infof("labels updated to: %v", reg.Labels)
|
logging.Infof("labels updated to: %v", reg.Labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
cli := client.New(
|
cli := client.New(
|
||||||
reg.Address,
|
reg.Address,
|
||||||
cfg.Runner.Insecure,
|
cfg.Runner.TLSConfig(),
|
||||||
reg.UUID,
|
reg.UUID,
|
||||||
reg.Token,
|
reg.Token,
|
||||||
config.RequestTimeout,
|
config.RequestTimeout,
|
||||||
|
cfg.Runner.ExtraHeaders,
|
||||||
)
|
)
|
||||||
|
|
||||||
runner := run.NewRunner(cfg, reg, cli)
|
runner := run.NewRunner(cfg, reg, cli)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := runner.Close(); err != nil {
|
if err := runner.Close(); err != nil {
|
||||||
log.Warnf("runner %s: cache server shutdown: %v", reg.Name, err)
|
logging.Warnf("runner %s: cache server shutdown: %v", reg.Name, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// declare the labels of the runner before fetching tasks
|
// declare the labels of the runner before fetching tasks
|
||||||
resp, err := runner.Declare(ctx, ls.Names())
|
resp, err := retry.NewWithData[*connect.Response[runnerv1.DeclareResponse]](
|
||||||
|
retry.Context(ctx),
|
||||||
|
retry.UntilSucceeded(),
|
||||||
|
retry.Delay(declareRetryDelay),
|
||||||
|
retry.DelayType(retry.FixedDelay),
|
||||||
|
retry.RetryIf(shouldRetryDeclare),
|
||||||
|
retry.OnRetry(func(attempt uint, err error) {
|
||||||
|
if attempt == 0 {
|
||||||
|
logging.WithError(err).Warn("fail to invoke Declare, retrying until Gitea is reachable")
|
||||||
|
} else {
|
||||||
|
logging.WithError(err).Debug("fail to invoke Declare, retrying")
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
).Do(func() (*connect.Response[runnerv1.DeclareResponse], error) {
|
||||||
|
return runner.Declare(ctx, ls.Names())
|
||||||
|
})
|
||||||
switch {
|
switch {
|
||||||
|
case err != nil && ctx.Err() != nil && !client.IsRegistrationRejected(err):
|
||||||
|
return nil
|
||||||
case err != nil && connect.CodeOf(err) == connect.CodeUnimplemented:
|
case err != nil && connect.CodeOf(err) == connect.CodeUnimplemented:
|
||||||
log.Errorf("Your Gitea version is too old to support runner declare, please upgrade to v1.21 or later")
|
logging.Errorf("Your Gitea version is too old to support runner declare, please upgrade to v1.21 or later")
|
||||||
return err
|
return err
|
||||||
case err != nil:
|
case err != nil:
|
||||||
log.WithError(err).Error("fail to invoke Declare")
|
logging.WithError(err).Error("fail to invoke Declare")
|
||||||
if client.IsRegistrationRejected(err) {
|
if client.IsRegistrationRejected(err) {
|
||||||
removeEphemeralRegistration(reg, cfg.Runner.File)
|
removeEphemeralRegistration(reg, cfg.Runner.File)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
default:
|
default:
|
||||||
log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully",
|
logging.Infof("runner: %s, with version: %s, with labels: %v, declare successfully",
|
||||||
resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels)
|
resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels)
|
||||||
}
|
}
|
||||||
runner.SetCapabilitiesFromDeclare(resp)
|
runner.SetCapabilitiesFromDeclare(resp)
|
||||||
@@ -208,6 +230,15 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
metrics.RunnerCapacity.Set(float64(cfg.Runner.Capacity))
|
metrics.RunnerCapacity.Set(float64(cfg.Runner.Capacity))
|
||||||
metrics.RegisterUptimeFunc(time.Now())
|
metrics.RegisterUptimeFunc(time.Now())
|
||||||
metrics.RegisterRunningJobsFunc(runner.RunningCount, cfg.Runner.Capacity)
|
metrics.RegisterRunningJobsFunc(runner.RunningCount, cfg.Runner.Capacity)
|
||||||
|
metrics.RegisterStateFunc(func() string {
|
||||||
|
if runner.RunningCount() > 0 {
|
||||||
|
return metrics.LabelStateBusy
|
||||||
|
}
|
||||||
|
if ready, _ := poller.Ready(cfg.Metrics.ReadinessGrace); !ready {
|
||||||
|
return metrics.LabelStateUnavailable
|
||||||
|
}
|
||||||
|
return metrics.LabelStateIdle
|
||||||
|
})
|
||||||
if cfg.Metrics.Enabled {
|
if cfg.Metrics.Enabled {
|
||||||
metrics.StartServer(ctx, cfg.Metrics.Addr, func() (bool, string) {
|
metrics.StartServer(ctx, cfg.Metrics.Addr, func() (bool, string) {
|
||||||
return poller.Ready(cfg.Metrics.ReadinessGrace)
|
return poller.Ready(cfg.Metrics.ReadinessGrace)
|
||||||
@@ -237,14 +268,14 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("runner: %s shutdown initiated, waiting %s for running jobs to complete before shutting down", resp.Msg.Runner.Name, cfg.Runner.ShutdownTimeout)
|
logging.Infof("runner: %s shutdown initiated, waiting %s for running jobs to complete before shutting down", resp.Msg.Runner.Name, cfg.Runner.ShutdownTimeout)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.Runner.ShutdownTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.Runner.ShutdownTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err = poller.Shutdown(ctx)
|
err = poller.Shutdown(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("runner: %s cancelled in progress jobs during shutdown", resp.Msg.Runner.Name)
|
logging.Warnf("runner: %s cancelled in progress jobs during shutdown", resp.Msg.Runner.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if poller.RegistrationRejected() {
|
if poller.RegistrationRejected() {
|
||||||
@@ -264,9 +295,9 @@ func removeEphemeralRegistration(reg *config.Registration, file string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := os.Remove(file); err == nil {
|
if err := os.Remove(file); err == nil {
|
||||||
log.Infof("removed the spent ephemeral registration %q", file)
|
logging.Infof("removed the spent ephemeral registration %q", file)
|
||||||
} else if !os.IsNotExist(err) {
|
} else if !os.IsNotExist(err) {
|
||||||
log.WithError(err).Warn("failed to remove the spent ephemeral registration")
|
logging.WithError(err).Warn("failed to remove the spent ephemeral registration")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,7 +328,7 @@ func splitLabels(s string) []string {
|
|||||||
return lbls
|
return lbls
|
||||||
}
|
}
|
||||||
|
|
||||||
// initLogging setup the global logrus logger.
|
// initLogging sets up the process-wide logger.
|
||||||
func initLogging(cfg *config.Config) {
|
func initLogging(cfg *config.Config) {
|
||||||
callPrettyfier := func(f *runtime.Frame) (string, string) {
|
callPrettyfier := func(f *runtime.Frame) (string, string) {
|
||||||
// get function name
|
// get function name
|
||||||
@@ -310,35 +341,49 @@ func initLogging(cfg *config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isTerm := isatty.IsTerminal(os.Stdout.Fd())
|
isTerm := isatty.IsTerminal(os.Stdout.Fd())
|
||||||
format := &log.TextFormatter{
|
logging.SetTextFormat(logging.TextOptions{
|
||||||
DisableColors: !isTerm,
|
DisableColors: !isTerm,
|
||||||
FullTimestamp: true,
|
FullTimestamp: true,
|
||||||
CallerPrettyfier: callPrettyfier,
|
CallerPrettyfier: callPrettyfier,
|
||||||
}
|
Filter: report.MaskGlobal,
|
||||||
log.SetFormatter(report.MaskingFormatter(format))
|
})
|
||||||
|
|
||||||
l := cfg.Log.Level
|
l := cfg.Log.Level
|
||||||
if l == "" {
|
if l == "" {
|
||||||
log.Infof("Log level not set, sticking to info")
|
logging.Infof("Log level not set, sticking to info")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
level, err := log.ParseLevel(l)
|
level, err := logging.ParseLevel(l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).
|
logging.WithError(err).
|
||||||
Errorf("invalid log level: %q", l)
|
Errorf("invalid log level: %q", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// debug level
|
// debug level
|
||||||
switch level {
|
switch level {
|
||||||
case log.DebugLevel, log.TraceLevel:
|
case logging.LevelDebug, logging.LevelTrace:
|
||||||
log.SetReportCaller(true) // Only in debug or trace because it takes a performance toll
|
logging.SetReportCaller(true) // Only in debug or trace because it takes a performance toll
|
||||||
log.Infof("Log level %s requested, setting up report caller for further debugging", level)
|
logging.Infof("Log level %s requested, setting up report caller for further debugging", level)
|
||||||
}
|
}
|
||||||
|
|
||||||
if log.GetLevel() != level {
|
if logging.GetLevel() != level {
|
||||||
log.Infof("log level set to %v", level)
|
logging.Infof("log level set to %v", level)
|
||||||
log.SetLevel(level)
|
logging.SetLevel(level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var declareRetryDelay = 30 * time.Second
|
||||||
|
|
||||||
|
func shouldRetryDeclare(err error) bool {
|
||||||
|
if errors.As(err, new(*tls.CertificateVerificationError)) || errors.As(err, new(tls.AlertError)) || errors.Is(err, http.ErrSchemeMismatch) { // misconfigurations never heal
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch connect.CodeOf(err) {
|
||||||
|
case connect.CodeUnauthenticated, connect.CodeUnimplemented, connect.CodePermissionDenied, connect.CodeInvalidArgument:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return !client.IsRegistrationRejected(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loaded 100 of 139 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user