add support for tag templates

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher 2024-02-21 21:33:40 +02:00
parent 0c31697e10
commit 83a8c5cae2
4 changed files with 20 additions and 1 deletions

View File

@ -22,6 +22,7 @@ func pkgCmd() *cobra.Command {
hashPath string
dirty bool
devMode bool
tag string
)
cmd := &cobra.Command{
@ -36,6 +37,7 @@ func pkgCmd() *cobra.Command {
HashPath: hashPath,
Dirty: dirty,
Dev: devMode,
Tag: tag,
}
if cmd.Flags().Changed("disable-cache") && cmd.Flags().Changed("enable-cache") {
return errors.New("cannot set but disable-cache and enable-cache")
@ -85,6 +87,7 @@ func pkgCmd() *cobra.Command {
cmd.PersistentFlags().StringVar(&argOrg, "org", piBase.Org, "Override the hub org")
cmd.PersistentFlags().StringVar(&buildYML, "build-yml", "build.yml", "Override the name of the yml file")
cmd.PersistentFlags().StringVar(&hash, "hash", "", "Override the image hash (default is to query git for the package's tree-sh)")
cmd.PersistentFlags().StringVar(&tag, "tag", "{{.Hash}}", "Override the tag using fixed strings and/or text templates. Acceptable are .Hash for the hash")
cmd.PersistentFlags().StringVar(&hashCommit, "hash-commit", "HEAD", "Override the git commit to use for the hash")
cmd.PersistentFlags().StringVar(&hashPath, "hash-path", "", "Override the directory to use for the image hash, must be a parent of the package dir (default is to use the package dir)")
cmd.PersistentFlags().BoolVar(&dirty, "force-dirty", false, "Force the pkg(s) to be considered dirty")

View File

@ -534,6 +534,7 @@ func TestBuild(t *testing.T) {
}
opts = append(opts, WithBuildPlatforms(targets...))
}
tt.p.dockerfile = "testdata/Dockerfile"
err := tt.p.Build(opts...)
switch {
case (tt.err == "" && err != nil) || (tt.err != "" && err == nil) || (tt.err != "" && err != nil && !strings.HasPrefix(err.Error(), tt.err)):

View File

@ -1,12 +1,14 @@
package pkglib
import (
"bytes"
"crypto/sha1"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"gopkg.in/yaml.v2"
@ -50,6 +52,7 @@ type PkglibConfig struct {
HashPath string
Dirty bool
Dev bool
Tag string // Tag is a text/template string, defaults to {{.Hash}}
}
// NewPkInfo returns a new pkgInfo with default values
@ -89,6 +92,7 @@ type Pkg struct {
path string
dockerfile string
hash string
tag string
dirty bool
commitHash string
git *git
@ -254,6 +258,16 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
}
}
// calculate the tag to use based on the template and the pkgHash
tmpl, err := template.New("tag").Parse(cfg.Tag)
if err != nil {
return nil, fmt.Errorf("invalid tag template: %v", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, map[string]string{"Hash": pkgHash}); err != nil {
return nil, fmt.Errorf("failed to execute tag template: %v", err)
}
tag := buf.String()
pkgs = append(pkgs, Pkg{
image: pi.Image,
org: pi.Org,
@ -271,6 +285,7 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
path: pkgPath,
dockerfile: pi.Dockerfile,
git: git,
tag: tag,
})
}
return pkgs, nil
@ -295,7 +310,7 @@ func (p Pkg) ReleaseTag(release string) (string, error) {
// Tag returns the tag to use for the package
func (p Pkg) Tag() string {
t := p.hash
t := p.tag
if t == "" {
t = "latest"
}

View File