2020-02-07 00:17:58 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-02-14 02:41:20 -06:00
|
|
|
"encoding/json"
|
2020-02-07 00:17:58 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2020-02-07 18:59:07 -06:00
|
|
|
"runtime"
|
2020-02-07 00:17:58 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/container"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
2020-02-14 02:41:20 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-07 00:17:58 -06:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RunContext contains info about current job
|
|
|
|
type RunContext struct {
|
2020-02-17 12:11:16 -06:00
|
|
|
Config *Config
|
|
|
|
Matrix map[string]interface{}
|
|
|
|
Run *model.Run
|
|
|
|
EventJSON string
|
|
|
|
Env map[string]string
|
|
|
|
Tempdir string
|
|
|
|
ExtraPath []string
|
|
|
|
CurrentStep string
|
|
|
|
StepResults map[string]*stepResult
|
|
|
|
ExprEval ExpressionEvaluator
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type stepResult struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Outputs map[string]string `json:"outputs"`
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnv returns the env for the context
|
|
|
|
func (rc *RunContext) GetEnv() map[string]string {
|
|
|
|
if rc.Env == nil {
|
|
|
|
rc.Env = mergeMaps(rc.Run.Workflow.Env, rc.Run.Job().Env)
|
|
|
|
}
|
|
|
|
return rc.Env
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up temp dir
|
|
|
|
func (rc *RunContext) Close(ctx context.Context) error {
|
|
|
|
return os.RemoveAll(rc.Tempdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor returns a pipeline executor for all the steps in the job
|
|
|
|
func (rc *RunContext) Executor() common.Executor {
|
2020-02-17 00:04:13 -06:00
|
|
|
|
2020-02-10 18:53:14 -06:00
|
|
|
err := rc.setupTempDir()
|
|
|
|
if err != nil {
|
|
|
|
return common.NewErrorExecutor(err)
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
steps := make([]common.Executor, 0)
|
|
|
|
|
2020-02-10 17:27:05 -06:00
|
|
|
for i, step := range rc.Run.Job().Steps {
|
|
|
|
if step.ID == "" {
|
|
|
|
step.ID = fmt.Sprintf("%d", i)
|
|
|
|
}
|
2020-02-11 11:10:35 -06:00
|
|
|
s := step
|
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
2020-02-14 02:41:20 -06:00
|
|
|
rc.CurrentStep = s.ID
|
|
|
|
rc.StepResults[rc.CurrentStep] = &stepResult{
|
|
|
|
Success: true,
|
|
|
|
Outputs: make(map[string]string),
|
|
|
|
}
|
2020-02-17 12:11:16 -06:00
|
|
|
rc.ExprEval = rc.NewStepExpressionEvaluator(s)
|
|
|
|
|
|
|
|
if !rc.EvalBool(s.If) {
|
|
|
|
log.Debugf("Skipping step '%s' due to '%s'", s.String(), s.If)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:38:30 -06:00
|
|
|
common.Logger(ctx).Infof("\u2B50 Run %s", s)
|
2020-02-11 11:10:35 -06:00
|
|
|
err := rc.newStepExecutor(s)(ctx)
|
|
|
|
if err == nil {
|
|
|
|
common.Logger(ctx).Infof(" \u2705 Success - %s", s)
|
|
|
|
} else {
|
|
|
|
common.Logger(ctx).Errorf(" \u274C Failure - %s", s)
|
2020-02-14 02:41:20 -06:00
|
|
|
rc.StepResults[rc.CurrentStep].Success = false
|
2020-02-11 11:10:35 -06:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2020-02-17 12:11:16 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
defer rc.Close(ctx)
|
|
|
|
job := rc.Run.Job()
|
|
|
|
log := common.Logger(ctx)
|
|
|
|
if !rc.EvalBool(job.If) {
|
|
|
|
log.Debugf("Skipping job '%s' due to '%s'", job.Name, job.If)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
platformName := rc.ExprEval.Interpolate(rc.Run.Job().RunsOn)
|
2020-02-19 21:16:40 -06:00
|
|
|
if img, ok := rc.Config.Platforms[strings.ToLower(platformName)]; !ok || img == "" {
|
2020-02-17 12:11:16 -06:00
|
|
|
log.Infof(" \U0001F6A7 Skipping unsupported platform '%s'", platformName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:43:20 -06:00
|
|
|
nullLogger := logrus.New()
|
|
|
|
nullLogger.Out = ioutil.Discard
|
|
|
|
if !rc.Config.ReuseContainers {
|
2020-02-21 10:42:00 -06:00
|
|
|
_ = rc.newContainerCleaner()(common.WithLogger(ctx, nullLogger))
|
2020-02-20 21:43:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := common.NewPipelineExecutor(steps...)(ctx)
|
|
|
|
|
|
|
|
if !rc.Config.ReuseContainers {
|
2020-02-21 10:42:00 -06:00
|
|
|
_ = rc.newContainerCleaner()(common.WithLogger(ctx, nullLogger))
|
2020-02-20 21:43:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalBool evaluates an expression against current run context
|
|
|
|
func (rc *RunContext) EvalBool(expr string) bool {
|
|
|
|
if expr != "" {
|
|
|
|
v, err := rc.ExprEval.Evaluate(expr)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error evaluating expression '%s' - %v", expr, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return v == "true"
|
|
|
|
}
|
|
|
|
return true
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeMaps(maps ...map[string]string) map[string]string {
|
|
|
|
rtnMap := make(map[string]string)
|
|
|
|
for _, m := range maps {
|
|
|
|
for k, v := range m {
|
|
|
|
rtnMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtnMap
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:27:05 -06:00
|
|
|
func (rc *RunContext) setupTempDir() error {
|
|
|
|
var err error
|
|
|
|
tempBase := ""
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
tempBase = "/tmp"
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2020-02-10 17:27:05 -06:00
|
|
|
rc.Tempdir, err = ioutil.TempDir(tempBase, "act-")
|
2020-02-10 18:53:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Chmod(rc.Tempdir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-10 17:27:05 -06:00
|
|
|
log.Debugf("Setup tempdir %s", rc.Tempdir)
|
|
|
|
return err
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) pullImage(containerSpec *model.ContainerSpec) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return container.NewDockerPullExecutor(container.NewDockerPullExecutorInput{
|
|
|
|
Image: containerSpec.Image,
|
|
|
|
ForcePull: rc.Config.ForcePull,
|
|
|
|
})(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) runContainer(containerSpec *model.ContainerSpec) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ghReader, err := rc.createGithubTarball()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
envList := make([]string, 0)
|
|
|
|
for k, v := range containerSpec.Env {
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
var cmd, entrypoint []string
|
|
|
|
if containerSpec.Args != "" {
|
2020-02-17 12:11:16 -06:00
|
|
|
cmd = strings.Fields(rc.ExprEval.Interpolate(containerSpec.Args))
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
if containerSpec.Entrypoint != "" {
|
2020-02-17 12:11:16 -06:00
|
|
|
entrypoint = strings.Fields(rc.ExprEval.Interpolate(containerSpec.Entrypoint))
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:38:30 -06:00
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) {
|
2020-02-12 01:55:20 -06:00
|
|
|
if rc.Config.LogOutput {
|
|
|
|
rawLogger.Infof(s)
|
|
|
|
} else {
|
|
|
|
rawLogger.Debugf(s)
|
|
|
|
}
|
2020-02-12 01:38:30 -06:00
|
|
|
})
|
2020-02-11 11:10:35 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
return container.NewDockerRunExecutor(container.NewDockerRunExecutorInput{
|
|
|
|
Cmd: cmd,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
Image: containerSpec.Image,
|
|
|
|
WorkingDir: "/github/workspace",
|
|
|
|
Env: envList,
|
2020-02-10 17:27:05 -06:00
|
|
|
Name: containerSpec.Name,
|
2020-02-07 00:17:58 -06:00
|
|
|
Binds: []string{
|
|
|
|
fmt.Sprintf("%s:%s", rc.Config.Workdir, "/github/workspace"),
|
|
|
|
fmt.Sprintf("%s:%s", rc.Tempdir, "/github/home"),
|
|
|
|
fmt.Sprintf("%s:%s", "/var/run/docker.sock", "/var/run/docker.sock"),
|
|
|
|
},
|
|
|
|
Content: map[string]io.Reader{"/github": ghReader},
|
2020-02-20 21:43:20 -06:00
|
|
|
ReuseContainers: containerSpec.Reuse,
|
2020-02-11 11:10:35 -06:00
|
|
|
Stdout: logWriter,
|
|
|
|
Stderr: logWriter,
|
2020-02-07 00:17:58 -06:00
|
|
|
})(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) createGithubTarball() (io.Reader, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
tw := tar.NewWriter(&buf)
|
|
|
|
var files = []struct {
|
|
|
|
Name string
|
|
|
|
Mode int64
|
|
|
|
Body string
|
|
|
|
}{
|
|
|
|
{"workflow/event.json", 0644, rc.EventJSON},
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
log.Debugf("Writing entry to tarball %s len:%d", file.Name, len(rc.EventJSON))
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: file.Name,
|
|
|
|
Mode: file.Mode,
|
|
|
|
Size: int64(len(rc.EventJSON)),
|
|
|
|
}
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := tw.Write([]byte(rc.EventJSON)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &buf, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:43:20 -06:00
|
|
|
func (rc *RunContext) createContainerName() string {
|
|
|
|
containerName := rc.Run.String()
|
2020-02-10 17:27:05 -06:00
|
|
|
containerName = regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(containerName, "-")
|
2020-02-07 00:17:58 -06:00
|
|
|
|
2020-02-20 21:43:20 -06:00
|
|
|
prefix := ""
|
2020-02-07 00:17:58 -06:00
|
|
|
suffix := ""
|
|
|
|
containerName = trimToLen(containerName, 30-(len(prefix)+len(suffix)))
|
|
|
|
return fmt.Sprintf("%s%s%s", prefix, containerName, suffix)
|
2020-02-20 21:43:20 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) createStepContainerName(stepID string) string {
|
|
|
|
|
|
|
|
prefix := regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(rc.createContainerName(), "-")
|
|
|
|
suffix := regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(stepID, "-")
|
2020-02-22 00:19:59 -06:00
|
|
|
prefix = trimToLen(prefix, 30-(1+len(suffix)))
|
|
|
|
name := strings.Trim(fmt.Sprintf("%s-%s", prefix, suffix), "-")
|
|
|
|
return name
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func trimToLen(s string, l int) string {
|
2020-02-20 21:43:20 -06:00
|
|
|
if l < 0 {
|
|
|
|
l = 0
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
if len(s) > l {
|
|
|
|
return s[:l]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
|
|
|
|
type jobContext struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Container struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Network string `json:"network"`
|
|
|
|
} `json:"container"`
|
|
|
|
Services map[string]struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
} `json:"services"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) getJobContext() *jobContext {
|
|
|
|
jobStatus := "success"
|
|
|
|
for _, stepStatus := range rc.StepResults {
|
|
|
|
if !stepStatus.Success {
|
|
|
|
jobStatus = "failure"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &jobContext{
|
|
|
|
Status: jobStatus,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) getStepsContext() map[string]*stepResult {
|
|
|
|
return rc.StepResults
|
|
|
|
}
|
|
|
|
|
|
|
|
type githubContext struct {
|
|
|
|
Event map[string]interface{} `json:"event"`
|
|
|
|
EventPath string `json:"event_path"`
|
|
|
|
Workflow string `json:"workflow"`
|
|
|
|
RunID string `json:"run_id"`
|
|
|
|
RunNumber string `json:"run_number"`
|
|
|
|
Actor string `json:"actor"`
|
|
|
|
Repository string `json:"repository"`
|
|
|
|
EventName string `json:"event_name"`
|
|
|
|
Sha string `json:"sha"`
|
|
|
|
Ref string `json:"ref"`
|
|
|
|
HeadRef string `json:"head_ref"`
|
|
|
|
BaseRef string `json:"base_ref"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Workspace string `json:"workspace"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) getGithubContext() *githubContext {
|
|
|
|
ghc := &githubContext{
|
|
|
|
Event: make(map[string]interface{}),
|
|
|
|
EventPath: "/github/workflow/event.json",
|
|
|
|
Workflow: rc.Run.Workflow.Name,
|
|
|
|
RunID: "1",
|
|
|
|
RunNumber: "1",
|
|
|
|
Actor: "nektos/act",
|
|
|
|
|
|
|
|
EventName: rc.Config.EventName,
|
|
|
|
Token: os.Getenv("GITHUB_TOKEN"),
|
|
|
|
Workspace: "/github/workspace",
|
|
|
|
Action: rc.CurrentStep,
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := rc.Config.Workdir
|
|
|
|
repo, err := common.FindGithubRepo(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("unable to get git repo: %v", err)
|
|
|
|
} else {
|
|
|
|
ghc.Repository = repo
|
|
|
|
}
|
|
|
|
|
|
|
|
_, sha, err := common.FindGitRevision(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("unable to get git revision: %v", err)
|
|
|
|
} else {
|
|
|
|
ghc.Sha = sha
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := common.FindGitRef(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("unable to get git ref: %v", err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("using github ref: %s", ref)
|
|
|
|
ghc.Ref = ref
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(rc.EventJSON), &ghc.Event)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
return ghc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
|
|
|
|
github := rc.getGithubContext()
|
|
|
|
env["HOME"] = "/github/home"
|
|
|
|
env["GITHUB_WORKFLOW"] = github.Workflow
|
|
|
|
env["GITHUB_RUN_ID"] = github.RunID
|
|
|
|
env["GITHUB_RUN_NUMBER"] = github.RunNumber
|
|
|
|
env["GITHUB_ACTION"] = github.Action
|
|
|
|
env["GITHUB_ACTOR"] = github.Actor
|
|
|
|
env["GITHUB_REPOSITORY"] = github.Repository
|
|
|
|
env["GITHUB_EVENT_NAME"] = github.EventName
|
|
|
|
env["GITHUB_EVENT_PATH"] = github.EventPath
|
|
|
|
env["GITHUB_WORKSPACE"] = github.Workspace
|
|
|
|
env["GITHUB_SHA"] = github.Sha
|
|
|
|
env["GITHUB_REF"] = github.Ref
|
|
|
|
return env
|
|
|
|
}
|