Skip to content

Commit

Permalink
feat(cli): adds support for polling git hash from Argo CD (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman authored Oct 29, 2024
1 parent ef39ba0 commit 2c8a7d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
38 changes: 29 additions & 9 deletions lib/project/project/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
12 changes: 12 additions & 0 deletions lib/tools/argo/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2c8a7d3

Please sign in to comment.