2020-02-07 00:17:58 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-14 02:41:20 -06:00
|
|
|
"encoding/json"
|
2020-02-07 00:17:58 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-02-23 17:01:25 -06:00
|
|
|
"path/filepath"
|
2020-02-07 00:17:58 -06:00
|
|
|
"regexp"
|
2020-02-24 12:56:49 -06:00
|
|
|
"runtime"
|
2020-02-07 00:17:58 -06:00
|
|
|
"strings"
|
|
|
|
|
2022-03-29 10:55:40 -05:00
|
|
|
"github.com/kballard/go-shellquote"
|
2021-09-10 00:03:40 -05:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2021-04-05 10:51:13 -05:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2021-03-28 23:08:40 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-02-07 00:17:58 -06:00
|
|
|
|
2021-11-25 23:18:31 -06:00
|
|
|
selinux "github.com/opencontainers/selinux/go-selinux"
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2021-03-28 23:08:40 -05:00
|
|
|
"github.com/nektos/act/pkg/container"
|
2020-02-07 00:17:58 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
2021-05-24 12:09:03 -05:00
|
|
|
const ActPath string = "/var/run/act"
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
// RunContext contains info about current job
|
|
|
|
type RunContext struct {
|
2021-12-22 13:19:50 -06:00
|
|
|
Name string
|
|
|
|
Config *Config
|
|
|
|
Matrix map[string]interface{}
|
|
|
|
Run *model.Run
|
|
|
|
EventJSON string
|
|
|
|
Env map[string]string
|
|
|
|
ExtraPath []string
|
|
|
|
CurrentStep string
|
2021-12-22 13:52:09 -06:00
|
|
|
StepResults map[string]*model.StepResult
|
2021-12-22 13:19:50 -06:00
|
|
|
ExprEval ExpressionEvaluator
|
|
|
|
JobContainer container.Container
|
|
|
|
OutputMappings map[MappableOutput]MappableOutput
|
|
|
|
JobName string
|
|
|
|
ActionPath string
|
|
|
|
ActionRef string
|
|
|
|
ActionRepository string
|
|
|
|
Composite *model.Action
|
|
|
|
Inputs map[string]interface{}
|
|
|
|
Parent *RunContext
|
2022-03-02 02:29:34 -06:00
|
|
|
Masks []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) AddMask(mask string) {
|
|
|
|
rc.Masks = append(rc.Masks, mask)
|
2021-12-22 13:19:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) Clone() *RunContext {
|
|
|
|
clone := *rc
|
|
|
|
clone.CurrentStep = ""
|
|
|
|
clone.Composite = nil
|
|
|
|
clone.Inputs = nil
|
2021-12-22 13:52:09 -06:00
|
|
|
clone.StepResults = make(map[string]*model.StepResult)
|
2021-12-22 13:19:50 -06:00
|
|
|
clone.Parent = rc
|
|
|
|
return &clone
|
2021-04-02 15:40:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type MappableOutput struct {
|
|
|
|
StepID string
|
|
|
|
OutputName string
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:29:43 -06:00
|
|
|
func (rc *RunContext) String() string {
|
|
|
|
return fmt.Sprintf("%s/%s", rc.Run.Workflow.Name, rc.Name)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-04-04 12:24:49 -05:00
|
|
|
rc.Env = mergeMaps(rc.Run.Workflow.Env, rc.Run.Job().Environment(), rc.Config.Env)
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
2020-11-18 09:14:34 -06:00
|
|
|
rc.Env["ACT"] = "true"
|
2020-02-07 00:17:58 -06:00
|
|
|
return rc.Env
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (rc *RunContext) jobContainerName() string {
|
2020-02-27 01:29:43 -06:00
|
|
|
return createContainerName("act", rc.String())
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
2021-05-04 16:50:35 -05:00
|
|
|
// Returns the binds and mounts for the container, resolving paths as appopriate
|
|
|
|
func (rc *RunContext) GetBindsAndMounts() ([]string, map[string]string) {
|
|
|
|
name := rc.jobContainerName()
|
|
|
|
|
2021-05-23 09:43:09 -05:00
|
|
|
if rc.Config.ContainerDaemonSocket == "" {
|
|
|
|
rc.Config.ContainerDaemonSocket = "/var/run/docker.sock"
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:50:35 -05:00
|
|
|
binds := []string{
|
2021-05-23 09:43:09 -05:00
|
|
|
fmt.Sprintf("%s:%s", rc.Config.ContainerDaemonSocket, "/var/run/docker.sock"),
|
2021-05-04 16:50:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mounts := map[string]string{
|
|
|
|
"act-toolcache": "/toolcache",
|
2021-09-24 08:32:22 -05:00
|
|
|
name + "-env": ActPath,
|
2021-05-04 16:50:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Config.BindWorkdir {
|
|
|
|
bindModifiers := ""
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
bindModifiers = ":delegated"
|
|
|
|
}
|
2021-11-25 23:18:31 -06:00
|
|
|
if selinux.GetEnabled() {
|
|
|
|
bindModifiers = ":z"
|
|
|
|
}
|
2021-05-04 16:50:35 -05:00
|
|
|
binds = append(binds, fmt.Sprintf("%s:%s%s", rc.Config.Workdir, rc.Config.ContainerWorkdir(), bindModifiers))
|
|
|
|
} else {
|
|
|
|
mounts[name] = rc.Config.ContainerWorkdir()
|
|
|
|
}
|
|
|
|
|
|
|
|
return binds, mounts
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (rc *RunContext) startJobContainer() common.Executor {
|
2020-03-16 16:58:10 -05:00
|
|
|
image := rc.platformImage()
|
2021-09-10 00:03:40 -05:00
|
|
|
hostname := rc.hostname()
|
2020-02-23 17:01:25 -06:00
|
|
|
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
2020-02-24 14:48:12 -06:00
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
|
2020-02-23 17:01:25 -06:00
|
|
|
if rc.Config.LogOutput {
|
2020-06-24 09:05:05 -05:00
|
|
|
rawLogger.Infof("%s", s)
|
2020-02-23 17:01:25 -06:00
|
|
|
} else {
|
2020-06-24 09:05:05 -05:00
|
|
|
rawLogger.Debugf("%s", s)
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
2020-02-24 14:48:12 -06:00
|
|
|
return true
|
2020-02-23 17:01:25 -06:00
|
|
|
})
|
|
|
|
|
2021-11-27 12:05:56 -06:00
|
|
|
username, password, err := rc.handleCredentials()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to handle credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
common.Logger(ctx).Infof("\U0001f680 Start image=%s", image)
|
|
|
|
name := rc.jobContainerName()
|
|
|
|
|
2020-02-24 12:56:49 -06:00
|
|
|
envList := make([]string, 0)
|
|
|
|
|
2020-02-25 10:52:05 -06:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache"))
|
2020-04-23 01:57:36 -05:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
|
2020-04-23 10:18:36 -05:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
|
2020-02-24 12:56:49 -06:00
|
|
|
|
2021-05-04 16:50:35 -05:00
|
|
|
binds, mounts := rc.GetBindsAndMounts()
|
2020-02-24 19:48:21 -06:00
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
rc.JobContainer = container.NewContainer(&container.NewContainerInput{
|
2021-05-04 16:50:35 -05:00
|
|
|
Cmd: nil,
|
|
|
|
Entrypoint: []string{"/usr/bin/tail", "-f", "/dev/null"},
|
|
|
|
WorkingDir: rc.Config.ContainerWorkdir(),
|
|
|
|
Image: image,
|
2021-11-27 12:05:56 -06:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
2021-05-04 16:50:35 -05:00
|
|
|
Name: name,
|
|
|
|
Env: envList,
|
|
|
|
Mounts: mounts,
|
2020-03-09 19:43:24 -05:00
|
|
|
NetworkMode: "host",
|
|
|
|
Binds: binds,
|
|
|
|
Stdout: logWriter,
|
|
|
|
Stderr: logWriter,
|
2020-08-01 15:21:49 -05:00
|
|
|
Privileged: rc.Config.Privileged,
|
2021-02-27 10:31:25 -06:00
|
|
|
UsernsMode: rc.Config.UsernsMode,
|
2021-03-28 23:08:40 -05:00
|
|
|
Platform: rc.Config.ContainerArchitecture,
|
2021-09-10 00:03:40 -05:00
|
|
|
Hostname: hostname,
|
2020-02-23 17:01:25 -06:00
|
|
|
})
|
|
|
|
|
2020-03-09 19:45:42 -05:00
|
|
|
var copyWorkspace bool
|
|
|
|
var copyToPath string
|
|
|
|
if !rc.Config.BindWorkdir {
|
|
|
|
copyToPath, copyWorkspace = rc.localCheckoutPath()
|
2021-05-04 16:50:35 -05:00
|
|
|
copyToPath = filepath.Join(rc.Config.ContainerWorkdir(), copyToPath)
|
2020-03-09 19:45:42 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
rc.JobContainer.Pull(rc.Config.ForcePull),
|
2020-06-18 10:21:55 -05:00
|
|
|
rc.stopJobContainer(),
|
2021-06-04 11:06:59 -05:00
|
|
|
rc.JobContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
|
2020-02-23 17:01:25 -06:00
|
|
|
rc.JobContainer.Start(false),
|
2021-09-27 14:01:14 -05:00
|
|
|
rc.JobContainer.UpdateFromImageEnv(&rc.Env),
|
2021-05-05 18:11:43 -05:00
|
|
|
rc.JobContainer.UpdateFromEnv("/etc/environment", &rc.Env),
|
2021-08-10 14:40:20 -05:00
|
|
|
rc.JobContainer.Exec([]string{"mkdir", "-m", "0777", "-p", ActPath}, rc.Env, "root", ""),
|
2021-05-03 09:37:20 -05:00
|
|
|
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".", rc.Config.UseGitIgnore).IfBool(copyWorkspace),
|
2021-05-24 12:09:03 -05:00
|
|
|
rc.JobContainer.Copy(ActPath+"/", &container.FileEntry{
|
2020-02-23 17:01:25 -06:00
|
|
|
Name: "workflow/event.json",
|
2020-08-08 15:31:26 -05:00
|
|
|
Mode: 0644,
|
2020-02-23 17:01:25 -06:00
|
|
|
Body: rc.EventJSON,
|
2021-01-12 00:39:43 -06:00
|
|
|
}, &container.FileEntry{
|
|
|
|
Name: "workflow/envs.txt",
|
2021-06-09 18:08:39 -05:00
|
|
|
Mode: 0666,
|
2021-01-12 00:39:43 -06:00
|
|
|
Body: "",
|
2021-05-06 08:30:12 -05:00
|
|
|
}, &container.FileEntry{
|
|
|
|
Name: "workflow/paths.txt",
|
2021-06-09 18:08:39 -05:00
|
|
|
Mode: 0666,
|
2021-05-06 08:30:12 -05:00
|
|
|
Body: "",
|
2020-02-23 17:01:25 -06:00
|
|
|
}),
|
|
|
|
)(ctx)
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 14:40:20 -05:00
|
|
|
|
|
|
|
func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user, workdir string) common.Executor {
|
2020-02-23 17:01:25 -06:00
|
|
|
return func(ctx context.Context) error {
|
2021-08-10 14:40:20 -05:00
|
|
|
return rc.JobContainer.Exec(cmd, env, user, workdir)(ctx)
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 10:21:55 -05:00
|
|
|
|
|
|
|
// stopJobContainer removes the job container (if it exists) and its volume (if it exists) if !rc.Config.ReuseContainers
|
2020-02-23 17:01:25 -06:00
|
|
|
func (rc *RunContext) stopJobContainer() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if rc.JobContainer != nil && !rc.Config.ReuseContainers {
|
|
|
|
return rc.JobContainer.Remove().
|
|
|
|
Then(container.NewDockerVolumeRemoveExecutor(rc.jobContainerName(), false))(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:50:35 -05:00
|
|
|
// Prepare the mounts and binds for the worker
|
|
|
|
|
2020-02-24 12:56:49 -06:00
|
|
|
// ActionCacheDir is for rc
|
|
|
|
func (rc *RunContext) ActionCacheDir() string {
|
2020-02-24 00:34:48 -06:00
|
|
|
var xdgCache string
|
|
|
|
var ok bool
|
2021-04-05 10:51:13 -05:00
|
|
|
if xdgCache, ok = os.LookupEnv("XDG_CACHE_HOME"); !ok || xdgCache == "" {
|
|
|
|
if home, err := homedir.Dir(); err == nil {
|
|
|
|
xdgCache = filepath.Join(home, ".cache")
|
|
|
|
} else if xdgCache, err = filepath.Abs("."); err != nil {
|
|
|
|
log.Fatal(err)
|
2020-02-24 00:34:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return filepath.Join(xdgCache, "act")
|
|
|
|
}
|
|
|
|
|
2021-11-24 09:49:08 -06:00
|
|
|
// Interpolate outputs after a job is done
|
|
|
|
func (rc *RunContext) interpolateOutputs() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
|
|
|
for k, v := range rc.Run.Job().Outputs {
|
|
|
|
interpolated := ee.Interpolate(v)
|
|
|
|
if v != interpolated {
|
|
|
|
rc.Run.Job().Outputs[k] = interpolated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) startContainer() common.Executor {
|
|
|
|
return rc.startJobContainer()
|
|
|
|
}
|
2020-02-27 01:29:43 -06:00
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) stopContainer() common.Executor {
|
|
|
|
return rc.stopJobContainer()
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) closeContainer() common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
if rc.JobContainer != nil {
|
|
|
|
return rc.JobContainer.Close()(ctx)
|
2020-02-10 17:27:05 -06:00
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
return nil
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) matrix() map[string]interface{} {
|
|
|
|
return rc.Matrix
|
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) result(result string) {
|
|
|
|
rc.Run.Job().Result = result
|
|
|
|
}
|
2020-02-17 12:11:16 -06:00
|
|
|
|
2022-02-08 11:22:41 -06:00
|
|
|
func (rc *RunContext) steps() []*model.Step {
|
|
|
|
return rc.Run.Job().Steps
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor returns a pipeline executor for all the steps in the job
|
|
|
|
func (rc *RunContext) Executor() common.Executor {
|
2022-02-25 12:39:50 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
isEnabled, err := rc.isEnabled(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isEnabled {
|
2022-03-22 16:13:00 -05:00
|
|
|
return newJobExecutor(rc, &stepFactoryImpl{}, rc)(ctx)
|
2022-02-25 12:39:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
2020-02-17 12:11:16 -06:00
|
|
|
|
2021-12-22 13:19:50 -06:00
|
|
|
// Executor returns a pipeline executor for all the steps in the job
|
2022-03-29 12:42:11 -05:00
|
|
|
func (rc *RunContext) compositeExecutor() common.Executor {
|
2021-12-22 13:19:50 -06:00
|
|
|
steps := make([]common.Executor, 0)
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
sf := &stepFactoryImpl{}
|
|
|
|
|
2021-12-22 13:19:50 -06:00
|
|
|
for i, step := range rc.Composite.Runs.Steps {
|
|
|
|
if step.ID == "" {
|
|
|
|
step.ID = fmt.Sprintf("%d", i)
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
// create a copy of the step, since this composite action could
|
|
|
|
// run multiple times and we might modify the instance
|
2021-12-22 13:19:50 -06:00
|
|
|
stepcopy := step
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
step, err := sf.newStep(&stepcopy, rc)
|
|
|
|
if err != nil {
|
|
|
|
return common.NewErrorExecutor(err)
|
|
|
|
}
|
|
|
|
stepExec := common.NewPipelineExecutor(step.pre(), step.main(), step.post())
|
|
|
|
|
2022-01-27 10:20:44 -06:00
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
|
|
|
err := stepExec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
common.Logger(ctx).Errorf("%v", err)
|
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
common.Logger(ctx).Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-12-22 13:19:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps, common.JobError)
|
2022-01-21 10:08:30 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return common.NewPipelineExecutor(steps...)(common.WithJobErrorContainer(ctx))
|
|
|
|
}
|
2021-12-22 13:19:50 -06:00
|
|
|
}
|
|
|
|
|
2020-03-16 16:58:10 -05:00
|
|
|
func (rc *RunContext) platformImage() string {
|
|
|
|
job := rc.Run.Job()
|
|
|
|
|
|
|
|
c := job.Container()
|
|
|
|
if c != nil {
|
2020-11-10 16:55:27 -06:00
|
|
|
return rc.ExprEval.Interpolate(c.Image)
|
2020-03-16 16:58:10 -05:00
|
|
|
}
|
|
|
|
|
2021-01-21 08:02:48 -06:00
|
|
|
if job.RunsOn() == nil {
|
|
|
|
log.Errorf("'runs-on' key not defined in %s", rc.String())
|
|
|
|
}
|
|
|
|
|
2020-03-16 16:58:10 -05:00
|
|
|
for _, runnerLabel := range job.RunsOn() {
|
|
|
|
platformName := rc.ExprEval.Interpolate(runnerLabel)
|
|
|
|
image := rc.Config.Platforms[strings.ToLower(platformName)]
|
|
|
|
if image != "" {
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:03:40 -05:00
|
|
|
func (rc *RunContext) hostname() string {
|
|
|
|
job := rc.Run.Job()
|
|
|
|
c := job.Container()
|
|
|
|
if c == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
optionsFlags := pflag.NewFlagSet("container_options", pflag.ContinueOnError)
|
|
|
|
hostname := optionsFlags.StringP("hostname", "h", "", "")
|
2022-03-29 10:55:40 -05:00
|
|
|
optionsArgs, err := shellquote.Split(c.Options)
|
2021-09-10 00:03:40 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot parse container options: %s", c.Options)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
err = optionsFlags.Parse(optionsArgs)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot parse container options: %s", c.Options)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *hostname
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:39:50 -06:00
|
|
|
func (rc *RunContext) isEnabled(ctx context.Context) (bool, error) {
|
2020-02-23 17:01:25 -06:00
|
|
|
job := rc.Run.Job()
|
2020-05-04 14:18:13 -05:00
|
|
|
l := common.Logger(ctx)
|
2021-12-08 14:57:42 -06:00
|
|
|
runJob, err := EvalBool(rc.ExprEval, job.If.Value)
|
2020-11-17 11:31:05 -06:00
|
|
|
if err != nil {
|
2022-02-25 12:39:50 -06:00
|
|
|
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", job.If.Value, err)
|
2020-11-17 11:31:05 -06:00
|
|
|
}
|
|
|
|
if !runJob {
|
2021-05-05 15:04:03 -05:00
|
|
|
l.Debugf("Skipping job '%s' due to '%s'", job.Name, job.If.Value)
|
2022-02-25 12:39:50 -06:00
|
|
|
return false, nil
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
2020-02-20 21:43:20 -06:00
|
|
|
|
2020-03-16 16:58:10 -05:00
|
|
|
img := rc.platformImage()
|
|
|
|
if img == "" {
|
2021-01-21 08:02:48 -06:00
|
|
|
if job.RunsOn() == nil {
|
|
|
|
log.Errorf("'runs-on' key not defined in %s", rc.String())
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:13:07 -06:00
|
|
|
for _, runnerLabel := range job.RunsOn() {
|
|
|
|
platformName := rc.ExprEval.Interpolate(runnerLabel)
|
2021-09-13 18:14:41 -05:00
|
|
|
l.Infof("\U0001F6A7 Skipping unsupported platform -- Try running with `-P %+v=...`", platformName)
|
2020-12-08 12:13:07 -06:00
|
|
|
}
|
2022-02-25 12:39:50 -06:00
|
|
|
return false, nil
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
2022-02-25 12:39:50 -06:00
|
|
|
return true, nil
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
|
|
|
|
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-23 17:01:25 -06:00
|
|
|
func createContainerName(parts ...string) string {
|
|
|
|
name := make([]string, 0)
|
|
|
|
pattern := regexp.MustCompile("[^a-zA-Z0-9]")
|
2020-02-23 18:36:44 -06:00
|
|
|
partLen := (30 / len(parts)) - 1
|
2020-02-24 00:34:48 -06:00
|
|
|
for i, part := range parts {
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
name = append(name, pattern.ReplaceAllString(part, "-"))
|
|
|
|
} else {
|
2021-04-05 10:51:13 -05:00
|
|
|
// If any part has a '-<number>' on the end it is likely part of a matrix job.
|
|
|
|
// Let's preserve the number to prevent clashes in container names.
|
|
|
|
re := regexp.MustCompile("-[0-9]+$")
|
|
|
|
num := re.FindStringSubmatch(part)
|
|
|
|
if len(num) > 0 {
|
|
|
|
name = append(name, trimToLen(pattern.ReplaceAllString(part, "-"), partLen-len(num[0])))
|
|
|
|
name = append(name, num[0])
|
|
|
|
} else {
|
|
|
|
name = append(name, trimToLen(pattern.ReplaceAllString(part, "-"), partLen))
|
2021-04-02 15:40:44 -05:00
|
|
|
}
|
2020-02-24 00:34:48 -06:00
|
|
|
}
|
2020-02-23 17:01:25 -06:00
|
|
|
}
|
2021-04-05 10:51:13 -05:00
|
|
|
return strings.ReplaceAll(strings.Trim(strings.Join(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
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
func (rc *RunContext) getJobContext() *model.JobContext {
|
2020-02-14 02:41:20 -06:00
|
|
|
jobStatus := "success"
|
|
|
|
for _, stepStatus := range rc.StepResults {
|
2021-12-22 13:52:09 -06:00
|
|
|
if stepStatus.Conclusion == model.StepStatusFailure {
|
2020-02-14 02:41:20 -06:00
|
|
|
jobStatus = "failure"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 13:52:09 -06:00
|
|
|
return &model.JobContext{
|
2020-02-14 02:41:20 -06:00
|
|
|
Status: jobStatus,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
func (rc *RunContext) getStepsContext() map[string]*model.StepResult {
|
2020-02-14 02:41:20 -06:00
|
|
|
return rc.StepResults
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
func (rc *RunContext) getGithubContext() *model.GithubContext {
|
|
|
|
ghc := &model.GithubContext{
|
2021-05-06 15:02:29 -05:00
|
|
|
Event: make(map[string]interface{}),
|
2021-05-24 12:09:03 -05:00
|
|
|
EventPath: ActPath + "/workflow/event.json",
|
2021-05-06 15:02:29 -05:00
|
|
|
Workflow: rc.Run.Workflow.Name,
|
|
|
|
RunID: rc.Config.Env["GITHUB_RUN_ID"],
|
|
|
|
RunNumber: rc.Config.Env["GITHUB_RUN_NUMBER"],
|
|
|
|
Actor: rc.Config.Actor,
|
|
|
|
EventName: rc.Config.EventName,
|
|
|
|
Workspace: rc.Config.ContainerWorkdir(),
|
|
|
|
Action: rc.CurrentStep,
|
|
|
|
Token: rc.Config.Secrets["GITHUB_TOKEN"],
|
2021-12-22 13:19:50 -06:00
|
|
|
ActionPath: rc.ActionPath,
|
|
|
|
ActionRef: rc.ActionRef,
|
|
|
|
ActionRepository: rc.ActionRepository,
|
2021-05-06 15:02:29 -05:00
|
|
|
RepositoryOwner: rc.Config.Env["GITHUB_REPOSITORY_OWNER"],
|
|
|
|
RetentionDays: rc.Config.Env["GITHUB_RETENTION_DAYS"],
|
|
|
|
RunnerPerflog: rc.Config.Env["RUNNER_PERFLOG"],
|
|
|
|
RunnerTrackingID: rc.Config.Env["RUNNER_TRACKING_ID"],
|
2020-03-06 16:17:57 -06:00
|
|
|
}
|
2021-05-05 18:11:43 -05:00
|
|
|
|
2021-05-06 15:02:29 -05:00
|
|
|
if ghc.RunID == "" {
|
|
|
|
ghc.RunID = "1"
|
2020-09-22 16:13:29 -05:00
|
|
|
}
|
2021-05-05 18:11:43 -05:00
|
|
|
|
2021-05-06 15:02:29 -05:00
|
|
|
if ghc.RunNumber == "" {
|
|
|
|
ghc.RunNumber = "1"
|
2020-09-22 16:13:29 -05:00
|
|
|
}
|
2021-05-05 18:11:43 -05:00
|
|
|
|
2021-05-06 15:02:29 -05:00
|
|
|
if ghc.RetentionDays == "" {
|
|
|
|
ghc.RetentionDays = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
if ghc.RunnerPerflog == "" {
|
|
|
|
ghc.RunnerPerflog = "/dev/null"
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
|
2020-05-12 02:14:56 -05:00
|
|
|
// Backwards compatibility for configs that require
|
|
|
|
// a default rather than being run as a cmd
|
|
|
|
if ghc.Actor == "" {
|
|
|
|
ghc.Actor = "nektos/act"
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
repoPath := rc.Config.Workdir
|
2022-04-04 12:53:08 -05:00
|
|
|
repo, err := common.FindGithubRepo(repoPath, rc.Config.GitHubInstance, rc.Config.RemoteName)
|
2020-02-14 02:41:20 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Warningf("unable to get git repo: %v", err)
|
|
|
|
} else {
|
|
|
|
ghc.Repository = repo
|
2021-05-06 15:02:29 -05:00
|
|
|
if ghc.RepositoryOwner == "" {
|
|
|
|
ghc.RepositoryOwner = strings.Split(repo, "/")[0]
|
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
|
2020-05-04 14:18:13 -05:00
|
|
|
if rc.EventJSON != "" {
|
|
|
|
err = json.Unmarshal([]byte(rc.EventJSON), &ghc.Event)
|
|
|
|
if err != nil {
|
2020-11-12 10:15:09 -06:00
|
|
|
log.Errorf("Unable to Unmarshal event '%s': %v", rc.EventJSON, err)
|
2020-05-04 14:18:13 -05:00
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
2020-03-06 16:17:57 -06:00
|
|
|
|
|
|
|
if ghc.EventName == "pull_request" {
|
|
|
|
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
|
|
|
|
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:10:00 -06:00
|
|
|
ghc.SetRefAndSha(rc.Config.DefaultBranch, repoPath)
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
return ghc
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
func isLocalCheckout(ghc *model.GithubContext, step *model.Step) bool {
|
2021-05-10 10:12:57 -05:00
|
|
|
if step.Type() == model.StepTypeInvalid {
|
2021-05-04 16:50:35 -05:00
|
|
|
// This will be errored out by the executor later, we need this here to avoid a null panic though
|
|
|
|
return false
|
|
|
|
}
|
2020-03-09 19:45:42 -05:00
|
|
|
if step.Type() != model.StepTypeUsesActionRemote {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
remoteAction := newRemoteAction(step.Uses)
|
2021-05-10 10:12:57 -05:00
|
|
|
if remoteAction == nil {
|
|
|
|
// IsCheckout() will nil panic if we dont bail out early
|
|
|
|
return false
|
|
|
|
}
|
2020-03-09 19:45:42 -05:00
|
|
|
if !remoteAction.IsCheckout() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if repository, ok := step.With["repository"]; ok && repository != ghc.Repository {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if repository, ok := step.With["ref"]; ok && repository != ghc.Ref {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:17:57 -06:00
|
|
|
func asString(v interface{}) string {
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
} else if s, ok := v.(string); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if len(ks) == 0 { // degenerate input
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if rval, ok = m[ks[0]]; !ok {
|
|
|
|
return nil
|
|
|
|
} else if len(ks) == 1 { // we've reached the final key
|
|
|
|
return rval
|
|
|
|
} else if m, ok = rval.(map[string]interface{}); !ok {
|
|
|
|
return nil
|
|
|
|
} else { // 1+ more keys
|
|
|
|
return nestedMapLookup(m, ks[1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
|
|
|
|
github := rc.getGithubContext()
|
2020-09-28 10:22:42 -05:00
|
|
|
env["CI"] = "true"
|
2021-05-24 12:09:03 -05:00
|
|
|
env["GITHUB_ENV"] = ActPath + "/workflow/envs.txt"
|
|
|
|
env["GITHUB_PATH"] = ActPath + "/workflow/paths.txt"
|
2020-02-14 02:41:20 -06:00
|
|
|
env["GITHUB_WORKFLOW"] = github.Workflow
|
|
|
|
env["GITHUB_RUN_ID"] = github.RunID
|
|
|
|
env["GITHUB_RUN_NUMBER"] = github.RunNumber
|
|
|
|
env["GITHUB_ACTION"] = github.Action
|
2021-12-22 13:19:50 -06:00
|
|
|
env["GITHUB_ACTION_PATH"] = github.ActionPath
|
|
|
|
env["GITHUB_ACTION_REPOSITORY"] = github.ActionRepository
|
|
|
|
env["GITHUB_ACTION_REF"] = github.ActionRef
|
2020-02-24 12:56:49 -06:00
|
|
|
env["GITHUB_ACTIONS"] = "true"
|
2020-02-14 02:41:20 -06:00
|
|
|
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
|
2020-03-09 19:45:42 -05:00
|
|
|
env["GITHUB_TOKEN"] = github.Token
|
2021-01-31 23:59:51 -06:00
|
|
|
env["GITHUB_SERVER_URL"] = "https://github.com"
|
|
|
|
env["GITHUB_API_URL"] = "https://api.github.com"
|
|
|
|
env["GITHUB_GRAPHQL_URL"] = "https://api.github.com/graphql"
|
2021-05-06 15:02:29 -05:00
|
|
|
env["GITHUB_ACTION_REF"] = github.ActionRef
|
|
|
|
env["GITHUB_ACTION_REPOSITORY"] = github.ActionRepository
|
|
|
|
env["GITHUB_BASE_REF"] = github.BaseRef
|
|
|
|
env["GITHUB_HEAD_REF"] = github.HeadRef
|
|
|
|
env["GITHUB_JOB"] = rc.JobName
|
|
|
|
env["GITHUB_REPOSITORY_OWNER"] = github.RepositoryOwner
|
|
|
|
env["GITHUB_RETENTION_DAYS"] = github.RetentionDays
|
|
|
|
env["RUNNER_PERFLOG"] = github.RunnerPerflog
|
|
|
|
env["RUNNER_TRACKING_ID"] = github.RunnerTrackingID
|
2021-05-05 11:42:34 -05:00
|
|
|
if rc.Config.GitHubInstance != "github.com" {
|
|
|
|
env["GITHUB_SERVER_URL"] = fmt.Sprintf("https://%s", rc.Config.GitHubInstance)
|
|
|
|
env["GITHUB_API_URL"] = fmt.Sprintf("https://%s/api/v3", rc.Config.GitHubInstance)
|
|
|
|
env["GITHUB_GRAPHQL_URL"] = fmt.Sprintf("https://%s/api/graphql", rc.Config.GitHubInstance)
|
|
|
|
}
|
2021-03-17 19:14:08 -05:00
|
|
|
|
2021-11-10 11:57:22 -06:00
|
|
|
if rc.Config.ArtifactServerPath != "" {
|
|
|
|
setActionRuntimeVars(rc, env)
|
|
|
|
}
|
|
|
|
|
2021-03-17 19:14:08 -05:00
|
|
|
job := rc.Run.Job()
|
|
|
|
if job.RunsOn() != nil {
|
|
|
|
for _, runnerLabel := range job.RunsOn() {
|
|
|
|
platformName := rc.ExprEval.Interpolate(runnerLabel)
|
|
|
|
if platformName != "" {
|
2021-03-28 23:46:09 -05:00
|
|
|
if platformName == "ubuntu-latest" {
|
|
|
|
// hardcode current ubuntu-latest since we have no way to check that 'on the fly'
|
|
|
|
env["ImageOS"] = "ubuntu20"
|
|
|
|
} else {
|
2021-08-01 10:39:39 -05:00
|
|
|
platformName = strings.SplitN(strings.Replace(platformName, `-`, ``, 1), `.`, 2)[0]
|
2021-03-28 23:46:09 -05:00
|
|
|
env["ImageOS"] = platformName
|
|
|
|
}
|
2021-03-17 19:14:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
return env
|
|
|
|
}
|
2020-03-09 19:45:42 -05:00
|
|
|
|
2021-11-10 11:57:22 -06:00
|
|
|
func setActionRuntimeVars(rc *RunContext, env map[string]string) {
|
|
|
|
actionsRuntimeURL := os.Getenv("ACTIONS_RUNTIME_URL")
|
|
|
|
if actionsRuntimeURL == "" {
|
|
|
|
actionsRuntimeURL = fmt.Sprintf("http://%s:%s/", common.GetOutboundIP().String(), rc.Config.ArtifactServerPort)
|
|
|
|
}
|
|
|
|
env["ACTIONS_RUNTIME_URL"] = actionsRuntimeURL
|
|
|
|
|
|
|
|
actionsRuntimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN")
|
|
|
|
if actionsRuntimeToken == "" {
|
|
|
|
actionsRuntimeToken = "token"
|
|
|
|
}
|
|
|
|
env["ACTIONS_RUNTIME_TOKEN"] = actionsRuntimeToken
|
|
|
|
}
|
|
|
|
|
2020-03-09 19:45:42 -05:00
|
|
|
func (rc *RunContext) localCheckoutPath() (string, bool) {
|
2022-03-21 06:23:06 -05:00
|
|
|
if rc.Config.NoSkipCheckout {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2020-03-09 19:45:42 -05:00
|
|
|
ghContext := rc.getGithubContext()
|
|
|
|
for _, step := range rc.Run.Job().Steps {
|
2021-12-22 13:52:09 -06:00
|
|
|
if isLocalCheckout(ghContext, step) {
|
2020-03-09 19:45:42 -05:00
|
|
|
return step.With["path"], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2021-11-27 12:05:56 -06:00
|
|
|
|
|
|
|
func (rc *RunContext) handleCredentials() (username, password string, err error) {
|
|
|
|
// TODO: remove below 2 lines when we can release act with breaking changes
|
|
|
|
username = rc.Config.Secrets["DOCKER_USERNAME"]
|
|
|
|
password = rc.Config.Secrets["DOCKER_PASSWORD"]
|
|
|
|
|
|
|
|
container := rc.Run.Job().Container()
|
|
|
|
if container == nil || container.Credentials == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.Credentials != nil && len(container.Credentials) != 2 {
|
|
|
|
err = fmt.Errorf("invalid property count for key 'credentials:'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
2022-01-21 10:07:20 -06:00
|
|
|
if username = ee.Interpolate(container.Credentials["username"]); username == "" {
|
2021-11-27 12:05:56 -06:00
|
|
|
err = fmt.Errorf("failed to interpolate container.credentials.username")
|
|
|
|
return
|
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
if password = ee.Interpolate(container.Credentials["password"]); password == "" {
|
2021-11-27 12:05:56 -06:00
|
|
|
err = fmt.Errorf("failed to interpolate container.credentials.password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.Credentials["username"] == "" || container.Credentials["password"] == "" {
|
|
|
|
err = fmt.Errorf("container.credentials cannot be empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, err
|
|
|
|
}
|