Skip to content

Commit 2c8a7d3

Browse files
authored
feat(cli): adds support for polling git hash from Argo CD (#89)
1 parent ef39ba0 commit 2c8a7d3

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

lib/project/project/runtime.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package project
33
import (
44
"fmt"
55
"log/slog"
6+
"os"
7+
8+
"github.com/input-output-hk/catalyst-forge/lib/tools/argo"
9+
tg "github.com/input-output-hk/catalyst-forge/lib/tools/git"
610

711
"cuelang.org/go/cue"
812
"github.com/go-git/go-git/v5"
@@ -55,15 +59,31 @@ func GetDefaultRuntimes(logger *slog.Logger) []RuntimeData {
5559

5660
// getCommitHash returns the commit hash of the HEAD commit.
5761
func getCommitHash(repo *git.Repository) (string, error) {
58-
ref, err := repo.Head()
59-
if err != nil {
60-
return "", fmt.Errorf("failed to get HEAD: %w", err)
61-
}
62+
if tg.InCI() {
63+
v, exists := os.LookupEnv("GITHUB_SHA")
64+
if !exists {
65+
return "", fmt.Errorf("GITHUB_SHA not found in environment")
66+
}
6267

63-
obj, err := repo.CommitObject(ref.Hash())
64-
if err != nil {
65-
return "", fmt.Errorf("failed to get commit object: %w", err)
66-
}
68+
return v, nil
69+
} else if argo.InArgo() {
70+
v, exists := os.LookupEnv("ARGOCD_APP_REVISION")
71+
if !exists {
72+
return "", fmt.Errorf("ARGOCD_APP_REVISION not found in environment")
73+
}
6774

68-
return obj.Hash.String(), nil
75+
return v, nil
76+
} else {
77+
ref, err := repo.Head()
78+
if err != nil {
79+
return "", fmt.Errorf("failed to get HEAD: %w", err)
80+
}
81+
82+
obj, err := repo.CommitObject(ref.Hash())
83+
if err != nil {
84+
return "", fmt.Errorf("failed to get commit object: %w", err)
85+
}
86+
87+
return obj.Hash.String(), nil
88+
}
6989
}

lib/tools/argo/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package argo
2+
3+
import "os"
4+
5+
// InArgo returns true if the code is running in an Argo CD environment.
6+
func InArgo() bool {
7+
if _, ok := os.LookupEnv("ARGOCD_APP_NAME"); ok {
8+
return true
9+
}
10+
11+
return false
12+
}

0 commit comments

Comments
 (0)