From 0445675a107a1f7c3a8fa17c14740aa198481ba5 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 13 Mar 2023 18:57:35 +0800 Subject: [PATCH] Inject version when building and report version to Gitea via log and header (#43) close #42 1. Inject runner version when `make build` After building, executing command line: `./act_runner -v` or `./act_runner --version`, the version of runner is printed. ![image](/attachments/e25efbd3-79b3-49a5-b93f-42646d42c707) 2. In `Actions` UI: ![image](/attachments/36c57470-2a1d-4796-9eb0-de3988ab88e1) 3. Set request header in http client interceptor. Co-authored-by: sillyguodong Reviewed-on: https://gitea.com/gitea/act_runner/pulls/43 Reviewed-by: delvh Reviewed-by: Jason Song Co-authored-by: sillyguodong Co-committed-by: sillyguodong --- Makefile | 5 +++-- client/http.go | 5 ++++- cmd/cmd.go | 5 +++-- cmd/daemon.go | 2 ++ cmd/register.go | 4 +++- core/runner.go | 5 +++-- runtime/runtime.go | 3 ++- runtime/task.go | 4 ++-- 8 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 80ff590..f3ddda8 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ 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 := codeberg.org/forgejo/runner/cmd LINUX_ARCHS ?= linux/amd64,linux/arm64 DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64 @@ -49,7 +50,7 @@ else ifneq ($(DRONE_BRANCH),) VERSION ?= $(subst release/v,,$(DRONE_BRANCH)) else - VERSION ?= master + VERSION ?= main endif STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null) @@ -61,7 +62,7 @@ else endif TAGS ?= -LDFLAGS ?= -X 'main.Version=$(VERSION)' +LDFLAGS ?= -X "$(RUNNER_CMD_PACKAGE_PATH).version=$(RELASE_VERSION)" all: build diff --git a/client/http.go b/client/http.go index 0f08f81..0aed490 100644 --- a/client/http.go +++ b/client/http.go @@ -26,7 +26,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, runnerVersion string, opts ...connect.ClientOption) *HTTPClient { baseURL := strings.TrimRight(endpoint, "/") + "/api/actions" opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { @@ -37,6 +37,9 @@ func New(endpoint string, insecure bool, uuid, token string, opts ...connect.Cli if token != "" { req.Header().Set(core.TokenHeader, token) } + if runnerVersion != "" { + req.Header().Set(core.VersionHeader, runnerVersion) + } return next(ctx, req) } }))) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6c1cc6a..3a45f9e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -7,7 +7,8 @@ import ( "github.com/spf13/cobra" ) -const version = "0.1.5" +// the version of act_runner +var version = "develop" type globalArgs struct { EnvFile string @@ -20,7 +21,7 @@ func Execute(ctx context.Context) { // ./act_runner rootCmd := &cobra.Command{ - Use: "act [event name to run]\nIf no event name passed, will default to \"on: push\"", + 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, diff --git a/cmd/daemon.go b/cmd/daemon.go index c3e7bd8..fafeb04 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -63,6 +63,7 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg cfg.Client.Insecure, cfg.Runner.UUID, cfg.Runner.Token, + version, ) runner := &runtime.Runner{ @@ -71,6 +72,7 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg ForgeInstance: cfg.Client.Address, Environ: cfg.Runner.Environ, Labels: cfg.Runner.Labels, + Version: version, CacheHandler: handler, } diff --git a/cmd/register.go b/cmd/register.go index 4ed791e..7bd5662 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -271,7 +271,9 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { cli := client.New( inputs.InstanceAddr, inputs.Insecure, - "", "", + "", + "", + version, ) for { diff --git a/core/runner.go b/core/runner.go index da58835..2975ca5 100644 --- a/core/runner.go +++ b/core/runner.go @@ -1,8 +1,9 @@ package core const ( - UUIDHeader = "x-runner-uuid" - TokenHeader = "x-runner-token" + UUIDHeader = "x-runner-uuid" + TokenHeader = "x-runner-token" + VersionHeader = "x-runner-version" ) // Runner struct diff --git a/runtime/runtime.go b/runtime/runtime.go index f23eed0..c5a9118 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -12,6 +12,7 @@ import ( // Runner runs the pipeline. type Runner struct { Machine string + Version string ForgeInstance string Environ map[string]string Client client.Client @@ -26,7 +27,7 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { env[k] = v } env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" - return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine) + return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version) } func (s *Runner) platformPicker(labels []string) string { diff --git a/runtime/task.go b/runtime/task.go index ac15323..af90b69 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -112,7 +112,7 @@ func getToken(task *runnerv1.Task) string { return token } -func (t *Task) Run(ctx context.Context, task *runnerv1.Task, runnerName string) (lastErr error) { +func (t *Task) Run(ctx context.Context, task *runnerv1.Task, runnerName, runnerVersion string) (lastErr error) { ctx, cancel := context.WithCancel(ctx) defer cancel() _, exist := globalTaskMap.Load(task.Id) @@ -141,7 +141,7 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task, runnerName string) }() reporter.RunDaemon() - reporter.Logf("%s received task %v of job %v", runnerName, task.Id, task.Context.Fields["job"].GetStringValue()) + reporter.Logf("%s(version:%s) received task %v of job %v", runnerName, runnerVersion, task.Id, task.Context.Fields["job"].GetStringValue()) workflowsPath, err := getWorkflowsPath(t.Input.repoDirectory) if err != nil {