Merge pull request #1196 from yw-liu/master
Add http support for git repository context
This commit is contained in:
commit
fe0500f8e6
|
|
@ -25,6 +25,16 @@ import (
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
gitPullMethodEnvKey = "GIT_PULL_METHOD"
|
||||||
|
gitPullMethodHTTPS = "https"
|
||||||
|
gitPullMethodHTTP = "http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
supportedGitPullMethods = map[string]bool{gitPullMethodHTTPS: true, gitPullMethodHTTP: true}
|
||||||
|
)
|
||||||
|
|
||||||
// Git unifies calls to download and unpack the build context.
|
// Git unifies calls to download and unpack the build context.
|
||||||
type Git struct {
|
type Git struct {
|
||||||
context string
|
context string
|
||||||
|
|
@ -35,7 +45,7 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) {
|
||||||
directory := constants.BuildContextDir
|
directory := constants.BuildContextDir
|
||||||
parts := strings.Split(g.context, "#")
|
parts := strings.Split(g.context, "#")
|
||||||
options := git.CloneOptions{
|
options := git.CloneOptions{
|
||||||
URL: "https://" + parts[0],
|
URL: getGitPullMethod() + "://" + parts[0],
|
||||||
Progress: os.Stdout,
|
Progress: os.Stdout,
|
||||||
}
|
}
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
|
|
@ -44,3 +54,11 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) {
|
||||||
_, err := git.PlainClone(directory, false, &options)
|
_, err := git.PlainClone(directory, false, &options)
|
||||||
return directory, err
|
return directory, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getGitPullMethod() string {
|
||||||
|
gitPullMethod := os.Getenv(gitPullMethodEnvKey)
|
||||||
|
if ok := supportedGitPullMethods[gitPullMethod]; !ok {
|
||||||
|
gitPullMethod = gitPullMethodHTTPS
|
||||||
|
}
|
||||||
|
return gitPullMethod
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 Google LLC
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package buildcontext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleContainerTools/kaniko/testutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetGitPullMethod(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
testName string
|
||||||
|
setEnv func() (expectedValue string)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
testName: "noEnv",
|
||||||
|
setEnv: func() (expectedValue string) {
|
||||||
|
expectedValue = gitPullMethodHTTPS
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "emptyEnv",
|
||||||
|
setEnv: func() (expectedValue string) {
|
||||||
|
_ = os.Setenv(gitPullMethodEnvKey, "")
|
||||||
|
expectedValue = gitPullMethodHTTPS
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "httpEnv",
|
||||||
|
setEnv: func() (expectedValue string) {
|
||||||
|
err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTP)
|
||||||
|
if nil != err {
|
||||||
|
expectedValue = gitPullMethodHTTPS
|
||||||
|
} else {
|
||||||
|
expectedValue = gitPullMethodHTTP
|
||||||
|
}
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "httpsEnv",
|
||||||
|
setEnv: func() (expectedValue string) {
|
||||||
|
_ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTPS)
|
||||||
|
expectedValue = gitPullMethodHTTPS
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "unknownEnv",
|
||||||
|
setEnv: func() (expectedValue string) {
|
||||||
|
_ = os.Setenv(gitPullMethodEnvKey, "unknown")
|
||||||
|
expectedValue = gitPullMethodHTTPS
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.testName, func(t *testing.T) {
|
||||||
|
expectedValue := tt.setEnv()
|
||||||
|
testutil.CheckDeepEqual(t, expectedValue, getGitPullMethod())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue