-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2799 from AkihiroSuda/dockerfile-add-git
dockerfile (labs): implement `ADD <git ref>`
- Loading branch information
Showing
12 changed files
with
391 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ run: | |
|
||
build-tags: | ||
- dfrunsecurity | ||
- dfaddgit | ||
|
||
linters: | ||
enable: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build dfaddgit | ||
// +build dfaddgit | ||
|
||
package dockerfile2llb | ||
|
||
const addGitEnabled = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build !dfaddgit | ||
// +build !dfaddgit | ||
|
||
package dockerfile2llb | ||
|
||
const addGitEnabled = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//go:build dfaddgit | ||
// +build dfaddgit | ||
|
||
package dockerfile | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/containerd/continuity/fs/fstest" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/frontend/dockerfile/builder" | ||
"github.com/moby/buildkit/util/testutil/integration" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var addGitTests = integration.TestFuncs( | ||
testAddGit, | ||
) | ||
|
||
func init() { | ||
allTests = append(allTests, addGitTests...) | ||
} | ||
|
||
func testAddGit(t *testing.T, sb integration.Sandbox) { | ||
f := getFrontend(t, sb) | ||
|
||
gitDir, err := os.MkdirTemp("", "buildkit") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(gitDir) | ||
gitCommands := []string{ | ||
"git init", | ||
"git config --local user.email test", | ||
"git config --local user.name test", | ||
} | ||
makeCommit := func(tag string) []string { | ||
return []string{ | ||
"echo foo of " + tag + " >foo", | ||
"git add foo", | ||
"git commit -m " + tag, | ||
"git tag " + tag, | ||
} | ||
} | ||
gitCommands = append(gitCommands, makeCommit("v0.0.1")...) | ||
gitCommands = append(gitCommands, makeCommit("v0.0.2")...) | ||
gitCommands = append(gitCommands, makeCommit("v0.0.3")...) | ||
gitCommands = append(gitCommands, "git update-server-info") | ||
err = runShell(gitDir, gitCommands...) | ||
require.NoError(t, err) | ||
|
||
server := httptest.NewServer(http.FileServer(http.Dir(filepath.Join(gitDir)))) | ||
defer server.Close() | ||
serverURL := server.URL | ||
t.Logf("serverURL=%q", serverURL) | ||
|
||
dockerfile, err := applyTemplate(` | ||
FROM alpine | ||
# Basic case | ||
ADD {{.ServerURL}}/.git#v0.0.1 /x | ||
RUN cd /x && \ | ||
[ "$(cat foo)" = "foo of v0.0.1" ] | ||
# Complicated case | ||
ARG REPO="{{.ServerURL}}/.git" | ||
ARG TAG="v0.0.2" | ||
ADD --keep-git-dir=true --chown=4242:8484 ${REPO}#${TAG} /buildkit-chowned | ||
RUN apk add git | ||
USER 4242 | ||
RUN cd /buildkit-chowned && \ | ||
[ "$(cat foo)" = "foo of v0.0.2" ] && \ | ||
[ "$(stat -c %u foo)" = "4242" ] && \ | ||
[ "$(stat -c %g foo)" = "8484" ] && \ | ||
[ -z "$(git status -s)" ] | ||
`, map[string]string{ | ||
"ServerURL": serverURL, | ||
}) | ||
require.NoError(t, err) | ||
t.Logf("dockerfile=%s", dockerfile) | ||
|
||
dir, err := integration.Tmpdir(t, | ||
fstest.CreateFile("Dockerfile", []byte(dockerfile), 0600), | ||
) | ||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
c, err := client.New(sb.Context(), sb.Address()) | ||
require.NoError(t, err) | ||
defer c.Close() | ||
|
||
_, err = f.Solve(sb.Context(), c, client.SolveOpt{ | ||
LocalDirs: map[string]string{ | ||
builder.DefaultLocalNameDockerfile: dir, | ||
builder.DefaultLocalNameContext: dir, | ||
}, | ||
}, nil) | ||
require.NoError(t, err) | ||
} | ||
|
||
func applyTemplate(tmpl string, x interface{}) (string, error) { | ||
var buf bytes.Buffer | ||
parsed, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
return "", err | ||
} | ||
if err := parsed.Execute(&buf, x); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1500,6 +1500,44 @@ guide – Leverage build cache](https://docs.docker.com/develop/develop-images/d | |
- If `<dest>` doesn't exist, it is created along with all missing directories | ||
in its path. | ||
|
||
### Adding a git repository `ADD <git ref> <dir>` | ||
|
||
> **Note** | ||
> | ||
> Available in [`docker/dockerfile-upstream:master-labs`](#syntax). | ||
> Will be included in `docker/dockerfile:1.5-labs`. | ||
This form allows adding a git repository to an image directly, without using the `git` command inside the image: | ||
``` | ||
ADD [--keep-git-dir=<boolean>] <git ref> <dir> | ||
``` | ||
|
||
```dockerfile | ||
# syntax=docker/dockerfile-upstream:master-labs | ||
FROM alpine | ||
ADD --keep-git-dir=true https://github.com/moby/buildkit.git#v0.10.1 /buildkit | ||
``` | ||
|
||
The `--keep-git-dir=true` flag adds the `.git` directory. This flag defaults to false. | ||
|
||
### Adding a private git repository | ||
To add a private repo via SSH, create a Dockerfile with the following form: | ||
```dockerfile | ||
# syntax = docker/dockerfile-upstream:master-labs | ||
FROM alpine | ||
ADD [email protected]:foo/bar.git /bar | ||
``` | ||
|
||
This Dockerfile can be built with `docker build --ssh` or `buildctl build --ssh`, e.g., | ||
|
||
```console | ||
$ docker build --ssh default | ||
``` | ||
|
||
```console | ||
$ buildctl build --frontend=dockerfile.v0 --local context=. --local dockerfile=. --ssh default | ||
``` | ||
|
||
## ADD --link | ||
|
||
See [`COPY --link`](#copy---link). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
dfrunsecurity | ||
dfrunsecurity dfaddgit |
Oops, something went wrong.