18874: Embed real git commit in wb2 build.
authorTom Clegg <tom@curii.com>
Sat, 11 Nov 2023 23:33:27 +0000 (18:33 -0500)
committerTom Clegg <tom@curii.com>
Sun, 12 Nov 2023 06:02:40 +0000 (01:02 -0500)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/cmd/cmd.go
lib/install/deps.go

index 3d4092e6b83e1dbb935e4e92f814ae74c48fe5a4..2b08ab4822b1d0e7fbd08055db415db56642c685 100644 (file)
@@ -14,6 +14,7 @@ import (
        "path/filepath"
        "regexp"
        "runtime"
+       "runtime/debug"
        "sort"
        "strings"
 
@@ -35,7 +36,13 @@ func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, std
 // 0.
 var Version versionCommand
 
-var version = "dev"
+var (
+       // These default version/commit strings should be set at build
+       // time: `go install -buildvcs=false -ldflags "-X
+       // git.arvados.org/arvados.git/lib/cmd.version=1.2.3"`
+       version = "dev"
+       commit  = "0000000000000000000000000000000000000000"
+)
 
 type versionCommand struct{}
 
@@ -43,6 +50,17 @@ func (versionCommand) String() string {
        return fmt.Sprintf("%s (%s)", version, runtime.Version())
 }
 
+func (versionCommand) Commit() string {
+       if bi, ok := debug.ReadBuildInfo(); ok {
+               for _, bs := range bi.Settings {
+                       if bs.Key == "vcs.revision" {
+                               return bs.Value
+                       }
+               }
+       }
+       return commit
+}
+
 func (versionCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
        prog = regexp.MustCompile(` -*version$`).ReplaceAllLiteralString(prog, "")
        fmt.Fprintf(stdout, "%s %s (%s)\n", prog, version, runtime.Version())
index 95a1df4dbdd64ca82ce1f7ec611fd628e8b324af..4dcbf889949fc99949919fc1880c2fb4394d0c2a 100644 (file)
@@ -49,6 +49,7 @@ var arvadosServiceFile []byte
 type installCommand struct {
        ClusterType    string
        SourcePath     string
+       Commit         string
        PackageVersion string
        EatMyData      bool
 }
@@ -71,6 +72,7 @@ func (inst *installCommand) RunCommand(prog string, args []string, stdin io.Read
        versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
        flags.StringVar(&inst.ClusterType, "type", "production", "cluster `type`: development, test, production, or package")
        flags.StringVar(&inst.SourcePath, "source", "/arvados", "source tree location (required for -type=package)")
+       flags.StringVar(&inst.Commit, "commit", "", "source commit `hash` to embed (blank means use 'git log' or all-zero placeholder)")
        flags.StringVar(&inst.PackageVersion, "package-version", "0.0.0", "version string to embed in executable files")
        flags.BoolVar(&inst.EatMyData, "eatmydata", false, "use eatmydata to speed up install")
 
@@ -80,6 +82,14 @@ func (inst *installCommand) RunCommand(prog string, args []string, stdin io.Read
                return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
        }
 
+       if inst.Commit == "" {
+               if commit, err := exec.Command("env", "-C", inst.SourcePath, "git", "log", "-n1", "--format=%H").CombinedOutput(); err == nil {
+                       inst.Commit = strings.TrimSpace(string(commit))
+               } else {
+                       inst.Commit = "0000000000000000000000000000000000000000"
+               }
+       }
+
        var dev, test, prod, pkg bool
        switch inst.ClusterType {
        case "development":
@@ -563,7 +573,10 @@ ln -sfv /var/lib/arvados/node-`+nodejsversion+`-linux-x64/bin/{yarn,yarnpkg} /us
                        // container using a non-root-owned git tree
                        // mounted from the host -- as in
                        // "arvados-package build".
-                       cmd := exec.Command("go", "install", "-buildvcs=false", "-ldflags", "-X git.arvados.org/arvados.git/lib/cmd.version="+inst.PackageVersion+" -X main.version="+inst.PackageVersion+" -s -w")
+                       cmd := exec.Command("go", "install", "-buildvcs=false",
+                               "-ldflags", "-s -w"+
+                                       " -X git.arvados.org/arvados.git/lib/cmd.version="+inst.PackageVersion+
+                                       " -X git.arvados.org/arvados.git/lib/cmd.commit="+inst.Commit)
                        cmd.Env = append(cmd.Env, os.Environ()...)
                        cmd.Env = append(cmd.Env, "GOBIN=/var/lib/arvados/bin")
                        cmd.Dir = filepath.Join(inst.SourcePath, srcdir)
@@ -678,8 +691,8 @@ done
 
                // Install workbench2 app to /var/lib/arvados/workbench2/
                if err = inst.runBash(`
-cd `+inst.SourcePath+`/services/workbench2
-VERSION="`+inst.PackageVersion+`" BUILD_NUMBER=1 GIT_COMMIT=000000000 yarn build
+cd "`+inst.SourcePath+`/services/workbench2"
+VERSION="`+inst.PackageVersion+`" BUILD_NUMBER=1 GIT_COMMIT="`+inst.Commit[:9]+`" yarn build
 rsync -a --delete-after build/ /var/lib/arvados/workbench2/
 `, stdout, stderr); err != nil {
                        return 1