Skip to content

Commit 7ae9916

Browse files
Ryan (hackercat)alanb-sony
andauthored
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 <[email protected]>
1 parent ea7503b commit 7ae9916

File tree

7 files changed

+20
-12
lines changed

7 files changed

+20
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ It will save that information to `~/.actrc`, please refer to [Configuration](#co
108108
-r, --reuse reuse action containers to maintain state
109109
-s, --secret stringArray secret to make available to actions with optional value (e.g. -s mysecret=foo or -s mysecret)
110110
--secret-file string file with list of secrets to read from (e.g. --secret-file .secrets) (default ".secrets")
111+
--use-gitignore Controls whether paths specified in .gitignore should be copied into container (default true)
111112
--userns string user namespace to use
112113
-v, --verbose verbose output
113114
-w, --watch watch the contents of the local repo and run when files change

cmd/input.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Input struct {
2727
privileged bool
2828
usernsMode string
2929
containerArchitecture string
30+
useGitIgnore bool
3031
}
3132

3233
func (i *Input) resolve(path string) string {

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func Execute(ctx context.Context, version string) {
4949
rootCmd.Flags().StringVar(&input.defaultBranch, "defaultbranch", "", "the name of the main branch")
5050
rootCmd.Flags().BoolVar(&input.privileged, "privileged", false, "use privileged mode")
5151
rootCmd.Flags().StringVar(&input.usernsMode, "userns", "", "user namespace to use")
52+
rootCmd.Flags().BoolVar(&input.useGitIgnore, "use-gitignore", true, "Controls whether paths specified in .gitignore should be copied into container")
5253
rootCmd.PersistentFlags().StringVarP(&input.actor, "actor", "a", "nektos/act", "user that triggered the event")
5354
rootCmd.PersistentFlags().StringVarP(&input.workflowsPath, "workflows", "W", "./.github/workflows/", "path to workflow file(s)")
5455
rootCmd.PersistentFlags().StringVarP(&input.workdir, "directory", "C", ".", "working directory")
@@ -252,6 +253,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
252253
Privileged: input.privileged,
253254
UsernsMode: input.usernsMode,
254255
ContainerArchitecture: input.containerArchitecture,
256+
UseGitIgnore: input.useGitIgnore,
255257
}
256258
r, err := runner.New(config)
257259
if err != nil {

pkg/container/docker_run.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type FileEntry struct {
6363
type Container interface {
6464
Create() common.Executor
6565
Copy(destPath string, files ...*FileEntry) common.Executor
66-
CopyDir(destPath string, srcPath string) common.Executor
66+
CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor
6767
Pull(forcePull bool) common.Executor
6868
Start(attach bool) common.Executor
6969
Exec(command []string, env map[string]string) common.Executor
@@ -136,13 +136,13 @@ func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common.
136136
).IfNot(common.Dryrun)
137137
}
138138

139-
func (cr *containerReference) CopyDir(destPath string, srcPath string) common.Executor {
139+
func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor {
140140
return common.NewPipelineExecutor(
141141
common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath),
142142
cr.connect(),
143143
cr.find(),
144144
cr.exec([]string{"mkdir", "-p", destPath}, nil),
145-
cr.copyDir(destPath, srcPath),
145+
cr.copyDir(destPath, srcPath, useGitIgnore),
146146
).IfNot(common.Dryrun)
147147
}
148148

@@ -448,7 +448,7 @@ func (cr *containerReference) exec(cmd []string, env map[string]string) common.E
448448
}
449449

450450
// nolint: gocyclo
451-
func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Executor {
451+
func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor {
452452
return func(ctx context.Context) error {
453453
logger := common.Logger(ctx)
454454
tarFile, err := ioutil.TempFile("", "act")
@@ -466,12 +466,15 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
466466
}
467467
log.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
468468

469-
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
470-
if err != nil {
471-
log.Debugf("Error loading .gitignore: %v", err)
472-
}
469+
var ignorer gitignore.Matcher
470+
if useGitIgnore {
471+
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
472+
if err != nil {
473+
log.Debugf("Error loading .gitignore: %v", err)
474+
}
473475

474-
ignorer := gitignore.NewMatcher(ps)
476+
ignorer = gitignore.NewMatcher(ps)
477+
}
475478

476479
err = filepath.Walk(srcPath, func(file string, fi os.FileInfo, err error) error {
477480
if err != nil {
@@ -480,7 +483,7 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
480483

481484
sansPrefix := strings.TrimPrefix(file, srcPrefix)
482485
split := strings.Split(sansPrefix, string(filepath.Separator))
483-
if ignorer.Match(split, fi.IsDir()) {
486+
if ignorer != nil && ignorer.Match(split, fi.IsDir()) {
484487
if fi.IsDir() {
485488
return filepath.SkipDir
486489
}

pkg/runner/run_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
129129
rc.stopJobContainer(),
130130
rc.JobContainer.Create(),
131131
rc.JobContainer.Start(false),
132-
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".").IfBool(copyWorkspace),
132+
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".", rc.Config.UseGitIgnore).IfBool(copyWorkspace),
133133
rc.JobContainer.Copy(filepath.Dir(rc.Config.Workdir), &container.FileEntry{
134134
Name: "workflow/event.json",
135135
Mode: 0644,

pkg/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Config struct {
3333
Privileged bool // use privileged mode
3434
UsernsMode string // user namespace to use
3535
ContainerArchitecture string // Desired OS/architecture platform for running containers
36+
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
3637
}
3738

3839
type runnerImpl struct {

pkg/runner/step_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe
415415
if err != nil {
416416
return err
417417
}
418-
err = rc.JobContainer.CopyDir(containerActionDir+"/", actionDir)(ctx)
418+
err = rc.JobContainer.CopyDir(containerActionDir+"/", actionDir, rc.Config.UseGitIgnore)(ctx)
419419
if err != nil {
420420
return err
421421
}

0 commit comments

Comments
 (0)