2023-02-28 11:44:46 +01:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-08-14 07:29:00 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-24 04:55:52 +01:00
|
|
|
"strings"
|
2022-08-14 07:29:00 +02:00
|
|
|
|
2022-12-06 09:37:38 +01:00
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
2023-02-28 16:39:30 +01:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/artifactcache"
|
2022-08-14 07:29:00 +02:00
|
|
|
"gitea.com/gitea/act_runner/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner runs the pipeline.
|
|
|
|
type Runner struct {
|
2022-10-16 17:51:53 +02:00
|
|
|
Machine string
|
|
|
|
ForgeInstance string
|
|
|
|
Environ map[string]string
|
|
|
|
Client client.Client
|
2022-11-24 04:55:52 +01:00
|
|
|
Labels []string
|
2023-02-28 16:39:30 +01:00
|
|
|
CacheHandler *artifactcache.Handler
|
2022-08-14 07:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the pipeline stage.
|
2022-09-25 12:54:00 +02:00
|
|
|
func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
|
2023-02-28 16:39:30 +01:00
|
|
|
env := map[string]string{}
|
|
|
|
for k, v := range s.Environ {
|
|
|
|
env[k] = v
|
|
|
|
}
|
|
|
|
env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/"
|
2023-03-06 11:42:07 +01:00
|
|
|
return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine)
|
2022-11-24 04:55:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Runner) platformPicker(labels []string) string {
|
|
|
|
// "ubuntu-18.04:docker://node:16-buster"
|
2023-01-27 13:42:02 +01:00
|
|
|
// "self-hosted"
|
2022-11-24 04:55:52 +01:00
|
|
|
|
|
|
|
platforms := make(map[string]string, len(labels))
|
|
|
|
for _, l := range s.Labels {
|
|
|
|
// "ubuntu-18.04:docker://node:16-buster"
|
|
|
|
splits := strings.SplitN(l, ":", 2)
|
2023-01-27 13:42:02 +01:00
|
|
|
if len(splits) == 1 {
|
|
|
|
// identifier for non docker execution environment
|
|
|
|
platforms[splits[0]] = "-self-hosted"
|
|
|
|
continue
|
|
|
|
}
|
2022-11-24 04:55:52 +01:00
|
|
|
// ["ubuntu-18.04", "docker://node:16-buster"]
|
|
|
|
k, v := splits[0], splits[1]
|
|
|
|
|
|
|
|
if prefix := "docker://"; !strings.HasPrefix(v, prefix) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
v = strings.TrimPrefix(v, prefix)
|
|
|
|
}
|
|
|
|
// ubuntu-18.04 => node:16-buster
|
|
|
|
platforms[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
// return default
|
|
|
|
return "node:16-bullseye"
|
2022-08-14 07:29:00 +02:00
|
|
|
}
|