1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/orchestrator/orchestrator.go
ffeldmann b224f2294c
Activates debug information for environment variables (#3630)
* Activates debug information for environment variables

* Adds tests for environment variable reading

* Reduces batch size to send messages to Splunk to 5000
2022-03-14 10:17:55 +01:00

110 lines
2.4 KiB
Go

package orchestrator
import (
"errors"
"github.com/SAP/jenkins-library/pkg/log"
"os"
"time"
)
type Orchestrator int
const (
Unknown Orchestrator = iota
AzureDevOps
GitHubActions
Jenkins
)
type OrchestratorSpecificConfigProviding interface {
InitOrchestratorProvider(settings *OrchestratorSettings)
OrchestratorType() string
OrchestratorVersion() string
GetStageName() string
GetBranch() string
GetBuildUrl() string
GetBuildId() string
GetJobUrl() string
GetJobName() string
GetCommit() string
GetPullRequestConfig() PullRequestConfig
GetRepoUrl() string
IsPullRequest() bool
GetLog() ([]byte, error)
GetPipelineStartTime() time.Time
GetBuildStatus() string
}
type PullRequestConfig struct {
Branch string
Base string
Key string
}
type OrchestratorSettings struct {
JenkinsUser string
JenkinsToken string
AzureToken string
}
func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProviding, error) {
switch DetectOrchestrator() {
case AzureDevOps:
return &AzureDevOpsConfigProvider{}, nil
case GitHubActions:
return &GitHubActionsConfigProvider{}, nil
case Jenkins:
return &JenkinsConfigProvider{}, nil
default:
return &UnknownOrchestratorConfigProvider{}, errors.New("unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)")
}
}
func DetectOrchestrator() Orchestrator {
if isAzure() {
return Orchestrator(AzureDevOps)
} else if isGitHubActions() {
return Orchestrator(GitHubActions)
} else if isJenkins() {
return Orchestrator(Jenkins)
} else {
return Orchestrator(Unknown)
}
}
func (o Orchestrator) String() string {
return [...]string{"Unknown", "AzureDevOps", "GitHubActions", "Jenkins"}[o]
}
func areIndicatingEnvVarsSet(envVars []string) bool {
for _, v := range envVars {
if truthy(v) {
return true
}
}
return false
}
// Checks if var is set and neither empty nor false
func truthy(key string) bool {
val, exists := os.LookupEnv(key)
if !exists {
return false
}
if len(val) == 0 || val == "no" || val == "false" || val == "off" || val == "0" {
return false
}
return true
}
// Wrapper function to read env variable and set default value
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
log.Entry().Debugf("For: %s, found: %s", key, value)
return value
}
log.Entry().Debugf("Could not read env variable %v using fallback value %v", key, fallback)
return fallback
}