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 <gedong_1994@163.com>
Reviewed-on: https://gitea.com/gitea/act_runner/pulls/43
Reviewed-by: delvh <dev.lh@web.de>
Reviewed-by: Jason Song <i@wolfogre.com>
Co-authored-by: sillyguodong <sillyguodong@noreply.gitea.io>
Co-committed-by: sillyguodong <sillyguodong@noreply.gitea.io>
This commit is contained in:
sillyguodong 2023-03-13 18:57:35 +08:00 committed by Jason Song
parent ebcf341de7
commit 0d71463662
9 changed files with 24 additions and 13 deletions

View File

@ -60,7 +60,7 @@ builds:
flags: flags:
- -trimpath - -trimpath
ldflags: ldflags:
- -s -w - -s -w -X gitea.com/gitea/act_runner/cmd.version={{ .Version }}
binary: >- binary: >-
{{ .ProjectName }}- {{ .ProjectName }}-
{{- if .IsSnapshot }}{{ .Branch }}- {{- if .IsSnapshot }}{{ .Branch }}-
@ -93,4 +93,4 @@ snapshot:
nightly: nightly:
publish_release: false publish_release: false
name_template: "{{ .Branch }}" name_template: "{{ .Branch }}"

View File

@ -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_PACKAGE ?= src.techknowlogick.com/xgo@latest
XGO_VERSION := go-1.18.x XGO_VERSION := go-1.18.x
GXZ_PAGAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.10 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 LINUX_ARCHS ?= linux/amd64,linux/arm64
DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64 DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64
@ -49,7 +50,7 @@ else
ifneq ($(DRONE_BRANCH),) ifneq ($(DRONE_BRANCH),)
VERSION ?= $(subst release/v,,$(DRONE_BRANCH)) VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
else else
VERSION ?= master VERSION ?= main
endif endif
STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null) STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null)
@ -61,7 +62,7 @@ else
endif endif
TAGS ?= TAGS ?=
LDFLAGS ?= -X 'main.Version=$(VERSION)' LDFLAGS ?= -X "$(RUNNER_CMD_PACKAGE_PATH).version=$(RELASE_VERSION)"
all: build all: build

View File

@ -29,7 +29,7 @@ func getHttpClient(endpoint string, insecure bool) *http.Client {
} }
// New returns a new runner 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" baseURL := strings.TrimRight(endpoint, "/") + "/api/actions"
opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
@ -40,6 +40,9 @@ func New(endpoint string, insecure bool, uuid, token string, opts ...connect.Cli
if token != "" { if token != "" {
req.Header().Set(core.TokenHeader, token) req.Header().Set(core.TokenHeader, token)
} }
if runnerVersion != "" {
req.Header().Set(core.VersionHeader, runnerVersion)
}
return next(ctx, req) return next(ctx, req)
} }
}))) })))

View File

@ -10,7 +10,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const version = "0.1.5" // the version of act_runner
var version = "develop"
type globalArgs struct { type globalArgs struct {
EnvFile string EnvFile string
@ -23,7 +24,7 @@ func Execute(ctx context.Context) {
// ./act_runner // ./act_runner
rootCmd := &cobra.Command{ 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.", Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
Version: version, Version: version,

View File

@ -66,6 +66,7 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg
cfg.Client.Insecure, cfg.Client.Insecure,
cfg.Runner.UUID, cfg.Runner.UUID,
cfg.Runner.Token, cfg.Runner.Token,
version,
) )
runner := &runtime.Runner{ runner := &runtime.Runner{
@ -74,6 +75,7 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg
ForgeInstance: cfg.Client.Address, ForgeInstance: cfg.Client.Address,
Environ: cfg.Runner.Environ, Environ: cfg.Runner.Environ,
Labels: cfg.Runner.Labels, Labels: cfg.Runner.Labels,
Version: version,
CacheHandler: handler, CacheHandler: handler,
} }

View File

@ -274,7 +274,9 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error {
cli := client.New( cli := client.New(
inputs.InstanceAddr, inputs.InstanceAddr,
inputs.Insecure, inputs.Insecure,
"", "", "",
"",
version,
) )
for { for {

View File

@ -4,8 +4,9 @@
package core package core
const ( const (
UUIDHeader = "x-runner-uuid" UUIDHeader = "x-runner-uuid"
TokenHeader = "x-runner-token" TokenHeader = "x-runner-token"
VersionHeader = "x-runner-version"
) )
// Runner struct // Runner struct

View File

@ -16,6 +16,7 @@ import (
// Runner runs the pipeline. // Runner runs the pipeline.
type Runner struct { type Runner struct {
Machine string Machine string
Version string
ForgeInstance string ForgeInstance string
Environ map[string]string Environ map[string]string
Client client.Client Client client.Client
@ -30,7 +31,7 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
env[k] = v env[k] = v
} }
env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" 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 { func (s *Runner) platformPicker(labels []string) string {

View File

@ -115,7 +115,7 @@ func getToken(task *runnerv1.Task) string {
return token 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) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
_, exist := globalTaskMap.Load(task.Id) _, exist := globalTaskMap.Load(task.Id)
@ -144,7 +144,7 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task, runnerName string)
}() }()
reporter.RunDaemon() 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) workflowsPath, err := getWorkflowsPath(t.Input.repoDirectory)
if err != nil { if err != nil {