2022-03-23 05:13:00 +08:00
package runner
import (
"context"
2022-06-11 05:16:42 +08:00
"errors"
2022-03-23 05:13:00 +08:00
"fmt"
"io"
"os"
2022-05-24 21:36:06 +08:00
"path"
2022-03-23 05:13:00 +08:00
"path/filepath"
"regexp"
"strings"
"github.com/nektos/act/pkg/common"
2022-06-11 05:16:42 +08:00
"github.com/nektos/act/pkg/common/git"
2022-03-23 05:13:00 +08:00
"github.com/nektos/act/pkg/model"
2022-06-11 05:16:42 +08:00
gogit "github.com/go-git/go-git/v5"
2022-03-23 05:13:00 +08:00
)
type stepActionRemote struct {
2022-05-24 21:36:06 +08:00
Step * model . Step
RunContext * RunContext
compositeRunContext * RunContext
compositeSteps * compositeSteps
readAction readAction
runAction runAction
action * model . Action
env map [ string ] string
remoteAction * remoteAction
2022-03-23 05:13:00 +08:00
}
var (
2022-06-11 05:16:42 +08:00
stepActionRemoteNewCloneExecutor = git . NewGitCloneExecutor
2022-03-23 05:13:00 +08:00
)
2022-06-08 23:36:08 +08:00
func ( sar * stepActionRemote ) prepareActionExecutor ( ) common . Executor {
2022-06-21 05:58:51 +08:00
return func ( ctx context . Context ) error {
if sar . remoteAction != nil && sar . action != nil {
// we are already good to run
return nil
}
2022-06-08 23:36:08 +08:00
2022-06-21 05:58:51 +08:00
sar . remoteAction = newRemoteAction ( sar . Step . Uses )
if sar . remoteAction == nil {
return fmt . Errorf ( "Expected format {org}/{repo}[/path]@ref. Actual '%s' Input string was not in a correct format" , sar . Step . Uses )
}
2022-05-24 21:36:06 +08:00
2022-06-21 05:58:51 +08:00
sar . remoteAction . URL = sar . RunContext . Config . GitHubInstance
2022-03-23 05:13:00 +08:00
2022-10-07 05:58:16 +08:00
github := sar . getGithubContext ( ctx )
2022-06-21 05:58:51 +08:00
if sar . remoteAction . IsCheckout ( ) && isLocalCheckout ( github , sar . Step ) && ! sar . RunContext . Config . NoSkipCheckout {
common . Logger ( ctx ) . Debugf ( "Skipping local actions/checkout because workdir was already copied" )
return nil
}
2022-05-24 21:36:06 +08:00
2022-06-21 21:52:21 +08:00
sar . remoteAction . URL = sar . RunContext . Config . GitHubInstance
for _ , action := range sar . RunContext . Config . ReplaceGheActionWithGithubCom {
if strings . EqualFold ( fmt . Sprintf ( "%s/%s" , sar . remoteAction . Org , sar . remoteAction . Repo ) , action ) {
sar . remoteAction . URL = "github.com"
github . Token = sar . RunContext . Config . ReplaceGheActionTokenWithGithubCom
}
}
2022-06-21 05:58:51 +08:00
actionDir := fmt . Sprintf ( "%s/%s" , sar . RunContext . ActionCacheDir ( ) , strings . ReplaceAll ( sar . Step . Uses , "/" , "-" ) )
gitClone := stepActionRemoteNewCloneExecutor ( git . NewGitCloneExecutorInput {
URL : sar . remoteAction . CloneURL ( ) ,
Ref : sar . remoteAction . Ref ,
Dir : actionDir ,
Token : github . Token ,
} )
var ntErr common . Executor
if err := gitClone ( ctx ) ; err != nil {
if errors . Is ( err , git . ErrShortRef ) {
return fmt . Errorf ( "Unable to resolve action `%s`, the provided ref `%s` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `%s` instead" ,
sar . Step . Uses , sar . remoteAction . Ref , err . ( * git . Error ) . Commit ( ) )
} else if errors . Is ( err , gogit . ErrForceNeeded ) { // TODO: figure out if it will be easy to shadow/alias go-git err's
ntErr = common . NewInfoExecutor ( "Non-terminating error while running 'git clone': %v" , err )
} else {
return err
2022-05-24 21:36:06 +08:00
}
2022-06-21 05:58:51 +08:00
}
2022-05-24 21:36:06 +08:00
2022-06-21 05:58:51 +08:00
remoteReader := func ( ctx context . Context ) actionYamlReader {
return func ( filename string ) ( io . Reader , io . Closer , error ) {
f , err := os . Open ( filepath . Join ( actionDir , sar . remoteAction . Path , filename ) )
return f , f , err
2022-05-24 21:36:06 +08:00
}
2022-06-21 05:58:51 +08:00
}
2022-03-23 05:13:00 +08:00
2022-06-21 05:58:51 +08:00
return common . NewPipelineExecutor (
ntErr ,
func ( ctx context . Context ) error {
2022-10-07 05:58:16 +08:00
actionModel , err := sar . readAction ( ctx , sar . Step , actionDir , sar . remoteAction . Path , remoteReader ( ctx ) , os . WriteFile )
2022-06-21 05:58:51 +08:00
sar . action = actionModel
return err
} ,
) ( ctx )
}
2022-06-08 23:36:08 +08:00
}
func ( sar * stepActionRemote ) pre ( ) common . Executor {
sar . env = map [ string ] string { }
return common . NewPipelineExecutor (
sar . prepareActionExecutor ( ) ,
2022-05-24 21:36:06 +08:00
runStepExecutor ( sar , stepStagePre , runPreStep ( sar ) ) . If ( hasPreStep ( sar ) ) . If ( shouldRunPreStep ( sar ) ) )
}
func ( sar * stepActionRemote ) main ( ) common . Executor {
2022-06-08 23:36:08 +08:00
return common . NewPipelineExecutor (
sar . prepareActionExecutor ( ) ,
runStepExecutor ( sar , stepStageMain , func ( ctx context . Context ) error {
2022-10-07 05:58:16 +08:00
github := sar . getGithubContext ( ctx )
2022-06-08 23:36:08 +08:00
if sar . remoteAction . IsCheckout ( ) && isLocalCheckout ( github , sar . Step ) && ! sar . RunContext . Config . NoSkipCheckout {
2022-06-21 06:14:14 +08:00
if sar . RunContext . Config . BindWorkdir {
common . Logger ( ctx ) . Debugf ( "Skipping local actions/checkout because you bound your workspace" )
return nil
}
eval := sar . RunContext . NewExpressionEvaluator ( ctx )
2022-11-17 05:29:45 +08:00
copyToPath := path . Join ( sar . RunContext . JobContainer . ToContainerPath ( sar . RunContext . Config . Workdir ) , eval . Interpolate ( ctx , sar . Step . With [ "path" ] ) )
2022-06-21 06:14:14 +08:00
return sar . RunContext . JobContainer . CopyDir ( copyToPath , sar . RunContext . Config . Workdir + string ( filepath . Separator ) + "." , sar . RunContext . Config . UseGitIgnore ) ( ctx )
2022-06-08 23:36:08 +08:00
}
2022-03-23 05:13:00 +08:00
2022-06-08 23:36:08 +08:00
actionDir := fmt . Sprintf ( "%s/%s" , sar . RunContext . ActionCacheDir ( ) , strings . ReplaceAll ( sar . Step . Uses , "/" , "-" ) )
2022-03-23 05:13:00 +08:00
2022-10-07 05:58:16 +08:00
return sar . runAction ( sar , actionDir , sar . remoteAction ) ( ctx )
2022-06-08 23:36:08 +08:00
} ) ,
)
2022-03-23 05:13:00 +08:00
}
func ( sar * stepActionRemote ) post ( ) common . Executor {
2022-05-24 21:36:06 +08:00
return runStepExecutor ( sar , stepStagePost , runPostStep ( sar ) ) . If ( hasPostStep ( sar ) ) . If ( shouldRunPostStep ( sar ) )
2022-03-23 05:13:00 +08:00
}
func ( sar * stepActionRemote ) getRunContext ( ) * RunContext {
return sar . RunContext
}
2022-10-07 05:58:16 +08:00
func ( sar * stepActionRemote ) getGithubContext ( ctx context . Context ) * model . GithubContext {
ghc := sar . getRunContext ( ) . getGithubContext ( ctx )
// extend github context if we already have an initialized remoteAction
remoteAction := sar . remoteAction
if remoteAction != nil {
ghc . ActionRepository = fmt . Sprintf ( "%s/%s" , remoteAction . Org , remoteAction . Repo )
ghc . ActionRef = remoteAction . Ref
}
return ghc
}
2022-03-23 05:13:00 +08:00
func ( sar * stepActionRemote ) getStepModel ( ) * model . Step {
return sar . Step
}
func ( sar * stepActionRemote ) getEnv ( ) * map [ string ] string {
return & sar . env
}
2022-06-17 23:55:21 +08:00
func ( sar * stepActionRemote ) getIfExpression ( ctx context . Context , stage stepStage ) string {
2022-05-24 21:36:06 +08:00
switch stage {
case stepStagePre :
2022-10-07 05:58:16 +08:00
github := sar . getGithubContext ( ctx )
2022-05-24 21:36:06 +08:00
if sar . remoteAction . IsCheckout ( ) && isLocalCheckout ( github , sar . Step ) && ! sar . RunContext . Config . NoSkipCheckout {
// skip local checkout pre step
return "false"
}
return sar . action . Runs . PreIf
case stepStageMain :
return sar . Step . If . Value
case stepStagePost :
return sar . action . Runs . PostIf
}
return ""
}
2022-03-23 05:13:00 +08:00
func ( sar * stepActionRemote ) getActionModel ( ) * model . Action {
return sar . action
}
2022-06-17 23:55:21 +08:00
func ( sar * stepActionRemote ) getCompositeRunContext ( ctx context . Context ) * RunContext {
2022-05-24 21:36:06 +08:00
if sar . compositeRunContext == nil {
actionDir := fmt . Sprintf ( "%s/%s" , sar . RunContext . ActionCacheDir ( ) , strings . ReplaceAll ( sar . Step . Uses , "/" , "-" ) )
actionLocation := path . Join ( actionDir , sar . remoteAction . Path )
_ , containerActionDir := getContainerActionPaths ( sar . getStepModel ( ) , actionLocation , sar . RunContext )
2022-06-17 23:55:21 +08:00
sar . compositeRunContext = newCompositeRunContext ( ctx , sar . RunContext , sar , containerActionDir )
2022-05-24 21:36:06 +08:00
sar . compositeSteps = sar . compositeRunContext . compositeExecutor ( sar . action )
2022-10-13 00:19:32 +08:00
} else {
// Re-evaluate environment here. For remote actions the environment
// need to be re-created for every stage (pre, main, post) as there
// might be required context changes (inputs/outputs) while the action
// stages are executed. (e.g. the output of another action is the
// input for this action during the main stage, but the env
// was already created during the pre stage)
env := evaluateCompositeInputAndEnv ( ctx , sar . RunContext , sar )
sar . compositeRunContext . Env = env
2022-05-24 21:36:06 +08:00
}
return sar . compositeRunContext
}
func ( sar * stepActionRemote ) getCompositeSteps ( ) * compositeSteps {
return sar . compositeSteps
}
2022-03-23 05:13:00 +08:00
type remoteAction struct {
URL string
Org string
Repo string
Path string
Ref string
}
func ( ra * remoteAction ) CloneURL ( ) string {
return fmt . Sprintf ( "https://%s/%s/%s" , ra . URL , ra . Org , ra . Repo )
}
func ( ra * remoteAction ) IsCheckout ( ) bool {
if ra . Org == "actions" && ra . Repo == "checkout" {
return true
}
return false
}
func newRemoteAction ( action string ) * remoteAction {
// GitHub's document[^] describes:
// > We strongly recommend that you include the version of
// > the action you are using by specifying a Git ref, SHA, or Docker tag number.
// Actually, the workflow stops if there is the uses directive that hasn't @ref.
// [^]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
r := regexp . MustCompile ( ` ^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$ ` )
matches := r . FindStringSubmatch ( action )
if len ( matches ) < 7 || matches [ 6 ] == "" {
return nil
}
return & remoteAction {
Org : matches [ 1 ] ,
Repo : matches [ 2 ] ,
Path : matches [ 4 ] ,
Ref : matches [ 6 ] ,
URL : "github.com" ,
}
}