2022-08-23 14:34:47 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2022-09-25 12:54:00 +02:00
|
|
|
"bytes"
|
2022-08-23 14:34:47 +02:00
|
|
|
"context"
|
2022-09-25 12:54:00 +02:00
|
|
|
"encoding/json"
|
2022-08-23 14:34:47 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-09-03 09:57:53 +02:00
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-09-03 14:57:32 +02:00
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
|
|
|
|
2022-08-23 14:34:47 +02:00
|
|
|
"github.com/nektos/act/pkg/artifacts"
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
"github.com/nektos/act/pkg/runner"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TaskInput struct {
|
|
|
|
repoDirectory string
|
|
|
|
actor string
|
|
|
|
// workdir string
|
|
|
|
// workflowsPath string
|
|
|
|
// autodetectEvent bool
|
|
|
|
// eventPath string
|
|
|
|
reuseContainers bool
|
|
|
|
bindWorkdir bool
|
|
|
|
// secrets []string
|
|
|
|
// envs []string
|
|
|
|
// platforms []string
|
|
|
|
// dryrun bool
|
|
|
|
forcePull bool
|
|
|
|
forceRebuild bool
|
|
|
|
// noOutput bool
|
|
|
|
// envfile string
|
|
|
|
// secretfile string
|
|
|
|
insecureSecrets bool
|
|
|
|
// defaultBranch string
|
|
|
|
privileged bool
|
|
|
|
usernsMode string
|
|
|
|
containerArchitecture string
|
|
|
|
containerDaemonSocket string
|
|
|
|
// noWorkflowRecurse bool
|
|
|
|
useGitIgnore bool
|
|
|
|
containerCapAdd []string
|
|
|
|
containerCapDrop []string
|
|
|
|
autoRemove bool
|
|
|
|
artifactServerPath string
|
|
|
|
artifactServerPort string
|
|
|
|
jsonLogger bool
|
|
|
|
noSkipCheckout bool
|
|
|
|
// remoteName string
|
|
|
|
|
|
|
|
ForgeInstance string
|
|
|
|
EnvFile string
|
|
|
|
}
|
|
|
|
|
2022-09-03 09:57:53 +02:00
|
|
|
type TaskState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TaskStateUnknown is the default state
|
|
|
|
TaskStateUnknown TaskState = iota
|
|
|
|
// TaskStatePending is the pending state
|
|
|
|
// pending means task is received, parsing actions and preparing to run
|
|
|
|
TaskStatePending
|
|
|
|
// TaskStateRunning is the state when the task is running
|
|
|
|
// running means task is running
|
|
|
|
TaskStateRunning
|
|
|
|
// TaskStateSuccess is the state when the task is successful
|
|
|
|
// success means task is successful without any error
|
|
|
|
TaskStateSuccess
|
|
|
|
// TaskStateFailure is the state when the task is failed
|
|
|
|
// failure means task is failed with error
|
|
|
|
TaskStateFailure
|
|
|
|
)
|
|
|
|
|
2022-08-23 14:34:47 +02:00
|
|
|
type Task struct {
|
2022-09-03 09:57:53 +02:00
|
|
|
BuildID int64
|
2022-08-23 14:34:47 +02:00
|
|
|
Input *TaskInput
|
2022-09-03 09:57:53 +02:00
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
state TaskState
|
|
|
|
client client.Client
|
|
|
|
log *log.Entry
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
// NewTask creates a new task
|
2022-09-03 14:57:32 +02:00
|
|
|
func NewTask(buildID int64, client client.Client) *Task {
|
2022-08-23 14:34:47 +02:00
|
|
|
task := &Task{
|
|
|
|
Input: &TaskInput{
|
|
|
|
reuseContainers: true,
|
|
|
|
ForgeInstance: "gitea",
|
|
|
|
},
|
2022-09-03 09:57:53 +02:00
|
|
|
BuildID: buildID,
|
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
state: TaskStatePending,
|
|
|
|
client: client,
|
|
|
|
log: log.WithField("buildID", buildID),
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
task.Input.repoDirectory, _ = os.Getwd()
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWorkflowsPath return the workflows directory, it will try .gitea first and then fallback to .github
|
|
|
|
func getWorkflowsPath(dir string) (string, error) {
|
|
|
|
p := filepath.Join(dir, ".gitea/workflows")
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, ".github/workflows"), nil
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func demoPlatforms() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"ubuntu-latest": "node:16-buster-slim",
|
|
|
|
"ubuntu-20.04": "node:16-buster-slim",
|
|
|
|
"ubuntu-18.04": "node:16-buster-slim",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error {
|
|
|
|
_, exist := globalTaskMap.Load(task.Id)
|
2022-09-04 09:44:29 +02:00
|
|
|
if exist {
|
2022-09-25 12:54:00 +02:00
|
|
|
return fmt.Errorf("task %d already exists", task.Id)
|
2022-09-04 09:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set task ve to global map
|
|
|
|
// when task is done or canceled, it will be removed from the map
|
2022-09-25 12:54:00 +02:00
|
|
|
globalTaskMap.Store(task.Id, t)
|
|
|
|
defer globalTaskMap.Delete(task.Id)
|
|
|
|
|
|
|
|
lastWords := ""
|
|
|
|
reporter := NewReporter(ctx, t.client, task.Id)
|
|
|
|
defer func() {
|
|
|
|
_ = reporter.Close(lastWords)
|
|
|
|
}()
|
|
|
|
reporter.RunDaemon()
|
|
|
|
|
|
|
|
reporter.Logf("received task %v of job %v", task.Id, task.Context.Fields["job"].GetStringValue())
|
2022-09-04 09:44:29 +02:00
|
|
|
|
2022-08-23 14:34:47 +02:00
|
|
|
workflowsPath, err := getWorkflowsPath(t.Input.repoDirectory)
|
|
|
|
if err != nil {
|
2022-09-25 12:54:00 +02:00
|
|
|
lastWords = err.Error()
|
2022-09-04 09:44:29 +02:00
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
2022-09-03 09:57:53 +02:00
|
|
|
t.log.Debugf("workflows path: %s", workflowsPath)
|
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
workflow, err := model.ReadWorkflow(bytes.NewReader(task.WorkflowPayload))
|
2022-08-23 14:34:47 +02:00
|
|
|
if err != nil {
|
2022-09-25 12:54:00 +02:00
|
|
|
lastWords = err.Error()
|
2022-09-04 09:44:29 +02:00
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var plan *model.Plan
|
2022-09-25 12:54:00 +02:00
|
|
|
if jobIDs := workflow.GetJobIDs(); len(jobIDs) != 1 {
|
|
|
|
err := fmt.Errorf("multiple jobs fould: %v", jobIDs)
|
|
|
|
lastWords = err.Error()
|
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
} else {
|
2022-09-25 12:54:00 +02:00
|
|
|
jobID := jobIDs[0]
|
|
|
|
plan = model.CombineWorkflowPlanner(workflow).PlanJob(jobID)
|
|
|
|
|
|
|
|
job := workflow.GetJob(jobID)
|
|
|
|
reporter.ResetSteps(len(job.Steps))
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 12:54:00 +02:00
|
|
|
log.Infof("plan: %+v", plan.Stages[0].Runs)
|
|
|
|
|
2022-08-23 14:34:47 +02:00
|
|
|
curDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
2022-09-25 12:54:00 +02:00
|
|
|
lastWords = err.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dataContext := task.Context.Fields
|
|
|
|
preset := &model.GithubContext{
|
|
|
|
Event: dataContext["event"].GetStructValue().AsMap(),
|
|
|
|
RunID: dataContext["run_id"].GetStringValue(),
|
|
|
|
RunNumber: dataContext["run_number"].GetStringValue(),
|
|
|
|
Actor: dataContext["actor"].GetStringValue(),
|
|
|
|
Repository: dataContext["repository"].GetStringValue(),
|
|
|
|
EventName: dataContext["event_name"].GetStringValue(),
|
|
|
|
Sha: dataContext["sha"].GetStringValue(),
|
|
|
|
Ref: dataContext["ref"].GetStringValue(),
|
|
|
|
RefName: dataContext["ref_name"].GetStringValue(),
|
|
|
|
RefType: dataContext["ref_type"].GetStringValue(),
|
|
|
|
HeadRef: dataContext["head_ref"].GetStringValue(),
|
|
|
|
BaseRef: dataContext["base_ref"].GetStringValue(),
|
|
|
|
Token: dataContext["token"].GetStringValue(),
|
|
|
|
RepositoryOwner: dataContext["repository_owner"].GetStringValue(),
|
|
|
|
RetentionDays: dataContext["retention_days"].GetStringValue(),
|
|
|
|
}
|
|
|
|
eventJSON, err := json.Marshal(preset.Event)
|
|
|
|
if err != nil {
|
|
|
|
lastWords = err.Error()
|
2022-09-04 09:44:29 +02:00
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
input := t.Input
|
|
|
|
config := &runner.Config{
|
2022-09-25 12:54:00 +02:00
|
|
|
Workdir: curDir, // TODO: temp dir?
|
|
|
|
BindWorkdir: input.bindWorkdir,
|
|
|
|
ReuseContainers: input.reuseContainers,
|
|
|
|
ForcePull: input.forcePull,
|
|
|
|
ForceRebuild: input.forceRebuild,
|
|
|
|
LogOutput: true,
|
|
|
|
JSONLogger: input.jsonLogger,
|
|
|
|
Secrets: task.Secrets,
|
2022-08-23 14:34:47 +02:00
|
|
|
InsecureSecrets: input.insecureSecrets,
|
2022-09-25 12:54:00 +02:00
|
|
|
Platforms: demoPlatforms(), // TODO: supported platforms
|
2022-08-23 14:34:47 +02:00
|
|
|
Privileged: input.privileged,
|
|
|
|
UsernsMode: input.usernsMode,
|
|
|
|
ContainerArchitecture: input.containerArchitecture,
|
|
|
|
ContainerDaemonSocket: input.containerDaemonSocket,
|
|
|
|
UseGitIgnore: input.useGitIgnore,
|
|
|
|
GitHubInstance: input.ForgeInstance,
|
|
|
|
ContainerCapAdd: input.containerCapAdd,
|
|
|
|
ContainerCapDrop: input.containerCapDrop,
|
|
|
|
AutoRemove: input.autoRemove,
|
|
|
|
ArtifactServerPath: input.artifactServerPath,
|
|
|
|
ArtifactServerPort: input.artifactServerPort,
|
|
|
|
NoSkipCheckout: input.noSkipCheckout,
|
2022-09-25 12:54:00 +02:00
|
|
|
PresetGitHubContext: preset,
|
|
|
|
EventJSON: string(eventJSON),
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
r, err := runner.New(config)
|
|
|
|
if err != nil {
|
2022-09-25 12:54:00 +02:00
|
|
|
lastWords = err.Error()
|
2022-09-04 09:44:29 +02:00
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel := artifacts.Serve(ctx, input.artifactServerPath, input.artifactServerPort)
|
2022-09-03 09:57:53 +02:00
|
|
|
t.log.Debugf("artifacts server started at %s:%s", input.artifactServerPath, input.artifactServerPort)
|
2022-08-23 14:34:47 +02:00
|
|
|
|
|
|
|
executor := r.NewPlanExecutor(plan).Finally(func(ctx context.Context) error {
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-09-03 09:57:53 +02:00
|
|
|
t.log.Infof("workflow prepared")
|
2022-09-25 12:54:00 +02:00
|
|
|
reporter.Logf("workflow prepared")
|
2022-09-03 09:57:53 +02:00
|
|
|
|
|
|
|
// add logger recorders
|
2022-09-25 12:54:00 +02:00
|
|
|
ctx = common.WithLoggerHook(ctx, reporter)
|
2022-09-03 09:57:53 +02:00
|
|
|
|
2022-08-23 14:34:47 +02:00
|
|
|
if err := executor(ctx); err != nil {
|
2022-09-25 12:54:00 +02:00
|
|
|
lastWords = err.Error()
|
2022-09-04 09:44:29 +02:00
|
|
|
return err
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|
2022-09-03 09:57:53 +02:00
|
|
|
|
2022-09-04 09:44:29 +02:00
|
|
|
return nil
|
2022-08-23 14:34:47 +02:00
|
|
|
}
|