2019-10-10 00:52:42 -04:00
using System ;
using System.Collections.Generic ;
using GitHub.DistributedTask.WebApi ;
using GitHub.Runner.Common.Util ;
using Pipelines = GitHub . DistributedTask . Pipelines ;
using GitHub.Runner.Common ;
using GitHub.Runner.Sdk ;
namespace GitHub.Runner.Worker.Handlers
{
[ServiceLocator(Default = typeof(HandlerFactory))]
public interface IHandlerFactory : IRunnerService
{
IHandler Create (
IExecutionContext executionContext ,
Pipelines . ActionStepDefinitionReference action ,
IStepHost stepHost ,
ActionExecutionData data ,
Dictionary < string , string > inputs ,
Dictionary < string , string > environment ,
Variables runtimeVariables ,
2021-07-28 15:35:21 -04:00
string actionDirectory ,
List < JobExtensionRunner > localActionContainerSetupSteps ) ;
2019-10-10 00:52:42 -04:00
}
public sealed class HandlerFactory : RunnerService , IHandlerFactory
{
public IHandler Create (
IExecutionContext executionContext ,
Pipelines . ActionStepDefinitionReference action ,
IStepHost stepHost ,
ActionExecutionData data ,
Dictionary < string , string > inputs ,
Dictionary < string , string > environment ,
Variables runtimeVariables ,
2021-07-28 15:35:21 -04:00
string actionDirectory ,
List < JobExtensionRunner > localActionContainerSetupSteps )
2019-10-10 00:52:42 -04:00
{
// Validate args.
Trace . Entering ( ) ;
ArgUtil . NotNull ( executionContext , nameof ( executionContext ) ) ;
ArgUtil . NotNull ( stepHost , nameof ( stepHost ) ) ;
ArgUtil . NotNull ( data , nameof ( data ) ) ;
ArgUtil . NotNull ( inputs , nameof ( inputs ) ) ;
ArgUtil . NotNull ( environment , nameof ( environment ) ) ;
ArgUtil . NotNull ( runtimeVariables , nameof ( runtimeVariables ) ) ;
// Create the handler.
IHandler handler ;
if ( data . ExecutionType = = ActionExecutionType . Container )
{
handler = HostContext . CreateService < IContainerActionHandler > ( ) ;
( handler as IContainerActionHandler ) . Data = data as ContainerActionExecutionData ;
}
else if ( data . ExecutionType = = ActionExecutionType . NodeJS )
{
handler = HostContext . CreateService < INodeScriptActionHandler > ( ) ;
2022-02-25 20:59:16 +01:00
var nodeData = data as NodeJSActionExecutionData ;
// With node12 EoL in 04/2022, we want to be able to uniformly upgrade all JS actions to node16 from the server
if ( string . Equals ( nodeData . NodeVersion , "node12" , StringComparison . InvariantCultureIgnoreCase ) & &
( executionContext . Global . Variables . GetBoolean ( "DistributedTask.ForceGithubJavascriptActionsToNode16" ) ? ? false ) )
{
// The user can opt out of this behaviour by setting this variable to true, either setting 'env' in their workflow or as an environment variable on their machine
executionContext . Global . EnvironmentVariables . TryGetValue ( Constants . Variables . Actions . AllowActionsUseUnsecureNodeVersion , out var workflowOptOut ) ;
var isWorkflowOptOutSet = ! string . IsNullOrEmpty ( workflowOptOut ) ;
var isLocalOptOut = StringUtil . ConvertToBoolean ( Environment . GetEnvironmentVariable ( Constants . Variables . Actions . AllowActionsUseUnsecureNodeVersion ) ) ;
bool isOptOut = isWorkflowOptOutSet ? StringUtil . ConvertToBoolean ( workflowOptOut ) : isLocalOptOut ;
if ( ! isOptOut )
{
nodeData . NodeVersion = "node16" ;
}
}
( handler as INodeScriptActionHandler ) . Data = nodeData ;
2019-10-10 00:52:42 -04:00
}
else if ( data . ExecutionType = = ActionExecutionType . Script )
{
handler = HostContext . CreateService < IScriptHandler > ( ) ;
( handler as IScriptHandler ) . Data = data as ScriptActionExecutionData ;
}
else if ( data . ExecutionType = = ActionExecutionType . Plugin )
{
2019-12-16 15:45:00 -05:00
// Runner plugin
2019-10-10 00:52:42 -04:00
handler = HostContext . CreateService < IRunnerPluginHandler > ( ) ;
( handler as IRunnerPluginHandler ) . Data = data as PluginActionExecutionData ;
}
2020-06-23 15:35:32 -04:00
else if ( data . ExecutionType = = ActionExecutionType . Composite )
{
2020-07-17 16:31:48 -04:00
handler = HostContext . CreateService < ICompositeActionHandler > ( ) ;
( handler as ICompositeActionHandler ) . Data = data as CompositeActionExecutionData ;
2020-06-23 15:35:32 -04:00
}
2019-10-10 00:52:42 -04:00
else
{
// This should never happen.
throw new NotSupportedException ( data . ExecutionType . ToString ( ) ) ;
}
handler . Action = action ;
handler . Environment = environment ;
handler . RuntimeVariables = runtimeVariables ;
handler . ExecutionContext = executionContext ;
handler . StepHost = stepHost ;
handler . Inputs = inputs ;
handler . ActionDirectory = actionDirectory ;
2021-07-28 15:35:21 -04:00
handler . LocalActionContainerSetupSteps = localActionContainerSetupSteps ;
2019-10-10 00:52:42 -04:00
return handler ;
}
}
}