Merge pull request #3243 from thaJeztah/fix_build_context

build: fix AddDockerfileToBuildContext not de-referencing tar header template
This commit is contained in:
Sebastiaan van Stijn 2021-08-11 11:57:22 +02:00 committed by GitHub
commit b4dde3a130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 12 deletions

View File

@ -378,26 +378,32 @@ func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCl
return nil, "", err return nil, "", err
} }
now := time.Now() now := time.Now()
hdrTmpl := &tar.Header{ randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
// Add the dockerfile with a random filename
randomName: func(_ string, _ *tar.Header, _ io.Reader) (*tar.Header, []byte, error) {
header := &tar.Header{
Name: randomName,
Mode: 0600, Mode: 0600,
Uid: 0,
Gid: 0,
ModTime: now, ModTime: now,
Typeflag: tar.TypeReg, Typeflag: tar.TypeReg,
AccessTime: now, AccessTime: now,
ChangeTime: now, ChangeTime: now,
} }
randomName := ".dockerfile." + stringid.GenerateRandomID()[:20] return header, file, nil
buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
// Add the dockerfile with a random filename
randomName: func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
return hdrTmpl, file, nil
}, },
// Update .dockerignore to include the random filename // Update .dockerignore to include the random filename
".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) { ".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
if h == nil { if h == nil {
h = hdrTmpl h = &tar.Header{
Name: ".dockerignore",
Mode: 0600,
ModTime: now,
Typeflag: tar.TypeReg,
AccessTime: now,
ChangeTime: now,
}
} }
b := &bytes.Buffer{} b := &bytes.Buffer{}