Skip to content

Commit

Permalink
Read git hash from RELEASE_COMMIT file if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
danehlim committed Dec 7, 2022
1 parent abeeaad commit c4894f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ amazon-linux-sources.tgz:
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.conf amazon-ecs-volume-plugin.conf
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.service amazon-ecs-volume-plugin.service
cp packaging/amazon-linux-ami-integrated/amazon-ecs-volume-plugin.socket amazon-ecs-volume-plugin.socket
tar -czf ./sources.tgz ecs-init scripts misc agent amazon-ecs-cni-plugins amazon-vpc-cni-plugins agent-container VERSION
tar -czf ./sources.tgz ecs-init scripts misc agent amazon-ecs-cni-plugins amazon-vpc-cni-plugins agent-container VERSION RELEASE_COMMIT

.amazon-linux-rpm-integrated-done: amazon-linux-sources.tgz
test -e SOURCES || ln -s . SOURCES
Expand Down
35 changes: 31 additions & 4 deletions agent/version/gen/version-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package main

import (
"io/ioutil"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -72,17 +72,44 @@ func gitHash() string {
cmd := exec.Command("git", "rev-parse", "--short=8", "HEAD")
hash, err := cmd.Output()
if err != nil {
log.Printf("failed to execute git rev-parse --short=8 HEAD, err: %v", err)
return "UNKNOWN"
}
return strings.TrimSpace(string(hash))
hashStr := strings.TrimSpace(string(hash))
log.Printf("Successfully retrieved short hash value from git rev-parse --short=8 HEAD, hashStr: %s",
hashStr)
return hashStr
}

func releaseCommitGitHash() (string, error) {
fullHash, err := os.ReadFile(filepath.Join("..", "..", "RELEASE_COMMIT"))
if err != nil {
return "", fmt.Errorf("unable to read RELEASE_COMMIT file, err: %v", err)
}
fullHashStr := strings.TrimSpace(string(fullHash))
if fullHashStr == "" || len(fullHashStr) < 8 {
log.Fatalf("Invalid hash value obtained from RELEASE_COMMIT file (empty or length <8), fullHashStr: %s",
fullHashStr)
}
log.Printf("Successfully retrieved full hash value from RELEASE_COMMIT file, fullHashStr: %s", fullHashStr)
// return short hash (first 8 characters) instead of full hash
return fullHashStr[0:8], nil
}

func selectGitHash() string {
hash, err := releaseCommitGitHash()
if err != nil {
return gitHash()
}
return hash
}

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

versionStr, _ := ioutil.ReadFile(filepath.Join("..", "..", "VERSION"))
versionStr, _ := os.ReadFile(filepath.Join("..", "..", "VERSION"))

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

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

0 comments on commit c4894f8

Please sign in to comment.