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
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func main() {
Usage: "git remote url",
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"},
},
&cli.StringFlag{
Name: "remote-ssh",
Usage: "git clone ssh url",
EnvVars: []string{"CI_REPO_CLONE_SSH_URL"},
6543 marked this conversation as resolved.
Show resolved Hide resolved
},
&cli.StringFlag{
Name: "path",
Usage: "git clone path",
Expand Down Expand Up @@ -140,6 +145,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: "ssh-user",
Usage: "Username for ssh clone",
EnvVars: []string{"PLUGIN_SSH_USER"},
},
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}

if err := app.Run(os.Args); err != nil {
Expand All @@ -154,7 +175,8 @@ func run(c *cli.Context) error {

plugin := Plugin{
Repo: Repo{
Clone: c.String("remote"),
Clone: c.String("remote"),
CloneSSH: c.String("remote-ssh"),
},
Pipeline: Pipeline{
Commit: c.String("sha"),
Expand All @@ -180,6 +202,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
17 changes: 16 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ func (p Plugin) Exec() error {
if isDirEmpty(filepath.Join(p.Pipeline.Path, ".git")) {
cmds = append(cmds, initGit(p.Config.Branch))
cmds = append(cmds, safeDirectory(p.Config.SafeDirectory))
cmds = append(cmds, remote(p.Repo.Clone))
if p.Config.UseSSH {
// If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS
cmds = append(cmds, remote(p.Repo.CloneSSH))
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))
}
} else {
cmds = append(cmds, remote(p.Repo.Clone))
}

}

// fetch ref in any case
Expand Down Expand Up @@ -210,6 +220,11 @@ func safeDirectory(safeDirectory string) *exec.Cmd {
return appendEnv(exec.Command("git", "config", "--global", "safe.directory", safeDirectory), defaultEnvVars...)
}

// 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
6 changes: 5 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

type (
Repo struct {
Clone string
Clone string
CloneSSH string
}

Pipeline struct {
Expand Down Expand Up @@ -38,6 +39,9 @@ type (
Partial bool
filter string
SafeDirectory string
UseSSH bool
SSHKey string
SSHUser string
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}

Backoff struct {
Expand Down