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

feat: accept GIT_TOKEN #1318

Merged
merged 5 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ If you are using Azure Blob Storage for context file, you will need to pass [Azu
You can use `Personal Access Tokens` for Build Contexts from Private Repositories from [GitHub](https://blog.github.com/2012-09-21-easier-builds-and-deployments-using-git-over-https-and-oauth/).

You can either pass this in as part of the git URL (e.g., `git://TOKEN@github.com/acme/myproject.git#refs/heads/mybranch`)
or using the environment variable `GIT_USERNAME`.
or using the environment variable `GIT_TOKEN`.

You can also pass `GIT_USERNAME` and `GIT_PASSWORD` (password being the token) if you want to be explicit about the username.

### Using Standard Input
If running kaniko and using Standard Input build context, you will need to add the docker or kubernetes `-i, --interactive` flag.
Expand Down
6 changes: 6 additions & 0 deletions pkg/buildcontext/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (

gitAuthUsernameEnvKey = "GIT_USERNAME"
gitAuthPasswordEnvKey = "GIT_PASSWORD"
gitAuthTokenEnvKey = "GIT_TOKEN"
)

var (
Expand Down Expand Up @@ -65,6 +66,11 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) {
func getGitAuth() transport.AuthMethod {
username := os.Getenv(gitAuthUsernameEnvKey)
password := os.Getenv(gitAuthPasswordEnvKey)
token := os.Getenv(gitAuthTokenEnvKey)
if token != "" {
username = token
password = ""
}
if username != "" || password != "" {
return &http.BasicAuth{
Username: username,
Expand Down
22 changes: 22 additions & 0 deletions pkg/buildcontext/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ func TestGetGitAuth(t *testing.T) {
return
},
},
{
testName: "withToken",
setEnv: func() (expectedValue transport.AuthMethod) {
token := "some-other-token"
_ = os.Setenv(gitAuthTokenEnvKey, token)
expectedValue = &http.BasicAuth{Username: token}
return
},
},
{
testName: "withTokenUsernamePassword",
setEnv: func() (expectedValue transport.AuthMethod) {
username := "foo-user"
token := "some-token-45678"
pass := "some-password-12345"
_ = os.Setenv(gitAuthUsernameEnvKey, username)
_ = os.Setenv(gitAuthPasswordEnvKey, pass)
_ = os.Setenv(gitAuthTokenEnvKey, token)
expectedValue = &http.BasicAuth{Username: token}
return
},
},
}

for _, tt := range tests {
Expand Down