From 2c8a7d317211823e2314839aa9b5e473b2b669e1 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Tue, 29 Oct 2024 16:47:55 -0700 Subject: [PATCH] feat(cli): adds support for polling git hash from Argo CD (#89) --- lib/project/project/runtime.go | 38 ++++++++++++++++++++++++++-------- lib/tools/argo/util.go | 12 +++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 lib/tools/argo/util.go diff --git a/lib/project/project/runtime.go b/lib/project/project/runtime.go index 75c84a8..871c65b 100644 --- a/lib/project/project/runtime.go +++ b/lib/project/project/runtime.go @@ -3,6 +3,10 @@ package project import ( "fmt" "log/slog" + "os" + + "github.com/input-output-hk/catalyst-forge/lib/tools/argo" + tg "github.com/input-output-hk/catalyst-forge/lib/tools/git" "cuelang.org/go/cue" "github.com/go-git/go-git/v5" @@ -55,15 +59,31 @@ func GetDefaultRuntimes(logger *slog.Logger) []RuntimeData { // getCommitHash returns the commit hash of the HEAD commit. func getCommitHash(repo *git.Repository) (string, error) { - ref, err := repo.Head() - if err != nil { - return "", fmt.Errorf("failed to get HEAD: %w", err) - } + if tg.InCI() { + v, exists := os.LookupEnv("GITHUB_SHA") + if !exists { + return "", fmt.Errorf("GITHUB_SHA not found in environment") + } - obj, err := repo.CommitObject(ref.Hash()) - if err != nil { - return "", fmt.Errorf("failed to get commit object: %w", err) - } + return v, nil + } else if argo.InArgo() { + v, exists := os.LookupEnv("ARGOCD_APP_REVISION") + if !exists { + return "", fmt.Errorf("ARGOCD_APP_REVISION not found in environment") + } - return obj.Hash.String(), nil + return v, nil + } else { + ref, err := repo.Head() + if err != nil { + return "", fmt.Errorf("failed to get HEAD: %w", err) + } + + obj, err := repo.CommitObject(ref.Hash()) + if err != nil { + return "", fmt.Errorf("failed to get commit object: %w", err) + } + + return obj.Hash.String(), nil + } } diff --git a/lib/tools/argo/util.go b/lib/tools/argo/util.go new file mode 100644 index 0000000..b946967 --- /dev/null +++ b/lib/tools/argo/util.go @@ -0,0 +1,12 @@ +package argo + +import "os" + +// InArgo returns true if the code is running in an Argo CD environment. +func InArgo() bool { + if _, ok := os.LookupEnv("ARGOCD_APP_NAME"); ok { + return true + } + + return false +}