github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/orchestrator/orchestrator.go (about) 1 package orchestrator 2 3 import ( 4 "errors" 5 "os" 6 "time" 7 8 "github.com/SAP/jenkins-library/pkg/log" 9 ) 10 11 type Orchestrator int 12 13 const ( 14 Unknown Orchestrator = iota 15 AzureDevOps 16 GitHubActions 17 Jenkins 18 ) 19 20 const ( 21 BuildStatusSuccess = "SUCCESS" 22 BuildStatusAborted = "ABORTED" 23 BuildStatusFailure = "FAILURE" 24 BuildStatusInProgress = "IN_PROGRESS" 25 26 BuildReasonManual = "Manual" 27 BuildReasonSchedule = "Schedule" 28 BuildReasonPullRequest = "PullRequest" 29 BuildReasonResourceTrigger = "ResourceTrigger" 30 BuildReasonIndividualCI = "IndividualCI" 31 BuildReasonUnknown = "Unknown" 32 ) 33 34 type OrchestratorSpecificConfigProviding interface { 35 InitOrchestratorProvider(settings *OrchestratorSettings) 36 OrchestratorType() string 37 OrchestratorVersion() string 38 GetStageName() string 39 GetBranch() string 40 GetReference() string 41 GetBuildURL() string 42 GetBuildID() string 43 GetJobURL() string 44 GetJobName() string 45 GetCommit() string 46 GetPullRequestConfig() PullRequestConfig 47 GetRepoURL() string 48 IsPullRequest() bool 49 GetLog() ([]byte, error) 50 GetPipelineStartTime() time.Time 51 GetBuildStatus() string 52 GetBuildReason() string 53 GetChangeSet() []ChangeSet 54 } 55 56 type PullRequestConfig struct { 57 Branch string 58 Base string 59 Key string 60 } 61 62 type ChangeSet struct { 63 CommitId string 64 Timestamp string 65 PrNumber int 66 } 67 68 // OrchestratorSettings struct to set orchestrator specific settings e.g. Jenkins credentials 69 type OrchestratorSettings struct { 70 JenkinsUser string 71 JenkinsToken string 72 AzureToken string 73 GitHubToken string 74 } 75 76 func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProviding, error) { 77 switch DetectOrchestrator() { 78 case AzureDevOps: 79 return &AzureDevOpsConfigProvider{}, nil 80 case GitHubActions: 81 ghProvider := &GitHubActionsConfigProvider{} 82 // Temporary workaround: The orchestrator provider is not always initialized after being created, 83 // which causes a panic in some places for GitHub Actions provider, as it needs to initialize 84 // github sdk client. 85 ghProvider.InitOrchestratorProvider(&OrchestratorSettings{}) 86 return ghProvider, nil 87 case Jenkins: 88 return &JenkinsConfigProvider{}, nil 89 default: 90 return &UnknownOrchestratorConfigProvider{}, errors.New("unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)") 91 } 92 } 93 94 // DetectOrchestrator returns the name of the current orchestrator e.g. Jenkins, Azure, Unknown 95 func DetectOrchestrator() Orchestrator { 96 if isAzure() { 97 return AzureDevOps 98 } else if isGitHubActions() { 99 return GitHubActions 100 } else if isJenkins() { 101 return Jenkins 102 } else { 103 return Unknown 104 } 105 } 106 107 func (o Orchestrator) String() string { 108 return [...]string{"Unknown", "AzureDevOps", "GitHubActions", "Jenkins"}[o] 109 } 110 111 func areIndicatingEnvVarsSet(envVars []string) bool { 112 for _, v := range envVars { 113 if truthy(v) { 114 return true 115 } 116 } 117 return false 118 } 119 120 // Checks if var is set and neither empty nor false 121 func truthy(key string) bool { 122 val, exists := os.LookupEnv(key) 123 if !exists { 124 return false 125 } 126 if len(val) == 0 || val == "no" || val == "false" || val == "off" || val == "0" { 127 return false 128 } 129 130 return true 131 } 132 133 // Wrapper function to read env variable and set default value 134 func getEnv(key, fallback string) string { 135 if value, ok := os.LookupEnv(key); ok { 136 log.Entry().Debugf("For: %s, found: %s", key, value) 137 return value 138 } 139 log.Entry().Debugf("Could not read env variable %v using fallback value %v", key, fallback) 140 return fallback 141 }