diff --git a/README.md b/README.md index 160cdde13..d4dc57bda 100644 --- a/README.md +++ b/README.md @@ -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#` | 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`. diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 1ca42c35b..62a2ae44c 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -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 }