2022-02-08 14:17:59 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-03-22 16:13:00 -05:00
|
|
|
"context"
|
2022-02-08 14:17:59 -06:00
|
|
|
"embed"
|
2022-03-22 16:13:00 -05:00
|
|
|
"fmt"
|
2022-02-08 14:17:59 -06:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2022-03-22 16:13:00 -05:00
|
|
|
"path"
|
2022-02-08 14:17:59 -06:00
|
|
|
"path/filepath"
|
2022-03-22 16:13:00 -05:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2022-02-08 14:17:59 -06:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
"github.com/kballard/go-shellquote"
|
2023-02-16 09:34:51 -06:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/container"
|
2022-02-08 14:17:59 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
type actionStep interface {
|
|
|
|
step
|
|
|
|
|
|
|
|
getActionModel() *model.Action
|
2022-06-17 10:55:21 -05:00
|
|
|
getCompositeRunContext(context.Context) *RunContext
|
2022-05-24 08:36:06 -05:00
|
|
|
getCompositeSteps() *compositeSteps
|
2022-02-08 14:17:59 -06:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
type readAction func(ctx context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error)
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
type actionYamlReader func(filename string) (io.Reader, io.Closer, error)
|
2023-02-16 09:34:51 -06:00
|
|
|
|
2022-02-08 14:17:59 -06:00
|
|
|
type fileWriter func(filename string, data []byte, perm fs.FileMode) error
|
|
|
|
|
2022-03-29 12:42:11 -05:00
|
|
|
type runAction func(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-02-08 14:17:59 -06:00
|
|
|
//go:embed res/trampoline.js
|
|
|
|
var trampoline embed.FS
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func readActionImpl(ctx context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) {
|
|
|
|
logger := common.Logger(ctx)
|
2022-02-08 14:17:59 -06:00
|
|
|
reader, closer, err := readFile("action.yml")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
reader, closer, err = readFile("action.yaml")
|
|
|
|
if err != nil {
|
|
|
|
if _, closer, err2 := readFile("Dockerfile"); err2 == nil {
|
|
|
|
closer.Close()
|
|
|
|
action := &model.Action{
|
|
|
|
Name: "(Synthetic)",
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "docker",
|
|
|
|
Image: "Dockerfile",
|
|
|
|
},
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("Using synthetic action %v for Dockerfile", action)
|
2022-02-08 14:17:59 -06:00
|
|
|
return action, nil
|
|
|
|
}
|
|
|
|
if step.With != nil {
|
|
|
|
if val, ok := step.With["args"]; ok {
|
|
|
|
var b []byte
|
|
|
|
if b, err = trampoline.ReadFile("res/trampoline.js"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-15 02:10:15 -06:00
|
|
|
err2 := writeFile(filepath.Join(actionDir, actionPath, "trampoline.js"), b, 0o400)
|
2022-02-08 14:17:59 -06:00
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
action := &model.Action{
|
|
|
|
Name: "(Synthetic)",
|
|
|
|
Inputs: map[string]model.Input{
|
|
|
|
"cwd": {
|
|
|
|
Description: "(Actual working directory)",
|
|
|
|
Required: false,
|
|
|
|
Default: filepath.Join(actionDir, actionPath),
|
|
|
|
},
|
|
|
|
"command": {
|
|
|
|
Description: "(Actual program)",
|
|
|
|
Required: false,
|
|
|
|
Default: val,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "node12",
|
|
|
|
Main: "trampoline.js",
|
|
|
|
},
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("Using synthetic action %v", action)
|
2022-02-08 14:17:59 -06:00
|
|
|
return action, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
action, err := model.ReadAction(reader)
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("Read action %v from '%s'", action, "Unknown")
|
2022-02-08 14:17:59 -06:00
|
|
|
return action, err
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir string, actionPath string, containerActionDir string) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
|
|
|
|
|
|
|
if stepModel.Type() != model.StepTypeUsesActionRemote {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
if err := removeGitIgnore(ctx, actionDir); err != nil {
|
2022-05-24 08:36:06 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var containerActionDirCopy string
|
|
|
|
containerActionDirCopy = strings.TrimSuffix(containerActionDir, actionPath)
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debug(containerActionDirCopy)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
if !strings.HasSuffix(containerActionDirCopy, `/`) {
|
|
|
|
containerActionDirCopy += `/`
|
|
|
|
}
|
|
|
|
return rc.JobContainer.CopyDir(containerActionDirCopy, actionDir+"/", rc.Config.UseGitIgnore)(ctx)
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:42:11 -05:00
|
|
|
func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor {
|
2022-03-22 16:13:00 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
return func(ctx context.Context) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-03-29 12:42:11 -05:00
|
|
|
actionPath := ""
|
2022-05-11 14:06:05 -05:00
|
|
|
if remoteAction != nil && remoteAction.Path != "" {
|
|
|
|
actionPath = remoteAction.Path
|
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2022-05-11 14:06:05 -05:00
|
|
|
action := step.getActionModel()
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("About to run action %v", action)
|
2022-05-11 14:06:05 -05:00
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
err := setupActionEnv(ctx, step, remoteAction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-29 12:42:11 -05:00
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-03-29 12:42:11 -05:00
|
|
|
actionLocation := path.Join(actionDir, actionPath)
|
2022-03-22 16:13:00 -05:00
|
|
|
actionName, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("type=%v actionDir=%s actionPath=%s workdir=%s actionCacheDir=%s actionName=%s containerActionDir=%s", stepModel.Type(), actionDir, actionPath, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir)
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
switch action.Runs.Using {
|
|
|
|
case model.ActionRunsUsingNode12, model.ActionRunsUsingNode16:
|
2022-05-24 08:36:06 -05:00
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
2022-03-22 16:13:00 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Main)}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("executing remote job container: %s", containerArgs)
|
2022-10-06 16:58:16 -05:00
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
2022-12-09 05:16:15 -06:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
|
|
|
case model.ActionRunsUsingDocker:
|
2022-03-29 12:42:11 -05:00
|
|
|
location := actionLocation
|
|
|
|
if remoteAction == nil {
|
|
|
|
location = containerActionDir
|
|
|
|
}
|
|
|
|
return execAsDocker(ctx, step, actionName, location, remoteAction == nil)
|
2022-03-22 16:13:00 -05:00
|
|
|
case model.ActionRunsUsingComposite:
|
2022-05-24 08:36:06 -05:00
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
2022-03-29 12:42:11 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
return execAsComposite(step)(ctx)
|
2023-02-15 02:10:15 -06:00
|
|
|
case model.ActionRunsUsingGo:
|
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-14 03:55:36 -05:00
|
|
|
|
2023-03-21 02:31:30 -05:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
|
|
|
|
2023-03-14 03:55:36 -05:00
|
|
|
execFileName := fmt.Sprintf("%s.out", action.Runs.Main)
|
|
|
|
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Main}
|
|
|
|
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
|
|
|
|
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
rc.execJobContainer(buildArgs, *step.getEnv(), "", containerActionDir),
|
|
|
|
rc.execJobContainer(execArgs, *step.getEnv(), "", ""),
|
|
|
|
)(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf(fmt.Sprintf("The runs.using key must be one of: %v, got %s", []string{
|
|
|
|
model.ActionRunsUsingDocker,
|
|
|
|
model.ActionRunsUsingNode12,
|
|
|
|
model.ActionRunsUsingNode16,
|
|
|
|
model.ActionRunsUsingComposite,
|
2023-03-09 08:51:58 -06:00
|
|
|
model.ActionRunsUsingGo,
|
2022-03-22 16:13:00 -05:00
|
|
|
}, action.Runs.Using))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
func setupActionEnv(ctx context.Context, step actionStep, remoteAction *remoteAction) error {
|
|
|
|
rc := step.getRunContext()
|
|
|
|
|
|
|
|
// A few fields in the environment (e.g. GITHUB_ACTION_REPOSITORY)
|
|
|
|
// are dependent on the action. That means we can complete the
|
|
|
|
// setup only after resolving the whole action model and cloning
|
|
|
|
// the action
|
|
|
|
rc.withGithubEnv(ctx, step.getGithubContext(ctx), *step.getEnv())
|
|
|
|
populateEnvsFromSavedState(step.getEnv(), step, rc)
|
|
|
|
populateEnvsFromInput(ctx, step.getEnv(), step.getActionModel(), rc)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
// https://github.com/nektos/act/issues/228#issuecomment-629709055
|
|
|
|
// files in .gitignore are not copied in a Docker container
|
|
|
|
// this causes issues with actions that ignore other important resources
|
|
|
|
// such as `node_modules` for example
|
2022-06-17 10:55:21 -05:00
|
|
|
func removeGitIgnore(ctx context.Context, directory string) error {
|
2022-03-22 16:13:00 -05:00
|
|
|
gitIgnorePath := path.Join(directory, ".gitignore")
|
|
|
|
if _, err := os.Stat(gitIgnorePath); err == nil {
|
|
|
|
// .gitignore exists
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Debugf("Removing %s before docker cp", gitIgnorePath)
|
2022-03-22 16:13:00 -05:00
|
|
|
err := os.Remove(gitIgnorePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: break out parts of function to reduce complexicity
|
2022-10-06 16:58:16 -05:00
|
|
|
//
|
|
|
|
//nolint:gocyclo
|
2022-03-29 12:42:11 -05:00
|
|
|
func execAsDocker(ctx context.Context, step actionStep, actionName string, basedir string, localAction bool) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-03-29 12:42:11 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
action := step.getActionModel()
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
var prepImage common.Executor
|
|
|
|
var image string
|
2023-02-02 11:24:35 -06:00
|
|
|
forcePull := false
|
2022-03-22 16:13:00 -05:00
|
|
|
if strings.HasPrefix(action.Runs.Image, "docker://") {
|
|
|
|
image = strings.TrimPrefix(action.Runs.Image, "docker://")
|
2023-02-02 11:24:35 -06:00
|
|
|
// Apply forcePull only for prebuild docker images
|
|
|
|
forcePull = rc.Config.ForcePull
|
2022-03-22 16:13:00 -05:00
|
|
|
} else {
|
|
|
|
// "-dockeraction" enshures that "./", "./test " won't get converted to "act-:latest", "act-test-:latest" which are invalid docker image names
|
|
|
|
image = fmt.Sprintf("%s-dockeraction:%s", regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(actionName, "-"), "latest")
|
|
|
|
image = fmt.Sprintf("act-%s", strings.TrimLeft(image, "-"))
|
|
|
|
image = strings.ToLower(image)
|
2023-02-08 11:14:43 -06:00
|
|
|
contextDir, fileName := filepath.Split(filepath.Join(basedir, action.Runs.Image))
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
anyArchExists, err := container.ImageExistsLocally(ctx, image, "any")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
correctArchExists, err := container.ImageExistsLocally(ctx, image, rc.Config.ContainerArchitecture)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyArchExists && !correctArchExists {
|
|
|
|
wasRemoved, err := container.RemoveImage(ctx, image, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !wasRemoved {
|
|
|
|
return fmt.Errorf("failed to remove image '%s'", image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !correctArchExists || rc.Config.ForceRebuild {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("image '%s' for architecture '%s' will be built from context '%s", image, rc.Config.ContainerArchitecture, contextDir)
|
2022-03-22 16:13:00 -05:00
|
|
|
var actionContainer container.Container
|
|
|
|
if localAction {
|
2022-03-29 12:42:11 -05:00
|
|
|
actionContainer = rc.JobContainer
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
prepImage = container.NewDockerBuildExecutor(container.NewDockerBuildExecutorInput{
|
|
|
|
ContextDir: contextDir,
|
2023-02-08 11:14:43 -06:00
|
|
|
Dockerfile: fileName,
|
2022-03-22 16:13:00 -05:00
|
|
|
ImageTag: image,
|
|
|
|
Container: actionContainer,
|
|
|
|
Platform: rc.Config.ContainerArchitecture,
|
|
|
|
})
|
|
|
|
} else {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("image '%s' for architecture '%s' already exists", image, rc.Config.ContainerArchitecture)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
eval := rc.NewStepExpressionEvaluator(ctx, step)
|
|
|
|
cmd, err := shellquote.Split(eval.Interpolate(ctx, step.getStepModel().With["args"]))
|
2022-03-22 16:13:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
cmd = action.Runs.Args
|
2022-06-17 10:55:21 -05:00
|
|
|
evalDockerArgs(ctx, step, action, &cmd)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
entrypoint := strings.Fields(eval.Interpolate(ctx, step.getStepModel().With["entrypoint"]))
|
2022-03-22 16:13:00 -05:00
|
|
|
if len(entrypoint) == 0 {
|
|
|
|
if action.Runs.Entrypoint != "" {
|
|
|
|
entrypoint, err = shellquote.Split(action.Runs.Entrypoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entrypoint = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stepContainer := newStepContainer(ctx, step, image, cmd, entrypoint)
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
prepImage,
|
2023-02-02 11:24:35 -06:00
|
|
|
stepContainer.Pull(forcePull),
|
2022-03-22 16:13:00 -05:00
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
stepContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
|
|
|
|
stepContainer.Start(true),
|
|
|
|
).Finally(
|
|
|
|
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
|
|
|
|
).Finally(stepContainer.Close())(ctx)
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func evalDockerArgs(ctx context.Context, step step, action *model.Action, cmd *[]string) {
|
2022-03-22 16:13:00 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
2022-10-06 16:58:16 -05:00
|
|
|
|
|
|
|
inputs := make(map[string]string)
|
2022-06-17 10:55:21 -05:00
|
|
|
eval := rc.NewExpressionEvaluator(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
// Set Defaults
|
|
|
|
for k, input := range action.Inputs {
|
2022-06-17 10:55:21 -05:00
|
|
|
inputs[k] = eval.Interpolate(ctx, input.Default)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
if stepModel.With != nil {
|
|
|
|
for k, v := range stepModel.With {
|
2022-06-17 10:55:21 -05:00
|
|
|
inputs[k] = eval.Interpolate(ctx, v)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-18 13:09:57 -05:00
|
|
|
mergeIntoMap(step, step.getEnv(), inputs)
|
2022-10-06 16:58:16 -05:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
stepEE := rc.NewStepExpressionEvaluator(ctx, step)
|
2022-03-22 16:13:00 -05:00
|
|
|
for i, v := range *cmd {
|
2022-06-17 10:55:21 -05:00
|
|
|
(*cmd)[i] = stepEE.Interpolate(ctx, v)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
2023-04-18 13:09:57 -05:00
|
|
|
mergeIntoMap(step, step.getEnv(), action.Runs.Env)
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
ee := rc.NewStepExpressionEvaluator(ctx, step)
|
2022-03-22 16:13:00 -05:00
|
|
|
for k, v := range *step.getEnv() {
|
2022-06-17 10:55:21 -05:00
|
|
|
(*step.getEnv())[k] = ee.Interpolate(ctx, v)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStepContainer(ctx context.Context, step step, image string, cmd []string, entrypoint []string) container.Container {
|
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
|
|
|
|
if rc.Config.LogOutput {
|
|
|
|
rawLogger.Infof("%s", s)
|
|
|
|
} else {
|
|
|
|
rawLogger.Debugf("%s", s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
envList := make([]string, 0)
|
|
|
|
for k, v := range *step.getEnv() {
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache"))
|
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
|
2022-08-29 10:39:31 -05:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx)))
|
2022-03-22 16:13:00 -05:00
|
|
|
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
|
|
|
|
|
|
|
|
binds, mounts := rc.GetBindsAndMounts()
|
2023-03-14 09:07:31 -05:00
|
|
|
networkMode := fmt.Sprintf("container:%s", rc.jobContainerName())
|
|
|
|
if rc.IsHostEnv(ctx) {
|
|
|
|
networkMode = "default"
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
stepContainer := container.NewContainer(&container.NewContainerInput{
|
2023-07-10 21:08:22 -05:00
|
|
|
Cmd: cmd,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
WorkingDir: rc.JobContainer.ToContainerPath(rc.Config.Workdir),
|
|
|
|
Image: image,
|
|
|
|
Username: rc.Config.Secrets["DOCKER_USERNAME"],
|
|
|
|
Password: rc.Config.Secrets["DOCKER_PASSWORD"],
|
|
|
|
Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID),
|
|
|
|
Env: envList,
|
|
|
|
Mounts: mounts,
|
|
|
|
NetworkMode: networkMode,
|
|
|
|
Binds: binds,
|
|
|
|
Stdout: logWriter,
|
|
|
|
Stderr: logWriter,
|
|
|
|
Privileged: rc.Config.Privileged,
|
|
|
|
UsernsMode: rc.Config.UsernsMode,
|
|
|
|
Platform: rc.Config.ContainerArchitecture,
|
|
|
|
Options: rc.Config.ContainerOptions,
|
|
|
|
AutoRemove: rc.Config.AutoRemove,
|
|
|
|
ValidVolumes: rc.Config.ValidVolumes,
|
2022-03-22 16:13:00 -05:00
|
|
|
})
|
|
|
|
return stepContainer
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
func populateEnvsFromSavedState(env *map[string]string, step actionStep, rc *RunContext) {
|
2022-12-15 11:08:31 -06:00
|
|
|
state, ok := rc.IntraActionState[step.getStepModel().ID]
|
|
|
|
if ok {
|
|
|
|
for name, value := range state {
|
2022-05-24 08:36:06 -05:00
|
|
|
envName := fmt.Sprintf("STATE_%s", name)
|
|
|
|
(*env)[envName] = value
|
2022-05-11 14:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func populateEnvsFromInput(ctx context.Context, env *map[string]string, action *model.Action, rc *RunContext) {
|
|
|
|
eval := rc.NewExpressionEvaluator(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
for inputID, input := range action.Inputs {
|
|
|
|
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_")
|
|
|
|
envKey = fmt.Sprintf("INPUT_%s", envKey)
|
|
|
|
if _, ok := (*env)[envKey]; !ok {
|
2022-06-17 10:55:21 -05:00
|
|
|
(*env)[envKey] = eval.Interpolate(ctx, input.Default)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerActionPaths(step *model.Step, actionDir string, rc *RunContext) (string, string) {
|
|
|
|
actionName := ""
|
|
|
|
containerActionDir := "."
|
|
|
|
if step.Type() != model.StepTypeUsesActionRemote {
|
|
|
|
actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir)
|
2022-11-16 15:29:45 -06:00
|
|
|
containerActionDir = rc.JobContainer.ToContainerPath(rc.Config.Workdir) + "/" + actionName
|
2022-03-22 16:13:00 -05:00
|
|
|
actionName = "./" + actionName
|
|
|
|
} else if step.Type() == model.StepTypeUsesActionRemote {
|
|
|
|
actionName = getOsSafeRelativePath(actionDir, rc.ActionCacheDir())
|
2022-11-16 15:29:45 -06:00
|
|
|
containerActionDir = rc.JobContainer.GetActPath() + "/actions/" + actionName
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if actionName == "" {
|
|
|
|
actionName = filepath.Base(actionDir)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
actionName = strings.ReplaceAll(actionName, "\\", "/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return actionName, containerActionDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOsSafeRelativePath(s, prefix string) string {
|
|
|
|
actionName := strings.TrimPrefix(s, prefix)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
actionName = strings.ReplaceAll(actionName, "\\", "/")
|
|
|
|
}
|
|
|
|
actionName = strings.TrimPrefix(actionName, "/")
|
|
|
|
|
|
|
|
return actionName
|
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
func shouldRunPreStep(step actionStep) common.Conditional {
|
|
|
|
return func(ctx context.Context) bool {
|
|
|
|
log := common.Logger(ctx)
|
|
|
|
|
|
|
|
if step.getActionModel() == nil {
|
|
|
|
log.Debugf("skip pre step for '%s': no action model available", step.getStepModel())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasPreStep(step actionStep) common.Conditional {
|
|
|
|
return func(ctx context.Context) bool {
|
|
|
|
action := step.getActionModel()
|
|
|
|
return action.Runs.Using == model.ActionRunsUsingComposite ||
|
|
|
|
((action.Runs.Using == model.ActionRunsUsingNode12 ||
|
2023-03-14 03:55:36 -05:00
|
|
|
action.Runs.Using == model.ActionRunsUsingNode16 ||
|
|
|
|
action.Runs.Using == model.ActionRunsUsingGo) &&
|
2022-05-24 08:36:06 -05:00
|
|
|
action.Runs.Pre != "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPreStep(step actionStep) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("run pre step for '%s'", step.getStepModel())
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
|
|
|
action := step.getActionModel()
|
|
|
|
|
|
|
|
switch action.Runs.Using {
|
|
|
|
case model.ActionRunsUsingNode12, model.ActionRunsUsingNode16:
|
2022-09-26 01:08:08 -05:00
|
|
|
// defaults in pre steps were missing, however provided inputs are available
|
|
|
|
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
2022-05-24 08:36:06 -05:00
|
|
|
// todo: refactor into step
|
|
|
|
var actionDir string
|
|
|
|
var actionPath string
|
|
|
|
if _, ok := step.(*stepActionRemote); ok {
|
|
|
|
actionPath = newRemoteAction(stepModel.Uses).Path
|
2023-02-24 00:20:34 -06:00
|
|
|
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(stepModel.Uses))
|
2022-05-24 08:36:06 -05:00
|
|
|
} else {
|
|
|
|
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
|
|
actionPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
actionLocation := ""
|
|
|
|
if actionPath != "" {
|
|
|
|
actionLocation = path.Join(actionDir, actionPath)
|
|
|
|
} else {
|
|
|
|
actionLocation = actionDir
|
|
|
|
}
|
|
|
|
|
|
|
|
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
|
|
|
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Pre)}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("executing remote job container: %s", containerArgs)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
2022-12-09 05:16:15 -06:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
|
|
|
|
|
|
|
case model.ActionRunsUsingComposite:
|
2022-10-06 16:58:16 -05:00
|
|
|
if step.getCompositeSteps() == nil {
|
|
|
|
step.getCompositeRunContext(ctx)
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:16:07 -06:00
|
|
|
if steps := step.getCompositeSteps(); steps != nil && steps.pre != nil {
|
|
|
|
return steps.pre(ctx)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("missing steps in composite action")
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2023-03-14 03:55:36 -05:00
|
|
|
case model.ActionRunsUsingGo:
|
|
|
|
// defaults in pre steps were missing, however provided inputs are available
|
|
|
|
populateEnvsFromInput(ctx, step.getEnv(), action, rc)
|
|
|
|
// todo: refactor into step
|
|
|
|
var actionDir string
|
|
|
|
var actionPath string
|
|
|
|
if _, ok := step.(*stepActionRemote); ok {
|
|
|
|
actionPath = newRemoteAction(stepModel.Uses).Path
|
|
|
|
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(stepModel.Uses))
|
|
|
|
} else {
|
|
|
|
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
|
|
actionPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
actionLocation := ""
|
|
|
|
if actionPath != "" {
|
|
|
|
actionLocation = path.Join(actionDir, actionPath)
|
|
|
|
} else {
|
|
|
|
actionLocation = actionDir
|
|
|
|
}
|
|
|
|
|
|
|
|
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
|
|
|
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-21 02:31:30 -05:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
|
|
|
|
2023-03-14 03:55:36 -05:00
|
|
|
execFileName := fmt.Sprintf("%s.out", action.Runs.Pre)
|
|
|
|
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Pre}
|
|
|
|
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
|
|
|
|
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
rc.execJobContainer(buildArgs, *step.getEnv(), "", containerActionDir),
|
|
|
|
rc.execJobContainer(execArgs, *step.getEnv(), "", ""),
|
|
|
|
)(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldRunPostStep(step actionStep) common.Conditional {
|
|
|
|
return func(ctx context.Context) bool {
|
|
|
|
log := common.Logger(ctx)
|
|
|
|
stepResults := step.getRunContext().getStepsContext()
|
|
|
|
stepResult := stepResults[step.getStepModel().ID]
|
|
|
|
|
|
|
|
if stepResult == nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; step was not executed", step.getStepModel())
|
2022-05-24 08:36:06 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if stepResult.Conclusion == model.StepStatusSkipped {
|
2022-06-17 10:55:21 -05:00
|
|
|
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s'; main step was skipped", step.getStepModel())
|
2022-05-24 08:36:06 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if step.getActionModel() == nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
log.WithField("stepResult", model.StepStatusSkipped).Debugf("skipping post step for '%s': no action model available", step.getStepModel())
|
2022-05-24 08:36:06 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasPostStep(step actionStep) common.Conditional {
|
|
|
|
return func(ctx context.Context) bool {
|
|
|
|
action := step.getActionModel()
|
|
|
|
return action.Runs.Using == model.ActionRunsUsingComposite ||
|
|
|
|
((action.Runs.Using == model.ActionRunsUsingNode12 ||
|
2023-03-14 03:55:36 -05:00
|
|
|
action.Runs.Using == model.ActionRunsUsingNode16 ||
|
|
|
|
action.Runs.Using == model.ActionRunsUsingGo) &&
|
2022-05-24 08:36:06 -05:00
|
|
|
action.Runs.Post != "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPostStep(step actionStep) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("run post step for '%s'", step.getStepModel())
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
|
|
|
action := step.getActionModel()
|
|
|
|
|
|
|
|
// todo: refactor into step
|
|
|
|
var actionDir string
|
|
|
|
var actionPath string
|
|
|
|
if _, ok := step.(*stepActionRemote); ok {
|
|
|
|
actionPath = newRemoteAction(stepModel.Uses).Path
|
2023-02-24 00:20:34 -06:00
|
|
|
actionDir = fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(stepModel.Uses))
|
2022-05-24 08:36:06 -05:00
|
|
|
} else {
|
|
|
|
actionDir = filepath.Join(rc.Config.Workdir, stepModel.Uses)
|
|
|
|
actionPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
actionLocation := ""
|
|
|
|
if actionPath != "" {
|
|
|
|
actionLocation = path.Join(actionDir, actionPath)
|
|
|
|
} else {
|
|
|
|
actionLocation = actionDir
|
|
|
|
}
|
|
|
|
|
|
|
|
_, containerActionDir := getContainerActionPaths(stepModel, actionLocation, rc)
|
|
|
|
|
|
|
|
switch action.Runs.Using {
|
|
|
|
case model.ActionRunsUsingNode12, model.ActionRunsUsingNode16:
|
|
|
|
|
|
|
|
populateEnvsFromSavedState(step.getEnv(), step, rc)
|
|
|
|
|
|
|
|
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Post)}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Debugf("executing remote job container: %s", containerArgs)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
2022-12-09 05:16:15 -06:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
return rc.execJobContainer(containerArgs, *step.getEnv(), "", "")(ctx)
|
|
|
|
|
|
|
|
case model.ActionRunsUsingComposite:
|
|
|
|
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:16:07 -06:00
|
|
|
if steps := step.getCompositeSteps(); steps != nil && steps.post != nil {
|
|
|
|
return steps.post(ctx)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("missing steps in composite action")
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2023-03-14 03:55:36 -05:00
|
|
|
case model.ActionRunsUsingGo:
|
|
|
|
populateEnvsFromSavedState(step.getEnv(), step, rc)
|
2023-03-21 02:31:30 -05:00
|
|
|
rc.ApplyExtraPath(ctx, step.getEnv())
|
2023-03-14 03:55:36 -05:00
|
|
|
|
|
|
|
execFileName := fmt.Sprintf("%s.out", action.Runs.Post)
|
|
|
|
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Post}
|
|
|
|
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
|
|
|
|
|
|
|
|
return common.NewPipelineExecutor(
|
|
|
|
rc.execJobContainer(buildArgs, *step.getEnv(), "", containerActionDir),
|
|
|
|
rc.execJobContainer(execArgs, *step.getEnv(), "", ""),
|
|
|
|
)(ctx)
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|