Merge pull request #1153 from why-xn/master

Checkout a specific git commit
This commit is contained in:
Tejal Desai 2020-08-13 10:41:26 -07:00 committed by GitHub
commit 1bf66ef435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -164,7 +164,7 @@ When running kaniko, use the `--context` flag with the appropriate prefix to spe
| GCS Bucket | gs://[bucket name]/[path to .tar.gz] | `gs://kaniko-bucket/path/to/context.tar.gz` |
| S3 Bucket | s3://[bucket name]/[path to .tar.gz] | `s3://kaniko-bucket/path/to/context.tar.gz` |
| Azure Blob Storage| https://[account].[azureblobhostsuffix]/[container]/[path to .tar.gz] | `https://myaccount.blob.core.windows.net/container/path/to/context.tar.gz` |
| Git Repository | git://[repository url][#reference] | `git://github.com/acme/myproject.git#refs/heads/mybranch` |
| Git Repository | git://[repository url][#reference][#commit-id] | `git://github.com/acme/myproject.git#refs/heads/mybranch#<desired-commit-id>` |
If you don't specify a prefix, kaniko will assume a local directory.
For example, to use a GCS bucket called `kaniko-bucket`, you would pass in `--context=gs://kaniko-bucket/path/to/context.tar.gz`.

View File

@ -59,7 +59,29 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) {
if len(parts) > 1 {
options.ReferenceName = plumbing.ReferenceName(parts[1])
}
_, err := git.PlainClone(directory, false, &options)
r, err := git.PlainClone(directory, false, &options)
if err == nil && len(parts) > 2 {
// ... retrieving the commit being pointed by HEAD
_, err := r.Head()
if err != nil {
return directory, err
}
w, err := r.Worktree()
if err != nil {
return directory, err
}
// ... checking out to desired commit
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(parts[2]),
})
if err != nil {
return directory, err
}
}
return directory, err
}