Skip to content

Commit 59a8e3f

Browse files
feat(build, stapel): add disableGitAfterPatch directive
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 0a93349 commit 59a8e3f

File tree

8 files changed

+62
-20
lines changed

8 files changed

+62
-20
lines changed

docs/_data/werf_yaml.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ sections:
342342
ru: "Версия кеша"
343343
detailsArticle:
344344
all: "/usage/build/stapel/base.html#fromcacheversion"
345+
- name: disableGitAfterPatch
346+
value: "bool"
347+
description:
348+
en: "Disable updating git sources (gitCache, gitLatestPatch stages)"
349+
ru: "Отключение актуализации исходников Git (стадии gitCache, gitLatestPatch)"
350+
detailsArticle:
351+
en: "/usage/build/stapel/instructions.html#disabling-source-updates-skipping-gitcache-and-gitlatestpatch-stages"
352+
ru: "/usage/build/stapel/instructions.html#отключение-обновления-исходников-стадии-gitcache-и-gitlatestpatch"
345353
- name: git
346354
description:
347355
en: "Set of directives to add source files from git repositories (both the project repository and any other)"

docs/pages_en/usage/build/stapel/instructions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ shell:
504504

505505
The _git mapping configuration_ in the above `werf.yaml` instructs werf to transfer the contents of the `/src` directory of the local Git repository to the `/app` directory of the image. During the first build, files will be cached at the _gitArchive_ stage, and assembly instructions for _install_ and _beforeSetup_ will be executed. During the builds triggered by the subsequent commits which leave the contents of the `/src` directory unchanged, werf will not run the assembly instructions. Changes in the `/src` directory due to some commit will also result in changes in the checksums of the files matching the mask. This will cause werf to apply the git patch and rebuild any existing stages starting with _beforeSetup_, namely _beforeSetup_ and _setup_. The git patch will be applied once during the _beforeSetup_ stage.
506506

507+
### Disabling source updates (skipping gitCache and gitLatestPatch stages)
508+
509+
The `disableGitAfterPatch` directive allows you to lock the source code in the image during the artifact build stage and prevent it from being updated in subsequent builds.
510+
511+
This approach is useful in scenarios where you need to use the actual set of source files during the build stage, but only changes to specific files should restart the build process.
512+
513+
Therefore:
514+
- If `git.stageDependencies` is used, the rebuild, and execution of commands with the current source files, will occur when relevant changes are detected.
515+
- If `git.stageDependencies` is not used, there will be no rebuilds.
516+
507517
## Dependency on the CacheVersion
508518

509519
There are situations when a user wants to rebuild all _user stages_ or just one of them. They can do so by changing `cacheVersion` or `<user stage name>CacheVersion` parameters.

docs/pages_ru/usage/build/stapel/instructions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,16 @@ shell:
533533

534534
Сборка следующего коммита, в котором будут только изменения файлов за пределами каталога `/src`, не приведет к выполнению инструкций каких-либо стадий. Если коммит будет содержать изменение внутри каталога `/src`, контрольные суммы файлов подпадающих под маску изменятся, werf применит Git-патч и пересоберёт все пользовательские стадии, начиная со стадии _beforeSetup_, а именно — _beforeSetup_ и _setup_. Применение Git-патча будет выполнено один раз на стадии _beforeSetup_.
535535

536+
### Отключение обновления исходников (стадии gitCache и gitLatestPatch)
537+
538+
Параметр `disableGitAfterPatch` позволяет зафиксировать исходный код в образе на этапе сборки артефакта и предотвратить его обновление при последующих сборках.
539+
540+
Этот подход полезен в случаях, когда необходимо использовать актуальный набор исходных файлов на этапе сборки, но изменения только определенных файлов должны перезапускать процесс сборки.
541+
542+
Таким образом:
543+
- если использовать `git.stageDepedencies`, то пересборка, выполнение команд с актуальными исходниками, будет выполняться при соответствующих изменениях;
544+
- если не использовать `git.stageDepedencies`, то пересборок не будет.
545+
536546
## Зависимость от значения CacheVersion
537547

538548
Существуют ситуации, когда необходимо принудительно пересобрать все или какую-то конкретную _пользовательскую стадию_. Этого можно достичь, изменяя параметры `cacheVersion` или `<user stage name>CacheVersion`.

pkg/build/image/stapel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func initStages(ctx context.Context, image *Image, metaConfig *config.Meta, stap
107107
stages = appendIfExist(ctx, stages, stage.GenerateSetupStage(ctx, imageBaseConfig, gitPatchStageOptions, baseStageOptions))
108108
stages = appendIfExist(ctx, stages, stage.GenerateDependenciesAfterSetupStage(imageBaseConfig, baseStageOptions))
109109

110-
if !stapelImageConfig.IsArtifact() {
110+
if !stapelImageConfig.IsGitAfterPatchDisabled() {
111111
if gitMappingsExist {
112112
stages = append(stages, stage.NewGitCacheStage(gitPatchStageOptions, baseStageOptions))
113113
stages = append(stages, stage.NewGitLatestPatchStage(gitPatchStageOptions, baseStageOptions))

pkg/config/git_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ package config
33
type GitManager struct {
44
Local []*GitLocal
55
Remote []*GitRemote
6+
7+
isGitAfterPatchDisabled bool
68
}

pkg/config/raw_stapel_image.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ import (
77
)
88

99
type rawStapelImage struct {
10-
Images []string `yaml:"-"`
11-
Final *bool `yaml:"final,omitempty"`
12-
Artifact string `yaml:"artifact,omitempty"`
13-
From string `yaml:"from,omitempty"`
14-
FromLatest bool `yaml:"fromLatest,omitempty"`
15-
FromCacheVersion string `yaml:"fromCacheVersion,omitempty"`
16-
FromImage string `yaml:"fromImage,omitempty"`
17-
FromArtifact string `yaml:"fromArtifact,omitempty"`
18-
RawGit []*rawGit `yaml:"git,omitempty"`
19-
RawShell *rawShell `yaml:"shell,omitempty"`
20-
RawAnsible *rawAnsible `yaml:"ansible,omitempty"`
21-
RawMount []*rawMount `yaml:"mount,omitempty"`
22-
RawDocker *rawDocker `yaml:"docker,omitempty"`
23-
RawImport []*rawImport `yaml:"import,omitempty"`
24-
RawDependencies []*rawDependency `yaml:"dependencies,omitempty"`
25-
Platform []string `yaml:"platform,omitempty"`
10+
Images []string `yaml:"-"`
11+
Final *bool `yaml:"final,omitempty"`
12+
Artifact string `yaml:"artifact,omitempty"`
13+
From string `yaml:"from,omitempty"`
14+
FromLatest bool `yaml:"fromLatest,omitempty"`
15+
FromCacheVersion string `yaml:"fromCacheVersion,omitempty"`
16+
FromImage string `yaml:"fromImage,omitempty"`
17+
FromArtifact string `yaml:"fromArtifact,omitempty"`
18+
DisableGitAfterPatch bool `yaml:"disableGitAfterPatch,omitempty"`
19+
RawGit []*rawGit `yaml:"git,omitempty"`
20+
RawShell *rawShell `yaml:"shell,omitempty"`
21+
RawAnsible *rawAnsible `yaml:"ansible,omitempty"`
22+
RawMount []*rawMount `yaml:"mount,omitempty"`
23+
RawDocker *rawDocker `yaml:"docker,omitempty"`
24+
RawImport []*rawImport `yaml:"import,omitempty"`
25+
RawDependencies []*rawDependency `yaml:"dependencies,omitempty"`
26+
Platform []string `yaml:"platform,omitempty"`
2627

2728
doc *doc `yaml:"-"` // parent
2829

@@ -121,6 +122,10 @@ func (c *rawStapelImage) toStapelImageArtifactDirectives(giterminismManager gite
121122
}
122123
imageArtifact.StapelImageBase.final = false
123124

125+
if imageArtifact.Git != nil {
126+
imageArtifact.Git.isGitAfterPatchDisabled = true
127+
}
128+
124129
if err := c.validateStapelImageArtifactDirective(imageArtifact); err != nil {
125130
return nil, err
126131
}
@@ -210,6 +215,8 @@ func (c *rawStapelImage) validateStapelImageArtifactDirective(imageArtifact *Sta
210215
return newDetailedConfigError("`docker` section is not supported for artifact!", nil, c.doc)
211216
} else if c.Final != nil {
212217
return newDetailedConfigError("`final` directive is not supported for artifact!", nil, c.doc)
218+
} else if c.DisableGitAfterPatch {
219+
return newDetailedConfigError("`disableGitAfterPatch` directive is not supported for artifact!", nil, c.doc)
213220
}
214221

215222
if err := imageArtifact.validate(); err != nil {
@@ -316,6 +323,7 @@ func (c *rawStapelImage) toBaseStapelImageBaseDirective(giterminismManager giter
316323
}
317324

318325
imageBase.Git = &GitManager{}
326+
imageBase.Git.isGitAfterPatchDisabled = c.DisableGitAfterPatch
319327

320328
imageBase.raw = c
321329

pkg/config/stapel_image_base.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ func (c *StapelImageBase) ImageBaseConfig() *StapelImageBase {
4040
return c
4141
}
4242

43-
func (c *StapelImageBase) IsArtifact() bool {
44-
return false
43+
func (c *StapelImageBase) IsGitAfterPatchDisabled() bool {
44+
if c.Git == nil {
45+
return false
46+
}
47+
48+
return c.Git.isGitAfterPatchDisabled
4549
}
4650

4751
func (c *StapelImageBase) IsFinal() bool {

pkg/config/stapel_image_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package config
33
type StapelImageInterface interface {
44
ImageInterface
55
ImageBaseConfig() *StapelImageBase
6-
IsArtifact() bool
6+
IsGitAfterPatchDisabled() bool
77
}

0 commit comments

Comments
 (0)