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"
|
2020-02-04 18:38:41 -06:00
|
|
|
"io/ioutil"
|
2021-05-04 16:50:35 -05:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2020-02-04 18:38:41 -06:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2021-03-28 23:08:40 -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
|
2021-03-30 14:26:25 -05:00
|
|
|
ForcePull bool // force pulling of the image, even if already present
|
2021-03-28 23:08:40 -05:00
|
|
|
LogOutput bool // log the output from docker run
|
|
|
|
Env map[string]string // env for containers
|
|
|
|
Secrets map[string]string // list of secrets
|
|
|
|
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
|
2021-05-23 09:43:09 -05:00
|
|
|
ContainerDaemonSocket string // Path to Docker daemon socket
|
2021-05-03 11:52:03 -05:00
|
|
|
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
|
2021-05-05 11:42:34 -05:00
|
|
|
GitHubInstance string // GitHub instance to use, default "github.com"
|
2021-06-04 11:06:59 -05:00
|
|
|
ContainerCapAdd []string // list of kernel capabilities to add to the containers
|
|
|
|
ContainerCapDrop []string // list of kernel capabilities to remove from the containers
|
2021-06-10 10:09:05 -05:00
|
|
|
AutoRemove bool // controls if the container is automatically removed upon workflow completion
|
2021-11-10 11:57:22 -06:00
|
|
|
ArtifactServerPath string // the path where the artifact server stores uploads
|
|
|
|
ArtifactServerPort string // the port the artifact server binds to
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2021-05-04 16:50:35 -05:00
|
|
|
// Resolves the equivalent host path inside the container
|
|
|
|
// This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject
|
|
|
|
// For use in docker volumes and binds
|
|
|
|
func (config *Config) containerPath(path string) string {
|
|
|
|
if runtime.GOOS == "windows" && strings.Contains(path, "/") {
|
|
|
|
log.Error("You cannot specify linux style local paths (/mnt/etc) on Windows as it does not understand them.")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
abspath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the path is a windows path
|
|
|
|
windowsPathRegex := regexp.MustCompile(`^([a-zA-Z]):\\(.+)$`)
|
|
|
|
windowsPathComponents := windowsPathRegex.FindStringSubmatch(abspath)
|
|
|
|
|
|
|
|
// Return as-is if no match
|
|
|
|
if windowsPathComponents == nil {
|
|
|
|
return abspath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to WSL2-compatible path if it is a windows path
|
|
|
|
// NOTE: Cannot use filepath because it will use the wrong path separators assuming we want the path to be windows
|
|
|
|
// based if running on Windows, and because we are feeding this to Docker, GoLang auto-path-translate doesn't work.
|
|
|
|
driveLetter := strings.ToLower(windowsPathComponents[1])
|
|
|
|
translatedPath := strings.ReplaceAll(windowsPathComponents[2], `\`, `/`)
|
|
|
|
// Should make something like /mnt/c/Users/person/My Folder/MyActProject
|
|
|
|
result := strings.Join([]string{"/mnt", driveLetter, translatedPath}, `/`)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves the equivalent host path inside the container
|
|
|
|
// This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject
|
|
|
|
func (config *Config) ContainerWorkdir() string {
|
|
|
|
return config.containerPath(config.Workdir)
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:38:41 -06:00
|
|
|
type runnerImpl struct {
|
|
|
|
config *Config
|
|
|
|
eventJSON string
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.eventJSON = "{}"
|
2020-02-07 00:17:58 -06:00
|
|
|
if runnerConfig.EventPath != "" {
|
2020-02-04 18:38:41 -06:00
|
|
|
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
|
|
|
eventJSONBytes, err := ioutil.ReadFile(runner.config.EventPath)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-02-04 18:38:41 -06:00
|
|
|
pipeline := make([]common.Executor, 0)
|
2021-06-10 10:09:05 -05:00
|
|
|
for s, stage := range plan.Stages {
|
2020-02-04 18:38:41 -06:00
|
|
|
stageExecutor := make([]common.Executor, 0)
|
2021-06-10 10:09:05 -05:00
|
|
|
for r, run := range stage.Runs {
|
2020-02-17 12:11:16 -06:00
|
|
|
job := run.Job()
|
2020-02-23 17:01:25 -06:00
|
|
|
matrixes := job.GetMatrixes()
|
2021-09-26 11:21:12 -05:00
|
|
|
maxParallel := 4
|
|
|
|
if job.Strategy != nil {
|
|
|
|
maxParallel = job.Strategy.MaxParallel
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matrixes) < maxParallel {
|
|
|
|
maxParallel = len(matrixes)
|
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
|
2021-09-26 11:21:12 -05:00
|
|
|
b := 0
|
2020-02-27 01:29:43 -06:00
|
|
|
for i, matrix := range matrixes {
|
|
|
|
rc := runner.newRunContext(run, matrix)
|
2021-05-06 15:02:29 -05:00
|
|
|
rc.JobName = rc.Name
|
2020-03-04 18:24:14 -06:00
|
|
|
if len(matrixes) > 1 {
|
2020-02-27 01:29:43 -06:00
|
|
|
rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1)
|
|
|
|
}
|
|
|
|
if len(rc.String()) > maxJobNameLen {
|
|
|
|
maxJobNameLen = len(rc.String())
|
|
|
|
}
|
2020-02-17 12:25:28 -06:00
|
|
|
stageExecutor = append(stageExecutor, func(ctx context.Context) error {
|
2020-02-27 01:29:43 -06:00
|
|
|
jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String())
|
2021-06-10 10:09:05 -05:00
|
|
|
return rc.Executor().Finally(func(ctx context.Context) error {
|
|
|
|
isLastRunningContainer := func(currentStage int, currentRun int) bool {
|
|
|
|
return currentStage == len(plan.Stages)-1 && currentRun == len(stage.Runs)-1
|
|
|
|
}
|
|
|
|
|
|
|
|
if runner.config.AutoRemove && isLastRunningContainer(s, r) {
|
|
|
|
log.Infof("Cleaning up container for job %s", rc.JobName)
|
|
|
|
if err := rc.stopJobContainer()(ctx); err != nil {
|
|
|
|
log.Errorf("Error while cleaning container: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})(WithJobLogger(ctx, jobName, rc.Config.Secrets, rc.Config.InsecureSecrets))
|
2020-02-17 12:25:28 -06:00
|
|
|
})
|
2021-09-26 11:21:12 -05:00
|
|
|
b++
|
|
|
|
if b == maxParallel {
|
|
|
|
pipeline = append(pipeline, common.NewParallelExecutor(stageExecutor...))
|
|
|
|
stageExecutor = make([]common.Executor, 0)
|
|
|
|
b = 0
|
|
|
|
}
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
return common.NewPipelineExecutor(pipeline...)
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:29:43 -06:00
|
|
|
func (runner *runnerImpl) newRunContext(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,
|
|
|
|
StepResults: make(map[string]*stepResult),
|
|
|
|
Matrix: matrix,
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
|
|
|
rc.ExprEval = rc.NewExpressionEvaluator()
|
2020-02-27 01:29:43 -06:00
|
|
|
rc.Name = rc.ExprEval.Interpolate(run.String())
|
|
|
|
return rc
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|