Skip to content

Commit

Permalink
feat(build): add global staged directive
Browse files Browse the repository at this point in the history
Signed-off-by: QinZhanlong <qinzhanlong071@163.com>
  • Loading branch information
QinZhanlong authored and alexey-igrychev committed Jan 25, 2025
1 parent a8c261b commit d99cff3
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/_data/werf_yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ sections:
en: Common list of target platforms for all images (for example ['linux/amd64', 'linux/arm64', 'linux/arm/v8'])
ru: Общий список целевых платформ для всех образов (например ['linux/amd64', 'linux/arm64', 'linux/arm/v8'])
value: "[ string, ... ]"
- name: staged
value: "bool"
description:
en: "Enable layer-by-layer caching of Dockerfile instructions in container registry globally for all images"
ru: "Включить послойное кеширование инструкций Dockerfile в container registry глобально для всех образов"
- name: deploy
description:
en: Settings for deployment
Expand Down
2 changes: 1 addition & 1 deletion docs/pages_en/usage/build/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ If you run a build with storing images in the repository, werf will first check

By default, Dockerfile images are cached by a single image in the container registry.

To enable layered caching of Dockerfile instructions in the container registry, use the `staged` directive in werf.yaml:
To enable layered caching of Dockerfile instructions in the container registry, use the `staged` directive in werf.yaml. The directive can be set globally at the root level of werf.yaml for all images, or locally for specific images where the local setting will override the global one:

```yaml
# werf.yaml
Expand Down
2 changes: 1 addition & 1 deletion docs/pages_ru/usage/build/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ werf build --repo REPO --add-custom-tag "%image%-latest"

По умолчанию Dockerfile-образы кешируются одним образом в container registry.

Для включения послойного кеширования Dockerfile-инструкций в container registry необходимо использовать директиву `staged` в werf.yaml:
Для включения послойного кеширования Dockerfile-инструкций в container registry необходимо использовать директиву `staged` в werf.yaml. Директива может быть установлена глобально на корневом уровне werf.yaml для всех образов или локально для конкретных образов, где локальная настройка переопределит глобальную:

```yaml
# werf.yaml
Expand Down
1 change: 1 addition & 0 deletions pkg/config/meta_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package config

type MetaBuild struct {
Platform []string
Staged bool
}
14 changes: 14 additions & 0 deletions pkg/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ func prepareWerfConfig(giterminismManager giterminism_manager.Interface, rawImag
}

for _, image := range imageList {
if !rawImage.isFillStaged {
image.Staged = meta.Build.Staged
}
images = append(images, image)
}
}
Expand Down Expand Up @@ -571,6 +574,9 @@ func splitByMetaAndRawImages(docs []*doc) (*Meta, []*rawStapelImage, []*rawImage
resultMeta = rawMeta.toMeta()
case isImageFromDockerfileDoc(raw):
imageFromDockerfile := &rawImageFromDockerfile{doc: doc}
if isStagedDoc(raw) {
imageFromDockerfile.isFillStaged = true
}
err := yaml.UnmarshalStrict(doc.Content, &imageFromDockerfile)
if err != nil {
return nil, nil, nil, newYamlUnmarshalError(err, doc)
Expand Down Expand Up @@ -617,6 +623,14 @@ func isImageFromDockerfileDoc(h map[string]interface{}) bool {
return false
}

func isStagedDoc(h map[string]interface{}) bool {
if _, ok := h["staged"]; ok {
return true
}

return false
}

func newYamlUnmarshalError(err error, doc *doc) error {
switch err.(type) {
case *configError:
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/raw_image_from_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type rawImageFromDockerfile struct {
Platform []string `yaml:"platform,omitempty"`
RawSecrets []*rawSecret `yaml:"secrets,omitempty"`

doc *doc `yaml:"-"` // parent
doc *doc `yaml:"-"` // parent
isFillStaged bool `yaml:"-"` // indicates whether 'staged' field is explicitly set in the image section

UnsupportedAttributes map[string]interface{} `yaml:",inline"`
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/raw_meta_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package config

type rawMetaBuild struct {
Platform []string `yaml:"platform,omitempty"`

rawMeta *rawMeta
Staged bool `yaml:"staged,omitempty"`
rawMeta *rawMeta

UnsupportedAttributes map[string]interface{} `yaml:",inline"`
}
Expand Down Expand Up @@ -31,5 +31,6 @@ func (c *rawMetaBuild) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (c *rawMetaBuild) toMetaBuild() MetaBuild {
metaBuild := MetaBuild{}
metaBuild.Platform = c.Platform
metaBuild.Staged = c.Staged
return metaBuild
}

0 comments on commit d99cff3

Please sign in to comment.