2020-02-04 18:38:41 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-02-11 11:10:35 -06:00
|
|
|
"context"
|
2020-02-17 12:25:28 -06:00
|
|
|
"fmt"
|
2022-10-29 12:15:38 -05:00
|
|
|
"os"
|
2020-02-04 18:38:41 -06:00
|
|
|
|
2022-03-22 14:26:10 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-02-04 18:38:41 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2022-03-22 14:26:10 -05:00
|
|
|
"github.com/nektos/act/pkg/container"
|
2020-02-04 18:38:41 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner provides capabilities to run GitHub actions
|
|
|
|
type Runner interface {
|
2020-02-07 00:17:58 -06:00
|
|
|
NewPlanExecutor(plan *model.Plan) common.Executor
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains the config for a new runner
|
|
|
|
type Config struct {
|
2022-06-21 08:52:21 -05:00
|
|
|
Actor string // the user that triggered the event
|
|
|
|
Workdir string // path to working directory
|
|
|
|
BindWorkdir bool // bind the workdir to the job container
|
|
|
|
EventName string // name of event to run
|
|
|
|
EventPath string // path to JSON file to use for event.json in containers
|
|
|
|
DefaultBranch string // name of the main branch for this repository
|
|
|
|
ReuseContainers bool // reuse containers to maintain state
|
|
|
|
ForcePull bool // force pulling of the image, even if already present
|
|
|
|
ForceRebuild bool // force rebuilding local docker image action
|
|
|
|
LogOutput bool // log the output from docker run
|
|
|
|
JSONLogger bool // use json or text logger
|
|
|
|
Env map[string]string // env for containers
|
|
|
|
Secrets map[string]string // list of secrets
|
|
|
|
Token string // GitHub token
|
|
|
|
InsecureSecrets bool // switch hiding output when printing to terminal
|
|
|
|
Platforms map[string]string // list of platforms
|
|
|
|
Privileged bool // use privileged mode
|
|
|
|
UsernsMode string // user namespace to use
|
|
|
|
ContainerArchitecture string // Desired OS/architecture platform for running containers
|
|
|
|
ContainerDaemonSocket string // Path to Docker daemon socket
|
2022-12-06 09:58:47 -06:00
|
|
|
ContainerOptions string // Options for the job container
|
2022-06-21 08:52:21 -05:00
|
|
|
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
|
|
|
|
GitHubInstance string // GitHub instance to use, default "github.com"
|
|
|
|
ContainerCapAdd []string // list of kernel capabilities to add to the containers
|
|
|
|
ContainerCapDrop []string // list of kernel capabilities to remove from the containers
|
|
|
|
AutoRemove bool // controls if the container is automatically removed upon workflow completion
|
|
|
|
ArtifactServerPath string // the path where the artifact server stores uploads
|
|
|
|
ArtifactServerPort string // the port the artifact server binds to
|
|
|
|
NoSkipCheckout bool // do not skip actions/checkout
|
|
|
|
RemoteName string // remote name in local git repo config
|
|
|
|
ReplaceGheActionWithGithubCom []string // Use actions from GitHub Enterprise instance to GitHub
|
|
|
|
ReplaceGheActionTokenWithGithubCom string // Token of private action repo on GitHub.
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2022-12-15 10:45:22 -06:00
|
|
|
type caller struct {
|
|
|
|
runContext *RunContext
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:38:41 -06:00
|
|
|
type runnerImpl struct {
|
|
|
|
config *Config
|
|
|
|
eventJSON string
|
2022-12-15 10:45:22 -06:00
|
|
|
caller *caller // the job calling this runner (caller of a reusable workflow)
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
// New Creates a new Runner
|
|
|
|
func New(runnerConfig *Config) (Runner, error) {
|
2020-02-04 18:38:41 -06:00
|
|
|
runner := &runnerImpl{
|
|
|
|
config: runnerConfig,
|
|
|
|
}
|
|
|
|
|
2022-12-15 10:45:22 -06:00
|
|
|
return runner.configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (runner *runnerImpl) configure() (Runner, error) {
|
2020-02-04 18:38:41 -06:00
|
|
|
runner.eventJSON = "{}"
|
2022-12-15 10:45:22 -06:00
|
|
|
if runner.config.EventPath != "" {
|
2020-02-04 18:38:41 -06:00
|
|
|
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
2022-10-29 12:15:38 -05:00
|
|
|
eventJSONBytes, err := os.ReadFile(runner.config.EventPath)
|
2020-02-04 18:38:41 -06:00
|
|
|
if err != nil {
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil, err
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
runner.eventJSON = string(eventJSONBytes)
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
return runner, nil
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:20:45 -05:00
|
|
|
// NewPlanExecutor ...
|
2020-02-07 00:17:58 -06:00
|
|
|
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
2020-02-27 01:29:43 -06:00
|
|
|
maxJobNameLen := 0
|
2020-02-17 12:25:28 -06:00
|
|
|
|
2022-02-15 10:35:02 -06:00
|
|
|
stagePipeline := make([]common.Executor, 0)
|
|
|
|
for i := range plan.Stages {
|
|
|
|
stage := plan.Stages[i]
|
|
|
|
stagePipeline = append(stagePipeline, func(ctx context.Context) error {
|
|
|
|
pipeline := make([]common.Executor, 0)
|
2022-12-06 09:45:06 -06:00
|
|
|
for _, run := range stage.Runs {
|
2022-02-25 12:47:16 -06:00
|
|
|
stageExecutor := make([]common.Executor, 0)
|
2022-02-15 10:35:02 -06:00
|
|
|
job := run.Job()
|
2022-03-30 12:20:45 -05:00
|
|
|
|
2022-02-15 10:35:02 -06:00
|
|
|
if job.Strategy != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
strategyRc := runner.newRunContext(ctx, run, nil)
|
|
|
|
if err := strategyRc.NewExpressionEvaluator(ctx).EvaluateYamlNode(ctx, &job.Strategy.RawMatrix); err != nil {
|
2022-02-15 10:35:02 -06:00
|
|
|
log.Errorf("Error while evaluating matrix: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matrixes := job.GetMatrixes()
|
|
|
|
maxParallel := 4
|
|
|
|
if job.Strategy != nil {
|
|
|
|
maxParallel = job.Strategy.MaxParallel
|
2020-02-27 01:29:43 -06:00
|
|
|
}
|
2022-02-15 10:35:02 -06:00
|
|
|
|
|
|
|
if len(matrixes) < maxParallel {
|
|
|
|
maxParallel = len(matrixes)
|
2020-02-27 01:29:43 -06:00
|
|
|
}
|
2022-02-15 10:35:02 -06:00
|
|
|
|
|
|
|
for i, matrix := range matrixes {
|
2022-10-05 17:13:00 -05:00
|
|
|
matrix := matrix
|
2022-06-17 10:55:21 -05:00
|
|
|
rc := runner.newRunContext(ctx, run, matrix)
|
2022-02-15 10:35:02 -06:00
|
|
|
rc.JobName = rc.Name
|
|
|
|
if len(matrixes) > 1 {
|
|
|
|
rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1)
|
|
|
|
}
|
|
|
|
if len(rc.String()) > maxJobNameLen {
|
|
|
|
maxJobNameLen = len(rc.String())
|
|
|
|
}
|
|
|
|
stageExecutor = append(stageExecutor, func(ctx context.Context) error {
|
|
|
|
jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String())
|
2022-12-06 09:45:06 -06:00
|
|
|
return rc.Executor()(common.WithJobErrorContainer(WithJobLogger(ctx, rc.Run.JobID, jobName, rc.Config, &rc.Masks, matrix)))
|
2022-02-15 10:35:02 -06:00
|
|
|
})
|
2021-09-26 11:21:12 -05:00
|
|
|
}
|
2022-02-25 12:47:16 -06:00
|
|
|
pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...))
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
2022-03-22 14:26:10 -05:00
|
|
|
var ncpu int
|
|
|
|
info, err := container.GetHostInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to obtain container engine info: %s", err)
|
|
|
|
ncpu = 1 // sane default?
|
|
|
|
} else {
|
|
|
|
ncpu = info.NCPU
|
|
|
|
}
|
|
|
|
return common.NewParallelExecutor(ncpu, pipeline...)(ctx)
|
2022-02-15 10:35:02 -06:00
|
|
|
})
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2022-02-15 10:35:02 -06:00
|
|
|
return common.NewPipelineExecutor(stagePipeline...).Then(handleFailure(plan))
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleFailure(plan *model.Plan) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
for _, stage := range plan.Stages {
|
|
|
|
for _, run := range stage.Runs {
|
|
|
|
if run.Job().Result == "failure" {
|
|
|
|
return fmt.Errorf("Job '%s' failed", run.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func (runner *runnerImpl) newRunContext(ctx context.Context, run *model.Run, matrix map[string]interface{}) *RunContext {
|
2020-02-23 17:01:25 -06:00
|
|
|
rc := &RunContext{
|
|
|
|
Config: runner.config,
|
|
|
|
Run: run,
|
|
|
|
EventJSON: runner.eventJSON,
|
2021-12-22 13:52:09 -06:00
|
|
|
StepResults: make(map[string]*model.StepResult),
|
2020-02-23 17:01:25 -06:00
|
|
|
Matrix: matrix,
|
2022-12-15 10:45:22 -06:00
|
|
|
caller: runner.caller,
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
|
|
|
rc.Name = rc.ExprEval.Interpolate(ctx, run.String())
|
2022-12-15 10:45:22 -06:00
|
|
|
|
2020-02-27 01:29:43 -06:00
|
|
|
return rc
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|