Skip to content

Commit d8f664e

Browse files
committed
[package] Support __git_commit built-in variable
1 parent ac53f4e commit d8f664e

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ require (
1111
github.com/gookit/color v1.4.2
1212
github.com/imdario/mergo v0.3.12
1313
github.com/karrick/godirwalk v1.16.1
14+
github.com/kr/text v0.2.0 // indirect
1415
github.com/minio/highwayhash v1.0.2
1516
github.com/segmentio/textio v1.2.0
1617
github.com/sirupsen/logrus v1.8.1
1718
github.com/spf13/cobra v1.1.3
19+
github.com/stretchr/testify v1.7.0 // indirect
1820
golang.org/x/mod v0.4.2
1921
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
22+
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 // indirect
2023
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
24+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2125
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2226
)

go.sum

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
3535
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
3636
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
3737
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
38+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3839
github.com/creack/pty v1.1.13 h1:rTPnd/xocYRjutMfqide2zle1u96upp1gm6eUHKi7us=
3940
github.com/creack/pty v1.1.13/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4041
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
@@ -121,11 +122,13 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
121122
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
122123
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
123124
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
124-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
125125
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
126+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
127+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
126128
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
127-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
128129
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
130+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
131+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
129132
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
130133
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
131134
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
@@ -190,8 +193,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
190193
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
191194
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
192195
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
193-
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
194196
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
197+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
198+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
195199
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
196200
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
197201
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
@@ -271,8 +275,9 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
271275
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
272276
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
273277
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
274-
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
275278
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
279+
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79 h1:RX8C8PRZc2hTIod4ds8ij+/4RQX3AqhYj3uOHmyaz4E=
280+
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
276281
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
277282
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
278283
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
@@ -324,8 +329,9 @@ google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq
324329
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
325330
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
326331
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
327-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
328332
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
333+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
334+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
329335
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
330336
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
331337
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=

pkg/leeway/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,10 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (err er
10411041
// The proper thing would be to export the image, but that's rather expensive. We'll place a tar file which
10421042
// contains the names of the image we just pushed instead.
10431043
for _, img := range cfg.Image {
1044-
commands = append(commands, []string{"sh", "-c", fmt.Sprintf("echo %s >> %s", img, dockerImageNamesFiles)})
1044+
commands = append(commands,
1045+
[]string{"sh", "-c", fmt.Sprintf("echo %s >> %s", img, dockerImageNamesFiles)},
1046+
[]string{"sh", "-c", fmt.Sprintf("echo built image: %s", img)},
1047+
)
10451048
}
10461049
// In addition to the imgnames.txt we also produce a file that contains the configured metadata,
10471050
// which provides a sensible way to add metadata to the image names.

pkg/leeway/package.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"os/exec"
10+
"path/filepath"
911
"regexp"
1012
"sort"
1113
"strings"
@@ -29,6 +31,10 @@ const (
2931
// BuiltinArgPackageVersion is a builtin argument/variable which contains the version of the package currently building
3032
BuiltinArgPackageVersion = "__pkg_version"
3133

34+
// BuildinArgGitCommit is a builtin argument/variable which contains the current Git commit if the build is executed from within a Git working copy.
35+
// If this variable is used and the build is not executed from within a Git working copy the variable resolution will fail.
36+
BuildinArgGitCommit = "__git_commit"
37+
3238
// contentHashKey is the key we use to hash source files. Change this key and you'll break all past versions of all leeway builds ever.
3339
contentHashKey = "0340f3c8947cad7875140f4c4af7c62b43131dc2a8c7fc4628f0685e369a3b0b"
3440
)
@@ -699,12 +705,18 @@ func (p *Package) resolveBuiltinVariables() error {
699705
if err != nil {
700706
return err
701707
}
702-
var found bool
708+
var (
709+
found bool
710+
foundGitVar bool
711+
)
703712
for _, n := range ur {
704713
n = strings.TrimSuffix(strings.TrimPrefix(n, "${"), "}")
705-
if n == BuiltinArgPackageVersion {
714+
switch n {
715+
case BuildinArgGitCommit:
716+
foundGitVar = true
717+
fallthrough
718+
case BuiltinArgPackageVersion:
706719
found = true
707-
break
708720
}
709721
}
710722
if !found {
@@ -719,6 +731,12 @@ func (p *Package) resolveBuiltinVariables() error {
719731
builtinArgs := map[string]string{
720732
BuiltinArgPackageVersion: version,
721733
}
734+
if foundGitVar {
735+
err = resolveBuiltinGitVariables(p, builtinArgs)
736+
if err != nil {
737+
return err
738+
}
739+
}
722740

723741
type configOnlyHelper struct {
724742
Config PackageConfig `yaml:"config"`
@@ -918,3 +936,31 @@ func TopologicalSort(pkgs []*Package) {
918936
return di > dj
919937
})
920938
}
939+
940+
func resolveBuiltinGitVariables(pkg *Package, vars map[string]string) error {
941+
candidates := []string{
942+
pkg.C.Origin,
943+
pkg.C.W.Origin,
944+
}
945+
var repoLoc string
946+
for _, c := range candidates {
947+
if stat, err := os.Stat(filepath.Join(c, ".git")); err == nil && stat.IsDir() {
948+
repoLoc = c
949+
break
950+
}
951+
}
952+
if repoLoc == "" {
953+
return fmt.Errorf("package sources are not within a Git working copy")
954+
}
955+
956+
cmd := exec.Command("git", "rev-parse", "HEAD")
957+
cmd.Dir = repoLoc
958+
out, err := cmd.CombinedOutput()
959+
if err != nil {
960+
return fmt.Errorf("cannot resolve builtin Git variables: %s: %w", string(out), err)
961+
}
962+
963+
vars[BuildinArgGitCommit] = strings.TrimSpace(string(out))
964+
965+
return nil
966+
}

0 commit comments

Comments
 (0)