1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-28 05:47:08 +02:00

fix: use orchestrator specific stage name (#3127)

* extend orchestator to provide stage name

* use orchestrator specific stage name

* fix test case

* remove comment

* fix test case

* prettify

* change something..

* do not exit

* Update pkg/orchestrator/azureDevOps.go

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Christopher Fenner 2021-09-29 08:31:45 +02:00 committed by GitHub
parent 3351250eb7
commit 4525c1daa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -223,10 +224,8 @@ func AccessTokensFromEnvJSON(env string) []string {
return accessTokens
}
const stageNameEnvKey = "STAGE_NAME"
// initStageName initializes GeneralConfig.StageName from either GeneralConfig.ParametersJSON
// or the environment variable 'STAGE_NAME', unless it has been provided as command line option.
// or the environment variable (orchestrator specific), unless it has been provided as command line option.
// Log output needs to be suppressed via outputToLog by the getConfig step.
func initStageName(outputToLog bool) {
var stageNameSource string
@ -243,15 +242,20 @@ func initStageName(outputToLog bool) {
}
// Use stageName from ENV as fall-back, for when extracting it from parametersJSON fails below
GeneralConfig.StageName = os.Getenv(stageNameEnvKey)
stageNameSource = fmt.Sprintf("env variable '%s'", stageNameEnvKey)
provider, err := orchestrator.NewOrchestratorSpecificConfigProvider()
if err != nil {
log.Entry().WithError(err).Warning("Cannot infer stage name from CI environment")
} else {
stageNameSource = "env variable"
GeneralConfig.StageName = provider.GetStageName()
}
if len(GeneralConfig.ParametersJSON) == 0 {
return
}
var params map[string]interface{}
err := json.Unmarshal([]byte(GeneralConfig.ParametersJSON), &params)
err = json.Unmarshal([]byte(GeneralConfig.ParametersJSON), &params)
if err != nil {
if outputToLog {
log.Entry().Infof("Failed to extract 'stageName' from parametersJSON: %v", err)

View File

@ -7,18 +7,27 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/mock"
)
func resetEnv(e []string) {
for _, val := range e {
tmp := strings.Split(val, "=")
os.Setenv(tmp[0], tmp[1])
}
}
func TestAddRootFlags(t *testing.T) {
var testRootCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
addRootFlags(testRootCmd)
@ -49,16 +58,16 @@ func TestAdoptStageNameFromParametersJSON(t *testing.T) {
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
// init
defer resetEnv(os.Environ())
os.Clearenv()
//mock Jenkins env
os.Setenv("JENKINS_HOME", "anything")
require.NotEmpty(t, os.Getenv("JENKINS_HOME"))
os.Setenv("STAGE_NAME", test.stageNameEnv)
GeneralConfig.StageName = test.stageNameArg
resetValue := os.Getenv(stageNameEnvKey)
defer func() { _ = os.Setenv(stageNameEnvKey, resetValue) }()
err := os.Setenv(stageNameEnvKey, test.stageNameEnv)
if err != nil {
t.Fatalf("could not set env var %s", stageNameEnvKey)
}
if test.stageNameJSON != "" {
GeneralConfig.ParametersJSON = fmt.Sprintf("{\"stageName\":\"%s\"}", test.stageNameJSON)
} else {

View File

@ -7,6 +7,10 @@ import (
type AzureDevOpsConfigProvider struct{}
func (a *AzureDevOpsConfigProvider) GetStageName() string {
return os.Getenv("SYSTEM_STAGEDISPLAYNAME")
}
func (a *AzureDevOpsConfigProvider) GetBranch() string {
tmp := os.Getenv("BUILD_SOURCEBRANCH")
return strings.TrimPrefix(tmp, "refs/heads/")

View File

@ -7,6 +7,10 @@ import (
type GitHubActionsConfigProvider struct{}
func (g *GitHubActionsConfigProvider) GetStageName() string {
return "GITHUB_WORKFLOW" //TODO: is there something like is "stage" in GH Actions?
}
func (g *GitHubActionsConfigProvider) GetBranch() string {
return strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/heads/")
}

View File

@ -6,6 +6,10 @@ import (
type JenkinsConfigProvider struct{}
func (a *JenkinsConfigProvider) GetStageName() string {
return os.Getenv("STAGE_NAME")
}
func (j *JenkinsConfigProvider) GetBranch() string {
return os.Getenv("GIT_BRANCH")
}

View File

@ -15,6 +15,7 @@ const (
)
type OrchestratorSpecificConfigProviding interface {
GetStageName() string
GetBranch() string
GetBuildUrl() string
GetCommit() string