From 8f950ccec0a9aae4a6e66d10dc28edd5fdf04ccb Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 18 Nov 2022 16:12:21 +0800 Subject: [PATCH] feat: auto remove containers --- go.mod | 2 +- go.sum | 4 ++-- runtime/task.go | 34 +++++++++++++++++++--------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index f932911..3aad8a6 100644 --- a/go.mod +++ b/go.mod @@ -72,4 +72,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/nektos/act => gitea.com/gitea/act v0.0.0-20221117110915-c92dcb7b0a01 +replace github.com/nektos/act => gitea.com/gitea/act v0.0.0-20221118080951-9b851be87df2 diff --git a/go.sum b/go.sum index 2720897..919efcf 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,8 @@ cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiy cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -gitea.com/gitea/act v0.0.0-20221117110915-c92dcb7b0a01 h1:7bwzZWEL0cvSRxeNN0N+4earbjfhgzSJ/zBYRFEnXL0= -gitea.com/gitea/act v0.0.0-20221117110915-c92dcb7b0a01/go.mod h1:lpzib6X73FHLSaTqTakan1xcsCAVhlZvPSpLns7jkRo= +gitea.com/gitea/act v0.0.0-20221118080951-9b851be87df2 h1:9ZzHV6AYMguxU1dnKajPWOObH0Bp8n0fcK67hutUop0= +gitea.com/gitea/act v0.0.0-20221118080951-9b851be87df2/go.mod h1:lpzib6X73FHLSaTqTakan1xcsCAVhlZvPSpLns7jkRo= gitea.com/gitea/proto-go v0.0.0-20221028125601-35c4f6b05835 h1:27PhT7Nli/pgRo1bDYVZ+hlCKuF9cfFuo+y9muaPVJY= gitea.com/gitea/proto-go v0.0.0-20221028125601-35c4f6b05835/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= diff --git a/runtime/task.go b/runtime/task.go index df28936..821a386 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "sync" + "time" "gitea.com/gitea/act_runner/client" runnerv1 "gitea.com/gitea/proto-go/runner/v1" @@ -28,8 +29,8 @@ type TaskInput struct { // workflowsPath string // autodetectEvent bool // eventPath string - reuseContainers bool - bindWorkdir bool + // reuseContainers bool + // bindWorkdir bool // secrets []string envs map[string]string // platforms []string @@ -46,17 +47,19 @@ type TaskInput struct { containerArchitecture string containerDaemonSocket string // noWorkflowRecurse bool - useGitIgnore bool - containerCapAdd []string - containerCapDrop []string - autoRemove bool + useGitIgnore bool + containerCapAdd []string + containerCapDrop []string + // autoRemove bool artifactServerPath string artifactServerPort string jsonLogger bool - noSkipCheckout bool + // noSkipCheckout bool // remoteName string EnvFile string + + containerNetworkMode string } type Task struct { @@ -71,9 +74,8 @@ type Task struct { func NewTask(forgeInstance string, buildID int64, client client.Client, runnerEnvs map[string]string) *Task { task := &Task{ Input: &TaskInput{ - reuseContainers: false, - envs: runnerEnvs, - noSkipCheckout: true, + envs: runnerEnvs, + containerNetworkMode: "bridge", // TODO should be configurable }, BuildID: buildID, @@ -197,9 +199,9 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error { input := t.Input config := &runner.Config{ - Workdir: "/root", - BindWorkdir: input.bindWorkdir, - ReuseContainers: input.reuseContainers, + Workdir: "/" + preset.Repository, + BindWorkdir: false, + ReuseContainers: false, ForcePull: input.forcePull, ForceRebuild: input.forceRebuild, LogOutput: true, @@ -216,13 +218,15 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error { GitHubInstance: t.client.Address(), ContainerCapAdd: input.containerCapAdd, ContainerCapDrop: input.containerCapDrop, - AutoRemove: input.autoRemove, + AutoRemove: true, ArtifactServerPath: input.artifactServerPath, ArtifactServerPort: input.artifactServerPort, - NoSkipCheckout: input.noSkipCheckout, + NoSkipCheckout: true, PresetGitHubContext: preset, EventJSON: string(eventJSON), ContainerNamePrefix: fmt.Sprintf("gitea-task-%d", task.Id), + ContainerMaxLifetime: 3 * time.Hour, // maybe should be specified by Gitea server + ContainerNetworkMode: input.containerNetworkMode, DefaultActionInstance: dataContext["gitea_default_bots_url"].GetStringValue(), } r, err := runner.New(config)