Add option to allow files/directories in .gitignore to be copied to container (#537)

* disable gitignore for actions

* feat: Add option to allow/disallow paths specified in .gitignore

Co-authored-by: Alan Birtles <alan.birtles@eu.sony.com>
This commit is contained in:
Ryan (hackercat)
2021-05-03 16:37:20 +02:00
committed by GitHub
parent ea7503bc25
commit 7ae9916de0
7 changed files with 20 additions and 12 deletions

View File

@@ -63,7 +63,7 @@ type FileEntry struct {
type Container interface {
Create() common.Executor
Copy(destPath string, files ...*FileEntry) common.Executor
CopyDir(destPath string, srcPath string) common.Executor
CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor
Pull(forcePull bool) common.Executor
Start(attach bool) common.Executor
Exec(command []string, env map[string]string) common.Executor
@@ -136,13 +136,13 @@ func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common.
).IfNot(common.Dryrun)
}
func (cr *containerReference) CopyDir(destPath string, srcPath string) common.Executor {
func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor {
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath),
cr.connect(),
cr.find(),
cr.exec([]string{"mkdir", "-p", destPath}, nil),
cr.copyDir(destPath, srcPath),
cr.copyDir(destPath, srcPath, useGitIgnore),
).IfNot(common.Dryrun)
}
@@ -448,7 +448,7 @@ func (cr *containerReference) exec(cmd []string, env map[string]string) common.E
}
// nolint: gocyclo
func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Executor {
func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor {
return func(ctx context.Context) error {
logger := common.Logger(ctx)
tarFile, err := ioutil.TempFile("", "act")
@@ -466,12 +466,15 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
}
log.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
if err != nil {
log.Debugf("Error loading .gitignore: %v", err)
}
var ignorer gitignore.Matcher
if useGitIgnore {
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
if err != nil {
log.Debugf("Error loading .gitignore: %v", err)
}
ignorer := gitignore.NewMatcher(ps)
ignorer = gitignore.NewMatcher(ps)
}
err = filepath.Walk(srcPath, func(file string, fi os.FileInfo, err error) error {
if err != nil {
@@ -480,7 +483,7 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
sansPrefix := strings.TrimPrefix(file, srcPrefix)
split := strings.Split(sansPrefix, string(filepath.Separator))
if ignorer.Match(split, fi.IsDir()) {
if ignorer != nil && ignorer.Match(split, fi.IsDir()) {
if fi.IsDir() {
return filepath.SkipDir
}

View File

@@ -129,7 +129,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
rc.stopJobContainer(),
rc.JobContainer.Create(),
rc.JobContainer.Start(false),
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".").IfBool(copyWorkspace),
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".", rc.Config.UseGitIgnore).IfBool(copyWorkspace),
rc.JobContainer.Copy(filepath.Dir(rc.Config.Workdir), &container.FileEntry{
Name: "workflow/event.json",
Mode: 0644,

View File

@@ -33,6 +33,7 @@ type Config struct {
Privileged bool // use privileged mode
UsernsMode string // user namespace to use
ContainerArchitecture string // Desired OS/architecture platform for running containers
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
}
type runnerImpl struct {

View File

@@ -415,7 +415,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe
if err != nil {
return err
}
err = rc.JobContainer.CopyDir(containerActionDir+"/", actionDir)(ctx)
err = rc.JobContainer.CopyDir(containerActionDir+"/", actionDir, rc.Config.UseGitIgnore)(ctx)
if err != nil {
return err
}