Skip to content

Commit 0076526

Browse files
committed
Buildpacks support Auto sync
Signed-off-by: David Gageot <[email protected]>
1 parent 2b9d573 commit 0076526

File tree

15 files changed

+123
-110
lines changed

15 files changed

+123
-110
lines changed

docs/content/en/docs/pipeline-stages/filesync.md

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,42 @@ aliases: [/docs/how-tos/filesync]
99
Skaffold supports copying changed files to a deployed container so as to avoid the need to rebuild, redeploy, and restart the corresponding pod.
1010
The file copying is enabled by adding a `sync` section with _sync rules_ to the `artifact` in the `skaffold.yaml`.
1111
Under the hood, Skaffold creates a tar file with changed files that match the sync rules.
12-
This tar file is sent to and extracted on the corresponding containers.
12+
This tar file is sent to and extracted on the corresponding containers.
13+
14+
Multiple types of sync are supported by Skaffold:
15+
16+
+ `manual`: The user must specify both the files in their local workspace and the destination in the running container.
17+
This is supported by every type of artifact.
18+
19+
+ `infer`: The destinations for each changed file is inferred from instructions in a Dockerfile.
20+
This is supported by docker and kaniko artifacts and also for custom artifacts that declare a
21+
dependency on a Dockerfile.
22+
23+
+ `auto`: Skaffold automatically configures the sync. This is only supported by Jib and Buildpacks artifacts.
24+
25+
### Manual sync mode
26+
27+
A manual sync rule must specify the `src` and `dest` field.
28+
The `src` field is a glob pattern to match files relative to the artifact _context_ directory, which may contain `**` to match nested files.
29+
The `dest` field is the absolute or relative destination path in the container.
30+
If the destination is a relative path, an absolute path will be inferred by prepending the path with the container's `WORKDIR`.
31+
By default, matched files are transplanted with their whole directory hierarchy below the artifact context directory onto the destination.
32+
The optional `strip` field can cut off some levels from the directory hierarchy.
33+
The following example showcases manual filesync:
34+
35+
{{% readfile file="samples/filesync/filesync-manual.yaml" %}}
36+
37+
- The first rule synchronizes the file `.filebaserc` to the `/etc` folder in the container.
38+
- The second rule synchronizes all `html` files in the `static-html` folder into the `<WORKDIR>/static` folder in the container.
39+
Note that this pattern does not match files in sub-folders below `static-html` (e.g. `static-html/a.html` but not `static-html/sub/a.html`).
40+
- The third rule synchronizes all `png` files from any sub-folder into the `assets` folder on the container.
41+
For example, `img.png``assets/img.png` or `sub/img.png``assets/sub/img.png`.
42+
- The last rule synchronizes all `md` files below the `content/en` directory into the `content` folder on the container.
43+
The `strip` directive ensures that only the directory hierarchy below `content/en` is re-created at the destination.
44+
For example, `content/en/index.md``content/index.md` or `content/en/sub/index.md``content/sub/index.md`.
1345

1446
### Inferred sync mode
47+
1548
For docker artifacts, Skaffold knows how to infer the desired destination from the artifact's `Dockerfile`.
1649
To enable syncing, you only need to specify which files are eligible for syncing in the sync rules.
1750
The sync rules for inferred sync mode is just a list of glob patterns.
@@ -42,26 +75,21 @@ And a `skaffold.yaml` with the following sync configuration:
4275
Inferred sync mode only applies to modified and added files.
4376
File deletion will always cause a complete rebuild.
4477

45-
### Manual sync mode
78+
### Auto sync mode (Buildpacks)
4679

47-
A manual sync rule must specify the `src` and `dest` field.
48-
The `src` field is a glob pattern to match files relative to the artifact _context_ directory, which may contain `**` to match nested files.
49-
The `dest` field is the absolute or relative destination path in the container.
50-
If the destination is a relative path, an absolute path will be inferred by prepending the path with the container's `WORKDIR`.
51-
By default, matched files are transplanted with their whole directory hierarchy below the artifact context directory onto the destination.
52-
The optional `strip` field can cut off some levels from the directory hierarchy.
53-
The following example showcases manual filesync:
80+
Skaffold requires special collaboration from the Buildpacks for the `auto` sync to work.
81+
The [gcr.io/buildpacks/builder](https://github.com/GoogleCloudPlatform/buildpacks) supports Skaffold
82+
out of the box, currently for Go and NodeJS.
5483

55-
{{% readfile file="samples/filesync/filesync-manual.yaml" %}}
84+
Cloud Native Buildpacks set a `io.buildpacks.build.metadata` label on the images they create.
85+
This labels points to json description of the [Bill-of-Materials, aka BOM](https://github.com/buildpacks/spec/blob/master/buildpack.md#bill-of-materials-toml) of the build.
86+
In the BOM, under the `metadata.devmode.sync` key, Buildpacks that want to collaborate with Skaffold
87+
have to output the sync rules based on their exploration of the source and the build process they had to apply to it.
88+
Those sync rules will then be used by Skaffold without the user having to configure them manually.
5689

57-
- The first rule synchronizes the file `.filebaserc` to the `/etc` folder in the container.
58-
- The second rule synchronizes all `html` files in the `static-html` folder into the `<WORKDIR>/static` folder in the container.
59-
Note that this pattern does not match files in sub-folders below `static-html` (e.g. `static-html/a.html` but not `static-html/sub/a.html`).
60-
- The third rule synchronizes all `png` files from any sub-folder into the `assets` folder on the container.
61-
For example, `img.png``assets/img.png` or `sub/img.png``assets/sub/img.png`.
62-
- The last rule synchronizes all `md` files below the `content/en` directory into the `content` folder on the container.
63-
The `strip` directive ensures that only the directory hierarchy below `content/en` is re-created at the destination.
64-
For example, `content/en/index.md``content/index.md` or `content/en/sub/index.md``content/sub/index.md`.
90+
Another thing the Buildpacks have to do is support the `GOOGLE_DEVMODE` environment variable. Skaffold will
91+
set it to `1` when running `skaffold dev` with sync configured to `auto: {}`. The Buildpacks can then use that
92+
signal to change the way the application is built so that it reloads the changes or rebuilds the app on each change.
6593

6694
## Limitations
6795

docs/content/en/schemas/v2beta3.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,16 +1947,16 @@
19471947
"properties": {
19481948
"auto": {
19491949
"$ref": "#/definitions/Auto",
1950-
"description": "delegates discovery of sync rules to the build system. Currently only available for jib.",
1951-
"x-intellij-html-description": "delegates discovery of sync rules to the build system. Currently only available for jib."
1950+
"description": "delegates discovery of sync rules to the build system. Only available for jib and buildpacks.",
1951+
"x-intellij-html-description": "delegates discovery of sync rules to the build system. Only available for jib and buildpacks."
19521952
},
19531953
"infer": {
19541954
"items": {
19551955
"type": "string"
19561956
},
19571957
"type": "array",
1958-
"description": "file patterns which may be synced into the container. The container destination is inferred by the builder. Currently only available for docker artifacts.",
1959-
"x-intellij-html-description": "file patterns which may be synced into the container. The container destination is inferred by the builder. Currently only available for docker artifacts.",
1958+
"description": "file patterns which may be synced into the container The container destination is inferred by the builder based on the instructions of a Dockerfile. Available for docker and kaniko artifacts and custom artifacts that declare dependencies on a dockerfile.",
1959+
"x-intellij-html-description": "file patterns which may be synced into the container The container destination is inferred by the builder based on the instructions of a Dockerfile. Available for docker and kaniko artifacts and custom artifacts that declare dependencies on a dockerfile.",
19601960
"default": "[]"
19611961
},
19621962
"manual": {

examples/buildpacks-node/skaffold.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ build:
55
- image: skaffold-buildpacks-node
66
buildpack:
77
builder: "gcr.io/buildpacks/builder"
8+
sync:
9+
auto: {}

examples/buildpacks/skaffold.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ build:
66
buildpack:
77
builder: "gcr.io/buildpacks/builder"
88
env:
9-
- GOVERSION=1.13.1
109
- GOPROXY={{.GOPROXY}}
10+
sync:
11+
auto: {}
1112
profiles:
1213
- name: gcb
1314
build:

integration/examples/buildpacks-node/skaffold.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ build:
55
- image: skaffold-buildpacks-node
66
buildpack:
77
builder: "gcr.io/buildpacks/builder"
8+
sync:
9+
auto: {}

integration/examples/buildpacks/skaffold.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ build:
77
builder: "gcr.io/buildpacks/builder"
88
env:
99
- GOPROXY={{.GOPROXY}}
10+
sync:
11+
auto: {}
1012
profiles:
1113
- name: gcb
1214
build:

pkg/skaffold/build/buildpacks/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestBuild(t *testing.T) {
7878
},
7979
{
8080
description: "dev mode",
81-
artifact: withSync(&latest.Sync{Infer: []string{"**/*"}}, buildpacksArtifact("another/builder", "another/run")),
81+
artifact: withSync(&latest.Sync{Auto: &latest.Auto{}}, buildpacksArtifact("another/builder", "another/run")),
8282
tag: "img:tag",
8383
api: &testutil.FakeAPIClient{},
8484
devMode: true,

pkg/skaffold/build/buildpacks/lifecycle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
5959
return "", fmt.Errorf("unable to evaluate env variables: %w", err)
6060
}
6161

62-
if b.devMode && a.Sync != nil && len(a.Sync.Infer) > 0 {
62+
if b.devMode && a.Sync != nil && a.Sync.Auto != nil {
6363
env = append(env, "GOOGLE_DEVMODE=1")
6464
}
6565

pkg/skaffold/build/cache/hash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func artifactConfig(a *latest.Artifact, devMode bool) (string, error) {
106106
return "", fmt.Errorf("marshalling the artifact's configuration for %q: %w", a.ImageName, err)
107107
}
108108

109-
if devMode && a.BuildpackArtifact != nil && a.Sync != nil && len(a.Sync.Infer) > 0 {
109+
if devMode && a.BuildpackArtifact != nil && a.Sync != nil && a.Sync.Auto != nil {
110110
return string(buf) + ".DEV", nil
111111
}
112112

pkg/skaffold/build/cache/hash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestArtifactConfigDevMode(t *testing.T) {
184184
},
185185
}
186186
sync := &latest.Sync{
187-
Infer: []string{"**/*"},
187+
Auto: &latest.Auto{},
188188
}
189189

190190
config, err := artifactConfig(&latest.Artifact{

0 commit comments

Comments
 (0)