Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to use SSH for cloning repo #75

Merged
merged 12 commits into from
Sep 13, 2023
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ func main() {
Usage: "Define safe directories",
EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"},
},
&cli.BoolFlag{
Name: "use-ssh",
Usage: "Using ssh for git clone",
EnvVars: []string{"PLUGIN_USE_SSH"},
Value: false,
},
&cli.StringFlag{
6543 marked this conversation as resolved.
Show resolved Hide resolved
Name: "ssh-key",
Usage: "SSH key for ssh clone",
EnvVars: []string{"PLUGIN_SSH_KEY"},
},
&cli.StringFlag{
Name: "forge-url",
Usage: "Forge URL for ssh clone",
EnvVars: []string{"CI_FORGE_URL"},
RayaneB75 marked this conversation as resolved.
Show resolved Hide resolved
},
}

if err := app.Run(os.Args); err != nil {
Expand All @@ -155,6 +171,7 @@ func run(c *cli.Context) error {
plugin := Plugin{
Repo: Repo{
Clone: c.String("remote"),
Forge: c.String("forge-url"),
},
Build: Build{
Commit: c.String("sha"),
Expand All @@ -180,6 +197,8 @@ func run(c *cli.Context) error {
Partial: c.Bool("partial"),
Home: c.String("home"),
SafeDirectory: c.String("safe-directory"),
UseSSH: c.Bool("use-ssh"),
SSHKey: c.String("ssh-key"),
},
Backoff: Backoff{
Attempts: c.Int("backoff-attempts"),
Expand Down
27 changes: 26 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (p Plugin) Exec() error {
if isDirEmpty(filepath.Join(p.Build.Path, ".git")) {
cmds = append(cmds, initGit(p.Config.Branch))
cmds = append(cmds, safeDirectory(p.Config.SafeDirectory))
if p.Config.UseSSH {
// If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS
cmds = append(cmds, useSSH(p.Repo.Forge))
if p.Config.SSHKey != "" {
// If env var PLUGIN_SSH_KEY is set, use it as the SSH key
cmds = append(cmds, sshKeyHandler(p.Config.SSHKey))
}
}
cmds = append(cmds, remote(p.Repo.Clone))
}

Expand Down Expand Up @@ -210,6 +218,23 @@ func safeDirectory(safeDirectory string) *exec.Cmd {
return appendEnv(exec.Command("git", "config", "--global", "safe.directory", safeDirectory), defaultEnvVars...)
}

// Replace the http(s) protocol with the ssh protocol.
func useSSH(forgeURL string) *exec.Cmd {
// Parsing FQDN from remote url to use it in config
var remoteURL string
if strings.Contains(forgeURL, "https://") {
remoteURL = strings.Replace(forgeURL, "https://", "", 1)
} else if strings.Contains(forgeURL, "http://") {
remoteURL = strings.Replace(forgeURL, "http://", "", 1)
}
return appendEnv(exec.Command("git", "config", fmt.Sprintf("url.git@%s:.insteadOf", remoteURL), forgeURL+"/"), defaultEnvVars...)
RayaneB75 marked this conversation as resolved.
Show resolved Hide resolved
}

// Use custom SSH Key thanks to core.sshCommand
func sshKeyHandler(sshKey string) *exec.Cmd {
return appendEnv(exec.Command("git", "config", "core.sshCommand", "ssh -i "+sshKey), defaultEnvVars...)
}

// Sets the remote origin for the repository.
func remote(remote string) *exec.Cmd {
return appendEnv(exec.Command(
Expand Down Expand Up @@ -256,7 +281,7 @@ func checkoutLFS() *exec.Cmd {
), defaultEnvVars...)
}

// fetch retuns git command that fetches from origin. If tags is true
// fetch returns git command that fetches from origin. If tags is true
// then tags will be fetched.
func fetch(ref string, tags bool, depth int, filter string) *exec.Cmd {
tags_option := "--no-tags"
Expand Down
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type (
Repo struct {
Clone string
Forge string
}

Build struct {
Expand Down Expand Up @@ -38,6 +39,8 @@ type (
Partial bool
filter string
SafeDirectory string
UseSSH bool
SSHKey string
}

Backoff struct {
Expand Down