From 4dff32ba95a3edfd0deb5818938772b18cddb7a9 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 4 Apr 2023 18:05:48 +0800 Subject: [PATCH] refacotr: ver --- .goreleaser.yaml | 2 +- Makefile | 3 +-- internal/app/cmd/cmd.go | 6 ++---- internal/app/cmd/daemon.go | 5 +++-- internal/app/cmd/register.go | 5 +++-- internal/app/run/runner.go | 19 +++++++++---------- internal/pkg/client/http.go | 6 +++--- internal/pkg/ver/version.go | 10 ++++++++++ 8 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 internal/pkg/ver/version.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 59eb868..8b82908 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -58,7 +58,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X gitea.com/gitea/act_runner/cmd.version={{ .Summary }} + - -s -w -X gitea.com/gitea/act_runner/internal/pkg/ver.version={{ .Summary }} binary: >- {{ .ProjectName }}- {{- .Version }}- diff --git a/Makefile b/Makefile index e7e0d39..139cc83 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" ) XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest XGO_VERSION := go-1.18.x GXZ_PAGAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.10 -RUNNER_CMD_PACKAGE_PATH := gitea.com/gitea/act_runner/cmd LINUX_ARCHS ?= linux/amd64,linux/arm64 DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64 @@ -63,7 +62,7 @@ else endif TAGS ?= -LDFLAGS ?= -X "$(RUNNER_CMD_PACKAGE_PATH).version=$(RELASE_VERSION)" +LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=$(RELASE_VERSION)" all: build diff --git a/internal/app/cmd/cmd.go b/internal/app/cmd/cmd.go index b2ac030..f6d2bda 100644 --- a/internal/app/cmd/cmd.go +++ b/internal/app/cmd/cmd.go @@ -11,18 +11,16 @@ import ( "github.com/spf13/cobra" "gitea.com/gitea/act_runner/internal/pkg/config" + "gitea.com/gitea/act_runner/internal/pkg/ver" ) -// the version of act_runner -var version = "develop" - func Execute(ctx context.Context) { // ./act_runner rootCmd := &cobra.Command{ Use: "act_runner [event name to run]\nIf no event name passed, will default to \"on: push\"", Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.", Args: cobra.MaximumNArgs(1), - Version: version, + Version: ver.Version(), SilenceUsage: true, } configFile := "" diff --git a/internal/app/cmd/daemon.go b/internal/app/cmd/daemon.go index 37436c2..a648d64 100644 --- a/internal/app/cmd/daemon.go +++ b/internal/app/cmd/daemon.go @@ -18,6 +18,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/ver" ) func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { @@ -63,10 +64,10 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, cfg.Runner.Insecure, reg.UUID, reg.Token, - version, + ver.Version(), ) - runner := run.NewRunner(cfg, reg, cli, version) + runner := run.NewRunner(cfg, reg, cli) poller := poll.New(cfg, cli, runner) poller.Poll(ctx) diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index 37a94bf..51318e8 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -23,6 +23,7 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/client" "gitea.com/gitea/act_runner/internal/pkg/config" "gitea.com/gitea/act_runner/internal/pkg/labels" + "gitea.com/gitea/act_runner/internal/pkg/ver" ) // runRegister registers a runner to the server @@ -37,7 +38,7 @@ func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string) log.SetLevel(log.DebugLevel) log.Infof("Registering runner, arch=%s, os=%s, version=%s.", - goruntime.GOARCH, goruntime.GOOS, version) + goruntime.GOARCH, goruntime.GOOS, ver.Version()) // runner always needs root permission if os.Getuid() != 0 { @@ -272,7 +273,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { cfg.Runner.Insecure, "", "", - version, + ver.Version(), ) for { diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index 8c53d5c..7a7dfb8 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -23,12 +23,12 @@ import ( "gitea.com/gitea/act_runner/internal/pkg/config" "gitea.com/gitea/act_runner/internal/pkg/labels" "gitea.com/gitea/act_runner/internal/pkg/report" + "gitea.com/gitea/act_runner/internal/pkg/ver" ) // Runner runs the pipeline. type Runner struct { - name string - version string + name string cfg *config.Config @@ -39,7 +39,7 @@ type Runner struct { runningTasks sync.Map } -func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client, version string) *Runner { +func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client) *Runner { ls := labels.Labels{} for _, v := range reg.Labels { if l, err := labels.Parse(v); err == nil { @@ -61,12 +61,11 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client, } return &Runner{ - name: reg.Name, - version: version, - cfg: cfg, - client: cli, - labels: ls, - envs: envs, + name: reg.Name, + cfg: cfg, + client: cli, + labels: ls, + envs: envs, } } @@ -96,7 +95,7 @@ func (r *Runner) Run(ctx context.Context, task *runnerv1.Task) error { } func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.Reporter) error { - reporter.Logf("%s(version:%s) received task %v of job %v, be triggered by event: %s", r.name, r.version, task.Id, task.Context.Fields["job"].GetStringValue(), task.Context.Fields["event_name"].GetStringValue()) + reporter.Logf("%s(version:%s) received task %v of job %v, be triggered by event: %s", r.name, ver.Version(), task.Id, task.Context.Fields["job"].GetStringValue(), task.Context.Fields["event_name"].GetStringValue()) workflow, err := model.ReadWorkflow(bytes.NewReader(task.WorkflowPayload)) if err != nil { diff --git a/internal/pkg/client/http.go b/internal/pkg/client/http.go index fc374f4..cc0c44e 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, runnerVersion 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,8 +39,8 @@ func New(endpoint string, insecure bool, uuid, token, runnerVersion string, opts if token != "" { req.Header().Set(TokenHeader, token) } - if runnerVersion != "" { - req.Header().Set(VersionHeader, runnerVersion) + if version != "" { + req.Header().Set(VersionHeader, version) } return next(ctx, req) } diff --git a/internal/pkg/ver/version.go b/internal/pkg/ver/version.go new file mode 100644 index 0000000..395c155 --- /dev/null +++ b/internal/pkg/ver/version.go @@ -0,0 +1,10 @@ +package ver + +var ( + // go build -ldflags "-X gitea.com/gitea/act_runner/internal/pkg/ver.version=1.2.3" + version = "dev" +) + +func Version() string { + return version +}