From: Tom Clegg Date: Sat, 11 Nov 2023 23:33:27 +0000 (-0500) Subject: 18874: Embed real git commit in wb2 build. X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/07a8dbd5a1b087ba23d85b1f26732a85ece4c791 18874: Embed real git commit in wb2 build. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- diff --git a/lib/cmd/cmd.go b/lib/cmd/cmd.go index 3d4092e6b8..2b08ab4822 100644 --- a/lib/cmd/cmd.go +++ b/lib/cmd/cmd.go @@ -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()) diff --git a/lib/install/deps.go b/lib/install/deps.go index 95a1df4dbd..4dcbf88994 100644 --- a/lib/install/deps.go +++ b/lib/install/deps.go @@ -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