Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read git hash from RELEASE_COMMIT file if possible #3508

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
singholt marked this conversation as resolved.
Show resolved Hide resolved
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