2023-02-28 13:44:46 +03:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-08-14 08:29:00 +03:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-24 06:55:52 +03:00
|
|
|
"strings"
|
2022-08-14 08:29:00 +03:00
|
|
|
|
2022-12-06 11:37:38 +03:00
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
2023-03-23 15:48:33 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-02-28 18:39:30 +03:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/artifactcache"
|
2022-08-14 08:29:00 +03:00
|
|
|
"gitea.com/gitea/act_runner/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner runs the pipeline.
|
|
|
|
type Runner struct {
|
2022-10-16 18:51:53 +03:00
|
|
|
Machine string
|
2023-03-13 13:57:35 +03:00
|
|
|
Version string
|
2022-10-16 18:51:53 +03:00
|
|
|
ForgeInstance string
|
|
|
|
Environ map[string]string
|
|
|
|
Client client.Client
|
2022-11-24 06:55:52 +03:00
|
|
|
Labels []string
|
2023-02-28 18:39:30 +03:00
|
|
|
CacheHandler *artifactcache.Handler
|
2022-08-14 08:29:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the pipeline stage.
|
2022-09-25 13:54:00 +03:00
|
|
|
func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
|
2023-02-28 18:39:30 +03:00
|
|
|
env := map[string]string{}
|
|
|
|
for k, v := range s.Environ {
|
|
|
|
env[k] = v
|
|
|
|
}
|
2023-03-24 12:55:13 +03:00
|
|
|
if s.CacheHandler != nil {
|
|
|
|
env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/"
|
|
|
|
}
|
2023-03-13 13:57:35 +03:00
|
|
|
return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version)
|
2022-11-24 06:55:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Runner) platformPicker(labels []string) string {
|
2023-03-23 15:48:33 +03:00
|
|
|
platforms := make(map[string]string, len(s.Labels))
|
2022-11-24 06:55:52 +03:00
|
|
|
for _, l := range s.Labels {
|
2023-03-23 15:48:33 +03:00
|
|
|
label, schema, arg, err := ParseLabel(l)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("invaid label %q: %v", l, err)
|
2023-01-27 15:42:02 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-11-24 06:55:52 +03:00
|
|
|
|
2023-03-23 15:48:33 +03:00
|
|
|
switch schema {
|
|
|
|
case "docker":
|
|
|
|
// TODO "//" will be ignored, maybe we should use 'ubuntu-18.04:docker:node:16-buster' instead
|
|
|
|
platforms[label] = strings.TrimPrefix(arg, "//")
|
|
|
|
case "host":
|
|
|
|
platforms[label] = "-self-hosted"
|
|
|
|
default:
|
|
|
|
// It should not happen, because ParseLabel has checked it.
|
2022-11-24 06:55:52 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range labels {
|
|
|
|
if v, ok := platforms[label]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support multiple labels
|
|
|
|
// like:
|
|
|
|
// ["ubuntu-22.04"] => "ubuntu:22.04"
|
|
|
|
// ["with-gpu"] => "linux:with-gpu"
|
|
|
|
// ["ubuntu-22.04", "with-gpu"] => "ubuntu:22.04_with-gpu"
|
|
|
|
|
2023-03-23 15:48:33 +03:00
|
|
|
// return default.
|
|
|
|
// So the runner receives a task with a label that the runner doesn't have,
|
|
|
|
// it happens when the user have edited the label of the runner in the web UI.
|
|
|
|
return "node:16-bullseye" // TODO: it may be not correct, what if the runner is used as host mode only?
|
2022-08-14 08:29:00 +03:00
|
|
|
}
|