Skip to content

Commit d56a4f2

Browse files
authored
Merge pull request #1288 from merico-dev/fix-jenkins-gitlab-status-error
fix: jenkins support offline and gitlab status
2 parents 18cdaaa + c55dfb5 commit d56a4f2

File tree

11 files changed

+75
-53
lines changed

11 files changed

+75
-53
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/Masterminds/semver v1.5.0
7+
github.com/Masterminds/sprig/v3 v3.2.2
78
github.com/adlio/trello v1.9.0
89
github.com/argoproj/argo-cd/v2 v2.2.2
910
github.com/argoproj/gitops-engine v0.5.2
@@ -55,7 +56,6 @@ require (
5556
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
5657
github.com/Masterminds/goutils v1.1.1 // indirect
5758
github.com/Masterminds/semver/v3 v3.1.1 // indirect
58-
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
5959
github.com/Masterminds/squirrel v1.5.2 // indirect
6060
github.com/Microsoft/go-winio v0.4.17 // indirect
6161
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect

internal/pkg/plugin/gitlabci/generic/options_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ var _ = Describe("action struct", func() {
3434
},
3535
}
3636
repoInfo = &git.RepoInfo{
37-
Repo: repoName,
37+
Repo: repoName,
38+
RepoType: "gitlab",
3839
}
3940
})
4041
Context("buildCIFileConfig method", func() {
@@ -51,6 +52,7 @@ var _ = Describe("action struct", func() {
5152
"AppName": "test_repo",
5253
"ImageRepoSecret": "IMAGE_REPO_SECRET",
5354
"ImageRepoDockerSecret": "image-repo-auth",
55+
"RepoType": "gitlab",
5456
"imageRepo": map[string]interface{}{
5557
"url": "exmaple.com",
5658
"user": "test_user",

internal/pkg/plugin/installer/ci/ci_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var _ = Describe("PipelineConfig struct", func() {
3333
},
3434
}
3535
r = &git.RepoInfo{
36-
Repo: repoName,
36+
Repo: repoName,
37+
RepoType: "gitlab",
3738
}
3839
})
3940
Context("BuildCIFileConfig method", func() {
@@ -60,6 +61,7 @@ var _ = Describe("PipelineConfig struct", func() {
6061
"StepGlobalVars": "",
6162
"configLocation": downloader.ResourceLocation("123/workflows"),
6263
"sonarqube": nilStepConfig,
64+
"RepoType": "gitlab",
6365
"GitlabConnectionID": "gitlabConnection",
6466
"test": map[string]interface{}{
6567
"enable": emptyBool,
@@ -89,6 +91,7 @@ var _ = Describe("PipelineConfig struct", func() {
8991
"ImageRepoSecret": "IMAGE_REPO_SECRET",
9092
"ImageRepoDockerSecret": "image-repo-auth",
9193
"StepGlobalVars": "",
94+
"RepoType": "gitlab",
9295
"imageRepo": map[string]interface{}{
9396
"url": "exmaple.com",
9497
"user": "test_user",

internal/pkg/plugin/installer/ci/step/step.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type StepGlobalVars struct {
2727
CredentialID string `mapstructure:"StepGlobalVars"`
2828
SonarqubeSecretKey string `mapstructure:"SonarqubeSecretKey"`
2929
GitlabConnectionID string `mapstructure:"GitlabConnectionID"`
30+
RepoType string `mapstructure:"RepoType"`
3031
}
3132

3233
// GetStepGlobalVars get global config vars for step
@@ -38,12 +39,16 @@ func GetStepGlobalVars(repoInfo *git.RepoInfo) *StepGlobalVars {
3839
DingTalkSecretToken: dingTalkSecretToken,
3940
SonarqubeSecretKey: sonarSecretKey,
4041
GitlabConnectionID: gitlabConnectionName,
42+
RepoType: repoInfo.RepoType,
4143
}
44+
// config credentialID for jenkins if SSHPrivateKey is configured
4245
switch repoInfo.RepoType {
4346
case "github":
4447
v.CredentialID = githubCredentialName
4548
case "gitlab":
46-
v.CredentialID = gitlabCredentialName
49+
if repoInfo.SSHPrivateKey != "" {
50+
v.CredentialID = gitlabCredentialName
51+
}
4752
}
4853
return v
4954
}

internal/pkg/plugin/jenkinspipeline/jenkins.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type jenkinsOption struct {
2121
User string `mapstructure:"user"`
2222
Namespace string `mapstructure:"namespace"`
2323
EnableRestart bool `mapstructure:"enableRestart"`
24+
Offline bool `mapstructure:"offline"`
2425
}
2526

2627
func (j *jenkinsOption) newClient() (jenkins.JenkinsAPI, error) {
@@ -33,6 +34,7 @@ func (j *jenkinsOption) newClient() (jenkins.JenkinsAPI, error) {
3334
URL: j.URL,
3435
Namespace: j.Namespace,
3536
EnableRestart: j.EnableRestart,
37+
Offline: j.Offline,
3638
}
3739
return jenkins.NewClient(jenkinsConfig)
3840
}

internal/pkg/plugin/jenkinspipeline/option.go

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@ const (
1919
ciType = server.CIServerType("jenkins")
2020
)
2121

22-
type jenkinsJobName string
23-
2422
type jobOptions struct {
2523
Jenkins jenkinsOption `mapstructure:"jenkins"`
2624
ci.CIConfig `mapstructure:",squash"`
2725
JobName jenkinsJobName `mapstructure:"jobName"`
28-
// SCM scm.SCMInfo `mapstructure:"scm"`
29-
// Pipeline pipeline `mapstructure:"pipeline"`
30-
31-
// used in package
32-
// CIFileConfig *cifile.CIFileConfig `mapstructure:"ci"`
33-
// ProjectRepo *git.RepoInfo `mapstructure:"projectRepo"`
3426
}
3527

3628
func newJobOptions(options configmanager.RawOptions) (*jobOptions, error) {
@@ -41,38 +33,18 @@ func newJobOptions(options configmanager.RawOptions) (*jobOptions, error) {
4133
return &opts, nil
4234
}
4335

44-
func (n jenkinsJobName) getJobName() string {
45-
jobNameStr := string(n)
46-
if strings.Contains(jobNameStr, "/") {
47-
return strings.Split(jobNameStr, "/")[1]
48-
}
49-
return jobNameStr
50-
}
51-
52-
func (n jenkinsJobName) getJobFolder() string {
53-
jobNameStr := string(n)
54-
if strings.Contains(jobNameStr, "/") {
55-
return strings.Split(jobNameStr, "/")[0]
36+
func (j *jobOptions) install(jenkinsClient jenkins.JenkinsAPI, secretToken string) error {
37+
// 1. install jenkins plugins
38+
pipelinePlugins := j.extractPlugins()
39+
if err := ensurePluginInstalled(jenkinsClient, pipelinePlugins); err != nil {
40+
return err
5641
}
57-
return ""
58-
}
59-
60-
func (n jenkinsJobName) checkValid() error {
61-
jobNameStr := string(n)
62-
if strings.Contains(jobNameStr, "/") {
63-
strs := strings.Split(jobNameStr, "/")
64-
if len(strs) != 2 || len(strs[0]) == 0 || len(strs[1]) == 0 {
65-
return fmt.Errorf("jenkins jobName illegal: %s", n)
66-
}
42+
// 2. config plugins by casc
43+
if err := configPlugins(jenkinsClient, pipelinePlugins); err != nil {
44+
return err
6745
}
68-
return nil
69-
}
70-
71-
func (j *jobOptions) extractPlugins() []step.StepConfigAPI {
72-
stepConfigs := step.ExtractValidStepConfig(j.Pipeline)
73-
// add repo plugin for repoInfo
74-
stepConfigs = append(stepConfigs, step.GetRepoStepConfig(j.ProjectRepo)...)
75-
return stepConfigs
46+
// 3. create or update jenkins job
47+
return j.createOrUpdateJob(jenkinsClient, secretToken)
7648
}
7749

7850
func (j *jobOptions) remove(jenkinsClient jenkins.JenkinsAPI) error {
@@ -120,16 +92,39 @@ func (j *jobOptions) createOrUpdateJob(jenkinsClient jenkins.JenkinsAPI, secretT
12092
return nil
12193
}
12294

123-
func (j *jobOptions) install(jenkinsClient jenkins.JenkinsAPI, secretToken string) error {
124-
// 1. install jenkins plugins
125-
pipelinePlugins := j.extractPlugins()
126-
if err := ensurePluginInstalled(jenkinsClient, pipelinePlugins); err != nil {
127-
return err
95+
func (j *jobOptions) extractPlugins() []step.StepConfigAPI {
96+
stepConfigs := step.ExtractValidStepConfig(j.Pipeline)
97+
// add repo plugin for repoInfo
98+
stepConfigs = append(stepConfigs, step.GetRepoStepConfig(j.ProjectRepo)...)
99+
return stepConfigs
100+
}
101+
102+
// jenkins jobName, can be like folder/jobName or jobName
103+
type jenkinsJobName string
104+
105+
func (n jenkinsJobName) getJobName() string {
106+
jobNameStr := string(n)
107+
if strings.Contains(jobNameStr, "/") {
108+
return strings.Split(jobNameStr, "/")[1]
128109
}
129-
// 2. config plugins by casc
130-
if err := configPlugins(jenkinsClient, pipelinePlugins); err != nil {
131-
return err
110+
return jobNameStr
111+
}
112+
113+
func (n jenkinsJobName) getJobFolder() string {
114+
jobNameStr := string(n)
115+
if strings.Contains(jobNameStr, "/") {
116+
return strings.Split(jobNameStr, "/")[0]
132117
}
133-
// 3. create or update jenkins job
134-
return j.createOrUpdateJob(jenkinsClient, secretToken)
118+
return ""
119+
}
120+
121+
func (n jenkinsJobName) checkValid() error {
122+
jobNameStr := string(n)
123+
if strings.Contains(jobNameStr, "/") {
124+
strs := strings.Split(jobNameStr, "/")
125+
if len(strs) != 2 || len(strs[0]) == 0 || len(strs[1]) == 0 {
126+
return fmt.Errorf("jenkins jobName illegal: %s", n)
127+
}
128+
}
129+
return nil
135130
}

internal/pkg/plugin/jenkinspipeline/plugins.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
"github.com/devstream-io/devstream/pkg/util/log"
99
)
1010

11+
// ensurePluginInstalled will ensure jenkins plugins are installed
1112
func ensurePluginInstalled(jenkinsClient jenkins.JenkinsAPI, pluginConfigs []step.StepConfigAPI) error {
13+
if jenkinsClient.GetBasicInfo().IsOffline() {
14+
return nil
15+
}
1216
var plugins []*jenkins.JenkinsPlugin
1317
for _, pluginConfig := range pluginConfigs {
1418
plugins = append(plugins, pluginConfig.GetJenkinsPlugins()...)
@@ -17,7 +21,9 @@ func ensurePluginInstalled(jenkinsClient jenkins.JenkinsAPI, pluginConfigs []ste
1721
}
1822

1923
func configPlugins(jenkinsClient jenkins.JenkinsAPI, pluginConfigs []step.StepConfigAPI) error {
20-
globalCascConfig := new(jenkins.RepoCascConfig)
24+
globalCascConfig := &jenkins.RepoCascConfig{
25+
Offline: jenkinsClient.GetBasicInfo().IsOffline(),
26+
}
2127
for _, pluginConfig := range pluginConfigs {
2228
cascConfig, err := pluginConfig.ConfigJenkins(jenkinsClient)
2329
if err != nil {

pkg/util/jenkins/jenkins.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type JenkinsConfigOption struct {
2828
URL string
2929
Namespace string
3030
EnableRestart bool
31+
Offline bool
3132
BasicAuth *BasicAuth
3233
}
3334

@@ -95,3 +96,7 @@ func NewClient(configOption *JenkinsConfigOption) (JenkinsAPI, error) {
9596
func (j *jenkins) GetBasicInfo() *JenkinsConfigOption {
9697
return j.BasicInfo
9798
}
99+
100+
func (o *JenkinsConfigOption) IsOffline() bool {
101+
return o.Offline
102+
}

pkg/util/jenkins/script.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type RepoCascConfig struct {
3131
// common variables
3232
RepoType string
3333
CredentialID string
34+
Offline bool
3435
// gitlab variables
3536
GitLabConnectionName string
3637
GitlabURL string

pkg/util/jenkins/tpl/repo-casc.tpl.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ unclassified:
2828
skipScmCause: false
2929
skipUpstreamCause: false
3030
[[- end ]]
31+
[[ if not .Offline ]]
3132
globalLibraries:
3233
libraries:
3334
- defaultVersion: "main"
@@ -41,3 +42,4 @@ unclassified:
4142
remote: "https://github.com/devstream-io/jenkins-share-library.git"
4243
traits:
4344
- "gitBranchDiscovery"
45+
[[- end ]]

0 commit comments

Comments
 (0)