0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-11-24 13:00:42 +00:00

fix: fix git config and missing git steps

This commit is contained in:
Robert Kaussow 2023-12-23 16:12:56 +01:00
parent 596afd50d1
commit 019f983e8f
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
9 changed files with 67 additions and 58 deletions

View File

@ -28,7 +28,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Name: "remote-ssh", Name: "remote-ssh",
Usage: "git clone ssh url", Usage: "git clone ssh url",
EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"}, EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"},
Destination: &settings.Repo.RemoteURL, Destination: &settings.Repo.RemoteSSH,
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -105,11 +105,10 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "insecure-ssl-verify", Name: "insecure-skip-ssl-verify",
Usage: "set SSL verification of the remote machine", Usage: "skip ssl verification of the remote machine",
EnvVars: []string{"PLUGIN_INSECURE_SSL_VERIFY"}, EnvVars: []string{"PLUGIN_INSECURE_SKIP_SSL_VERIFY"},
Destination: &settings.Repo.InsecureSSLVerify, Destination: &settings.Repo.InsecureSkipSSLVerify,
Value: false,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{

View File

@ -57,10 +57,10 @@ docker build --file Containerfile.multiarch --tag thegeeklab/wp-git-clone .
docker run --rm \ docker run --rm \
-e CI_REPO_CLONE_URL=https://github.com/octocat/Hello-World.git \ -e CI_REPO_CLONE_URL=https://github.com/octocat/Hello-World.git \
-e CI_PIPELINE_EVENT=push \ -e CI_PIPELINE_EVENT=push \
-e CI_COMMIT_SHA=553c2077f0edc3d5dc5d17262f6aa498e69d6f8e \ -e CI_COMMIT_SHA=b3cbd5bbd7e81436d2eee04537ea2b4c0cad4cdf \
-e CI_COMMIT_REF=refs/heads/master \ -e CI_COMMIT_REF=refs/heads/test \
-e CI_WORKSPACE=/tmp/wp_git_testrepo \ -e CI_WORKSPACE=/tmp/wp_git_testrepo \
-v $(pwd):/build:z \ -v $(pwd):/build:z \
-w /build \ -w /build \
quay.io/thegeeklab/wp-git-clone thegeeklab/wp-git-clone
``` ```

View File

@ -36,10 +36,12 @@ properties:
Clone depth. Clone depth.
defaultvalue: 0 defaultvalue: 0
- name: insecure_ssl_verify - name: insecure_skip_ssl_verify
description: | description: |
Set SSL verification of the remote machine. Skip SSL verification of the remote machine. Activating this option is insecure
defaultvalue: false and should be avoided in most cases.
defaultvalue: true
type: bool
- name: lfs - name: lfs
description: | description: |

View File

@ -11,9 +11,9 @@ import (
func ConfigSSLVerify(repo Repository) *execabs.Cmd { func ConfigSSLVerify(repo Repository) *execabs.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--global",
"http.sslVerify", "http.sslVerify",
strconv.FormatBool(repo.InsecureSSLVerify), strconv.FormatBool(!repo.InsecureSkipSSLVerify),
} }
return execabs.Command( return execabs.Command(
@ -26,7 +26,7 @@ func ConfigSSLVerify(repo Repository) *execabs.Cmd {
func ConfigSafeDirectory(repo Repository) *execabs.Cmd { func ConfigSafeDirectory(repo Repository) *execabs.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--global",
"--replace-all", "--replace-all",
"safe.directory", "safe.directory",
repo.SafeDirectory, repo.SafeDirectory,
@ -43,7 +43,7 @@ func ConfigSafeDirectory(repo Repository) *execabs.Cmd {
func ConfigRemapSubmodule(name, url string) *execabs.Cmd { func ConfigRemapSubmodule(name, url string) *execabs.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--global",
fmt.Sprintf("submodule.%s.url", name), fmt.Sprintf("submodule.%s.url", name),
url, url,
} }
@ -58,7 +58,7 @@ func ConfigRemapSubmodule(name, url string) *execabs.Cmd {
func ConfigSSHCommand(sshKey string) *execabs.Cmd { func ConfigSSHCommand(sshKey string) *execabs.Cmd {
args := []string{ args := []string{
"config", "config",
"--local", "--global",
"core.sshCommand", "core.sshCommand",
"ssh -i " + sshKey, "ssh -i " + sshKey,
} }

View File

@ -12,7 +12,7 @@ type Repository struct {
SubmoduleRemote bool SubmoduleRemote bool
SubmodulePartial bool SubmodulePartial bool
InsecureSSLVerify bool InsecureSkipSSLVerify bool
SafeDirectory string SafeDirectory string
InitExists bool InitExists bool
} }

View File

@ -16,7 +16,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-git-clone/git" "github.com/thegeeklab/wp-git-clone/git"
"github.com/thegeeklab/wp-plugin-go/types" "github.com/thegeeklab/wp-plugin-go/types"
@ -85,14 +84,6 @@ func (p *Plugin) Validate() error {
func (p *Plugin) Execute() error { func (p *Plugin) Execute() error {
cmds := make([]*execabs.Cmd, 0) cmds := make([]*execabs.Cmd, 0)
if err := os.Setenv("GIT_TERMINAL_PROMPT", "0"); err != nil {
return err
}
// prevents git-lfs from retrieving any LFS files
if err := os.Setenv("GIT_LFS_SKIP_SMUDGE", "1"); err != nil {
return err
}
// Handle init // Handle init
initPath := filepath.Join(p.Settings.WorkDir, ".git") initPath := filepath.Join(p.Settings.WorkDir, ".git")
@ -102,6 +93,8 @@ func (p *Plugin) Execute() error {
//nolint:nestif //nolint:nestif
if _, err := os.Stat(initPath); os.IsNotExist(err) { if _, err := os.Stat(initPath); os.IsNotExist(err) {
cmds = append(cmds, git.ConfigSafeDirectory(p.Settings.Repo))
if err := p.execCmd(git.Init(p.Settings.Repo), new(bytes.Buffer)); err != nil { if err := p.execCmd(git.Init(p.Settings.Repo), new(bytes.Buffer)); err != nil {
return err return err
} }
@ -116,7 +109,9 @@ func (p *Plugin) Execute() error {
} }
} }
if p.Settings.Repo.InsecureSkipSSLVerify {
cmds = append(cmds, git.ConfigSSLVerify(p.Settings.Repo)) cmds = append(cmds, git.ConfigSSLVerify(p.Settings.Repo))
}
if err := git.WriteNetrc(p.Settings.Netrc.Machine, p.Settings.Netrc.Login, p.Settings.Netrc.Password); err != nil { if err := git.WriteNetrc(p.Settings.Netrc.Machine, p.Settings.Netrc.Login, p.Settings.Netrc.Password); err != nil {
return err return err
@ -156,26 +151,7 @@ func (p *Plugin) Execute() error {
switch { switch {
case err != nil && shouldRetry(buf.String()): case err != nil && shouldRetry(buf.String()):
backoffOps := func() error { return retryCmd(cmd)
// copy the original command
//nolint:gosec
retry := execabs.Command(cmd.Args[0], cmd.Args[1:]...)
retry.Dir = cmd.Dir
retry.Env = cmd.Env
retry.Stdout = os.Stdout
retry.Stderr = os.Stderr
trace(cmd)
return cmd.Run()
}
backoffLog := func(err error, delay time.Duration) {
log.Error().Msgf("failed to find remote ref: %v: retry in %s", err, delay.Truncate(time.Second))
}
if err := backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog); err != nil {
return err
}
case err != nil: case err != nil:
return err return err
} }
@ -196,12 +172,23 @@ func (p *Plugin) FlagsFromContext() error {
} }
func (p *Plugin) execCmd(cmd *execabs.Cmd, buf *bytes.Buffer) error { func (p *Plugin) execCmd(cmd *execabs.Cmd, buf *bytes.Buffer) error {
cmd.Env = os.Environ() // Don' set GIT_TERMINAL_PROMPT=0 as it prevents git from loading .netrc
defaultEnvVars := []string{
"GIT_LFS_SKIP_SMUDGE=1", // prevents git-lfs from retrieving any LFS files
}
if p.Settings.Home != "" {
if _, err := os.Stat(p.Settings.Home); !os.IsNotExist(err) {
defaultEnvVars = append(defaultEnvVars, fmt.Sprintf("HOME=%s", p.Settings.Home))
}
}
cmd.Env = append(os.Environ(), defaultEnvVars...)
cmd.Stdout = io.MultiWriter(os.Stdout, buf) cmd.Stdout = io.MultiWriter(os.Stdout, buf)
cmd.Stderr = io.MultiWriter(os.Stderr, buf) cmd.Stderr = io.MultiWriter(os.Stderr, buf)
cmd.Dir = p.Settings.WorkDir cmd.Dir = p.Settings.WorkDir
fmt.Println(cmd.Dir) trace(cmd)
return cmd.Run() return cmd.Run()
} }

View File

@ -1,7 +1,6 @@
package plugin package plugin
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -41,6 +40,7 @@ func TestClone(t *testing.T) {
Pipeline: Pipeline{ Pipeline: Pipeline{
Event: tt.event, Event: tt.event,
}, },
Home: "/tmp",
WorkDir: filepath.Join(dir, tt.path), WorkDir: filepath.Join(dir, tt.path),
Recursive: tt.recursive, Recursive: tt.recursive,
Lfs: tt.lfs, Lfs: tt.lfs,
@ -86,14 +86,13 @@ func TestCloneNonEmpty(t *testing.T) {
Pipeline: Pipeline{ Pipeline: Pipeline{
Event: tt.event, Event: tt.event,
}, },
Home: "/tmp",
WorkDir: filepath.Join(dir, tt.path), WorkDir: filepath.Join(dir, tt.path),
Recursive: tt.recursive, Recursive: tt.recursive,
Lfs: tt.lfs, Lfs: tt.lfs,
}, },
} }
fmt.Println(plugin.Settings.Repo.CommitSha, tt.commit, fmt.Sprintf("%q", tt.data))
if err := plugin.Execute(); err != nil { if err := plugin.Execute(); err != nil {
t.Errorf("Expected successful clone. Got error. %s.", err) t.Errorf("Expected successful clone. Got error. %s.", err)
} }
@ -133,7 +132,6 @@ func teardown(dir string) {
// helper function to read a file in the temporary worskapce. // helper function to read a file in the temporary worskapce.
func readFile(dir, file string) string { func readFile(dir, file string) string {
filename := filepath.Join(dir, file) filename := filepath.Join(dir, file)
fmt.Println(filename)
data, _ := os.ReadFile(filename) data, _ := os.ReadFile(filename)
return string(data) return string(data)

View File

@ -18,7 +18,6 @@ type Plugin struct {
type Pipeline struct { type Pipeline struct {
Event string Event string
Number int
} }
type Netrc struct { type Netrc struct {
@ -37,6 +36,7 @@ type Settings struct {
Filter string Filter string
UseSSH bool UseSSH bool
SSHKey string SSHKey string
Home string
WorkDir string WorkDir string
Pipeline Pipeline Pipeline Pipeline

View File

@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog/log"
"golang.org/x/sys/execabs" "golang.org/x/sys/execabs"
) )
@ -26,3 +28,24 @@ func newBackoff(maxRetries uint64) backoff.BackOff {
func trace(cmd *execabs.Cmd) { func trace(cmd *execabs.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " ")) fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
} }
func retryCmd(cmd *execabs.Cmd) error {
backoffOps := func() error {
// copy the original command
//nolint:gosec
retry := execabs.Command(cmd.Args[0], cmd.Args[1:]...)
retry.Dir = cmd.Dir
retry.Env = cmd.Env
retry.Stdout = os.Stdout
retry.Stderr = os.Stderr
trace(cmd)
return cmd.Run()
}
backoffLog := func(err error, delay time.Duration) {
log.Error().Msgf("failed to find remote ref: %v: retry in %s", err, delay.Truncate(time.Second))
}
return backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog)
}