Skip to content

Commit 47b4832

Browse files
steinliberdaniel-hutao
authored andcommitted
feat: support github
Signed-off-by: Meng JiaFeng <[email protected]>
1 parent e78442f commit 47b4832

26 files changed

+583
-328
lines changed

docs/plugins/jenkins-github-integ.zh.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,16 @@ tools:
122122
# if all set, devstream will read the password from the config file first.
123123
password: ${{jenkins.default.outputs.jenkinsPasswordOfAdmin}}
124124
# jenkins job name, mandatory
125-
jobName:
125+
jobName:
126126
# path to the pipeline file, relative to the git repo root directory. default: Jenkinsfile-pr
127127
pipelineScriptPath: Jenkinsfile-pr
128128
helm:
129129
# namespace of the jenkins, default: jenkins
130130
namespace: jenkins
131131
# release name of the jenkins helm chart, mandatory
132-
releaseName:
132+
releaseName:
133133
# GitHub repo where to put the pipeline script and project. mandatory
134134
githubRepoUrl: https://github.com/YOUR_GITHUB_ACCOUNT/YOUR_TEST_PROJECT_NAME
135135
adminList:
136136
- YOUR_GITHUB_USERNAME
137137
```
138-

internal/pkg/plugin/jenkinspipeline/create.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
1616
jenkins.ValidateJobConfig,
1717
},
1818
ExecuteOperations: plugininstaller.ExecuteOperations{
19-
jenkins.PreInstall(jenkinsPlugins, gitlabConnectionCascConfig),
19+
jenkins.PreInstall,
2020
jenkins.CreateOrUpdateJob,
21+
jenkins.ConfigRepo,
2122
ci.PushCIFiles,
2223
},
2324
GetStateOperation: jenkins.GetStatus,
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
11
package jenkinspipeline
2-
3-
import (
4-
_ "embed"
5-
6-
"github.com/devstream-io/devstream/pkg/util/jenkins"
7-
)
8-
9-
//go:embed tpl/gitlab-casc.tpl.yaml
10-
var gitlabConnectionCascConfig string
11-
12-
var jenkinsPlugins = []*jenkins.JenkinsPlugin{
13-
// gitlab-plugin is used for jenkins gitlab integration
14-
{
15-
Name: "gitlab-plugin",
16-
Version: "1.5.35",
17-
},
18-
}

internal/pkg/plugininstaller/common/repo.go

+19
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,22 @@ func (d *Repo) updateRepoPathByCloneURL(cloneURL string) error {
154154
d.Repo = strings.TrimSuffix(projectPaths[1], ".git")
155155
return nil
156156
}
157+
158+
// BuildURL return url build from repo struct
159+
func (d *Repo) BuildURL() string {
160+
repoInfo := d.BuildRepoInfo()
161+
switch d.RepoType {
162+
case "github":
163+
return fmt.Sprintf("https://github.com/%s/%s", repoInfo.GetRepoOwner(), d.Repo)
164+
case "gitlab":
165+
var gitlabURL string
166+
if d.BaseURL != "" {
167+
gitlabURL = d.BaseURL
168+
} else {
169+
gitlabURL = gitlab.DefaultGitlabHost
170+
}
171+
return fmt.Sprintf("%s/%s/%s.git", gitlabURL, repoInfo.GetRepoOwner(), d.Repo)
172+
default:
173+
return ""
174+
}
175+
}

internal/pkg/plugininstaller/jenkins/installer.go

+29-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package jenkins
22

33
import (
44
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
5-
"github.com/devstream-io/devstream/pkg/util/jenkins"
65
"github.com/devstream-io/devstream/pkg/util/log"
76
"github.com/devstream-io/devstream/pkg/util/scm"
87
)
@@ -19,12 +18,14 @@ func CreateOrUpdateJob(options plugininstaller.RawOptions) error {
1918
return err
2019
}
2120
// 2. create or update jenkins job
22-
err = opts.createOrUpdateJob(jenkinsClient)
21+
return opts.createOrUpdateJob(jenkinsClient)
22+
}
23+
24+
func ConfigRepo(options plugininstaller.RawOptions) error {
25+
opts, err := newJobOptions(options)
2326
if err != nil {
24-
log.Debugf("jenkins execute script failed: %s", err)
2527
return err
2628
}
27-
// 3. create repo webhook
2829
scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo())
2930
if err != nil {
3031
return err
@@ -37,6 +38,7 @@ func DeleteJob(options plugininstaller.RawOptions) error {
3738
if err != nil {
3839
return err
3940
}
41+
// 1. delete jenkins job
4042
client, err := opts.newJenkinsClient()
4143
if err != nil {
4244
log.Debugf("jenkins init client failed: %s", err)
@@ -46,44 +48,36 @@ func DeleteJob(options plugininstaller.RawOptions) error {
4648
if err != nil {
4749
return err
4850
}
49-
// delete repo webhook
51+
// 2. delete repo webhook
5052
scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo())
5153
if err != nil {
5254
return err
5355
}
5456
return scmClient.DeleteWebhook(opts.buildWebhookInfo())
5557
}
5658

57-
func PreInstall(plugins []*jenkins.JenkinsPlugin, cascTemplate string) plugininstaller.BaseOperation {
58-
return func(options plugininstaller.RawOptions) error {
59-
opts, err := newJobOptions(options)
60-
if err != nil {
61-
return err
62-
}
63-
// 1. init jenkins client
64-
jenkinsClient, err := opts.newJenkinsClient()
65-
if err != nil {
66-
log.Debugf("jenkins init client failed: %s", err)
67-
return err
68-
}
69-
// 2. install plugins
70-
err = opts.installPlugins(jenkinsClient, plugins)
71-
if err != nil {
72-
log.Debugf("jenkins preinstall plugins failed: %s", err)
73-
return err
74-
}
59+
func PreInstall(options plugininstaller.RawOptions) error {
60+
opts, err := newJobOptions(options)
61+
if err != nil {
62+
return err
63+
}
64+
// 1. init jenkins client
65+
jenkinsClient, err := opts.newJenkinsClient()
66+
if err != nil {
67+
log.Debugf("jenkins init client failed: %s", err)
68+
return err
69+
}
70+
71+
// 2. get all plugins need to preConfig
72+
pluginsConfigs := opts.extractJenkinsPlugins()
7573

76-
switch opts.ProjectRepo.RepoType {
77-
case "gitlab":
78-
// 3. create gitlab connection for gitlab
79-
err := opts.createGitlabSSHPrivateKey(jenkinsClient)
80-
if err != nil {
81-
return err
82-
}
83-
return opts.createGitlabConnection(jenkinsClient, cascTemplate)
84-
default:
85-
log.Debugf("jenkins preinstall only support gitlab for now")
86-
return nil
87-
}
74+
// 3. install all plugins
75+
err = installPlugins(jenkinsClient, pluginsConfigs, opts.Jenkins.EnableRestart)
76+
if err != nil {
77+
log.Debugf("jenkins preinstall plugins failed: %s", err)
78+
return err
8879
}
80+
81+
// 4. config repo related config in jenkins
82+
return preConfigPlugins(jenkinsClient, pluginsConfigs)
8983
}

0 commit comments

Comments
 (0)