mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-16 04:43:42 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ecb57c91e | ||
|
|
4f59e3e418 | ||
|
|
c2046d597c | ||
|
|
645b17de72 | ||
|
|
0dfec377d2 |
No files matched your search
@@ -87,7 +87,7 @@ jobs:
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4
|
||||
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4
|
||||
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
@@ -82,7 +82,7 @@ jobs:
|
||||
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4
|
||||
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4
|
||||
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
@@ -3,7 +3,7 @@ GO ?= go
|
||||
EXECUTABLE := gitea-runner$(shell $(GO) env GOEXE)
|
||||
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
|
||||
|
||||
PODMAN_TEST_IMAGE ?= quay.io/containers/podman:v5.8.9-immutable@sha256:87f42f37fa85b421664c30b8f5f9cfc23920057f5e5fcadf0b6f68d9edc6d415 # renovate: datasource=docker
|
||||
PODMAN_TEST_IMAGE ?= quay.io/containers/podman:v6.1.3-immutable@sha256:1d844fd1fc6ce68c1e72ae19550cc3caef039306219ee007d88f7e67c5df4dd1 # renovate: datasource=docker
|
||||
E2E_JOB_IMAGE ?= docker.gitea.com/runner-images:ubuntu-latest@sha256:b96ba465021b1a4c6a1c9e79220c2b9bfad0f29b09dbb528bbfb35411443a3a4 # renovate: datasource=docker
|
||||
SERVICE_IMAGE ?= nginx:1.31.7-alpine@sha256:892bb2152464f0c5d8972593a5f5afaf8054552580b699d95f8ff4af08c2d33d # renovate: datasource=docker
|
||||
E2E_GITEA_IMAGE ?= gitea/gitea:main-nightly
|
||||
|
||||
@@ -62,6 +62,7 @@ type JobCredential struct {
|
||||
Ref string `json:"ref"`
|
||||
BaseRef string `json:"base_ref"`
|
||||
DefaultRef string `json:"default_ref"`
|
||||
CacheMode string `json:"cache_mode"` // empty allows everything
|
||||
|
||||
// Results is the instance whose artifact service this server forwards for the job, and
|
||||
// InsecureTLS how the runner reaches it; see results.go.
|
||||
@@ -218,10 +219,10 @@ func StartHandler(opts Options) (*Handler, error) {
|
||||
}
|
||||
|
||||
router := http.NewServeMux()
|
||||
router.HandleFunc("GET "+apiPath+"/cache", h.bearerAuth(h.find))
|
||||
router.HandleFunc("POST "+apiPath+"/caches", h.bearerAuth(h.reserve))
|
||||
router.HandleFunc("PATCH "+apiPath+"/caches/{id}", h.bearerAuth(h.upload))
|
||||
router.HandleFunc("POST "+apiPath+"/caches/{id}", h.bearerAuth(h.commit))
|
||||
router.HandleFunc("GET "+apiPath+"/cache", h.cacheModeAuth(false, h.find))
|
||||
router.HandleFunc("POST "+apiPath+"/caches", h.cacheModeAuth(true, h.reserve))
|
||||
router.HandleFunc("PATCH "+apiPath+"/caches/{id}", h.cacheModeAuth(true, h.upload))
|
||||
router.HandleFunc("POST "+apiPath+"/caches/{id}", h.cacheModeAuth(true, h.commit))
|
||||
router.HandleFunc("POST "+apiPath+"/clean", h.bearerAuth(h.clean))
|
||||
// Artifact GET is signed via query-string HMAC because @actions/cache
|
||||
// does not attach Authorization when downloading archiveLocation.
|
||||
@@ -956,6 +957,30 @@ func (h *Handler) bearerAuth(handler http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// cacheModeAuth is bearerAuth refusing what the job's cache-mode forbids, in the shapes the toolkit reports as a warning.
|
||||
func (h *Handler) cacheModeAuth(write bool, handler http.HandlerFunc) http.HandlerFunc {
|
||||
return h.bearerAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||
mode := credFromContext(r.Context()).CacheMode
|
||||
access, forbidden := "read", "write-only"
|
||||
if write {
|
||||
access, forbidden = "write", "read"
|
||||
}
|
||||
if mode != "none" && mode != forbidden {
|
||||
handler(w, r)
|
||||
return
|
||||
}
|
||||
message := fmt.Sprintf("cache %s denied: the job's cache-mode is %s", access, mode)
|
||||
switch {
|
||||
case !strings.HasPrefix(r.URL.Path, cacheServiceV2Path):
|
||||
h.responseJSON(w, r, http.StatusForbidden, map[string]any{"message": message})
|
||||
case write:
|
||||
h.responseJSON(w, r, http.StatusOK, map[string]any{"ok": false, "message": message})
|
||||
default:
|
||||
h.twirpError(w, r, twirpDenied, errors.New(message))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) unauthorized(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if strings.HasPrefix(r.URL.Path, cacheServiceV2Path) {
|
||||
h.twirpError(w, r, twirpUnauthenticated, err)
|
||||
|
||||
@@ -1003,7 +1003,7 @@ func TestHandler_UnregisterRevokes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_CrossRepoIsolation(t *testing.T) {
|
||||
func TestHandler_CrossRepoAndCacheModeIsolation(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||
handler, err := StartHandler(Options{Dir: dir})
|
||||
@@ -1012,6 +1012,8 @@ func TestHandler_CrossRepoIsolation(t *testing.T) {
|
||||
handler.RegisterJob("token-a", JobCredential{Repo: "owner/repoA"})
|
||||
handler.RegisterJob("token-b", JobCredential{Repo: "owner/repoB"})
|
||||
handler.RegisterJob("token-a-pull", JobCredential{Repo: "owner/repoA", Ref: "refs/pull/1/head"})
|
||||
handler.RegisterJob("token-a-read", JobCredential{Repo: "owner/repoA", CacheMode: "read"})
|
||||
handler.RegisterJob("token-a-write-only", JobCredential{Repo: "owner/repoA", CacheMode: "write-only"})
|
||||
|
||||
base := handler.ExternalURL() + apiPath
|
||||
key := "shared-key"
|
||||
@@ -1041,12 +1043,31 @@ func TestHandler_CrossRepoIsolation(t *testing.T) {
|
||||
resp.Body.Close()
|
||||
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
|
||||
|
||||
resp, err = clientA.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
clientRead := &http.Client{Transport: &bearerTransport{token: "token-a-read"}}
|
||||
for _, client := range []*http.Client{clientA, clientRead} {
|
||||
resp, err = client.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||
require.NoError(t, err)
|
||||
resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
for _, client := range []*http.Client{clientB, {Transport: &bearerTransport{token: "token-a-pull"}}} {
|
||||
requireDenied := func(prefix string, do func() (*http.Response, error)) {
|
||||
resp, err := do()
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
denied := map[string]string{}
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &denied))
|
||||
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
||||
assert.True(t, strings.HasPrefix(denied["message"], prefix), denied["message"])
|
||||
}
|
||||
requireDenied("cache write denied:", func() (*http.Response, error) {
|
||||
return clientRead.Post(base+"/caches", "application/json", strings.NewReader(`{"key":"new-key","version":"`+version+`"}`))
|
||||
})
|
||||
requireDenied("cache read denied:", func() (*http.Response, error) {
|
||||
return (&http.Client{Transport: &bearerTransport{token: "token-a-write-only"}}).Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
|
||||
})
|
||||
|
||||
for _, client := range []*http.Client{clientB, {Transport: &bearerTransport{token: "token-a-pull"}}, clientRead} {
|
||||
req, err = http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, id), bytes.NewReader([]byte("poison")))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Content-Range", "bytes 0-5/*")
|
||||
|
||||
@@ -40,12 +40,13 @@ const (
|
||||
twirpInternal = "internal"
|
||||
twirpUnavailable = "unavailable"
|
||||
twirpUnauthenticated = "unauthenticated"
|
||||
twirpDenied = "permission_denied"
|
||||
)
|
||||
|
||||
func (h *Handler) registerV2Routes(router *http.ServeMux) {
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/CreateCacheEntry", h.bearerAuth(h.v2CreateCacheEntry))
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/FinalizeCacheEntryUpload", h.bearerAuth(h.v2FinalizeCacheEntryUpload))
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/GetCacheEntryDownloadURL", h.bearerAuth(h.v2GetCacheEntryDownloadURL))
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/CreateCacheEntry", h.cacheModeAuth(true, h.v2CreateCacheEntry))
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/FinalizeCacheEntryUpload", h.cacheModeAuth(true, h.v2FinalizeCacheEntryUpload))
|
||||
router.HandleFunc("POST "+cacheServiceV2Path+"/GetCacheEntryDownloadURL", h.cacheModeAuth(false, h.v2GetCacheEntryDownloadURL))
|
||||
router.HandleFunc("PUT "+blobPath+"/{id}", h.signedAuth(blobUploadPurpose, h.v2UploadBlob))
|
||||
}
|
||||
|
||||
@@ -267,6 +268,8 @@ func (h *Handler) twirpError(w http.ResponseWriter, r *http.Request, code string
|
||||
status = http.StatusServiceUnavailable
|
||||
case twirpUnauthenticated:
|
||||
status = http.StatusUnauthorized
|
||||
case twirpDenied:
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
h.responseJSON(w, r, status, map[string]any{"code": code, "msg": err.Error()})
|
||||
}
|
||||
|
||||
@@ -227,6 +227,30 @@ func TestCacheServiceV2Lookups(t *testing.T) {
|
||||
assert.NotEmpty(t, got["archiveLocation"])
|
||||
})
|
||||
|
||||
t.Run("a read-only job only restores and a write-only one only saves", func(t *testing.T) {
|
||||
handler.RegisterJob("read-only", JobCredential{Repo: testRepo, CacheMode: "read"})
|
||||
readOnlyClient := &http.Client{Transport: &bearerTransport{token: "read-only"}}
|
||||
|
||||
assert.Equal(t, true, v2Call(t, handler, readOnlyClient, "GetCacheEntryDownloadURL", map[string]any{"key": "deps-abc", "version": "v1"})["ok"])
|
||||
for _, method := range []string{"CreateCacheEntry", "FinalizeCacheEntryUpload"} {
|
||||
got := v2Call(t, handler, readOnlyClient, method, map[string]any{"key": "deps-new", "version": "v1", "size_bytes": 1})
|
||||
assert.Equal(t, false, got["ok"], method)
|
||||
assert.True(t, strings.HasPrefix(got["message"].(string), "cache write denied:"), method)
|
||||
}
|
||||
|
||||
handler.RegisterJob("write-only", JobCredential{Repo: testRepo, CacheMode: "write-only"})
|
||||
writeOnlyClient := &http.Client{Transport: &bearerTransport{token: "write-only"}}
|
||||
assert.Equal(t, true, v2Call(t, handler, writeOnlyClient, "CreateCacheEntry", map[string]any{"key": "deps-write-only", "version": "v1"})["ok"])
|
||||
resp, err := writeOnlyClient.Post(handler.ExternalURL()+cacheServiceV2Path+"/GetCacheEntryDownloadURL", "application/json", strings.NewReader(`{"key":"deps-abc","version":"v1"}`))
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
got := map[string]string{}
|
||||
require.NoError(t, json.UnmarshalRead(resp.Body, &got))
|
||||
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
||||
assert.Equal(t, twirpDenied, got["code"])
|
||||
assert.True(t, strings.HasPrefix(got["msg"], "cache read denied:"), got["msg"])
|
||||
})
|
||||
|
||||
t.Run("another repository sees nothing", func(t *testing.T) {
|
||||
handler.RegisterJob("other-runtime-token", JobCredential{Repo: "other/repo"})
|
||||
otherClient := &http.Client{Transport: &bearerTransport{token: "other-runtime-token"}}
|
||||
|
||||
@@ -152,6 +152,8 @@ type NewDockerBuildExecutorInput struct {
|
||||
ContextDir string
|
||||
Dockerfile string
|
||||
BuildContext io.Reader
|
||||
Dockerignore []byte // applied to BuildContext as the docker CLI applies it
|
||||
Stdout io.Writer
|
||||
ImageTag string
|
||||
Platform string
|
||||
BuildArgs map[string]*string
|
||||
|
||||
@@ -8,10 +8,13 @@ package container
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -63,7 +66,7 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
||||
}
|
||||
var buildContext io.ReadCloser
|
||||
if input.BuildContext != nil {
|
||||
buildContext = io.NopCloser(input.BuildContext)
|
||||
buildContext, err = filterBuildContext(input.BuildContext, input.Dockerignore, input.Dockerfile)
|
||||
} else {
|
||||
buildContext, err = createBuildContext(ctx, input.ContextDir, input.Dockerfile)
|
||||
}
|
||||
@@ -78,10 +81,97 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return logDockerResponse(logger, resp.Body, false)
|
||||
err = logDockerResponse(logger, resp.Body, func(format string, args ...any) { fmt.Fprintf(input.Stdout, format, args...) })
|
||||
common.FlushWriter(input.Stdout)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func filterBuildContext(buildContext io.Reader, dockerignore []byte, relDockerfile string) (io.ReadCloser, error) {
|
||||
excludes, err := ignorefile.ReadAll(bytes.NewReader(dockerignore))
|
||||
if err != nil || len(excludes) == 0 {
|
||||
return io.NopCloser(buildContext), err
|
||||
}
|
||||
pm, err := patternmatcher.New(excludes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader, writer := io.Pipe()
|
||||
go func() {
|
||||
writer.CloseWithError(writeFilteredBuildContext(writer, buildContext, pm, path.Clean(filepath.ToSlash(relDockerfile))))
|
||||
}()
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
func writeFilteredBuildContext(w io.Writer, buildContext io.Reader, pm *patternmatcher.PatternMatcher, relDockerfile string) error {
|
||||
spool, err := os.CreateTemp("", "gitea-runner-build-") // excluded content, for a kept hardlink to it
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(spool.Name())
|
||||
defer spool.Close()
|
||||
|
||||
reader := tar.NewReader(buildContext)
|
||||
writer := tar.NewWriter(w)
|
||||
buffer := make([]byte, 32*1024)
|
||||
excluded := map[string]*excludedBuildContextEntry{}
|
||||
for {
|
||||
header, err := reader.Next()
|
||||
if err == io.EOF {
|
||||
return writer.Close()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := path.Clean(header.Name)
|
||||
var target *excludedBuildContextEntry
|
||||
if header.Typeflag == tar.TypeLink {
|
||||
target = excluded[path.Clean(header.Linkname)]
|
||||
}
|
||||
if name != ".dockerignore" && name != relDockerfile {
|
||||
skip, err := pm.MatchesOrParentMatches(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if skip {
|
||||
if target != nil {
|
||||
excluded[name] = target
|
||||
} else if header.Typeflag != tar.TypeDir {
|
||||
offset, err := spool.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
excluded[name] = &excludedBuildContextEntry{header: header, offset: offset}
|
||||
if _, err := io.CopyBuffer(spool, reader, buffer); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
var content io.Reader = reader
|
||||
if target != nil && target.keptName != "" {
|
||||
header.Linkname = target.keptName
|
||||
} else if target != nil {
|
||||
header.Typeflag, header.Linkname, header.Size = target.header.Typeflag, target.header.Linkname, target.header.Size
|
||||
content = io.NewSectionReader(spool, target.offset, header.Size)
|
||||
target.keptName = header.Name
|
||||
}
|
||||
if err := writer.WriteHeader(header); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.CopyBuffer(writer, content, buffer); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type excludedBuildContextEntry struct {
|
||||
header *tar.Header
|
||||
offset int64
|
||||
keptName string
|
||||
}
|
||||
|
||||
func createBuildContext(ctx context.Context, contextDir, relDockerfile string) (io.ReadCloser, error) {
|
||||
common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile)
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ package container
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -35,7 +37,7 @@ func TestDockerBuildLabelsImageWithItsTagAndReportsDaemonError(t *testing.T) {
|
||||
t.Setenv("DOCKER_AUTH_CONFIG", "")
|
||||
|
||||
require.ErrorContains(t, NewDockerBuildExecutor(NewDockerBuildExecutorInput{
|
||||
ImageTag: "action:test", Dockerfile: "Dockerfile", BuildContext: strings.NewReader(""),
|
||||
ImageTag: "action:test", Dockerfile: "Dockerfile", BuildContext: strings.NewReader(""), Stdout: io.Discard,
|
||||
})(t.Context()), "build rejected")
|
||||
require.JSONEq(t, `{"com.gitea.runner.image":"action:test"}`, labels)
|
||||
}
|
||||
@@ -72,3 +74,56 @@ func TestCreateBuildContextFollowsDockerignoreAndSymlinkedRootButKeepsDockerfile
|
||||
require.Equal(t, []string{".dockerignore", "a.txt", "build/keep.txt", "empty/", "Dockerfile"}, names, contextDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterBuildContextAppliesDockerignoreAndMaterializesHardlinksToExcludedFiles(t *testing.T) {
|
||||
const dockerignore = "*.log\nbuild\n!build/keep.txt\nActionfile\n.dockerignore\n"
|
||||
for _, prefix := range []string{"", "./"} {
|
||||
var archive bytes.Buffer
|
||||
writer := tar.NewWriter(&archive)
|
||||
for _, entry := range []struct {
|
||||
name, body, link string
|
||||
typeflag byte
|
||||
}{
|
||||
{"Actionfile", "FROM scratch", "", tar.TypeReg},
|
||||
{"output.log", "log", "", tar.TypeReg},
|
||||
{"build/", "", "", tar.TypeDir},
|
||||
{"build/drop.txt", "drop", "", tar.TypeReg},
|
||||
{"build/keep.txt", "keep", "", tar.TypeReg},
|
||||
{"entrypoint", "", prefix + "output.log", tar.TypeLink},
|
||||
{"other-entrypoint", "", prefix + "output.log", tar.TypeLink},
|
||||
{".dockerignore", dockerignore, "", tar.TypeReg},
|
||||
} {
|
||||
require.NoError(t, writer.WriteHeader(&tar.Header{Name: prefix + entry.name, Linkname: entry.link, Typeflag: entry.typeflag, Size: int64(len(entry.body)), Mode: 0o644}))
|
||||
_, err := io.WriteString(writer, entry.body)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, writer.Close())
|
||||
|
||||
unfiltered, err := filterBuildContext(bytes.NewReader(archive.Bytes()), nil, "Actionfile")
|
||||
require.NoError(t, err)
|
||||
passedThrough, err := io.ReadAll(unfiltered)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, archive.Bytes(), passedThrough)
|
||||
|
||||
filtered, err := filterBuildContext(bytes.NewReader(archive.Bytes()), []byte(dockerignore), "Actionfile")
|
||||
require.NoError(t, err)
|
||||
var entries []string
|
||||
for reader := tar.NewReader(filtered); ; {
|
||||
header, err := reader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
require.NoError(t, err)
|
||||
body, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
entries = append(entries, fmt.Sprintf("%s %c %s %s", strings.TrimPrefix(header.Name, prefix), header.Typeflag, strings.TrimPrefix(header.Linkname, prefix), body))
|
||||
}
|
||||
require.Equal(t, []string{
|
||||
"Actionfile 0 FROM scratch",
|
||||
"build/keep.txt 0 keep",
|
||||
"entrypoint 0 log",
|
||||
"other-entrypoint 1 entrypoint ",
|
||||
".dockerignore 0 " + dockerignore,
|
||||
}, entries, prefix)
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func loadScratchImage(t *testing.T, platform string) string {
|
||||
response, err := cli.ImageLoad(t.Context(), &archive)
|
||||
require.NoError(t, err)
|
||||
defer response.Close()
|
||||
require.NoError(t, logDockerResponse(common.Logger(t.Context()), response, false))
|
||||
require.NoError(t, logDockerResponse(common.Logger(t.Context()), response, common.Logger(t.Context()).Debugf))
|
||||
t.Cleanup(func() {
|
||||
_, _ = cli.ImageRemove(context.WithoutCancel(t.Context()), tag, mobyclient.ImageRemoveOptions{Force: true})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ package container
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"cmp"
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -26,10 +27,7 @@ type dockerMessage struct {
|
||||
Progress string `json:"progress"`
|
||||
}
|
||||
|
||||
func logDockerResponse(logger *logging.Logger, dockerResponse io.ReadCloser, isError bool) error {
|
||||
if dockerResponse == nil {
|
||||
return nil
|
||||
}
|
||||
func logDockerResponse(logger *logging.Logger, dockerResponse io.ReadCloser, log func(string, ...any)) error {
|
||||
defer dockerResponse.Close()
|
||||
|
||||
scanner := bufio.NewScanner(dockerResponse)
|
||||
@@ -38,41 +36,28 @@ func logDockerResponse(logger *logging.Logger, dockerResponse io.ReadCloser, isE
|
||||
line := scanner.Bytes()
|
||||
var msg dockerMessage
|
||||
if err := json.Unmarshal(line, &msg); err != nil {
|
||||
writeLog(logger, false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
|
||||
logger.Debugf("Unable to unmarshal line [%s] ==> %v", string(line), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if msg.Error != "" {
|
||||
writeLog(logger, isError, "%s", msg.Error)
|
||||
return errors.New(msg.Error)
|
||||
}
|
||||
|
||||
if msg.ErrorDetail.Message != "" {
|
||||
writeLog(logger, isError, "%s", msg.ErrorDetail.Message)
|
||||
return errors.New(msg.ErrorDetail.Message)
|
||||
if message := cmp.Or(msg.Error, msg.ErrorDetail.Message); message != "" {
|
||||
log("%s", message)
|
||||
return errors.New(message)
|
||||
}
|
||||
|
||||
switch {
|
||||
case msg.Status != "":
|
||||
if msg.Progress != "" {
|
||||
writeLog(logger, isError, "%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
|
||||
log("%s :: %s :: %s\n", msg.Status, msg.ID, msg.Progress)
|
||||
} else {
|
||||
writeLog(logger, isError, "%s :: %s\n", msg.Status, msg.ID)
|
||||
log("%s :: %s\n", msg.Status, msg.ID)
|
||||
}
|
||||
case msg.Stream != "":
|
||||
writeLog(logger, isError, "%s", msg.Stream)
|
||||
log("%s", msg.Stream)
|
||||
default:
|
||||
writeLog(logger, false, "Unable to handle line: %s", string(line))
|
||||
logger.Debugf("Unable to handle line: %s", string(line))
|
||||
}
|
||||
}
|
||||
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func writeLog(logger *logging.Logger, isError bool, format string, args ...any) {
|
||||
if isError {
|
||||
logger.Errorf(format, args...)
|
||||
} else {
|
||||
logger.Debugf(format, args...)
|
||||
}
|
||||
}
|
||||
@@ -61,11 +61,10 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
||||
// stream rather than on the call itself, so both have to be checked
|
||||
pullOnce := func(opts client.ImagePullOptions) error {
|
||||
reader, err := cli.ImagePull(ctx, imageRef, opts)
|
||||
streamErr := logDockerResponse(logger, reader, err != nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return streamErr
|
||||
return logDockerResponse(logger, reader, logger.Debugf)
|
||||
}
|
||||
|
||||
err = pullOnce(imagePullOptions)
|
||||
|
||||
@@ -95,15 +95,16 @@ func TestIsPinnedImage(t *testing.T) {
|
||||
// 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.
|
||||
func TestLogDockerResponseError(t *testing.T) {
|
||||
logger := logging.New(logtest.New())
|
||||
logger, hook := logtest.NewLogger()
|
||||
streamErr := func(line string) error {
|
||||
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), false)
|
||||
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), logger.Infof)
|
||||
}
|
||||
require.EqualError(t, streamErr(`{"error":"toomanyrequests: rate limit exceeded"}`), "toomanyrequests: rate limit exceeded")
|
||||
require.EqualError(t, streamErr(`{"errorDetail":{"message":"unexpected EOF"}}`), "unexpected EOF")
|
||||
require.NoError(t, streamErr(`{"status":"Downloading"}`))
|
||||
require.NoError(t, streamErr(`{"stream":"`+strings.Repeat("x", 70000)+`"}`))
|
||||
assert.Equal(t, logging.LevelInfo, hook.LastRecord().Level)
|
||||
require.ErrorIs(t, logDockerResponse(logger, io.NopCloser(io.MultiReader(
|
||||
strings.NewReader(`{"status":"Downloading"}`+"\n"), iotest.ErrReader(io.ErrUnexpectedEOF),
|
||||
)), false), io.ErrUnexpectedEOF)
|
||||
)), logger.Infof), io.ErrUnexpectedEOF)
|
||||
}
|
||||
@@ -31,18 +31,15 @@ import (
|
||||
)
|
||||
|
||||
type HostEnvironment struct {
|
||||
Path string
|
||||
TmpDir string
|
||||
ToolCache string
|
||||
Workdir string
|
||||
// CleanWorkdir means teardown owns Workdir and may delete it. Leave false
|
||||
// when Workdir points at a caller-owned checkout (e.g. `act` local mode).
|
||||
CleanWorkdir bool
|
||||
ActPath string
|
||||
Root string
|
||||
TrackingID string // RUNNER_TRACKING_ID of the job, a step with another value keeps its leftovers
|
||||
StdOut io.Writer
|
||||
AllocatePTY bool // allocate a pseudo-TTY for each step's process
|
||||
Path string
|
||||
TmpDir string
|
||||
ToolCache string
|
||||
Workdir string
|
||||
ActPath string
|
||||
Root string
|
||||
TrackingID string // RUNNER_TRACKING_ID of the job, a step with another value keeps its leftovers
|
||||
StdOut io.Writer
|
||||
AllocatePTY bool // allocate a pseudo-TTY for each step's process
|
||||
|
||||
// procGroup owns every process the job's steps start. Atomic: Remove may read
|
||||
// it while a step is still starting.
|
||||
@@ -283,6 +280,7 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
|
||||
// wait, so an orphan holding cmd's stdout pipe cannot hang cmd.Wait().
|
||||
treeKill := process.NewTreeKill(cmd)
|
||||
treeKill.KeepLeftovers = !tracked
|
||||
treeKill.GracefulCancel = true
|
||||
|
||||
var ppty *os.File
|
||||
var tty *os.File
|
||||
@@ -435,24 +433,14 @@ func (e *HostEnvironment) Remove() common.Executor {
|
||||
rmCtx, rmCancel := context.WithTimeout(context.Background(), hostCleanupTimeout)
|
||||
defer rmCancel()
|
||||
|
||||
var errs []error
|
||||
if err := removePathWithRetry(rmCtx, cmp.Or(e.Root, e.Path)); err != nil {
|
||||
err := removePathWithRetry(rmCtx, cmp.Or(e.Root, e.Path))
|
||||
if err != nil {
|
||||
logger.Warnf("failed to remove host misc state %s: %v", cmp.Or(e.Root, e.Path), err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if e.CleanWorkdir {
|
||||
if err := removePathWithRetry(rmCtx, e.Workdir); err != nil {
|
||||
logger.Warnf("failed to remove host workspace %s: %v", e.Workdir, err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil // a stalled delete must not fail job completion
|
||||
}
|
||||
for _, err := range errs {
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
}
|
||||
// Teardown timed out; warned above. Do not fail job completion over it.
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -158,27 +158,12 @@ func TestHostEnvironmentAllocatePTY(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHostEnvironmentRemoveWorkdir(t *testing.T) {
|
||||
for name, owned := range map[string]bool{"preserves caller workdir": false, "cleans owned workdir": true} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
miscRoot, workdir := t.TempDir(), t.TempDir()
|
||||
e := &HostEnvironment{
|
||||
Path: filepath.Join(miscRoot, "hostexecutor"),
|
||||
Workdir: workdir,
|
||||
CleanWorkdir: owned,
|
||||
Root: miscRoot,
|
||||
}
|
||||
require.NoError(t, os.MkdirAll(e.Path, 0o700))
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(miscRoot, "tmp"), 0o700))
|
||||
require.NoError(t, e.Remove()(t.Context()))
|
||||
assert.NoDirExists(t, miscRoot)
|
||||
_, err := os.Stat(workdir)
|
||||
if owned {
|
||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
miscRoot, workdir := t.TempDir(), t.TempDir()
|
||||
e := &HostEnvironment{Path: filepath.Join(miscRoot, "hostexecutor"), Workdir: workdir, Root: miscRoot}
|
||||
require.NoError(t, os.MkdirAll(e.Path, 0o700))
|
||||
require.NoError(t, e.Remove()(t.Context()))
|
||||
assert.NoDirExists(t, miscRoot)
|
||||
assert.DirExists(t, workdir)
|
||||
}
|
||||
|
||||
func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
|
||||
|
||||
@@ -346,8 +346,10 @@ func execAsDocker(ctx context.Context, step actionStep, actionName, actionDir, b
|
||||
ImageTag: image,
|
||||
Platform: rc.Config.ContainerArchitecture,
|
||||
BuildArgs: rc.proxyBuildArgs(),
|
||||
Stdout: common.NewLineWriter(rawLogLine(ctx)),
|
||||
}
|
||||
if localAction {
|
||||
input.Dockerignore, _ = readSingleFileFromContainerArchive(ctx, rc.JobContainer, contextDir+"/.dockerignore", 1<<20)
|
||||
buildContext, err := rc.JobContainer.GetContainerArchive(ctx, contextDir+"/.")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -587,7 +589,8 @@ func shouldRunPreStep(step actionStep) common.Conditional {
|
||||
func hasPreStep(step actionStep) common.Conditional {
|
||||
return func(ctx context.Context) bool {
|
||||
action := step.getActionModel()
|
||||
return action.Runs.Using.IsComposite() ||
|
||||
// a pre stage evaluates `with` before earlier steps run, so only a composite nesting actions gets one
|
||||
return (action.Runs.Using.IsComposite() && slices.ContainsFunc(action.Runs.Steps, func(compositeStep model.Step) bool { return compositeStep.Uses != "" })) ||
|
||||
(action.Runs.Using.IsNode() &&
|
||||
action.Runs.Pre != "") ||
|
||||
(action.Runs.Using.IsDocker() &&
|
||||
|
||||
@@ -217,11 +217,11 @@ func (rc *RunContext) compositeExecutor(action *model.Action) *compositeSteps {
|
||||
steps = append(steps, newCompositeStepLogExecutor(rc.newCompositeCommandExecutor(step.main()), stepID))
|
||||
|
||||
// run the post executor in reverse order
|
||||
stepPost := rc.newCompositeCommandExecutor(step.post())
|
||||
stepPost := newCompositeStepLogExecutor(rc.newCompositeCommandExecutor(step.post()), stepID)
|
||||
if postExecutor != nil {
|
||||
stepPost = stepPost.Finally(postExecutor)
|
||||
}
|
||||
postExecutor = newCompositeStepLogExecutor(stepPost, stepID)
|
||||
postExecutor = stepPost
|
||||
}
|
||||
|
||||
steps = append(steps, common.JobError)
|
||||
|
||||
@@ -99,6 +99,19 @@ func TestCompositeActionParity(t *testing.T) {
|
||||
|
||||
require.ErrorIs(t, rc.compositeExecutor(&model.Action{Runs: model.ActionRuns{Using: "composite", Steps: []model.Step{{ID: "nested", Uses: "org/action@v1"}}}}).pre(t.Context()), assert.AnError)
|
||||
})
|
||||
|
||||
t.Run("post steps log under their own step ids", func(t *testing.T) {
|
||||
hook := logtest.NewWithLevel(logging.LevelDebug)
|
||||
rc := &RunContext{Config: &Config{}, JobContainer: &jobContainerMock{}}
|
||||
steps := rc.compositeExecutor(&model.Action{Runs: model.ActionRuns{Using: "composite", Steps: []model.Step{{ID: "first", Uses: "./first"}, {ID: "second", Uses: "./second"}}}})
|
||||
require.NoError(t, steps.post(common.WithLogger(t.Context(), logging.New(hook))))
|
||||
var stepIDs []any
|
||||
for _, record := range hook.AllRecords() {
|
||||
ids, _ := record.Attr("stepID")
|
||||
stepIDs = append(stepIDs, ids)
|
||||
}
|
||||
assert.Equal(t, []any{[]string{"second"}, []string{"first"}}, stepIDs)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppendUniqueMasks(t *testing.T) {
|
||||
|
||||
@@ -751,12 +751,15 @@ func TestExecAsDockerStageEntrypoint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDockerActionHasPreAndPostStep(t *testing.T) {
|
||||
func TestActionHasPreAndPostStep(t *testing.T) {
|
||||
newStep := func(runs model.ActionRuns) actionStep {
|
||||
return &stepActionRemote{action: &model.Action{Runs: runs}}
|
||||
}
|
||||
ctx := t.Context()
|
||||
|
||||
assert.False(t, hasPreStep(newStep(model.ActionRuns{Using: "composite", Steps: []model.Step{{Run: "true"}}}))(ctx))
|
||||
assert.True(t, hasPreStep(newStep(model.ActionRuns{Using: "composite", Steps: []model.Step{{Run: "true"}, {Uses: "actions/cache@v4"}}}))(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))
|
||||
|
||||
|
||||
+10
-8
@@ -44,7 +44,11 @@ func (rc *RunContext) runJobHook(ctx context.Context, hookPath, name string) err
|
||||
rc.CurrentStep = ""
|
||||
defer func() { rc.CurrentStep = previousStep }()
|
||||
|
||||
cmd, shell := hookCommand(hookPath)
|
||||
env := rc.mergeEnv(rc.caEnv, rc.containerSpec.Env, rc.GetEnv())
|
||||
rc.withGithubEnv(ctx, rc.getGithubContext(ctx), env)
|
||||
rc.ApplyExtraPath(ctx, &env)
|
||||
|
||||
cmd, shell := hookCommand(hookPath, func(preferred, fallback string) string { return rc.shellOrFallback(ctx, env, preferred, fallback) })
|
||||
rawLogger := common.Logger(ctx).With(rawOutputField, true)
|
||||
rawLogger.Infof("A %s hook has been configured by the runner administrator", name)
|
||||
rawLogger.Infof("::group::Run '%s'", EscapeCommandData(hookPath))
|
||||
@@ -53,10 +57,6 @@ func (rc *RunContext) runJobHook(ctx context.Context, hookPath, name string) err
|
||||
}
|
||||
rawLogger.Infof("::endgroup::")
|
||||
|
||||
env := rc.mergeEnv(rc.caEnv, rc.containerSpec.Env, rc.GetEnv())
|
||||
rc.withGithubEnv(ctx, rc.getGithubContext(ctx), env)
|
||||
rc.ApplyExtraPath(ctx, &env)
|
||||
|
||||
err := rc.setupHookFileCommands(ctx, env)
|
||||
if err == nil {
|
||||
err = rc.runWithCommands(ctx, env, func(ctx context.Context) error {
|
||||
@@ -104,12 +104,14 @@ func (rc *RunContext) processHookFileCommands(ctx context.Context) error {
|
||||
// gives `run:` steps — a hook sets its own. See docs/adrs/1751-runner-job-hooks.md there.
|
||||
// The second return value is how the invocation is shown in the log, empty when the file is
|
||||
// executed directly.
|
||||
func hookCommand(hookPath string) (cmd []string, shell string) {
|
||||
func hookCommand(hookPath string, shellOrFallback func(preferred, fallback string) string) (cmd []string, shell string) {
|
||||
switch strings.ToLower(path.Ext(hookPath)) {
|
||||
case ".sh":
|
||||
return []string{"bash", "-e", hookPath}, "bash -e {0}"
|
||||
shellName := shellOrFallback("bash", "sh")
|
||||
return []string{shellName, "-e", hookPath}, shellName + " -e {0}"
|
||||
case ".ps1":
|
||||
return []string{"pwsh", "-command", ". '" + expressionStringEscape(hookPath) + "'"}, `pwsh -command ". '{0}'"`
|
||||
shellName := shellOrFallback("pwsh", "powershell")
|
||||
return []string{shellName, "-command", ". '" + expressionStringEscape(hookPath) + "'"}, shellName + ` -command ". '{0}'"`
|
||||
default:
|
||||
return []string{hookPath}, ""
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ func TestRunJobHook(t *testing.T) {
|
||||
rc, ctx := newHookRunContext(jobContainer, &Config{JobStartedHook: "/hooks/started.sh"})
|
||||
|
||||
require.ErrorContains(t, rc.runJobStartedHook(ctx), "boom")
|
||||
assert.Nil(t, jobContainer.cmd)
|
||||
assert.NotContains(t, jobContainer.cmd, "/hooks/started.sh")
|
||||
assert.NotContains(t, rc.Env, "STALE")
|
||||
})
|
||||
|
||||
@@ -242,17 +242,26 @@ func TestRunJobHook(t *testing.T) {
|
||||
// actions/runner deliberately runs a hook without the flags it gives `run:` steps, and an
|
||||
// executable without a known extension speaks for itself through its shebang.
|
||||
func TestHookCommand(t *testing.T) {
|
||||
for hookPath, want := range map[string]struct {
|
||||
cmd []string
|
||||
shell string
|
||||
for _, tc := range []struct {
|
||||
hookPath string
|
||||
hasShell bool
|
||||
cmd []string
|
||||
shell string
|
||||
}{
|
||||
"/hooks/started.sh": {[]string{"bash", "-e", "/hooks/started.sh"}, "bash -e {0}"},
|
||||
"/hooks/started.PS1": {[]string{"pwsh", "-command", ". '/hooks/started.PS1'"}, `pwsh -command ". '{0}'"`},
|
||||
"/hooks/started": {[]string{"/hooks/started"}, ""},
|
||||
`C:\hooks\O'Brien\started.ps1`: {[]string{"pwsh", "-command", `. 'C:\hooks\O''Brien\started.ps1'`}, `pwsh -command ". '{0}'"`},
|
||||
{"/hooks/started.sh", true, []string{"bash", "-e", "/hooks/started.sh"}, "bash -e {0}"},
|
||||
{"/hooks/started.sh", false, []string{"sh", "-e", "/hooks/started.sh"}, "sh -e {0}"},
|
||||
{"/hooks/started.PS1", true, []string{"pwsh", "-command", ". '/hooks/started.PS1'"}, `pwsh -command ". '{0}'"`},
|
||||
{"/hooks/started.PS1", false, []string{"powershell", "-command", ". '/hooks/started.PS1'"}, `powershell -command ". '{0}'"`},
|
||||
{"/hooks/started", false, []string{"/hooks/started"}, ""},
|
||||
{`C:\hooks\O'Brien\started.ps1`, true, []string{"pwsh", "-command", `. 'C:\hooks\O''Brien\started.ps1'`}, `pwsh -command ". '{0}'"`},
|
||||
} {
|
||||
cmd, shell := hookCommand(hookPath)
|
||||
assert.Equal(t, want.cmd, cmd, hookPath)
|
||||
assert.Equal(t, want.shell, shell, hookPath)
|
||||
cmd, shell := hookCommand(tc.hookPath, func(preferred, fallback string) string {
|
||||
if tc.hasShell {
|
||||
return preferred
|
||||
}
|
||||
return fallback
|
||||
})
|
||||
assert.Equal(t, tc.cmd, cmd, tc.hookPath)
|
||||
assert.Equal(t, tc.shell, shell, tc.hookPath)
|
||||
}
|
||||
}
|
||||
+22
-13
@@ -468,16 +468,15 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
|
||||
return err
|
||||
}
|
||||
rc.JobContainer = &container.HostEnvironment{
|
||||
Path: path,
|
||||
TmpDir: runnerTmp,
|
||||
ToolCache: toolCache,
|
||||
Workdir: rc.Config.Workdir,
|
||||
CleanWorkdir: rc.Config.CleanWorkdir,
|
||||
ActPath: actPath,
|
||||
Root: miscpath,
|
||||
TrackingID: rc.runnerTrackingID(),
|
||||
StdOut: logWriter,
|
||||
AllocatePTY: rc.Config.AllocatePTY,
|
||||
Path: path,
|
||||
TmpDir: runnerTmp,
|
||||
ToolCache: toolCache,
|
||||
Workdir: rc.Config.Workdir,
|
||||
ActPath: actPath,
|
||||
Root: miscpath,
|
||||
TrackingID: rc.runnerTrackingID(),
|
||||
StdOut: logWriter,
|
||||
AllocatePTY: rc.Config.AllocatePTY,
|
||||
}
|
||||
rc.cleanUpJobContainer = rc.JobContainer.Remove()
|
||||
rc.setRunnerContextEnv(ctx)
|
||||
@@ -655,6 +654,11 @@ func (rc *RunContext) startJobContainer() common.Executor {
|
||||
rc.serviceContainers = append(rc.serviceContainers, &serviceContainer{name: serviceID, image: input.Image, container: newContainer(input)})
|
||||
}
|
||||
|
||||
exposedPorts, portBindings, err := nat.ParsePortSpecs(rc.containerSpec.Ports)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse container ports: %w", err)
|
||||
}
|
||||
|
||||
ext := container.LinuxContainerEnvironmentExtensions{}
|
||||
containerInput := &container.NewContainerInput{
|
||||
Entrypoint: []string{"sleep", strconv.FormatInt(int64(rc.Config.ContainerMaxLifetime.Round(time.Second).Seconds()), 10)},
|
||||
@@ -666,6 +670,8 @@ func (rc *RunContext) startJobContainer() common.Executor {
|
||||
Env: envList,
|
||||
NetworkMode: networkName,
|
||||
NetworkAliases: []string{rc.Name},
|
||||
ExposedPorts: exposedPorts,
|
||||
PortBindings: portBindings,
|
||||
Stdout: logWriter,
|
||||
Stderr: logWriter,
|
||||
Privileged: rc.Config.Privileged,
|
||||
@@ -1635,7 +1641,7 @@ func (rc *RunContext) getRunnerContext(ctx context.Context) map[string]any {
|
||||
runnerContext["name"] = rc.Config.RunnerName
|
||||
runnerContext["environment"] = "self-hosted"
|
||||
runnerContext["workspace"] = parentDir(rc.githubWorkspace())
|
||||
if rc.Config.RunnerDebug() {
|
||||
if rc.topLevelRunContext().Config.RunnerDebug() { // a composite's config has no secrets
|
||||
runnerContext["debug"] = "1"
|
||||
}
|
||||
return runnerContext
|
||||
@@ -1679,7 +1685,9 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
||||
Workspace: rc.githubWorkspace(),
|
||||
}
|
||||
if rc.JobContainer != nil {
|
||||
ghc.EventPath = rc.JobContainer.GetActPath() + "/workflow/event.json"
|
||||
actPath := rc.JobContainer.GetActPath()
|
||||
ghc.EventPath = actPath + "/workflow/event.json"
|
||||
ghc.Env, ghc.Path = container.JoinPath(actPath, envFileCommand), container.JoinPath(actPath, pathFileCommand)
|
||||
}
|
||||
|
||||
ghc.RunnerTrackingID = rc.runnerTrackingID()
|
||||
@@ -1687,6 +1695,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
||||
if preset := rc.Config.PresetGitHubContext; preset != nil {
|
||||
merged := *preset
|
||||
merged.Workflow, merged.Job, merged.Workspace, merged.EventPath = ghc.Workflow, ghc.Job, ghc.Workspace, ghc.EventPath
|
||||
merged.Env, merged.Path = ghc.Env, ghc.Path
|
||||
merged.Action, merged.ActionPath, merged.ActionRepository, merged.ActionRef = ghc.Action, ghc.ActionPath, ghc.ActionRepository, ghc.ActionRef
|
||||
merged.RunnerPerflog, merged.RunnerTrackingID = ghc.RunnerPerflog, ghc.RunnerTrackingID
|
||||
*ghc = merged
|
||||
@@ -1841,7 +1850,7 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
|
||||
if workspace := parentDir(github.Workspace); workspace != "" {
|
||||
env["RUNNER_WORKSPACE"] = workspace
|
||||
}
|
||||
if rc.Config.RunnerDebug() {
|
||||
if rc.topLevelRunContext().Config.RunnerDebug() {
|
||||
env["RUNNER_DEBUG"] = "1"
|
||||
}
|
||||
|
||||
|
||||
@@ -342,6 +342,7 @@ jobs:
|
||||
container:
|
||||
image: node:20
|
||||
credentials: {username: "${{ env.REG_USER }}", password: pass}
|
||||
ports: ["8080:80"]
|
||||
services:
|
||||
redis:
|
||||
image: redis:latest
|
||||
@@ -351,6 +352,7 @@ jobs:
|
||||
`, &Config{Vars: map[string]string{"REG_USER": "user", "TEXT": "${{ 'literal' }}"}})
|
||||
redis, job = inputs[0], inputs[1]
|
||||
require.Equal(t, "user", job.Username)
|
||||
require.Equal(t, "8080", job.PortBindings["80/tcp"][0].HostPort)
|
||||
require.Equal(t, "user", redis.Username)
|
||||
require.Equal(t, []string{"TEXT=${{ 'literal' }}"}, redis.Env)
|
||||
require.Equal(t, "--volumes-from "+job.Name, redis.WorkflowOptions)
|
||||
@@ -1781,23 +1783,33 @@ func TestRunContextGetRunnerContext(t *testing.T) {
|
||||
assert.NotContains(t, runnerContext, "debug")
|
||||
})
|
||||
|
||||
t.Run("reports debug when step debugging is on", func(t *testing.T) {
|
||||
t.Run("reports debug from the secret, else the variable, also in composites", func(t *testing.T) {
|
||||
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
||||
rc.Config.Secrets = map[string]string{"ACTIONS_STEP_DEBUG": "true"}
|
||||
|
||||
assert.Equal(t, "1", rc.getRunnerContext(ctx)["debug"])
|
||||
assert.Equal(t, "1", (&RunContext{Parent: rc, Config: &Config{}}).getRunnerContext(ctx)["debug"])
|
||||
|
||||
rc.Config.Secrets = map[string]string{}
|
||||
rc.Config.Vars = map[string]string{"ACTIONS_STEP_DEBUG": "true"}
|
||||
assert.Equal(t, "1", rc.getRunnerContext(ctx)["debug"])
|
||||
|
||||
rc.Config.Secrets = map[string]string{"ACTIONS_STEP_DEBUG": "false"}
|
||||
assert.NotContains(t, rc.getRunnerContext(ctx), "debug")
|
||||
})
|
||||
|
||||
t.Run("keeps the execution environment values", func(t *testing.T) {
|
||||
rc := createRunsOnRunContext(t, "ubuntu-latest")
|
||||
rc.Config.Workdir = "/host/work/owner/repo"
|
||||
rc.JobContainer = &container.HostEnvironment{Workdir: rc.Config.Workdir, Path: "/container/work/owner/repo", TmpDir: "/tmp/act", ToolCache: "/tmp/tool_cache"}
|
||||
rc.JobContainer = &container.HostEnvironment{Workdir: rc.Config.Workdir, Path: "/container/work/owner/repo", TmpDir: "/tmp/act", ToolCache: "/tmp/tool_cache", ActPath: "/tmp/act-files"}
|
||||
|
||||
runnerContext := rc.getRunnerContext(ctx)
|
||||
assert.Equal(t, "/tmp/act", runnerContext["temp"])
|
||||
assert.Equal(t, "/tmp/tool_cache", runnerContext["tool_cache"])
|
||||
assert.Equal(t, "/container/work/owner", runnerContext["workspace"])
|
||||
assert.Equal(t, "/container/work/owner/repo", rc.getGithubContext(ctx).Workspace)
|
||||
githubContext := rc.getGithubContext(ctx)
|
||||
assert.Equal(t, "/container/work/owner/repo", githubContext.Workspace)
|
||||
assert.Equal(t, "/tmp/act-files/workflow/envs.txt", githubContext.Env)
|
||||
assert.Equal(t, "/tmp/act-files/workflow/pathcmd.txt", githubContext.Path)
|
||||
assert.NotEmpty(t, runnerContext["os"])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -70,7 +70,6 @@ type Config struct {
|
||||
EventJSON string // the content of JSON file to use for event.json in containers, overrides EventPath
|
||||
ContainerNamePrefix string // the prefix of container name
|
||||
ContainerMaxLifetime time.Duration // the max lifetime of job containers
|
||||
CleanWorkdir bool // remove host executor workdir on teardown
|
||||
DefaultActionInstance string // the default actions web site
|
||||
// DefaultActionInstanceIsSelfHosted reports whether DefaultActionInstance is this
|
||||
// self-hosted Gitea (DEFAULT_ACTIONS_URL=self). It gates token trust: only then may the
|
||||
@@ -100,7 +99,11 @@ type Config struct {
|
||||
|
||||
// RunnerDebug reports whether debug logging is on, exposed as `runner.debug` and RUNNER_DEBUG.
|
||||
func (c Config) RunnerDebug() bool {
|
||||
return c.Secrets["ACTIONS_STEP_DEBUG"] == "true" || c.Env["ACTIONS_STEP_DEBUG"] == "true"
|
||||
stepDebug, ok := c.Secrets["ACTIONS_STEP_DEBUG"]
|
||||
if !ok {
|
||||
stepDebug = c.Vars["ACTIONS_STEP_DEBUG"]
|
||||
}
|
||||
return stepDebug == "true" || c.Env["ACTIONS_STEP_DEBUG"] == "true"
|
||||
}
|
||||
|
||||
// GetToken: Adapt to Gitea
|
||||
|
||||
+5
-3
@@ -21,6 +21,11 @@ import (
|
||||
"gitea.dev/actionslib/pkg/model"
|
||||
)
|
||||
|
||||
const (
|
||||
pathFileCommand = "workflow/pathcmd.txt"
|
||||
envFileCommand = "workflow/envs.txt"
|
||||
)
|
||||
|
||||
type step interface {
|
||||
pre() common.Executor
|
||||
main() common.Executor
|
||||
@@ -149,10 +154,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
stateFileCommand := path.Join("workflow", "statecmd.txt")
|
||||
(*step.getEnv())["GITHUB_STATE"] = container.JoinPath(actPath, stateFileCommand)
|
||||
|
||||
pathFileCommand := path.Join("workflow", "pathcmd.txt")
|
||||
(*step.getEnv())["GITHUB_PATH"] = container.JoinPath(actPath, pathFileCommand)
|
||||
|
||||
envFileCommand := path.Join("workflow", "envs.txt")
|
||||
(*step.getEnv())["GITHUB_ENV"] = container.JoinPath(actPath, envFileCommand)
|
||||
|
||||
// Per-step summary file. Composite sub-steps share the outer job step's index
|
||||
|
||||
+17
-18
@@ -10,7 +10,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
@@ -358,29 +357,29 @@ func (sr *stepRun) setupShell(ctx context.Context, eval *expressionEvaluator) (b
|
||||
|
||||
implicitShell := step.Shell == ""
|
||||
if implicitShell {
|
||||
shellWithFallback := []string{"bash", "sh"}
|
||||
preferred, fallback := "bash", "sh"
|
||||
if rc.isWindowsHost() {
|
||||
preferred, fallback = "pwsh", "powershell" // Don't use bash on windows by default, if not using a docker container
|
||||
}
|
||||
env := maps.Clone(sr.env)
|
||||
rc.ApplyExtraPath(ctx, &env)
|
||||
if _, ok := rc.JobContainer.(*container.HostEnvironment); ok {
|
||||
// Don't use bash on windows by default, if not using a docker container
|
||||
if runtime.GOOS == "windows" {
|
||||
shellWithFallback = []string{"pwsh", "powershell"}
|
||||
}
|
||||
step.Shell = shellWithFallback[0]
|
||||
_, err := lookpath.LookPath2(shellWithFallback[0], env)
|
||||
if err != nil {
|
||||
step.Shell = shellWithFallback[1]
|
||||
}
|
||||
} else {
|
||||
step.Shell = shellWithFallback[0]
|
||||
if !rc.containerHasBash(ctx, env) {
|
||||
step.Shell = shellWithFallback[1]
|
||||
}
|
||||
}
|
||||
step.Shell = rc.shellOrFallback(ctx, env, preferred, fallback)
|
||||
}
|
||||
return implicitShell, nil
|
||||
}
|
||||
|
||||
// shellOrFallback probes a container only for bash.
|
||||
func (rc *RunContext) shellOrFallback(ctx context.Context, env map[string]string, preferred, fallback string) string {
|
||||
if _, ok := rc.JobContainer.(*container.HostEnvironment); ok {
|
||||
if _, err := lookpath.LookPath2(preferred, env); err != nil {
|
||||
return fallback
|
||||
}
|
||||
} else if preferred == "bash" && !rc.containerHasBash(ctx, env) {
|
||||
return fallback
|
||||
}
|
||||
return preferred
|
||||
}
|
||||
|
||||
// containerHasBash probes once per job, else every implicit-shell step pays for an exec.
|
||||
func (rc *RunContext) containerHasBash(ctx context.Context, env map[string]string) bool {
|
||||
top := rc.topLevelRunContext()
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ steps:
|
||||
|
||||
- Set the `RUNNER_TRACKING_ID` override in the step's `env` before it starts. Any value different from the job's ID opts out, including an empty string. On Linux and macOS a script can also set it before starting a process.
|
||||
- Opted-out processes and descendants that inherit the override survive normal job cleanup and expired output waits. Detach helper output because the runner closes leftover output pipes after a bounded wait.
|
||||
- Cancelling or timing out an active step still terminates its process tree.
|
||||
- Cancelling or timing out an active step still terminates its process tree, on Unix with SIGINT, then SIGTERM after 7.5 seconds and SIGKILL after 2.5 more.
|
||||
- The override does not preserve workspace or temporary files. Store persistent helper files outside those directories and arrange the helper's eventual shutdown.
|
||||
|
||||
This follows [actions/runner's tracking ID and orphan cleanup](https://github.com/actions/runner/blob/main/src/Runner.Worker/JobExtension.cs). Container jobs follow their container's lifecycle.
|
||||
+2
-2
@@ -28,8 +28,8 @@ The script is run according to its extension:
|
||||
|
||||
| Extension | Command |
|
||||
| --- | --- |
|
||||
| `.sh` | `bash -e <path>` |
|
||||
| `.ps1` | `pwsh -command . '<path>'` |
|
||||
| `.sh` | `bash -e <path>`, or `sh -e <path>` without bash |
|
||||
| `.ps1` | `pwsh -command . '<path>'`, or `powershell` on a host without pwsh |
|
||||
| anything else | the file itself, which needs its own shebang and executable bit |
|
||||
|
||||
As on GitHub, the shell flags applied to `run:` steps are **not** applied to a hook — set `pipefail` or anything else you want inside the script.
|
||||
|
||||
@@ -9,13 +9,13 @@ It is configured under `runner.post_task_script` in the runner YAML config (see
|
||||
For each task, execution order is:
|
||||
|
||||
1. Workflow runs (steps, actions, containers).
|
||||
2. In-job cleanup (action `post:` steps, container stop/remove).
|
||||
2. In-job cleanup (action `post:` steps, container stop/remove, host job directory removal).
|
||||
3. Job outputs are reported to Gitea.
|
||||
4. Bind-workdir workspace removal, when `container.bind_workdir` is enabled.
|
||||
5. **Post-task script** (this hook).
|
||||
6. Final task acknowledgement to Gitea (`reporter.Close()`).
|
||||
|
||||
The script is **additive**: it does not replace any built-in cleanup. When `container.bind_workdir` is enabled, the task workspace directory has usually already been deleted before the script starts. `GITEA_WORKSPACE` is still set to the path the job used, for reference.
|
||||
The script is **additive**: it does not replace any built-in cleanup. The workspace of host jobs, and of container jobs with `container.bind_workdir`, is deleted before the script starts.
|
||||
|
||||
## Runner stays offline until the script finishes
|
||||
|
||||
@@ -75,7 +75,7 @@ The script receives `runner.envs` / `runner.env_file` values plus:
|
||||
| `GITEA_TASK_ID` | Numeric task ID. |
|
||||
| `GITEA_RUN_ID` | Workflow run ID, when provided by the server. |
|
||||
| `GITEA_REPOSITORY` | Repository slug (`owner/name`). |
|
||||
| `GITEA_WORKSPACE` | Workspace path used for the job (may already be deleted). |
|
||||
| `GITEA_WORKSPACE` | Workspace path of the job. |
|
||||
| `GITEA_JOB_RESULT` | `success`, `failure`, `cancelled`, `skipped`, or `unknown`. |
|
||||
|
||||
The script environment is **not** a full copy of the job container environment. System variables such as `PATH` are only present if you define them in `runner.envs` or `runner.env_file`.
|
||||
|
||||
@@ -2,11 +2,11 @@ module gitea.com/gitea/runner
|
||||
|
||||
go 1.27
|
||||
|
||||
toolchain go1.27.3
|
||||
toolchain go1.27.4
|
||||
|
||||
require (
|
||||
connectrpc.com/connect/v2 v2.0.2
|
||||
gitea.dev/actionslib v1.5.2
|
||||
gitea.dev/actionslib v1.6.0
|
||||
github.com/bmatcuk/doublestar/v4 v4.10.4
|
||||
github.com/coder/websocket v1.8.15
|
||||
github.com/containerd/errdefs v1.0.0
|
||||
@@ -24,7 +24,7 @@ require (
|
||||
github.com/prometheus/client_golang v1.25.0
|
||||
github.com/prometheus/client_model v0.6.3
|
||||
github.com/spf13/cobra v1.10.2
|
||||
github.com/spf13/pflag v1.0.10
|
||||
github.com/spf13/pflag v1.1.0
|
||||
github.com/stretchr/testify v1.12.1
|
||||
go.etcd.io/bbolt v1.5.0
|
||||
go.opentelemetry.io/otel v1.47.0
|
||||
@@ -34,10 +34,10 @@ require (
|
||||
go.opentelemetry.io/proto/otlp v1.11.1
|
||||
go.yaml.in/yaml/v4 v4.0.0
|
||||
golang.org/x/net v0.62.0
|
||||
golang.org/x/sync v0.24.0
|
||||
golang.org/x/sys v0.49.0
|
||||
golang.org/x/term v0.47.0
|
||||
golang.org/x/text v0.43.0
|
||||
golang.org/x/sync v0.25.0
|
||||
golang.org/x/sys v0.50.0
|
||||
golang.org/x/term v0.48.0
|
||||
golang.org/x/text v0.44.0
|
||||
google.golang.org/protobuf v1.36.12
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
connectrpc.com/connect/v2 v2.0.2 h1:PoPDmOlhc46elsuCdKCKTxjNBb3HJlKF+s/sP2oucPw=
|
||||
connectrpc.com/connect/v2 v2.0.2/go.mod h1:N32OLrstcPUzkykFynnefFro3+xzSdOrZlK2jHmS/xo=
|
||||
gitea.dev/actionslib v1.5.2 h1:PSSUd+4jIjI+ycqo62+BRgAWkBnzjgeGQAcI3ZqH7LY=
|
||||
gitea.dev/actionslib v1.5.2/go.mod h1:i8wUQMnvGwT13f2re2ZTMCFL/TyauQLeOb1L4ngjtPk=
|
||||
gitea.dev/actionslib v1.6.0 h1:1xKhGyJzL9CYHJ+ESqFdxetbAU+2U7YIEuPYtsS3Fuk=
|
||||
gitea.dev/actionslib v1.6.0/go.mod h1:i8wUQMnvGwT13f2re2ZTMCFL/TyauQLeOb1L4ngjtPk=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
@@ -74,8 +74,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
|
||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.1.0 h1:6W6MbVUrnMb8PEhjkqwsVLY7ryuzRfFHF4mQd7L94ag=
|
||||
github.com/spf13/pflag v1.1.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
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/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
@@ -113,14 +113,14 @@ go.yaml.in/yaml/v4 v4.0.0 h1:/LG1pNO84Z4l603j/xMn69CcS5OYhMQ/f7l2LoQUGuw=
|
||||
go.yaml.in/yaml/v4 v4.0.0/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
|
||||
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
|
||||
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
|
||||
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
|
||||
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
|
||||
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
|
||||
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
|
||||
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.48.0 h1:G6UU1eiSB8nl3DzeUGt7Mg+2SctI8tPS6Z5/mBt6cOk=
|
||||
golang.org/x/term v0.48.0/go.mod h1:BRDLHCZr2SUjIeBHHpRyQeC9xzSAVbzY2rvKgafnHf4=
|
||||
golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
|
||||
golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
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=
|
||||
|
||||
@@ -543,7 +543,14 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||
// is removed when the task finishes, so a leaked token stops working as
|
||||
// soon as the job ends rather than remaining valid for the runner's
|
||||
// lifetime.
|
||||
revokeCache, resultsURL := r.registerCacheForTask(giteaRuntimeToken, cacheCredential(preset, cacheURL), reporter)
|
||||
cred := cacheCredential(preset, cacheURL, cmp.Or(job.CacheMode, workflow.CacheMode))
|
||||
if cacheURL != "" {
|
||||
envs["ACTIONS_CACHE_MODE"] = cred.CacheMode // @actions/cache skips what the mode forbids
|
||||
if cred.CacheMode != "write" {
|
||||
reporter.Logf("Cache mode: %s", cred.CacheMode)
|
||||
}
|
||||
}
|
||||
revokeCache, resultsURL := r.registerCacheForTask(giteaRuntimeToken, cred, reporter)
|
||||
defer revokeCache()
|
||||
|
||||
eventJSON, err := json.Marshal(preset.Event)
|
||||
@@ -603,7 +610,6 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||
EventJSON: string(eventJSON),
|
||||
ContainerNamePrefix: fmt.Sprintf("GITEA-ACTIONS-TASK-%d", task.Id),
|
||||
ContainerMaxLifetime: maxLifetime,
|
||||
CleanWorkdir: true,
|
||||
ContainerNetworkMode: docker_container.NetworkMode(r.cfg.Container.Network),
|
||||
CacheContainer: cacheContainer,
|
||||
ContainerNetworkCreateOptions: container.NewDockerNetworkCreateExecutorInput{
|
||||
@@ -729,17 +735,24 @@ func (r *Runner) registerCacheForTask(token string, cred artifactcache.JobCreden
|
||||
}
|
||||
|
||||
// cacheCredential scopes the job's cache entries by ref, as GitHub does.
|
||||
func cacheCredential(preset *model.GithubContext, publicURL string) artifactcache.JobCredential {
|
||||
func cacheCredential(preset *model.GithubContext, publicURL, cacheMode string) artifactcache.JobCredential {
|
||||
ref := preset.Ref
|
||||
if strings.HasPrefix(preset.EventName, "pull_request") && preset.EventName != "pull_request_target" {
|
||||
number, _ := preset.Event["number"].(float64)
|
||||
if number, ok := preset.Event["number"].(float64); ok && strings.HasPrefix(preset.EventName, "pull_request") && preset.EventName != "pull_request_target" {
|
||||
ref = fmt.Sprintf("refs/pull/%d/head", int64(number)) // review events take github.ref from the reviewer's commit_id
|
||||
}
|
||||
defaultRef := branchRef(model.AsString(model.NestedMapLookup(preset.Event, "repository", "default_branch")))
|
||||
if cacheMode == "" { // only trusted events save caches for the default branch
|
||||
cacheMode = "write"
|
||||
if ref == defaultRef && !slices.Contains([]string{"push", "workflow_dispatch", "schedule", "delete", "registry_package"}, preset.EventName) {
|
||||
cacheMode = "read"
|
||||
}
|
||||
}
|
||||
return artifactcache.JobCredential{
|
||||
Repo: preset.Repository,
|
||||
Ref: ref,
|
||||
BaseRef: branchRef(preset.BaseRef),
|
||||
DefaultRef: branchRef(model.AsString(model.NestedMapLookup(preset.Event, "repository", "default_branch"))),
|
||||
DefaultRef: defaultRef,
|
||||
CacheMode: cacheMode,
|
||||
PublicURL: publicURL,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,15 +120,29 @@ func TestCacheCredential(t *testing.T) {
|
||||
Event: map[string]any{"repository": repository, "number": float64(3)},
|
||||
}
|
||||
assert.Equal(t, artifactcache.JobCredential{
|
||||
Repo: "owner/repo", Ref: "refs/pull/3/head", BaseRef: "refs/heads/release", DefaultRef: "refs/heads/main", PublicURL: "http://cache",
|
||||
}, cacheCredential(review, "http://cache"))
|
||||
Repo: "owner/repo", Ref: "refs/pull/3/head", BaseRef: "refs/heads/release", DefaultRef: "refs/heads/main", CacheMode: "write", PublicURL: "http://cache",
|
||||
}, cacheCredential(review, "http://cache", ""))
|
||||
|
||||
review.EventName, review.Ref = "pull_request_target", "refs/heads/release"
|
||||
assert.Equal(t, "refs/heads/release", cacheCredential(review, "").Ref)
|
||||
assert.Equal(t, "refs/heads/release", cacheCredential(review, "", "").Ref)
|
||||
assert.Equal(t, "write", cacheCredential(review, "", "").CacheMode)
|
||||
|
||||
assert.Equal(t, artifactcache.JobCredential{
|
||||
Repo: "owner/repo", Ref: "refs/heads/feature", DefaultRef: "refs/heads/main",
|
||||
}, cacheCredential(&model.GithubContext{Repository: "owner/repo", Ref: "refs/heads/feature", Event: map[string]any{"repository": repository}}, ""))
|
||||
Repo: "owner/repo", Ref: "refs/heads/feature", DefaultRef: "refs/heads/main", CacheMode: "write",
|
||||
}, cacheCredential(&model.GithubContext{Repository: "owner/repo", Ref: "refs/heads/feature", Event: map[string]any{"repository": repository}}, "", ""))
|
||||
|
||||
for _, tc := range [][3]string{
|
||||
{"pull_request_target", "", "read"},
|
||||
{"issue_comment", "", "read"},
|
||||
{"pull_request_comment", "", "read"},
|
||||
{"push", "", "write"},
|
||||
{"issue_comment", "write", "write"},
|
||||
{"push", "none", "none"},
|
||||
} {
|
||||
cred := cacheCredential(&model.GithubContext{EventName: tc[0], Ref: "refs/heads/main", Event: map[string]any{"repository": repository}}, "", tc[1])
|
||||
assert.Equal(t, "refs/heads/main", cred.Ref, tc)
|
||||
assert.Equal(t, tc[2], cred.CacheMode, tc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
|
||||
@@ -26,4 +26,6 @@ func (k *Killer) Kill() error {
|
||||
return k.p.Kill()
|
||||
}
|
||||
|
||||
func (k *Killer) cancel(_process *os.Process, _graceful bool) error { return k.Kill() }
|
||||
|
||||
func (k *Killer) Close() error { return nil }
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Killer terminates a started process together with its whole process group,
|
||||
@@ -52,5 +53,37 @@ func (k *Killer) Kill() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Killer) cancel(process *os.Process, graceful bool) error {
|
||||
if graceful && k.pgid > 0 {
|
||||
for _, stage := range []struct {
|
||||
signal syscall.Signal
|
||||
delay time.Duration
|
||||
}{
|
||||
{syscall.SIGINT, 7500 * time.Millisecond},
|
||||
{syscall.SIGTERM, 2500 * time.Millisecond},
|
||||
} {
|
||||
if process.Signal(syscall.Signal(0)) != nil || syscall.Kill(-k.pgid, stage.signal) != nil || waitForExit(process, stage.delay) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return k.Kill()
|
||||
}
|
||||
|
||||
func waitForExit(process *os.Process, delay time.Duration) bool {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for !errors.Is(process.Signal(syscall.Signal(0)), os.ErrProcessDone) {
|
||||
select {
|
||||
case <-timer.C:
|
||||
return false
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Close is a no-op on Unix; there is no job handle to release.
|
||||
func (k *Killer) Close() error { return nil }
|
||||
@@ -6,9 +6,11 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
@@ -95,3 +97,34 @@ func TestTreeKillCancelBeforeCaptureKillsTree(t *testing.T) {
|
||||
require.Error(t, cmd.Wait())
|
||||
require.Eventually(t, func() bool { return !processAlive(childPID) }, 5*time.Second, 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestTreeKillCancelRunsInterruptTrap(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
marker := filepath.Join(t.TempDir(), "interrupted")
|
||||
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", `trap 'echo interrupted > "$1"; exit 0' INT; echo ready; read value`, "sh", marker)
|
||||
cmd.SysProcAttr = SysProcAttr("", false)
|
||||
input, err := cmd.StdinPipe()
|
||||
require.NoError(t, err)
|
||||
defer input.Close()
|
||||
output, err := cmd.StdoutPipe()
|
||||
require.NoError(t, err)
|
||||
treeKill := NewTreeKill(cmd)
|
||||
treeKill.GracefulCancel = true
|
||||
require.NoError(t, cmd.Start())
|
||||
killer, err := treeKill.Capture(cmd.Process)
|
||||
require.NoError(t, err)
|
||||
defer killer.Close()
|
||||
t.Cleanup(func() {
|
||||
_ = killer.Kill()
|
||||
_ = cmd.Wait()
|
||||
})
|
||||
var ready string
|
||||
_, err = fmt.Fscanln(output, &ready)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "ready", ready)
|
||||
|
||||
cancel()
|
||||
require.ErrorIs(t, treeKill.Wait(ctx, cmd), context.Canceled)
|
||||
require.FileExists(t, marker)
|
||||
}
|
||||
@@ -68,6 +68,8 @@ func (k *Killer) Kill() error {
|
||||
return windows.TerminateJobObject(k.job, 1)
|
||||
}
|
||||
|
||||
func (k *Killer) cancel(_process *os.Process, _graceful bool) error { return k.Kill() }
|
||||
|
||||
// Close releases the job handle. It does not terminate the processes.
|
||||
func (k *Killer) Close() error {
|
||||
if k == nil {
|
||||
|
||||
@@ -32,8 +32,9 @@ const treeKillWaitDelay = 10 * time.Second
|
||||
// Callers still set cmd.SysProcAttr (via SysProcAttr) themselves, because the
|
||||
// value differs between the plain and PTY execution paths.
|
||||
type TreeKill struct {
|
||||
killer atomic.Pointer[Killer]
|
||||
KeepLeftovers bool
|
||||
killer atomic.Pointer[Killer]
|
||||
KeepLeftovers bool
|
||||
GracefulCancel bool
|
||||
}
|
||||
|
||||
// NewTreeKill sets cmd.Cancel, cmd.WaitDelay and CREATE_SUSPENDED. Call it after setting cmd.SysProcAttr, and Capture once after a successful Start.
|
||||
@@ -42,12 +43,12 @@ func NewTreeKill(cmd *exec.Cmd) *TreeKill {
|
||||
t := &TreeKill{}
|
||||
cmd.Cancel = func() error {
|
||||
if k := t.killer.Load(); k != nil {
|
||||
return k.Kill()
|
||||
return k.cancel(cmd.Process, t.GracefulCancel)
|
||||
}
|
||||
if cmd.Process != nil {
|
||||
if killer, err := NewKiller(cmd.Process); err == nil {
|
||||
defer killer.Close()
|
||||
return killer.Kill()
|
||||
return killer.cancel(cmd.Process, t.GracefulCancel)
|
||||
}
|
||||
return cmd.Process.Kill()
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ func TestTreeKillWaitWithLeftoverHoldingOutput(t *testing.T) {
|
||||
cmd.Stdout = io.Discard
|
||||
cmd.SysProcAttr = SysProcAttr("", false)
|
||||
tk := NewTreeKill(cmd)
|
||||
tk.GracefulCancel = true
|
||||
cmd.WaitDelay = 50 * time.Millisecond
|
||||
require.NoError(t, cmd.Start())
|
||||
killer, err := tk.Capture(cmd.Process)
|
||||
|
||||
Reference in new issue
Block a user