feat: auto remove containers

This commit is contained in:
Jason Song 2022-11-18 16:12:21 +08:00
parent 45de6199d1
commit 8f950ccec0
3 changed files with 22 additions and 18 deletions

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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
@ -49,14 +50,16 @@ type TaskInput struct {
useGitIgnore bool
containerCapAdd []string
containerCapDrop []string
autoRemove bool
// 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,
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)