2022-02-08 11:22:41 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-05-24 09:52:25 -05:00
|
|
|
"time"
|
2022-02-08 11:22:41 -06:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2023-06-27 21:27:12 -05:00
|
|
|
"github.com/nektos/act/pkg/container"
|
2022-02-08 11:22:41 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jobInfo interface {
|
|
|
|
matrix() map[string]interface{}
|
|
|
|
steps() []*model.Step
|
|
|
|
startContainer() common.Executor
|
|
|
|
stopContainer() common.Executor
|
|
|
|
closeContainer() common.Executor
|
|
|
|
interpolateOutputs() common.Executor
|
|
|
|
result(result string)
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executor {
|
2022-02-08 11:22:41 -06:00
|
|
|
steps := make([]common.Executor, 0)
|
2022-03-22 16:13:00 -05:00
|
|
|
preSteps := make([]common.Executor, 0)
|
2022-05-24 08:36:06 -05:00
|
|
|
var postExecutor common.Executor
|
2022-02-08 11:22:41 -06:00
|
|
|
|
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-02-08 11:22:41 -06:00
|
|
|
if len(info.matrix()) > 0 {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Infof("\U0001F9EA Matrix: %v", info.matrix())
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
infoSteps := info.steps()
|
2022-02-08 11:22:41 -06:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
if len(infoSteps) == 0 {
|
|
|
|
return common.NewDebugExecutor("No steps found")
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:29:45 -06:00
|
|
|
preSteps = append(preSteps, func(ctx context.Context) error {
|
|
|
|
// Have to be skipped for some Tests
|
|
|
|
if rc.Run == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
|
|
|
|
// evaluate environment variables since they can contain
|
|
|
|
// GitHub's special environment variables.
|
|
|
|
for k, v := range rc.GetEnv() {
|
|
|
|
rc.Env[k] = rc.ExprEval.Interpolate(ctx, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
for i, stepModel := range infoSteps {
|
2022-06-17 10:55:21 -05:00
|
|
|
stepModel := stepModel
|
2022-05-12 14:23:34 -05:00
|
|
|
if stepModel == nil {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return fmt.Errorf("invalid Step %v: missing run or uses key", i)
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
if stepModel.ID == "" {
|
|
|
|
stepModel.ID = fmt.Sprintf("%d", i)
|
|
|
|
}
|
2022-09-22 08:56:43 -05:00
|
|
|
stepModel.Number = i
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
step, err := sf.newStep(stepModel, rc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return common.NewErrorExecutor(err)
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2023-04-14 02:42:03 -05:00
|
|
|
preExec := step.pre()
|
|
|
|
preSteps = append(preSteps, useStepLogger(rc, stepModel, stepStagePre, func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
preErr := preExec(ctx)
|
|
|
|
if preErr != nil {
|
|
|
|
logger.Errorf("%v", preErr)
|
|
|
|
common.SetJobError(ctx, preErr)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
logger.Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return preErr
|
|
|
|
}))
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
stepExec := step.main()
|
2022-06-17 10:55:21 -05:00
|
|
|
steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
err := stepExec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("%v", err)
|
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
logger.Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
postExec := useStepLogger(rc, stepModel, stepStagePost, step.post())
|
2022-05-24 08:36:06 -05:00
|
|
|
if postExecutor != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
// run the post exector in reverse order
|
|
|
|
postExecutor = postExec.Finally(postExecutor)
|
2022-05-24 08:36:06 -05:00
|
|
|
} else {
|
2022-06-17 10:55:21 -05:00
|
|
|
postExecutor = postExec
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-02-10 10:54:58 -06:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
2022-02-08 11:22:41 -06:00
|
|
|
jobError := common.JobError(ctx)
|
2022-12-06 09:45:06 -06:00
|
|
|
var err error
|
|
|
|
if rc.Config.AutoRemove || jobError == nil {
|
|
|
|
// always allow 1 min for stopping and removing the runner, even if we were cancelled
|
|
|
|
ctx, cancel := context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), time.Minute)
|
|
|
|
defer cancel()
|
2023-04-18 22:23:28 -05:00
|
|
|
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
|
|
|
if err := rc.stopServiceContainers()(ctx); err != nil {
|
|
|
|
logger.Errorf("Error while cleaning services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("Cleaning up container for job %s", rc.JobName)
|
Fix container network issue (#56)
Follow: https://gitea.com/gitea/act_runner/pulls/184
Close https://gitea.com/gitea/act_runner/issues/177
#### changes:
- `act` create new networks only if the value of `NeedCreateNetwork` is true, and remove these networks at last. `NeedCreateNetwork` is passed by `act_runner`. 'NeedCreateNetwork' is true only if `container.network` in the configuration file of the `act_runner` is empty.
- In the `docker create` phase, specify the network to which containers will connect. Because, if not specify , container will connect to `bridge` network which is created automatically by Docker.
- If the network is user defined network ( the value of `container.network` is empty or `<custom-network>`. Because, the network created by `act` is also user defined network.), will also specify alias by `--network-alias`. The alias of service is `<service-id>`. So we can be access service container by `<service-id>:<port>` in the steps of job.
- Won't try to `docker network connect ` network after `docker start` any more.
- Because on the one hand, `docker network connect` applies only to user defined networks, if try to `docker network connect host <container-name>` will return error.
- On the other hand, we just specify network in the stage of `docker create`, the same effect can be achieved.
- Won't try to remove containers and networks berfore the stage of `docker start`, because the name of these containers and netwoks won't be repeat.
Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act/pulls/56
Reviewed-by: Jason Song <i@wolfogre.com>
Co-authored-by: sillyguodong <gedong_1994@163.com>
Co-committed-by: sillyguodong <gedong_1994@163.com>
2023-05-16 01:03:55 -05:00
|
|
|
if err = info.stopContainer()(ctx); err != nil {
|
|
|
|
logger.Errorf("Error while stop job container: %v", err)
|
|
|
|
}
|
2023-06-27 21:27:12 -05:00
|
|
|
if !rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == "" {
|
|
|
|
// clean network in docker mode only
|
Fix container network issue (#56)
Follow: https://gitea.com/gitea/act_runner/pulls/184
Close https://gitea.com/gitea/act_runner/issues/177
#### changes:
- `act` create new networks only if the value of `NeedCreateNetwork` is true, and remove these networks at last. `NeedCreateNetwork` is passed by `act_runner`. 'NeedCreateNetwork' is true only if `container.network` in the configuration file of the `act_runner` is empty.
- In the `docker create` phase, specify the network to which containers will connect. Because, if not specify , container will connect to `bridge` network which is created automatically by Docker.
- If the network is user defined network ( the value of `container.network` is empty or `<custom-network>`. Because, the network created by `act` is also user defined network.), will also specify alias by `--network-alias`. The alias of service is `<service-id>`. So we can be access service container by `<service-id>:<port>` in the steps of job.
- Won't try to `docker network connect ` network after `docker start` any more.
- Because on the one hand, `docker network connect` applies only to user defined networks, if try to `docker network connect host <container-name>` will return error.
- On the other hand, we just specify network in the stage of `docker create`, the same effect can be achieved.
- Won't try to remove containers and networks berfore the stage of `docker start`, because the name of these containers and netwoks won't be repeat.
Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act/pulls/56
Reviewed-by: Jason Song <i@wolfogre.com>
Co-authored-by: sillyguodong <gedong_1994@163.com>
Co-committed-by: sillyguodong <gedong_1994@163.com>
2023-05-16 01:03:55 -05:00
|
|
|
// if the value of `ContainerNetworkMode` is empty string,
|
|
|
|
// it means that the network to which containers are connecting is created by `act_runner`,
|
|
|
|
// so, we should remove the network at last.
|
|
|
|
logger.Infof("Cleaning up network for job %s, and network name is: %s", rc.JobName, rc.networkName())
|
2023-06-27 21:27:12 -05:00
|
|
|
if err := container.NewDockerNetworkRemoveExecutor(rc.networkName())(ctx); err != nil {
|
Fix container network issue (#56)
Follow: https://gitea.com/gitea/act_runner/pulls/184
Close https://gitea.com/gitea/act_runner/issues/177
#### changes:
- `act` create new networks only if the value of `NeedCreateNetwork` is true, and remove these networks at last. `NeedCreateNetwork` is passed by `act_runner`. 'NeedCreateNetwork' is true only if `container.network` in the configuration file of the `act_runner` is empty.
- In the `docker create` phase, specify the network to which containers will connect. Because, if not specify , container will connect to `bridge` network which is created automatically by Docker.
- If the network is user defined network ( the value of `container.network` is empty or `<custom-network>`. Because, the network created by `act` is also user defined network.), will also specify alias by `--network-alias`. The alias of service is `<service-id>`. So we can be access service container by `<service-id>:<port>` in the steps of job.
- Won't try to `docker network connect ` network after `docker start` any more.
- Because on the one hand, `docker network connect` applies only to user defined networks, if try to `docker network connect host <container-name>` will return error.
- On the other hand, we just specify network in the stage of `docker create`, the same effect can be achieved.
- Won't try to remove containers and networks berfore the stage of `docker start`, because the name of these containers and netwoks won't be repeat.
Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act/pulls/56
Reviewed-by: Jason Song <i@wolfogre.com>
Co-authored-by: sillyguodong <gedong_1994@163.com>
Co-committed-by: sillyguodong <gedong_1994@163.com>
2023-05-16 01:03:55 -05:00
|
|
|
logger.Errorf("Error while cleaning network: %v", err)
|
|
|
|
}
|
2023-04-18 22:23:28 -05:00
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-12-06 09:45:06 -06:00
|
|
|
setJobResult(ctx, info, rc, jobError == nil)
|
2022-12-15 10:45:22 -06:00
|
|
|
setJobOutputs(ctx, rc)
|
2022-02-08 11:22:41 -06:00
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
return err
|
2022-02-08 11:22:41 -06:00
|
|
|
})
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
pipeline = append(pipeline, preSteps...)
|
|
|
|
pipeline = append(pipeline, steps...)
|
|
|
|
|
2022-09-26 01:17:06 -05:00
|
|
|
return common.NewPipelineExecutor(info.startContainer(), common.NewPipelineExecutor(pipeline...).
|
2023-07-10 19:12:12 -05:00
|
|
|
Finally(func(ctx context.Context) error { //nolint:contextcheck
|
2022-05-24 09:52:25 -05:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
if ctx.Err() == context.Canceled {
|
|
|
|
// in case of an aborted run, we still should execute the
|
|
|
|
// post steps to allow cleanup.
|
2022-12-06 09:45:06 -06:00
|
|
|
ctx, cancel = context.WithTimeout(common.WithLogger(context.Background(), common.Logger(ctx)), 5*time.Minute)
|
2022-05-24 09:52:25 -05:00
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
return postExecutor(ctx)
|
|
|
|
}).
|
2022-05-24 08:36:06 -05:00
|
|
|
Finally(info.interpolateOutputs()).
|
2022-09-26 01:17:06 -05:00
|
|
|
Finally(info.closeContainer()))
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
func setJobResult(ctx context.Context, info jobInfo, rc *RunContext, success bool) {
|
|
|
|
logger := common.Logger(ctx)
|
2023-01-10 15:31:12 -06:00
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
jobResult := "success"
|
2023-01-10 15:31:12 -06:00
|
|
|
// we have only one result for a whole matrix build, so we need
|
|
|
|
// to keep an existing result state if we run a matrix
|
|
|
|
if len(info.matrix()) > 0 && rc.Run.Job().Result != "" {
|
|
|
|
jobResult = rc.Run.Job().Result
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
if !success {
|
|
|
|
jobResult = "failure"
|
|
|
|
}
|
2023-01-10 15:31:12 -06:00
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
info.result(jobResult)
|
2022-12-15 10:45:22 -06:00
|
|
|
if rc.caller != nil {
|
|
|
|
// set reusable workflow job result
|
|
|
|
rc.caller.runContext.result(jobResult)
|
|
|
|
}
|
2023-01-10 15:31:12 -06:00
|
|
|
|
|
|
|
jobResultMessage := "succeeded"
|
|
|
|
if jobResult != "success" {
|
|
|
|
jobResultMessage = "failed"
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:45:06 -06:00
|
|
|
logger.WithField("jobResult", jobResult).Infof("\U0001F3C1 Job %s", jobResultMessage)
|
|
|
|
}
|
|
|
|
|
2022-12-15 10:45:22 -06:00
|
|
|
func setJobOutputs(ctx context.Context, rc *RunContext) {
|
|
|
|
if rc.caller != nil {
|
|
|
|
// map outputs for reusable workflows
|
|
|
|
callerOutputs := make(map[string]string)
|
|
|
|
|
|
|
|
ee := rc.NewExpressionEvaluator(ctx)
|
2023-02-23 16:34:47 -06:00
|
|
|
|
|
|
|
for k, v := range rc.Run.Workflow.WorkflowCallConfig().Outputs {
|
|
|
|
callerOutputs[k] = ee.Interpolate(ctx, ee.Interpolate(ctx, v.Value))
|
2022-12-15 10:45:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rc.caller.runContext.Run.Job().Outputs = callerOutputs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
2022-12-05 03:08:17 -06:00
|
|
|
ctx = withStepLogger(ctx, stepModel.Number, stepModel.ID, rc.ExprEval.Interpolate(ctx, stepModel.String()), stage.String())
|
2022-06-17 10:55:21 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
|
|
|
|
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)
|
|
|
|
|
|
|
|
return executor(ctx)
|
|
|
|
}
|
|
|
|
}
|