Skip to content

Commit

Permalink
feat(export): add %image_content_based_tag% shortcut
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
  • Loading branch information
alexey-igrychev committed Apr 13, 2022
1 parent efd1072 commit 7122ee9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
24 changes: 15 additions & 9 deletions cmd/werf/export/export.go
Expand Up @@ -36,6 +36,9 @@ func NewExportCmd() *cobra.Command {
Use: "export [IMAGE_NAME...] [options]",
Short: "Export images",
Long: common.GetLongCommandDescription(`Export images to an arbitrary repository according to a template specified by the --tag option (build if needed).
The tag may contain the following shortcuts:
- %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there is more than one image in the werf config);
- %image_content_based_tag% to use a content-based tag.
All meta-information related to werf is removed from the exported images, and then images are completely under the user's responsibility`),
DisableFlagsInUseLine: true,
Example: ` # Export images to Docker Hub and GitHub Container Registry
Expand Down Expand Up @@ -232,16 +235,17 @@ func run(ctx context.Context, imagesToProcess, tagTemplateList []string) error {
})
}

func getTagFuncList(imageNameList, tagTemplateList []string) ([]func(string) string, error) {
func getTagFuncList(imageNameList, tagTemplateList []string) ([]build.ExportTagFunc, error) {
templateName := "--tag"
tmpl := template.New(templateName).Delims("%", "%")
tmpl = tmpl.Funcs(map[string]interface{}{
"image": func() string { return "%[1]s" },
"image_slug": func() string { return "%[2]s" },
"image_safe_slug": func() string { return "%[3]s" },
"image": func() string { return "%[1]s" },
"image_slug": func() string { return "%[2]s" },
"image_safe_slug": func() string { return "%[3]s" },
"image_content_based_tag": func() string { return "%[4]s" },
})

var tagFuncList []func(string) string
var tagFuncList []build.ExportTagFunc
for _, tagTemplate := range tagTemplateList {
tagFunc, err := getExportTagFunc(tmpl, templateName, imageNameList, tagTemplate)
if err != nil {
Expand All @@ -254,7 +258,7 @@ func getTagFuncList(imageNameList, tagTemplateList []string) ([]func(string) str
return tagFuncList, nil
}

func getExportTagFunc(tmpl *template.Template, templateName string, imageNameList []string, tagTemplate string) (func(imageName string) string, error) {
func getExportTagFunc(tmpl *template.Template, templateName string, imageNameList []string, tagTemplate string) (build.ExportTagFunc, error) {
tmpl, err := tmpl.Parse(tagTemplate)
if err != nil {
return nil, err
Expand All @@ -266,17 +270,19 @@ func getExportTagFunc(tmpl *template.Template, templateName string, imageNameLis
}

tagOrFormat := buf.String()
tagFunc := func(imageName string) string {
var tagFunc build.ExportTagFunc
tagFunc = func(imageName string, contentBasedTag string) string {
if strings.ContainsRune(tagOrFormat, '%') {
return fmt.Sprintf(tagOrFormat, imageName, slug.Slug(imageName), slug.DockerTag(imageName))
return fmt.Sprintf(tagOrFormat, imageName, slug.Slug(imageName), slug.DockerTag(imageName), contentBasedTag)
} else {
return tagOrFormat
}
}

contentBasedTagStub := strings.Repeat("x", 70) // 1b77754d35b0a3e603731828ee6f2400c4f937382874db2566c616bb-1624991915332
var prevImageTag string
for _, imageName := range imageNameList {
imageTag := tagFunc(imageName)
imageTag := tagFunc(imageName, contentBasedTagStub)

ref, err := name.ParseReference(imageTag, name.WeakValidation)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions docs/_includes/reference/cli/werf_export.md
Expand Up @@ -5,6 +5,10 @@
{% endif %}
Export images to an arbitrary repository according to a template specified by the --tag option
(build if needed).
The tag may contain the following shortcuts:
- %image%, %image_slug% or %image_safe_slug% to use the image name (necessary if there is more than
one image in the werf config);
- %image_content_based_tag% to use a content-based tag.
All meta-information related to werf is removed from the exported images, and then images are
completely under the user&#39;s responsibility

Expand Down
1 change: 1 addition & 0 deletions pkg/build/build_phase.go
Expand Up @@ -47,6 +47,7 @@ type BuildOptions struct {
}

type CustomTagFunc func(string, string) string
type ExportTagFunc func(string, string) string

type IntrospectOptions struct {
Targets []IntrospectTarget
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/export_phase.go
Expand Up @@ -14,7 +14,7 @@ type ExportPhase struct {
}

type ExportPhaseOptions struct {
ExportTagFuncList []func(string) string
ExportTagFuncList []ExportTagFunc
}

func NewExportPhase(c *Conveyor, opts ExportPhaseOptions) *ExportPhase {
Expand Down Expand Up @@ -51,7 +51,7 @@ func (phase *ExportPhase) exportLastStageImage(ctx context.Context, img *Image)
}).
DoError(func() error {
for _, tagFunc := range phase.ExportTagFuncList {
tag := tagFunc(img.GetName())
tag := tagFunc(img.GetName(), img.GetStageID())
if err := logboek.Context(ctx).Default().LogProcess("tag %s", tag).
DoError(func() error {
stageDesc := img.GetLastNonEmptyStage().GetStageImage().Image.GetStageDescription()
Expand Down

0 comments on commit 7122ee9

Please sign in to comment.