From 4721abfa6dbc661922d34d20b9f60aa14eb30b34 Mon Sep 17 00:00:00 2001 From: Shubh Bapna <38372682+shubhbapna@users.noreply.github.com> Date: Wed, 3 May 2023 11:46:28 -0400 Subject: [PATCH] fix: remove hardcoded reference to github.com when using reusable remote workflows and remote actions (#1784) * fix filename for remote reusable workflow and remove hardcoded reference to github.com * remove hardcoded reference to github.com for remote action --- pkg/runner/reusable_workflow.go | 26 ++++++++++++++++---------- pkg/runner/step_action_remote.go | 11 +++++------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/runner/reusable_workflow.go b/pkg/runner/reusable_workflow.go index 1ffa22b..67e0403 100644 --- a/pkg/runner/reusable_workflow.go +++ b/pkg/runner/reusable_workflow.go @@ -26,9 +26,12 @@ func newRemoteReusableWorkflowExecutor(rc *RunContext) common.Executor { if remoteReusableWorkflow == nil { return common.NewErrorExecutor(fmt.Errorf("expected format {owner}/{repo}/.github/workflows/{filename}@{ref}. Actual '%s' Input string was not in a correct format", uses)) } - remoteReusableWorkflow.URL = rc.Config.GitHubInstance - workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(uses)) + // uses with safe filename makes the target directory look something like this {owner}-{repo}-.github-workflows-{filename}@{ref} + // instead we will just use {owner}-{repo}@{ref} as our target directory. This should also improve performance when we are using + // multiple reusable workflows from the same repository and ref since for each workflow we won't have to clone it again + filename := fmt.Sprintf("%s/%s@%s", remoteReusableWorkflow.Org, remoteReusableWorkflow.Repo, remoteReusableWorkflow.Ref) + workflowDir := fmt.Sprintf("%s/%s", rc.ActionCacheDir(), safeFilename(filename)) return common.NewPipelineExecutor( newMutexExecutor(cloneIfRequired(rc, *remoteReusableWorkflow, workflowDir)), @@ -56,12 +59,15 @@ func cloneIfRequired(rc *RunContext, remoteReusableWorkflow remoteReusableWorkfl notExists := errors.Is(err, fs.ErrNotExist) return notExists }, - git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{ - URL: remoteReusableWorkflow.CloneURL(), - Ref: remoteReusableWorkflow.Ref, - Dir: targetDirectory, - Token: rc.Config.Token, - }), + func(ctx context.Context) error { + remoteReusableWorkflow.URL = rc.getGithubContext(ctx).ServerURL + return git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{ + URL: remoteReusableWorkflow.CloneURL(), + Ref: remoteReusableWorkflow.Ref, + Dir: targetDirectory, + Token: rc.Config.Token, + })(ctx) + }, nil, ) } @@ -108,7 +114,7 @@ type remoteReusableWorkflow struct { } func (r *remoteReusableWorkflow) CloneURL() string { - return fmt.Sprintf("https://%s/%s/%s", r.URL, r.Org, r.Repo) + return fmt.Sprintf("%s/%s/%s", r.URL, r.Org, r.Repo) } func newRemoteReusableWorkflow(uses string) *remoteReusableWorkflow { @@ -124,6 +130,6 @@ func newRemoteReusableWorkflow(uses string) *remoteReusableWorkflow { Repo: matches[2], Filename: matches[3], Ref: matches[4], - URL: "github.com", + URL: "https://github.com", } } diff --git a/pkg/runner/step_action_remote.go b/pkg/runner/step_action_remote.go index 029ed5c..e23dcf9 100644 --- a/pkg/runner/step_action_remote.go +++ b/pkg/runner/step_action_remote.go @@ -46,18 +46,17 @@ func (sar *stepActionRemote) prepareActionExecutor() common.Executor { return fmt.Errorf("Expected format {org}/{repo}[/path]@ref. Actual '%s' Input string was not in a correct format", sar.Step.Uses) } - sar.remoteAction.URL = sar.RunContext.Config.GitHubInstance - github := sar.getGithubContext(ctx) + sar.remoteAction.URL = github.ServerURL + 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 } - 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" + sar.remoteAction.URL = "https://github.com" github.Token = sar.RunContext.Config.ReplaceGheActionTokenWithGithubCom } } @@ -214,7 +213,7 @@ type remoteAction struct { } func (ra *remoteAction) CloneURL() string { - return fmt.Sprintf("https://%s/%s/%s", ra.URL, ra.Org, ra.Repo) + return fmt.Sprintf("%s/%s/%s", ra.URL, ra.Org, ra.Repo) } func (ra *remoteAction) IsCheckout() bool { @@ -240,7 +239,7 @@ func newRemoteAction(action string) *remoteAction { Repo: matches[2], Path: matches[4], Ref: matches[6], - URL: "github.com", + URL: "https://github.com", } }