Skip to content

Commit 9fc6bdb

Browse files
committed
Read git hash from RELEASE_COMMIT file if possible
1 parent abeeaad commit 9fc6bdb

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ amazon-linux-sources.tgz:
377377
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.conf amazon-ecs-volume-plugin.conf
378378
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.service amazon-ecs-volume-plugin.service
379379
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.socket amazon-ecs-volume-plugin.socket
380-
tar -czf ./sources.tgz ecs-init scripts misc agent amazon-ecs-cni-plugins amazon-vpc-cni-plugins agent-container VERSION
380+
tar -czf ./sources.tgz ecs-init scripts misc agent amazon-ecs-cni-plugins amazon-vpc-cni-plugins agent-container VERSION RELEASE_COMMIT
381381

382382
.amazon-linux-rpm-integrated-done: amazon-linux-sources.tgz
383383
test -e SOURCES || ln -s . SOURCES

agent/version/gen/version-gen.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package main
1515

1616
import (
17-
"io/ioutil"
17+
"fmt"
1818
"log"
1919
"os"
2020
"os/exec"
@@ -72,17 +72,44 @@ func gitHash() string {
7272
cmd := exec.Command("git", "rev-parse", "--short=8", "HEAD")
7373
hash, err := cmd.Output()
7474
if err != nil {
75+
log.Printf("failed to execute git rev-parse --short=8 HEAD, err: %v", err)
7576
return "UNKNOWN"
7677
}
77-
return strings.TrimSpace(string(hash))
78+
hashStr := strings.TrimSpace(string(hash))
79+
log.Printf("Successfully retrieved short hash value from git rev-parse --short=8 HEAD, hashStr: %s",
80+
hashStr)
81+
return hashStr
82+
}
83+
84+
func releaseCommitGitHash() (string, error) {
85+
fullHash, err := os.ReadFile(filepath.Join("..", "..", "RELEASE_COMMIT"))
86+
if err != nil {
87+
return "", fmt.Errorf("unable to read RELEASE_COMMIT file, err: %v", err)
88+
}
89+
fullHashStr := strings.TrimSpace(string(fullHash))
90+
if fullHashStr == "" || len(fullHashStr) < 8 {
91+
log.Fatalf("Invalid hash value obtained from RELEASE_COMMIT file (empty or length <8), fullHashStr: %s",
92+
fullHashStr)
93+
}
94+
log.Printf("Successfully retrieved full hash value from RELEASE_COMMIT file, fullHashStr: %s", fullHashStr)
95+
// return short hash (first 8 characters) instead of full hash
96+
return fullHashStr[0:8], nil
97+
}
98+
99+
func selectGitHash() string {
100+
hash, err := releaseCommitGitHash()
101+
if err != nil {
102+
return gitHash()
103+
}
104+
return hash
78105
}
79106

80107
// version-gen is a simple program that generates the agent's version file,
81108
// containing information about the agent's version, commit hash, and repository
82109
// cleanliness.
83110
func main() {
84111

85-
versionStr, _ := ioutil.ReadFile(filepath.Join("..", "..", "VERSION"))
112+
versionStr, _ := os.ReadFile(filepath.Join("..", "..", "VERSION"))
86113

87114
// default values
88115
info := versionInfo{
@@ -101,7 +128,7 @@ func main() {
101128
// have a commit hash so that it does not churn with every commit. This
102129
// env var should not be set when building, and go generate should be
103130
// run before any build, such that the commithash will be set correctly.
104-
info.Hash = gitHash()
131+
info.Hash = selectGitHash()
105132
}
106133

107134
outFile, err := os.Create("version.go")

0 commit comments

Comments
 (0)