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
This commit is contained in:
Shubh Bapna 2023-05-03 11:46:28 -04:00 committed by GitHub
parent 3eb6e83ea4
commit 4721abfa6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 16 deletions

View file

@ -26,9 +26,12 @@ func newRemoteReusableWorkflowExecutor(rc *RunContext) common.Executor {
if remoteReusableWorkflow == nil { 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)) 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( return common.NewPipelineExecutor(
newMutexExecutor(cloneIfRequired(rc, *remoteReusableWorkflow, workflowDir)), newMutexExecutor(cloneIfRequired(rc, *remoteReusableWorkflow, workflowDir)),
@ -56,12 +59,15 @@ func cloneIfRequired(rc *RunContext, remoteReusableWorkflow remoteReusableWorkfl
notExists := errors.Is(err, fs.ErrNotExist) notExists := errors.Is(err, fs.ErrNotExist)
return notExists return notExists
}, },
git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{ func(ctx context.Context) error {
URL: remoteReusableWorkflow.CloneURL(), remoteReusableWorkflow.URL = rc.getGithubContext(ctx).ServerURL
Ref: remoteReusableWorkflow.Ref, return git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{
Dir: targetDirectory, URL: remoteReusableWorkflow.CloneURL(),
Token: rc.Config.Token, Ref: remoteReusableWorkflow.Ref,
}), Dir: targetDirectory,
Token: rc.Config.Token,
})(ctx)
},
nil, nil,
) )
} }
@ -108,7 +114,7 @@ type remoteReusableWorkflow struct {
} }
func (r *remoteReusableWorkflow) CloneURL() string { 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 { func newRemoteReusableWorkflow(uses string) *remoteReusableWorkflow {
@ -124,6 +130,6 @@ func newRemoteReusableWorkflow(uses string) *remoteReusableWorkflow {
Repo: matches[2], Repo: matches[2],
Filename: matches[3], Filename: matches[3],
Ref: matches[4], Ref: matches[4],
URL: "github.com", URL: "https://github.com",
} }
} }

View file

@ -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) 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) github := sar.getGithubContext(ctx)
sar.remoteAction.URL = github.ServerURL
if sar.remoteAction.IsCheckout() && isLocalCheckout(github, sar.Step) && !sar.RunContext.Config.NoSkipCheckout { 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") common.Logger(ctx).Debugf("Skipping local actions/checkout because workdir was already copied")
return nil return nil
} }
sar.remoteAction.URL = sar.RunContext.Config.GitHubInstance
for _, action := range sar.RunContext.Config.ReplaceGheActionWithGithubCom { for _, action := range sar.RunContext.Config.ReplaceGheActionWithGithubCom {
if strings.EqualFold(fmt.Sprintf("%s/%s", sar.remoteAction.Org, sar.remoteAction.Repo), action) { 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 github.Token = sar.RunContext.Config.ReplaceGheActionTokenWithGithubCom
} }
} }
@ -214,7 +213,7 @@ type remoteAction struct {
} }
func (ra *remoteAction) CloneURL() string { 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 { func (ra *remoteAction) IsCheckout() bool {
@ -240,7 +239,7 @@ func newRemoteAction(action string) *remoteAction {
Repo: matches[2], Repo: matches[2],
Path: matches[4], Path: matches[4],
Ref: matches[6], Ref: matches[6],
URL: "github.com", URL: "https://github.com",
} }
} }