diff --git a/envbuilder.go b/envbuilder.go index ecef41d5..7607a3ee 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -43,6 +43,7 @@ import ( "github.com/fatih/color" "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-git/v5/plumbing/transport" githttp "github.com/go-git/go-git/v5/plumbing/transport/http" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/remote" @@ -196,6 +197,10 @@ type Options struct { // This is optional! GitPassword string `env:"GIT_PASSWORD"` + // GitHTTPProxyURL is the url for the http proxy. + // This is optional! + GitHTTPProxyURL string `env:"GIT_HTTP_PROXY_URL"` + // WorkspaceFolder is the path to the workspace folder // that will be built. This is optional! WorkspaceFolder string `env:"WORKSPACE_FOLDER"` @@ -363,6 +368,11 @@ func Run(ctx context.Context, options Options) error { Password: options.GitPassword, } } + if options.GitHTTPProxyURL != "" { + cloneOpts.ProxyOptions = transport.ProxyOptions{ + URL: options.GitHTTPProxyURL, + } + } cloneOpts.RepoURL = options.GitURL cloned, fallbackErr = CloneRepo(ctx, cloneOpts) diff --git a/envbuilder_test.go b/envbuilder_test.go index 84c476a0..ecd9d663 100644 --- a/envbuilder_test.go +++ b/envbuilder_test.go @@ -32,6 +32,7 @@ func TestSystemOptions(t *testing.T) { "GIT_CLONE_DEPTH": "1", "GIT_URL": "https://github.com/coder/coder", "WORKSPACE_FOLDER": "/workspaces/coder", + "GIT_HTTP_PROXY_URL": "http://company-proxy.com:8081", } env := envbuilder.OptionsFromEnv(func(s string) (string, bool) { return opts[s], true @@ -47,4 +48,5 @@ func TestSystemOptions(t *testing.T) { require.Equal(t, 1, env.GitCloneDepth) require.Equal(t, "https://github.com/coder/coder", env.GitURL) require.Equal(t, "/workspaces/coder", env.WorkspaceFolder) + require.Equal(t, "http://company-proxy.com:8081", env.GitHTTPProxyURL) } diff --git a/git.go b/git.go index 70419fc2..bbe7b096 100644 --- a/git.go +++ b/git.go @@ -26,6 +26,7 @@ type CloneRepoOptions struct { SingleBranch bool Depth int CABundle []byte + ProxyOptions transport.ProxyOptions } // CloneRepo will clone the repository at the given URL into the given path. @@ -78,6 +79,7 @@ func CloneRepo(ctx context.Context, opts CloneRepoOptions) (bool, error) { Depth: opts.Depth, SingleBranch: opts.SingleBranch, CABundle: opts.CABundle, + ProxyOptions: opts.ProxyOptions, }) if errors.Is(err, git.ErrRepositoryAlreadyExists) { return false, nil