From d41d03589cf49eb8d80d01871d69adae7e668f6c Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 18 May 2023 16:57:35 +0800 Subject: [PATCH 01/24] register --- go.mod | 2 + internal/app/cmd/register.go | 55 ++++++++++++++++--------- internal/pkg/config/config.example.yaml | 8 ++-- internal/pkg/config/config.go | 1 + 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 81e43a2..d92ac78 100644 --- a/go.mod +++ b/go.mod @@ -88,3 +88,5 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 + +replace code.gitea.io/actions-proto-go => ../actions-proto-go diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index 51318e8..32082a1 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -46,6 +46,7 @@ func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string) log.Warnf("Runner in user-mode.") } + log.Warnf("configFile: %s", *configFile) if regArgs.NoInteractive { if err := registerNoInteractive(*configFile, regArgs); err != nil { return err @@ -85,7 +86,7 @@ const ( StageInputInstance StageInputToken StageInputRunnerName - StageInputCustomLabels + StageInputLabels StageWaitingForRegistration StageExit ) @@ -101,7 +102,7 @@ type registerInputs struct { InstanceAddr string Token string RunnerName string - CustomLabels []string + Labels []string } func (r *registerInputs) validate() error { @@ -111,8 +112,8 @@ func (r *registerInputs) validate() error { if r.Token == "" { return fmt.Errorf("token is empty") } - if len(r.CustomLabels) > 0 { - return validateLabels(r.CustomLabels) + if len(r.Labels) > 0 { + return validateLabels(r.Labels) } return nil } @@ -126,7 +127,7 @@ func validateLabels(ls []string) error { return nil } -func (r *registerInputs) assignToNext(stage registerStage, value string) registerStage { +func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *config.Config) registerStage { // must set instance address and token. // if empty, keep current stage. if stage == StageInputInstance || stage == StageInputToken { @@ -154,16 +155,24 @@ func (r *registerInputs) assignToNext(stage registerStage, value string) registe return StageInputRunnerName case StageInputRunnerName: r.RunnerName = value - return StageInputCustomLabels - case StageInputCustomLabels: - r.CustomLabels = defaultLabels + if len(cfg.Runner.Labels) > 0 { + if validateLabels(cfg.Runner.Labels) == nil { + log.Info("Skip input labels, use labels defined in config file.") + r.Labels = cfg.Runner.Labels + return StageWaitingForRegistration + } + log.Warnf("Invalid labels in config file, please input again.") + } + return StageInputLabels + case StageInputLabels: + r.Labels = defaultLabels if value != "" { - r.CustomLabels = strings.Split(value, ",") + r.Labels = strings.Split(value, ",") } - if validateLabels(r.CustomLabels) != nil { + if validateLabels(r.Labels) != nil { log.Infoln("Invalid labels, please input again, leave blank to use the default labels (for example, ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster,linux_arm:host)") - return StageInputCustomLabels + return StageInputLabels } return StageWaitingForRegistration } @@ -192,10 +201,10 @@ func registerInteractive(configFile string) error { if err != nil { return err } - stage = inputs.assignToNext(stage, strings.TrimSpace(cmdString)) + stage = inputs.assignToNext(stage, strings.TrimSpace(cmdString), cfg) if stage == StageWaitingForRegistration { - log.Infof("Registering runner, name=%s, instance=%s, labels=%v.", inputs.RunnerName, inputs.InstanceAddr, inputs.CustomLabels) + log.Infof("Registering runner, name=%s, instance=%s, labels=%v.", inputs.RunnerName, inputs.InstanceAddr, inputs.Labels) if err := doRegister(cfg, inputs); err != nil { log.Errorf("Failed to register runner: %v", err) } else { @@ -226,7 +235,7 @@ func printStageHelp(stage registerStage) { case StageInputRunnerName: hostname, _ := os.Hostname() log.Infof("Enter the runner name (if set empty, use hostname: %s):\n", hostname) - case StageInputCustomLabels: + case StageInputLabels: log.Infoln("Enter the runner labels, leave blank to use the default labels (comma-separated, for example, ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster,linux_arm:host):") case StageWaitingForRegistration: log.Infoln("Waiting for registration...") @@ -242,11 +251,17 @@ func registerNoInteractive(configFile string, regArgs *registerArgs) error { InstanceAddr: regArgs.InstanceAddr, Token: regArgs.Token, RunnerName: regArgs.RunnerName, - CustomLabels: defaultLabels, + Labels: defaultLabels, } regArgs.Labels = strings.TrimSpace(regArgs.Labels) if regArgs.Labels != "" { - inputs.CustomLabels = strings.Split(regArgs.Labels, ",") + inputs.Labels = strings.Split(regArgs.Labels, ",") + } + if len(cfg.Runner.Labels) > 0 { + if regArgs.Labels != "" { + log.Warnf("Labels from command will be ignored, use labels defined in config file.") + } + inputs.Labels = cfg.Runner.Labels } if inputs.RunnerName == "" { inputs.RunnerName, _ = os.Hostname() @@ -303,7 +318,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { Name: inputs.RunnerName, Token: inputs.Token, Address: inputs.InstanceAddr, - Labels: inputs.CustomLabels, + Labels: inputs.Labels, } ls := make([]string, len(reg.Labels)) @@ -313,9 +328,9 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { } // register new runner. resp, err := cli.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ - Name: reg.Name, - Token: reg.Token, - AgentLabels: ls, + Name: reg.Name, + Token: reg.Token, + Labels: ls, })) if err != nil { log.WithError(err).Error("poller: cannot register new runner") diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index fbcdf57..77fca6b 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -26,17 +26,19 @@ runner: fetch_timeout: 5s # The interval for fetching the job from the Gitea instance. fetch_interval: 2s + # The labels of a runner are used to determine which jobs the runner can run, and how to run them. + labels: cache: # Enable cache server to use actions/cache. enabled: true # The directory to store the cache data. # If it's empty, the cache data will be stored in $HOME/.cache/actcache. - dir: "" + dir: '' # The host of the cache server. # It's not for the address to listen, but the address to connect from job containers. # So 0.0.0.0 is a bad choice, leave it empty to detect automatically. - host: "" + host: '' # The port of the cache server. # 0 means to use a random available port. port: 0 @@ -45,7 +47,7 @@ container: # Specifies the network to which the container will connect. # Could be host, bridge or the name of a custom network. # If it's empty, act_runner will create a network automatically. - network: "" + network: '' # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker). privileged: false # And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway). diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 4edf1d6..216aa4a 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -27,6 +27,7 @@ type Config struct { Insecure bool `yaml:"insecure"` FetchTimeout time.Duration `yaml:"fetch_timeout"` FetchInterval time.Duration `yaml:"fetch_interval"` + Labels []string `yaml:"labels"` } `yaml:"runner"` Cache struct { Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set -- 2.40.1 From acabf1b782cec8a818ac062f53b092491b72126e Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 18 May 2023 16:59:57 +0800 Subject: [PATCH 02/24] double quotes --- internal/pkg/config/config.example.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 77fca6b..75113df 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -34,11 +34,11 @@ cache: enabled: true # The directory to store the cache data. # If it's empty, the cache data will be stored in $HOME/.cache/actcache. - dir: '' + dir: "" # The host of the cache server. # It's not for the address to listen, but the address to connect from job containers. # So 0.0.0.0 is a bad choice, leave it empty to detect automatically. - host: '' + host: "" # The port of the cache server. # 0 means to use a random available port. port: 0 @@ -47,7 +47,7 @@ container: # Specifies the network to which the container will connect. # Could be host, bridge or the name of a custom network. # If it's empty, act_runner will create a network automatically. - network: '' + network: "" # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker). privileged: false # And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway). -- 2.40.1 From b4d901a98d5a731280caca19d77a1cb8d5a7a899 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 18 May 2023 17:01:34 +0800 Subject: [PATCH 03/24] revert go mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index d92ac78..81e43a2 100644 --- a/go.mod +++ b/go.mod @@ -88,5 +88,3 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 - -replace code.gitea.io/actions-proto-go => ../actions-proto-go -- 2.40.1 From 1857cdd3d57849243a225d26ff91b32e4afafc1b Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 18 May 2023 17:17:51 +0800 Subject: [PATCH 04/24] mockery --- internal/pkg/client/mocks/Client.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/internal/pkg/client/mocks/Client.go b/internal/pkg/client/mocks/Client.go index a689c54..d80992d 100644 --- a/internal/pkg/client/mocks/Client.go +++ b/internal/pkg/client/mocks/Client.go @@ -33,6 +33,32 @@ func (_m *Client) Address() string { return r0 } +// Declare provides a mock function with given fields: _a0, _a1 +func (_m *Client) Declare(_a0 context.Context, _a1 *connect.Request[runnerv1.DeclareRequest]) (*connect.Response[runnerv1.DeclareResponse], error) { + ret := _m.Called(_a0, _a1) + + var r0 *connect.Response[runnerv1.DeclareResponse] + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.DeclareRequest]) (*connect.Response[runnerv1.DeclareResponse], error)); ok { + return rf(_a0, _a1) + } + if rf, ok := ret.Get(0).(func(context.Context, *connect.Request[runnerv1.DeclareRequest]) *connect.Response[runnerv1.DeclareResponse]); ok { + r0 = rf(_a0, _a1) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*connect.Response[runnerv1.DeclareResponse]) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *connect.Request[runnerv1.DeclareRequest]) error); ok { + r1 = rf(_a0, _a1) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // FetchTask provides a mock function with given fields: _a0, _a1 func (_m *Client) FetchTask(_a0 context.Context, _a1 *connect.Request[runnerv1.FetchTaskRequest]) (*connect.Response[runnerv1.FetchTaskResponse], error) { ret := _m.Called(_a0, _a1) -- 2.40.1 From 81502dd33bfa2d46ae7373676225d09a4d02dac0 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 18 May 2023 18:37:18 +0800 Subject: [PATCH 05/24] not skip to input labels if labels exist in config file --- internal/app/cmd/register.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index 32082a1..3990f12 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -155,13 +155,22 @@ func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *co return StageInputRunnerName case StageInputRunnerName: r.RunnerName = value + // if there are some labels configured in config file, skip input labels stage if len(cfg.Runner.Labels) > 0 { - if validateLabels(cfg.Runner.Labels) == nil { - log.Info("Skip input labels, use labels defined in config file.") - r.Labels = cfg.Runner.Labels - return StageWaitingForRegistration + ls := make([]string, 0, len(cfg.Runner.Labels)) + for _, l := range cfg.Runner.Labels { + _, err := labels.Parse(l) + if err != nil { + log.WithError(err).Warnf("ignored invalid label %q", l) + continue + } + ls = append(ls, l) } - log.Warnf("Invalid labels in config file, please input again.") + if len(ls) == 0 { + log.Warn("no valid labels configured in config file, runner may not be able to pick up jobs") + } + r.Labels = ls + return StageWaitingForRegistration } return StageInputLabels case StageInputLabels: -- 2.40.1 From 6dece3226f4ebad3ed2b3f10086da947b9046124 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 11:18:35 +0800 Subject: [PATCH 06/24] support changing labels --- internal/app/cmd/daemon.go | 13 +++++++++++++ internal/app/run/runner.go | 11 +++++++++++ internal/pkg/labels/labels.go | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index a648d64..222572b 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -40,6 +40,14 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, return fmt.Errorf("failed to load registration file: %w", err) } + if len(cfg.Runner.Labels) > 0 { + // overwirte the labels in the config file to the state file + reg.Labels = cfg.Runner.Labels + if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { + return fmt.Errorf("failed to save runner config: %w", err) + } + } + ls := labels.Labels{} for _, l := range reg.Labels { label, err := labels.Parse(l) @@ -68,6 +76,11 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, ) runner := run.NewRunner(cfg, reg, cli) + // declare the labels of the runner before fetching tasks + err = runner.Declare(ctx, ls.Names()) + if err != nil { + return fmt.Errorf("failed to declare runner: %w", err) + } poller := poll.New(cfg, cli, runner) poller.Poll(ctx) diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index b07e59c..6df2f40 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -13,6 +13,7 @@ import ( "time" runnerv1 "code.gitea.io/actions-proto-go/runner/v1" + "github.com/bufbuild/connect-go" "github.com/docker/docker/api/types/container" "github.com/nektos/act/pkg/artifactcache" "github.com/nektos/act/pkg/common" @@ -214,3 +215,13 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report. reporter.SetOutputs(job.Outputs) return execErr } + +func (r *Runner) Declare(ctx context.Context, labels []string) error { + _, err := r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{ + Labels: labels, + })) + if err != nil { + return err + } + return nil +} diff --git a/internal/pkg/labels/labels.go b/internal/pkg/labels/labels.go index 0848222..5908700 100644 --- a/internal/pkg/labels/labels.go +++ b/internal/pkg/labels/labels.go @@ -82,3 +82,11 @@ func (l Labels) PickPlatform(runsOn []string) string { // TODO: it may be not correct, what if the runner is used as host mode only? return "node:16-bullseye" } + +func (l Labels) Names() []string { + names := make([]string, 0, len(l)) + for _, label := range l { + names = append(names, label.Name) + } + return names +} -- 2.40.1 From a20a89fb75e6106e0c7ae4c20443a07e29adad16 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 12:21:36 +0800 Subject: [PATCH 07/24] fix comment --- go.mod | 2 ++ internal/pkg/config/config.example.yaml | 1 + 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 81e43a2..d92ac78 100644 --- a/go.mod +++ b/go.mod @@ -88,3 +88,5 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 + +replace code.gitea.io/actions-proto-go => ../actions-proto-go diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 75113df..4f25470 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -27,6 +27,7 @@ runner: # The interval for fetching the job from the Gitea instance. fetch_interval: 2s # The labels of a runner are used to determine which jobs the runner can run, and how to run them. + # Please enter sequences (equivalent to lists or arrays). labels: cache: -- 2.40.1 From 44366109b23d97c097a846b905c9d8e0383ebf57 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 13:54:08 +0800 Subject: [PATCH 08/24] go mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index d92ac78..81e43a2 100644 --- a/go.mod +++ b/go.mod @@ -88,5 +88,3 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 - -replace code.gitea.io/actions-proto-go => ../actions-proto-go -- 2.40.1 From ca6b1291633c91378c0fada3ec345908cc84a815 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 14:10:01 +0800 Subject: [PATCH 09/24] test --- go.mod | 2 ++ internal/pkg/config/config.example.yaml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 81e43a2..d92ac78 100644 --- a/go.mod +++ b/go.mod @@ -88,3 +88,5 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 + +replace code.gitea.io/actions-proto-go => ../actions-proto-go diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 4f25470..409d04f 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -27,7 +27,7 @@ runner: # The interval for fetching the job from the Gitea instance. fetch_interval: 2s # The labels of a runner are used to determine which jobs the runner can run, and how to run them. - # Please enter sequences (equivalent to lists or arrays). + # Please enter sequences (equivalent to lists or arrays).. labels: cache: -- 2.40.1 From 0b38c4f6c7901ab6ff7b9e8a933b44d572fc23c2 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 14:14:29 +0800 Subject: [PATCH 10/24] fix --- go.mod | 2 -- internal/pkg/config/config.example.yaml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d92ac78..81e43a2 100644 --- a/go.mod +++ b/go.mod @@ -88,5 +88,3 @@ require ( ) replace github.com/nektos/act => gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 - -replace code.gitea.io/actions-proto-go => ../actions-proto-go diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 409d04f..4f25470 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -27,7 +27,7 @@ runner: # The interval for fetching the job from the Gitea instance. fetch_interval: 2s # The labels of a runner are used to determine which jobs the runner can run, and how to run them. - # Please enter sequences (equivalent to lists or arrays).. + # Please enter sequences (equivalent to lists or arrays). labels: cache: -- 2.40.1 From 3c6d18a2b8f195a80c80b480dd70a44d4842597e Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 19 May 2023 16:31:42 +0800 Subject: [PATCH 11/24] comment --- internal/app/cmd/daemon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 222572b..5679fdf 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -41,7 +41,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, } if len(cfg.Runner.Labels) > 0 { - // overwirte the labels in the config file to the state file + // overwirte the labels in the config file to the state file. reg.Labels = cfg.Runner.Labels if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { return fmt.Errorf("failed to save runner config: %w", err) -- 2.40.1 From 9a0d46fa6674d780e637c14049540ac820def04d Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Sat, 20 May 2023 20:22:22 +0800 Subject: [PATCH 12/24] pass version when registering and declaring --- internal/app/cmd/register.go | 7 ++++--- internal/app/run/runner.go | 7 +++++-- internal/pkg/client/http.go | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index 3990f12..e7bbe60 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -337,9 +337,10 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { } // register new runner. resp, err := cli.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ - Name: reg.Name, - Token: reg.Token, - Labels: ls, + Name: reg.Name, + Token: reg.Token, + Version: ver.Version(), + Labels: ls, })) if err != nil { log.WithError(err).Error("poller: cannot register new runner") diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index 6df2f40..bd30743 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -217,11 +217,14 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report. } func (r *Runner) Declare(ctx context.Context, labels []string) error { - _, err := r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{ - Labels: labels, + resp, err := r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{ + Version: ver.Version(), + Labels: labels, })) if err != nil { return err } + log.Info("runner [%s], with version: [%s], with labels: [%v], declare successfully", + resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) return nil } diff --git a/internal/pkg/client/http.go b/internal/pkg/client/http.go index cc0c44e..f0f956f 100644 --- a/internal/pkg/client/http.go +++ b/internal/pkg/client/http.go @@ -39,9 +39,9 @@ func New(endpoint string, insecure bool, uuid, token, version string, opts ...co if token != "" { req.Header().Set(TokenHeader, token) } - if version != "" { - req.Header().Set(VersionHeader, version) - } + // if version != "" { + // req.Header().Set(VersionHeader, version) + // } return next(ctx, req) } }))) -- 2.40.1 From 5d4e8cb73da6462d4ed098cdb1873128867f3303 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 22 May 2023 12:20:54 +0800 Subject: [PATCH 13/24] no pass version when new grpc client --- internal/app/cmd/daemon.go | 2 -- internal/app/cmd/register.go | 1 - internal/pkg/client/http.go | 5 +---- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 5679fdf..c4cfcfc 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -18,7 +18,6 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/config" "gitea.com/gitea/act_runner/internal/pkg/envcheck" "gitea.com/gitea/act_runner/internal/pkg/labels" - "gitea.com/gitea/act_runner/internal/pkg/ver" ) func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { @@ -72,7 +71,6 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, cfg.Runner.Insecure, reg.UUID, reg.Token, - ver.Version(), ) runner := run.NewRunner(cfg, reg, cli) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index e7bbe60..e6814af 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -297,7 +297,6 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { cfg.Runner.Insecure, "", "", - ver.Version(), ) for { diff --git a/internal/pkg/client/http.go b/internal/pkg/client/http.go index f0f956f..24284a8 100644 --- a/internal/pkg/client/http.go +++ b/internal/pkg/client/http.go @@ -28,7 +28,7 @@ func getHttpClient(endpoint string, insecure bool) *http.Client { } // New returns a new runner client. -func New(endpoint string, insecure bool, uuid, token, version string, opts ...connect.ClientOption) *HTTPClient { +func New(endpoint string, insecure bool, uuid, token string, opts ...connect.ClientOption) *HTTPClient { baseURL := strings.TrimRight(endpoint, "/") + "/api/actions" opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { @@ -39,9 +39,6 @@ func New(endpoint string, insecure bool, uuid, token, version string, opts ...co if token != "" { req.Header().Set(TokenHeader, token) } - // if version != "" { - // req.Header().Set(VersionHeader, version) - // } return next(ctx, req) } }))) -- 2.40.1 From 4dd6a5536dbcb8d904188b55ed80bcfcb336b511 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 5 Jun 2023 18:35:34 +0800 Subject: [PATCH 14/24] fix log --- internal/app/run/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index bd30743..b0a7c96 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -224,7 +224,7 @@ func (r *Runner) Declare(ctx context.Context, labels []string) error { if err != nil { return err } - log.Info("runner [%s], with version: [%s], with labels: [%v], declare successfully", + log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully", resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) return nil } -- 2.40.1 From 87f105a8a691ba2c0984089cd1ca7c5a95129ee2 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 10:06:49 +0800 Subject: [PATCH 15/24] update go mod --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 81e43a2..9f9d8a4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitea.com/gitea/act_runner go 1.20 require ( - code.gitea.io/actions-proto-go v0.2.1 + code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610 code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5 github.com/avast/retry-go/v4 v4.3.1 github.com/bufbuild/connect-go v1.3.1 diff --git a/go.sum b/go.sum index 4d8f638..25505bb 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -code.gitea.io/actions-proto-go v0.2.1 h1:ToMN/8thz2q10TuCq8dL2d8mI+/pWpJcHCvG+TELwa0= -code.gitea.io/actions-proto-go v0.2.1/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A= +code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610 h1:+GGIH6PhAHxjr9HyUwomTymgup8tEHOlq/fEmvH8Cr4= +code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A= code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5 h1:daBEK2GQeqGikJESctP5Cu1i33z5ztAD4kyQWiw185M= code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 h1:hvyzFmxDmdSZBd8S2+r8VqPSK9eihTD2SrTBAvwgYsA= -- 2.40.1 From 2efdf1dbb7626d30929d981ab3607da60abd5579 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 11:03:39 +0800 Subject: [PATCH 16/24] add version v prefix in makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 454d2f5..f04c0ba 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ GO_PACKAGES_TO_VET ?= $(filter-out gitea.com/gitea/act_runner/internal/pkg/clien TAGS ?= -LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=$(RELASE_VERSION)" +LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=v$(RELASE_VERSION)" all: build -- 2.40.1 From e89f91316caee5572effa753d8556a29ac8f3abe Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 11:45:24 +0800 Subject: [PATCH 17/24] update go mod --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9f9d8a4..0ea65c9 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitea.com/gitea/act_runner go 1.20 require ( - code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610 + code.gitea.io/actions-proto-go v0.3.0 code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5 github.com/avast/retry-go/v4 v4.3.1 github.com/bufbuild/connect-go v1.3.1 diff --git a/go.sum b/go.sum index 25505bb..1ef81d7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610 h1:+GGIH6PhAHxjr9HyUwomTymgup8tEHOlq/fEmvH8Cr4= -code.gitea.io/actions-proto-go v0.2.2-0.20230606002559-f3d8a4e6d610/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A= +code.gitea.io/actions-proto-go v0.3.0 h1:9Tvg8+TaaCXPKi6EnWl9vVgs2VZsj1Cs5afnsHa4AmM= +code.gitea.io/actions-proto-go v0.3.0/go.mod h1:00ys5QDo1iHN1tHNvvddAcy2W/g+425hQya1cCSvq9A= code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5 h1:daBEK2GQeqGikJESctP5Cu1i33z5ztAD4kyQWiw185M= code.gitea.io/gitea-vet v0.2.3-0.20230113022436-2b1561217fa5/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= gitea.com/gitea/act v0.245.2-0.20230516060355-9283cfc9b166 h1:hvyzFmxDmdSZBd8S2+r8VqPSK9eihTD2SrTBAvwgYsA= -- 2.40.1 From 71a61d161baf36da888f7a5313d28624af96e312 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 16:57:34 +0800 Subject: [PATCH 18/24] fix --- internal/app/cmd/daemon.go | 32 ++++++++++++++----- internal/app/cmd/register.go | 1 - internal/app/run/runner.go | 10 ++---- internal/pkg/config/config.example.yaml | 6 ++-- internal/pkg/utils/utils.go | 23 ++++++++++++++ internal/pkg/utils/utils_test.go | 41 +++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 internal/pkg/utils/utils.go create mode 100644 internal/pkg/utils/utils_test.go diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index f393d68..5508791 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" + "github.com/bufbuild/connect-go" "github.com/mattn/go-isatty" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -22,6 +23,7 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/config" "gitea.com/gitea/act_runner/internal/pkg/envcheck" "gitea.com/gitea/act_runner/internal/pkg/labels" + "gitea.com/gitea/act_runner/internal/pkg/utils" ) func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { @@ -42,11 +44,13 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, return fmt.Errorf("failed to load registration file: %w", err) } + var overwrite bool if len(cfg.Runner.Labels) > 0 { - // overwirte the labels in the config file to the state file. - reg.Labels = cfg.Runner.Labels - if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { - return fmt.Errorf("failed to save runner config: %w", err) + // Determine if the labels in the `.runner` file are the same as the labels in config. + if isEqual := utils.AreStrSlicesElemsEqual(cfg.Runner.Labels, reg.Labels); !isEqual { + // If not, set `overwrite` to true, and will overwrite `.runner` after declaring successfully. + overwrite = true + reg.Labels = cfg.Runner.Labels } } @@ -78,10 +82,24 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, runner := run.NewRunner(cfg, reg, cli) // declare the labels of the runner before fetching tasks - err = runner.Declare(ctx, ls.Names()) - if err != nil { - return fmt.Errorf("failed to declare runner: %w", err) + resp, err := runner.Declare(ctx, ls.Names()) + if err != nil && connect.CodeOf(err) == connect.CodeUnimplemented { + // Gitea instance is older version. skip declare step. + log.Warn("Because the Gitea instance is an old version, skip declare labels and version.") + } else if err != nil { + log.WithError(err).Error("fail to invoke Declare") + return err + } else { + log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully", + resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) + if overwrite { + // overwrite .runner file + if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { + return fmt.Errorf("failed to save runner config: %w", err) + } + } } + poller := poll.New(cfg, cli, runner) poller.Poll(ctx) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index e6814af..efd850d 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -46,7 +46,6 @@ func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string) log.Warnf("Runner in user-mode.") } - log.Warnf("configFile: %s", *configFile) if regArgs.NoInteractive { if err := registerNoInteractive(*configFile, regArgs); err != nil { return err diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index 19bf989..47b7b50 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -216,15 +216,9 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report. return execErr } -func (r *Runner) Declare(ctx context.Context, labels []string) error { - resp, err := r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{ +func (r *Runner) Declare(ctx context.Context, labels []string) (*connect.Response[runnerv1.DeclareResponse], error) { + return r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{ Version: ver.Version(), Labels: labels, })) - if err != nil { - return err - } - log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully", - resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) - return nil } diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index 4f25470..e08ffda 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -27,8 +27,10 @@ runner: # The interval for fetching the job from the Gitea instance. fetch_interval: 2s # The labels of a runner are used to determine which jobs the runner can run, and how to run them. - # Please enter sequences (equivalent to lists or arrays). - labels: + # Like: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"] + # If it's empty when registering, will use default labels. + # If it's empty when execute `deamon`, will use labels in `.runner` file. + labels: [] cache: # Enable cache server to use actions/cache. diff --git a/internal/pkg/utils/utils.go b/internal/pkg/utils/utils.go new file mode 100644 index 0000000..c0a8e01 --- /dev/null +++ b/internal/pkg/utils/utils.go @@ -0,0 +1,23 @@ +package utils + +import "reflect" + +// AreStrSlicesElemsEqual determines whether elements in two slices are equal (ignoring order). +func AreStrSlicesElemsEqual(s1, s2 []string) bool { + set1 := make(map[string]struct{}) + set2 := make(map[string]struct{}) + + for _, elem := range s1 { + set1[elem] = struct{}{} + } + + for _, elem := range s2 { + set2[elem] = struct{}{} + } + + if len(set1) != len(set2) { + return false + } + + return reflect.DeepEqual(set1, set2) +} diff --git a/internal/pkg/utils/utils_test.go b/internal/pkg/utils/utils_test.go new file mode 100644 index 0000000..1084646 --- /dev/null +++ b/internal/pkg/utils/utils_test.go @@ -0,0 +1,41 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAreStrSlicesElemsEqual(t *testing.T) { + tests := []struct { + s1 []string + s2 []string + expect bool + }{ + { + s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, + s2: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, + expect: true, + }, + { + s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, + s2: []string{"macos-arm64:host", "windows-amd64:host"}, + expect: false, + }, + { + s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, + s2: []string{"windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye", "macos-arm64:host"}, + expect: true, + }, + } + + for _, tt := range tests { + t.Run("test", func(t *testing.T) { + actual := AreStrSlicesElemsEqual(tt.s1, tt.s2) + assert.Equal(t, actual, tt.expect) + }) + } +} -- 2.40.1 From e8873419aad7f825017ddd06a286d8cf7697902a Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 17:16:48 +0800 Subject: [PATCH 19/24] still pass version by request header --- internal/app/cmd/daemon.go | 2 ++ internal/app/cmd/register.go | 1 + internal/pkg/client/http.go | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 5508791..932d436 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -24,6 +24,7 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/envcheck" "gitea.com/gitea/act_runner/internal/pkg/labels" "gitea.com/gitea/act_runner/internal/pkg/utils" + "gitea.com/gitea/act_runner/internal/pkg/ver" ) func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { @@ -78,6 +79,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, cfg.Runner.Insecure, reg.UUID, reg.Token, + ver.Version(), ) runner := run.NewRunner(cfg, reg, cli) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index efd850d..d79c3b9 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -296,6 +296,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { cfg.Runner.Insecure, "", "", + ver.Version(), ) for { diff --git a/internal/pkg/client/http.go b/internal/pkg/client/http.go index 24284a8..bcd74ae 100644 --- a/internal/pkg/client/http.go +++ b/internal/pkg/client/http.go @@ -28,7 +28,7 @@ func getHttpClient(endpoint string, insecure bool) *http.Client { } // New returns a new runner client. -func New(endpoint string, insecure bool, uuid, token string, opts ...connect.ClientOption) *HTTPClient { +func New(endpoint string, insecure bool, uuid, token, version string, opts ...connect.ClientOption) *HTTPClient { baseURL := strings.TrimRight(endpoint, "/") + "/api/actions" opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { @@ -39,6 +39,10 @@ func New(endpoint string, insecure bool, uuid, token string, opts ...connect.Cli if token != "" { req.Header().Set(TokenHeader, token) } + // TODO: version will be removed from request header after Gitea 1.20 released. + if version != "" { + req.Header().Set(VersionHeader, version) + } return next(ctx, req) } }))) -- 2.40.1 From 8bbc2f22d554fdc3b82c87be2b99c0b30a5f0b5c Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 6 Jun 2023 17:25:01 +0800 Subject: [PATCH 20/24] vet --- internal/pkg/utils/utils.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/pkg/utils/utils.go b/internal/pkg/utils/utils.go index c0a8e01..3db52ce 100644 --- a/internal/pkg/utils/utils.go +++ b/internal/pkg/utils/utils.go @@ -1,3 +1,6 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package utils import "reflect" -- 2.40.1 From f3209257aeaccd8fe5fb5ecf8436326074de743f Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 8 Jun 2023 15:29:56 +0800 Subject: [PATCH 21/24] update --- internal/app/cmd/daemon.go | 21 +++++-------- internal/app/cmd/register.go | 14 ++++++--- internal/pkg/client/header.go | 5 +-- internal/pkg/config/config.example.yaml | 2 +- internal/pkg/labels/labels.go | 15 +++++++++ internal/pkg/utils/utils.go | 26 ---------------- internal/pkg/utils/utils_test.go | 41 ------------------------- 7 files changed, 36 insertions(+), 88 deletions(-) delete mode 100644 internal/pkg/utils/utils.go delete mode 100644 internal/pkg/utils/utils_test.go diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 932d436..619f99c 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -23,7 +23,6 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/config" "gitea.com/gitea/act_runner/internal/pkg/envcheck" "gitea.com/gitea/act_runner/internal/pkg/labels" - "gitea.com/gitea/act_runner/internal/pkg/utils" "gitea.com/gitea/act_runner/internal/pkg/ver" ) @@ -45,18 +44,13 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, return fmt.Errorf("failed to load registration file: %w", err) } - var overwrite bool + lbls := reg.Labels if len(cfg.Runner.Labels) > 0 { - // Determine if the labels in the `.runner` file are the same as the labels in config. - if isEqual := utils.AreStrSlicesElemsEqual(cfg.Runner.Labels, reg.Labels); !isEqual { - // If not, set `overwrite` to true, and will overwrite `.runner` after declaring successfully. - overwrite = true - reg.Labels = cfg.Runner.Labels - } + lbls = cfg.Runner.Labels } ls := labels.Labels{} - for _, l := range reg.Labels { + for _, l := range lbls { label, err := labels.Parse(l) if err != nil { log.WithError(err).Warnf("ignored invalid label %q", l) @@ -94,11 +88,10 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, } else { log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully", resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) - if overwrite { - // overwrite .runner file - if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { - return fmt.Errorf("failed to save runner config: %w", err) - } + // if declare successfully, overrides the labels in .runner file that are valid in config file. + reg.Labels = ls.ToStrings() + if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { + return fmt.Errorf("failed to save runner config: %w", err) } } diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index d79c3b9..c541290 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -265,12 +265,18 @@ func registerNoInteractive(configFile string, regArgs *registerArgs) error { if regArgs.Labels != "" { inputs.Labels = strings.Split(regArgs.Labels, ",") } - if len(cfg.Runner.Labels) > 0 { - if regArgs.Labels != "" { - log.Warnf("Labels from command will be ignored, use labels defined in config file.") + if configFile != "" { + if len(cfg.Runner.Labels) > 0 { + if regArgs.Labels != "" { + log.Warn("Labels from command will be ignored, use labels defined in config file.") + } + inputs.Labels = cfg.Runner.Labels + } else { + log.Error("Please specify labels in config, and re-regitser.") + return nil } - inputs.Labels = cfg.Runner.Labels } + if inputs.RunnerName == "" { inputs.RunnerName, _ = os.Hostname() log.Infof("Runner name is empty, use hostname '%s'.", inputs.RunnerName) diff --git a/internal/pkg/client/header.go b/internal/pkg/client/header.go index df8627a..24844fa 100644 --- a/internal/pkg/client/header.go +++ b/internal/pkg/client/header.go @@ -4,7 +4,8 @@ package client const ( - UUIDHeader = "x-runner-uuid" - TokenHeader = "x-runner-token" + UUIDHeader = "x-runner-uuid" + TokenHeader = "x-runner-token" + // Deprecated: could be removed after Gitea 1.20 released VersionHeader = "x-runner-version" ) diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index e08ffda..86a3998 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -28,7 +28,7 @@ runner: fetch_interval: 2s # The labels of a runner are used to determine which jobs the runner can run, and how to run them. # Like: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"] - # If it's empty when registering, will use default labels. + # If it's empty when registering, it will ask for inputting labels. # If it's empty when execute `deamon`, will use labels in `.runner` file. labels: [] diff --git a/internal/pkg/labels/labels.go b/internal/pkg/labels/labels.go index 5908700..8c38b14 100644 --- a/internal/pkg/labels/labels.go +++ b/internal/pkg/labels/labels.go @@ -90,3 +90,18 @@ func (l Labels) Names() []string { } return names } + +func (l Labels) ToStrings() []string { + ls := make([]string, 0, len(l)) + for _, label := range l { + lbl := label.Name + if label.Schema != "" { + lbl += ":" + label.Schema + if label.Arg != "" { + lbl += ":" + label.Arg + } + } + ls = append(ls, lbl) + } + return ls +} diff --git a/internal/pkg/utils/utils.go b/internal/pkg/utils/utils.go deleted file mode 100644 index 3db52ce..0000000 --- a/internal/pkg/utils/utils.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package utils - -import "reflect" - -// AreStrSlicesElemsEqual determines whether elements in two slices are equal (ignoring order). -func AreStrSlicesElemsEqual(s1, s2 []string) bool { - set1 := make(map[string]struct{}) - set2 := make(map[string]struct{}) - - for _, elem := range s1 { - set1[elem] = struct{}{} - } - - for _, elem := range s2 { - set2[elem] = struct{}{} - } - - if len(set1) != len(set2) { - return false - } - - return reflect.DeepEqual(set1, set2) -} diff --git a/internal/pkg/utils/utils_test.go b/internal/pkg/utils/utils_test.go deleted file mode 100644 index 1084646..0000000 --- a/internal/pkg/utils/utils_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package utils - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestAreStrSlicesElemsEqual(t *testing.T) { - tests := []struct { - s1 []string - s2 []string - expect bool - }{ - { - s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, - s2: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, - expect: true, - }, - { - s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, - s2: []string{"macos-arm64:host", "windows-amd64:host"}, - expect: false, - }, - { - s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"}, - s2: []string{"windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye", "macos-arm64:host"}, - expect: true, - }, - } - - for _, tt := range tests { - t.Run("test", func(t *testing.T) { - actual := AreStrSlicesElemsEqual(tt.s1, tt.s2) - assert.Equal(t, actual, tt.expect) - }) - } -} -- 2.40.1 From 1373c1e828403836ad002503c1c506a7a0d0206b Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Thu, 8 Jun 2023 15:35:50 +0800 Subject: [PATCH 22/24] comment --- internal/app/cmd/daemon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 619f99c..d9b1821 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -88,7 +88,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, } else { log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully", resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels) - // if declare successfully, overrides the labels in .runner file that are valid in config file. + // if declare successfully, override the labels in the.runner file with valid labels in the config file (if specified) reg.Labels = ls.ToStrings() if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { return fmt.Errorf("failed to save runner config: %w", err) -- 2.40.1 From 489d8df450b0d9a4171369bd6514fa1f60f4dfed Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 9 Jun 2023 11:16:15 +0800 Subject: [PATCH 23/24] do not exit if not specify labels in config --- internal/app/cmd/register.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index c541290..0a16aa2 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -262,19 +262,16 @@ func registerNoInteractive(configFile string, regArgs *registerArgs) error { Labels: defaultLabels, } regArgs.Labels = strings.TrimSpace(regArgs.Labels) + // command line flag. if regArgs.Labels != "" { inputs.Labels = strings.Split(regArgs.Labels, ",") } - if configFile != "" { - if len(cfg.Runner.Labels) > 0 { - if regArgs.Labels != "" { - log.Warn("Labels from command will be ignored, use labels defined in config file.") - } - inputs.Labels = cfg.Runner.Labels - } else { - log.Error("Please specify labels in config, and re-regitser.") - return nil + // specify labels in config file. + if len(cfg.Runner.Labels) > 0 { + if regArgs.Labels != "" { + log.Warn("Labels from command will be ignored, use labels defined in config file.") } + inputs.Labels = cfg.Runner.Labels } if inputs.RunnerName == "" { -- 2.40.1 From f24147ce0f39a5b8089c2c084a9c3cca3df9306d Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 13 Jun 2023 16:18:36 +0800 Subject: [PATCH 24/24] adapt to older version Gitea --- internal/app/cmd/register.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index 0c3e8b3..fcea74d 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -338,10 +338,11 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { } // register new runner. resp, err := cli.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ - Name: reg.Name, - Token: reg.Token, - Version: ver.Version(), - Labels: ls, + Name: reg.Name, + Token: reg.Token, + Version: ver.Version(), + AgentLabels: ls, // Could be removed after Gitea 1.20 + Labels: ls, })) if err != nil { log.WithError(err).Error("poller: cannot register new runner") -- 2.40.1