Skip to content
Snippets Groups Projects
Commit 3b936e20 authored by Shreyas Prabhu's avatar Shreyas Prabhu
Browse files

feat: add .env support and improve credential handling with better error messages

parent 829faa4c
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
"location": "asia-south1",
"default_conn_type": "GITLAB",
"auth": {
"credentials_file": "/home/prajwal/Projects/cloud build deployment/cloud-run-go/tech-rnd-project-2e698053b09e.json"
"credentials_file": "/home/niveus/Documents/projects/cloud-build-go/tech-rnd-project-2e698053b09e.json"
},
"connections": [
{
......@@ -15,6 +15,7 @@
},
"gitlab_config": {
"host_uri": "https://gitlab.niveussolutions.com",
"webhook_secret_version": "projects/460440866465/secrets/poc-webhooksecreat/versions/1",
"read_token_version": "projects/460440866465/secrets/prajwalgitlabreadapi/versions/1",
"auth_token_version": "projects/460440866465/secrets/prajwalgitlabapi/versions/1"
}
......
......@@ -13,6 +13,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
......
......@@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/joho/godotenv"
"github.com/niveus/cloud-build-go/cloudbuild"
"github.com/niveus/cloud-build-go/config"
cloudbuildv1 "google.golang.org/api/cloudbuild/v1"
......@@ -42,6 +43,14 @@ func formatTimestamp(id string) string {
}
func main() {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
log.Printf("Warning: Error loading .env file: %v", err)
log.Println("Using environment variables from system instead")
} else {
log.Println("Successfully loaded environment variables from .env file")
}
// Define command-line flags
configPath := flag.String("config", "config/config.json", "Path to config file")
action := flag.String("action", "list-connections", "Action to perform: list-connections, create-gitlab, delete-connection, list-linkable-repos, inspect, list-repositories, list-repositories-for-triggers, create-trigger, list-triggers")
......@@ -107,6 +116,7 @@ func main() {
if serviceAccountKeyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); serviceAccountKeyFile != "" {
client, err = cloudbuild.NewClientWithCredentialsFile(ctx, serviceAccountKeyFile)
} else {
fmt.Printf("GOOGLE_APPLICATION_CREDENTIALS environment variable is not set. Falling back to default authentication methods.\n")
// Fall back to default authentication methods
client, err = cloudbuild.NewClient(ctx)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment