mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-19 09:16:29 +00:00
add support for tag templates
Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
parent
0c31697e10
commit
83a8c5cae2
@ -22,6 +22,7 @@ func pkgCmd() *cobra.Command {
|
|||||||
hashPath string
|
hashPath string
|
||||||
dirty bool
|
dirty bool
|
||||||
devMode bool
|
devMode bool
|
||||||
|
tag string
|
||||||
)
|
)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -36,6 +37,7 @@ func pkgCmd() *cobra.Command {
|
|||||||
HashPath: hashPath,
|
HashPath: hashPath,
|
||||||
Dirty: dirty,
|
Dirty: dirty,
|
||||||
Dev: devMode,
|
Dev: devMode,
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
if cmd.Flags().Changed("disable-cache") && cmd.Flags().Changed("enable-cache") {
|
if cmd.Flags().Changed("disable-cache") && cmd.Flags().Changed("enable-cache") {
|
||||||
return errors.New("cannot set but disable-cache and 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(&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(&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(&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(&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().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")
|
cmd.PersistentFlags().BoolVar(&dirty, "force-dirty", false, "Force the pkg(s) to be considered dirty")
|
||||||
|
@ -534,6 +534,7 @@ func TestBuild(t *testing.T) {
|
|||||||
}
|
}
|
||||||
opts = append(opts, WithBuildPlatforms(targets...))
|
opts = append(opts, WithBuildPlatforms(targets...))
|
||||||
}
|
}
|
||||||
|
tt.p.dockerfile = "testdata/Dockerfile"
|
||||||
err := tt.p.Build(opts...)
|
err := tt.p.Build(opts...)
|
||||||
switch {
|
switch {
|
||||||
case (tt.err == "" && err != nil) || (tt.err != "" && err == nil) || (tt.err != "" && err != nil && !strings.HasPrefix(err.Error(), tt.err)):
|
case (tt.err == "" && err != nil) || (tt.err != "" && err == nil) || (tt.err != "" && err != nil && !strings.HasPrefix(err.Error(), tt.err)):
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package pkglib
|
package pkglib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
@ -50,6 +52,7 @@ type PkglibConfig struct {
|
|||||||
HashPath string
|
HashPath string
|
||||||
Dirty bool
|
Dirty bool
|
||||||
Dev bool
|
Dev bool
|
||||||
|
Tag string // Tag is a text/template string, defaults to {{.Hash}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPkInfo returns a new pkgInfo with default values
|
// NewPkInfo returns a new pkgInfo with default values
|
||||||
@ -89,6 +92,7 @@ type Pkg struct {
|
|||||||
path string
|
path string
|
||||||
dockerfile string
|
dockerfile string
|
||||||
hash string
|
hash string
|
||||||
|
tag string
|
||||||
dirty bool
|
dirty bool
|
||||||
commitHash string
|
commitHash string
|
||||||
git *git
|
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{
|
pkgs = append(pkgs, Pkg{
|
||||||
image: pi.Image,
|
image: pi.Image,
|
||||||
org: pi.Org,
|
org: pi.Org,
|
||||||
@ -271,6 +285,7 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) {
|
|||||||
path: pkgPath,
|
path: pkgPath,
|
||||||
dockerfile: pi.Dockerfile,
|
dockerfile: pi.Dockerfile,
|
||||||
git: git,
|
git: git,
|
||||||
|
tag: tag,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return pkgs, nil
|
return pkgs, nil
|
||||||
@ -295,7 +310,7 @@ func (p Pkg) ReleaseTag(release string) (string, error) {
|
|||||||
|
|
||||||
// Tag returns the tag to use for the package
|
// Tag returns the tag to use for the package
|
||||||
func (p Pkg) Tag() string {
|
func (p Pkg) Tag() string {
|
||||||
t := p.hash
|
t := p.tag
|
||||||
if t == "" {
|
if t == "" {
|
||||||
t = "latest"
|
t = "latest"
|
||||||
}
|
}
|
||||||
|
0
src/cmd/linuxkit/pkglib/testdata/Dockerfile
vendored
Normal file
0
src/cmd/linuxkit/pkglib/testdata/Dockerfile
vendored
Normal file
Loading…
Reference in New Issue
Block a user