Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read git hash from RELEASE_COMMIT file if possible
Browse files Browse the repository at this point in the history
danehlim committed Dec 7, 2022
1 parent abeeaad commit 1f6256a
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
28 changes: 22 additions & 6 deletions agent/version/gen/version-gen.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
package main

import (
"io/ioutil"
"log"
"os"
"os/exec"
@@ -69,20 +68,37 @@ func gitDirty() bool {
}

func gitHash() string {
cmd := exec.Command("git", "rev-parse", "--short=8", "HEAD")
hash, err := cmd.Output()
if err != nil {
readFileHash, readFileErr := os.ReadFile(filepath.Join("..", "..", "RELEASE_COMMIT"))
if readFileErr != nil {
log.Printf("Unable to read RELEASE_COMMIT file, readFileErr: %v", readFileErr)
cmd := exec.Command("git", "rev-parse", "--short=8", "HEAD")
revParseHash, revParseErr := cmd.Output()
if revParseErr != nil {
log.Printf("Failed to execute git rev-parse --short=8 HEAD, revParseErr: %v", revParseErr)
return "UNKNOWN"
}
hashStr := strings.TrimSpace(string(revParseHash))
log.Printf("Successfully retrieved short hash value from git rev-parse --short=8 HEAD, hashStr: %s",
revParseHash)
return hashStr
}
hashStr := strings.TrimSpace(string(readFileHash))
if hashStr == "" || len(hashStr) < 8 {
log.Printf("Invalid hash value obtained from RELEASE_COMMIT file (empty or has length <8), hashStr: %s",
hashStr)
return "UNKNOWN"
}
return strings.TrimSpace(string(hash))
log.Printf("Successfully retrieved full hash value from RELEASE_COMMIT file, hashStr: %s", hashStr)
// return short hash (first 8 characters) instead of full hash
return hashStr[0:8]
}

// 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{

0 comments on commit 1f6256a

Please sign in to comment.