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 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.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= 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= 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-20221118080951-9b851be87df2 h1:9ZzHV6AYMguxU1dnKajPWOObH0Bp8n0fcK67hutUop0=
gitea.com/gitea/act v0.0.0-20221117110915-c92dcb7b0a01/go.mod h1:lpzib6X73FHLSaTqTakan1xcsCAVhlZvPSpLns7jkRo= 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 h1:27PhT7Nli/pgRo1bDYVZ+hlCKuF9cfFuo+y9muaPVJY=
gitea.com/gitea/proto-go v0.0.0-20221028125601-35c4f6b05835/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= 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= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"time"
"gitea.com/gitea/act_runner/client" "gitea.com/gitea/act_runner/client"
runnerv1 "gitea.com/gitea/proto-go/runner/v1" runnerv1 "gitea.com/gitea/proto-go/runner/v1"
@ -28,8 +29,8 @@ type TaskInput struct {
// workflowsPath string // workflowsPath string
// autodetectEvent bool // autodetectEvent bool
// eventPath string // eventPath string
reuseContainers bool // reuseContainers bool
bindWorkdir bool // bindWorkdir bool
// secrets []string // secrets []string
envs map[string]string envs map[string]string
// platforms []string // platforms []string
@ -49,14 +50,16 @@ type TaskInput struct {
useGitIgnore bool useGitIgnore bool
containerCapAdd []string containerCapAdd []string
containerCapDrop []string containerCapDrop []string
autoRemove bool // autoRemove bool
artifactServerPath string artifactServerPath string
artifactServerPort string artifactServerPort string
jsonLogger bool jsonLogger bool
noSkipCheckout bool // noSkipCheckout bool
// remoteName string // remoteName string
EnvFile string EnvFile string
containerNetworkMode string
} }
type Task struct { type Task struct {
@ -71,9 +74,8 @@ type Task struct {
func NewTask(forgeInstance string, buildID int64, client client.Client, runnerEnvs map[string]string) *Task { func NewTask(forgeInstance string, buildID int64, client client.Client, runnerEnvs map[string]string) *Task {
task := &Task{ task := &Task{
Input: &TaskInput{ Input: &TaskInput{
reuseContainers: false,
envs: runnerEnvs, envs: runnerEnvs,
noSkipCheckout: true, containerNetworkMode: "bridge", // TODO should be configurable
}, },
BuildID: buildID, BuildID: buildID,
@ -197,9 +199,9 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error {
input := t.Input input := t.Input
config := &runner.Config{ config := &runner.Config{
Workdir: "/root", Workdir: "/" + preset.Repository,
BindWorkdir: input.bindWorkdir, BindWorkdir: false,
ReuseContainers: input.reuseContainers, ReuseContainers: false,
ForcePull: input.forcePull, ForcePull: input.forcePull,
ForceRebuild: input.forceRebuild, ForceRebuild: input.forceRebuild,
LogOutput: true, LogOutput: true,
@ -216,13 +218,15 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error {
GitHubInstance: t.client.Address(), GitHubInstance: t.client.Address(),
ContainerCapAdd: input.containerCapAdd, ContainerCapAdd: input.containerCapAdd,
ContainerCapDrop: input.containerCapDrop, ContainerCapDrop: input.containerCapDrop,
AutoRemove: input.autoRemove, AutoRemove: true,
ArtifactServerPath: input.artifactServerPath, ArtifactServerPath: input.artifactServerPath,
ArtifactServerPort: input.artifactServerPort, ArtifactServerPort: input.artifactServerPort,
NoSkipCheckout: input.noSkipCheckout, NoSkipCheckout: true,
PresetGitHubContext: preset, PresetGitHubContext: preset,
EventJSON: string(eventJSON), EventJSON: string(eventJSON),
ContainerNamePrefix: fmt.Sprintf("gitea-task-%d", task.Id), 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(), DefaultActionInstance: dataContext["gitea_default_bots_url"].GetStringValue(),
} }
r, err := runner.New(config) r, err := runner.New(config)