Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: jenkins-pipeline plugin support github #1130

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/plugins/jenkins-github-integ.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ tools:
# if all set, devstream will read the password from the config file first.
password: ${{jenkins.default.outputs.jenkinsPasswordOfAdmin}}
# jenkins job name, mandatory
jobName:
jobName:
# path to the pipeline file, relative to the git repo root directory. default: Jenkinsfile-pr
pipelineScriptPath: Jenkinsfile-pr
helm:
# namespace of the jenkins, default: jenkins
namespace: jenkins
# release name of the jenkins helm chart, mandatory
releaseName:
releaseName:
# GitHub repo where to put the pipeline script and project. mandatory
githubRepoUrl: https://github.com/YOUR_GITHUB_ACCOUNT/YOUR_TEST_PROJECT_NAME
adminList:
- YOUR_GITHUB_USERNAME
```

3 changes: 2 additions & 1 deletion internal/pkg/plugin/jenkinspipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
jenkins.ValidateJobConfig,
},
ExecuteOperations: plugininstaller.ExecuteOperations{
jenkins.PreInstall(jenkinsPlugins, gitlabConnectionCascConfig),
jenkins.PreInstall,
jenkins.CreateOrUpdateJob,
jenkins.ConfigRepo,
ci.PushCIFiles,
},
GetStateOperation: jenkins.GetStatus,
Expand Down
17 changes: 0 additions & 17 deletions internal/pkg/plugin/jenkinspipeline/jenkinspipeline.go
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
package jenkinspipeline

import (
_ "embed"

"github.com/devstream-io/devstream/pkg/util/jenkins"
)

//go:embed tpl/gitlab-casc.tpl.yaml
var gitlabConnectionCascConfig string

var jenkinsPlugins = []*jenkins.JenkinsPlugin{
// gitlab-plugin is used for jenkins gitlab integration
{
Name: "gitlab-plugin",
Version: "1.5.35",
},
}
19 changes: 19 additions & 0 deletions internal/pkg/plugininstaller/common/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,22 @@ func (d *Repo) updateRepoPathByCloneURL(cloneURL string) error {
d.Repo = strings.TrimSuffix(projectPaths[1], ".git")
return nil
}

// BuildURL return url build from repo struct
func (d *Repo) BuildURL() string {
repoInfo := d.BuildRepoInfo()
switch d.RepoType {
case "github":
return fmt.Sprintf("https://github.com/%s/%s", repoInfo.GetRepoOwner(), d.Repo)
case "gitlab":
var gitlabURL string
if d.BaseURL != "" {
gitlabURL = d.BaseURL
} else {
gitlabURL = gitlab.DefaultGitlabHost
}
return fmt.Sprintf("%s/%s/%s.git", gitlabURL, repoInfo.GetRepoOwner(), d.Repo)
default:
return ""
}
}
64 changes: 29 additions & 35 deletions internal/pkg/plugininstaller/jenkins/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jenkins

import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/pkg/util/jenkins"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/scm"
)
Expand All @@ -19,12 +18,14 @@ func CreateOrUpdateJob(options plugininstaller.RawOptions) error {
return err
}
// 2. create or update jenkins job
err = opts.createOrUpdateJob(jenkinsClient)
return opts.createOrUpdateJob(jenkinsClient)
}

func ConfigRepo(options plugininstaller.RawOptions) error {
opts, err := newJobOptions(options)
if err != nil {
log.Debugf("jenkins execute script failed: %s", err)
return err
}
// 3. create repo webhook
scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo())
if err != nil {
return err
Expand All @@ -37,6 +38,7 @@ func DeleteJob(options plugininstaller.RawOptions) error {
if err != nil {
return err
}
// 1. delete jenkins job
client, err := opts.newJenkinsClient()
if err != nil {
log.Debugf("jenkins init client failed: %s", err)
Expand All @@ -46,44 +48,36 @@ func DeleteJob(options plugininstaller.RawOptions) error {
if err != nil {
return err
}
// delete repo webhook
// 2. delete repo webhook
scmClient, err := scm.NewClient(opts.ProjectRepo.BuildRepoInfo())
if err != nil {
return err
}
return scmClient.DeleteWebhook(opts.buildWebhookInfo())
}

func PreInstall(plugins []*jenkins.JenkinsPlugin, cascTemplate string) plugininstaller.BaseOperation {
return func(options plugininstaller.RawOptions) error {
opts, err := newJobOptions(options)
if err != nil {
return err
}
// 1. init jenkins client
jenkinsClient, err := opts.newJenkinsClient()
if err != nil {
log.Debugf("jenkins init client failed: %s", err)
return err
}
// 2. install plugins
err = opts.installPlugins(jenkinsClient, plugins)
if err != nil {
log.Debugf("jenkins preinstall plugins failed: %s", err)
return err
}
func PreInstall(options plugininstaller.RawOptions) error {
opts, err := newJobOptions(options)
if err != nil {
return err
}
// 1. init jenkins client
jenkinsClient, err := opts.newJenkinsClient()
if err != nil {
log.Debugf("jenkins init client failed: %s", err)
return err
}

// 2. get all plugins need to preConfig
pluginsConfigs := opts.extractJenkinsPlugins()

switch opts.ProjectRepo.RepoType {
case "gitlab":
// 3. create gitlab connection for gitlab
err := opts.createGitlabSSHPrivateKey(jenkinsClient)
if err != nil {
return err
}
return opts.createGitlabConnection(jenkinsClient, cascTemplate)
default:
log.Debugf("jenkins preinstall only support gitlab for now")
return nil
}
// 3. install all plugins
err = installPlugins(jenkinsClient, pluginsConfigs, opts.Jenkins.EnableRestart)
if err != nil {
log.Debugf("jenkins preinstall plugins failed: %s", err)
return err
}

// 4. config repo related config in jenkins
return preConfigPlugins(jenkinsClient, pluginsConfigs)
}
Loading