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

Checkout a specific git commit #1153

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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`.
Expand Down
24 changes: 23 additions & 1 deletion pkg/buildcontext/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ 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()
Copy link
Member

@tejal29 tejal29 Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add more explanation around whyr.Head() is necessary?
It would be great to add some tests around this integration.
Please see your existing Git Context integration test.

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
}