fix #122 - support actions/checkout for repos other the one for this workflow (#143)

This commit is contained in:
Casey Lee 2020-03-09 17:45:42 -07:00 committed by GitHub
parent 143676fcfb
commit 16520bb277
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 3 deletions

View file

@ -105,6 +105,7 @@ func (cr *containerReference) CopyDir(destPath string, srcPath string) common.Ex
common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath), common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath),
cr.connect(), cr.connect(),
cr.find(), cr.find(),
cr.exec([]string{"mkdir", "-p", destPath}, nil),
cr.copyDir(destPath, srcPath), cr.copyDir(destPath, srcPath),
).IfNot(common.Dryrun) ).IfNot(common.Dryrun)
} }

View file

@ -113,12 +113,19 @@ func (rc *RunContext) startJobContainer() common.Executor {
Stderr: logWriter, Stderr: logWriter,
}) })
var copyWorkspace bool
var copyToPath string
if !rc.Config.BindWorkdir {
copyToPath, copyWorkspace = rc.localCheckoutPath()
copyToPath = filepath.Join("/github/workspace", copyToPath)
}
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
rc.JobContainer.Pull(rc.Config.ForcePull), rc.JobContainer.Pull(rc.Config.ForcePull),
rc.JobContainer.Remove().IfBool(!rc.Config.ReuseContainers), rc.JobContainer.Remove().IfBool(!rc.Config.ReuseContainers),
rc.JobContainer.Create(), rc.JobContainer.Create(),
rc.JobContainer.Start(false), rc.JobContainer.Start(false),
rc.JobContainer.CopyDir("/github/workspace", rc.Config.Workdir+"/.").IfBool(!rc.Config.BindWorkdir), rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+"/.").IfBool(copyWorkspace),
rc.JobContainer.Copy("/github/", &container.FileEntry{ rc.JobContainer.Copy("/github/", &container.FileEntry{
Name: "workflow/event.json", Name: "workflow/event.json",
Mode: 644, Mode: 644,
@ -380,6 +387,24 @@ func (rc *RunContext) getGithubContext() *githubContext {
return ghc return ghc
} }
func (ghc *githubContext) isLocalCheckout(step *model.Step) bool {
if step.Type() != model.StepTypeUsesActionRemote {
return false
}
remoteAction := newRemoteAction(step.Uses)
if !remoteAction.IsCheckout() {
return false
}
if repository, ok := step.With["repository"]; ok && repository != ghc.Repository {
return false
}
if repository, ok := step.With["ref"]; ok && repository != ghc.Ref {
return false
}
return true
}
func asString(v interface{}) string { func asString(v interface{}) string {
if v == nil { if v == nil {
return "" return ""
@ -421,5 +446,16 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
env["GITHUB_WORKSPACE"] = github.Workspace env["GITHUB_WORKSPACE"] = github.Workspace
env["GITHUB_SHA"] = github.Sha env["GITHUB_SHA"] = github.Sha
env["GITHUB_REF"] = github.Ref env["GITHUB_REF"] = github.Ref
env["GITHUB_TOKEN"] = github.Token
return env return env
} }
func (rc *RunContext) localCheckoutPath() (string, bool) {
ghContext := rc.getGithubContext()
for _, step := range rc.Run.Job().Steps {
if ghContext.isLocalCheckout(step) {
return step.With["path"], true
}
}
return "", false
}

View file

@ -55,7 +55,7 @@ func (sc *StepContext) Executor() common.Executor {
) )
case model.StepTypeUsesActionRemote: case model.StepTypeUsesActionRemote:
remoteAction := newRemoteAction(step.Uses) remoteAction := newRemoteAction(step.Uses)
if remoteAction.Org == "actions" && remoteAction.Repo == "checkout" { if remoteAction.IsCheckout() && rc.getGithubContext().isLocalCheckout(step) {
return func(ctx context.Context) error { return func(ctx context.Context) error {
common.Logger(ctx).Debugf("Skipping actions/checkout") common.Logger(ctx).Debugf("Skipping actions/checkout")
return nil return nil
@ -232,7 +232,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_") envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_")
envKey = fmt.Sprintf("INPUT_%s", envKey) envKey = fmt.Sprintf("INPUT_%s", envKey)
if _, ok := sc.Env[envKey]; !ok { if _, ok := sc.Env[envKey]; !ok {
sc.Env[envKey] = input.Default sc.Env[envKey] = rc.ExprEval.Interpolate(input.Default)
} }
} }
@ -311,6 +311,13 @@ func (ra *remoteAction) CloneURL() string {
return fmt.Sprintf("https://github.com/%s/%s", ra.Org, ra.Repo) return fmt.Sprintf("https://github.com/%s/%s", 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 { func newRemoteAction(action string) *remoteAction {
r := regexp.MustCompile(`^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$`) r := regexp.MustCompile(`^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$`)
matches := r.FindStringSubmatch(action) matches := r.FindStringSubmatch(action)

19
pkg/runner/testdata/issue-122/main.yaml vendored Normal file
View file

@ -0,0 +1,19 @@
name: Checkout
on: push
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
path: test-subdir1
- run: grep "Checkout" test-subdir1/issue-122/main.yaml
- uses: actions/checkout@v2
with:
repository: actions/checkout
path: test-subdir2
- run: grep "Checkout" test-subdir2/action.yml