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 support image repo variable #1061

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
4 changes: 2 additions & 2 deletions internal/pkg/plugininstaller/ci/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func (c *CIConfig) getFromLocation(appName string) (git.GitFileContentMap, error
if c.Type == ciGitHubWorkConfigLocation {
gitFilePath = filepath.Join(gitFilePath, filepath.Base(c.LocalPath))
}
content, err := os.ReadFile(c.LocalPath)
content, err := template.New().FromLocalFile(c.LocalPath).SetDefaultRender(appName, c.Vars).Render()
if err != nil {
return nil, err
}
gitFileMap[gitFilePath] = content
gitFileMap[gitFilePath] = []byte(content)
return gitFileMap, nil
}

Expand Down
38 changes: 38 additions & 0 deletions internal/pkg/plugininstaller/jenkins/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package jenkins
import (
"context"
"fmt"
"net/url"
"os"
"path"
"strings"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -172,3 +174,39 @@ func (j *JobOptions) getJobName() string {
}
return j.Pipeline.JobName
}

func (j *JobOptions) getImageHost() string {
harborAddress := j.Pipeline.ImageRepo.URL
harborURL, err := url.ParseRequestURI(harborAddress)
if err != nil {
return harborAddress
}
return harborURL.Host
}

func (j *JobOptions) buildCIConfig() {
jenkinsFilePath := j.Pipeline.JenkinsfilePath
ciConfig := &ci.CIConfig{
Type: "jenkins",
}
// config CIConfig
jenkinsfileURL, err := url.ParseRequestURI(jenkinsFilePath)
// if path is url, download from remote
if err != nil || jenkinsfileURL.Host == "" {
ciConfig.LocalPath = jenkinsFilePath
} else {
ciConfig.RemoteURL = jenkinsFilePath
}
var imageName string
if j.ProjectRepo != nil {
imageName = j.ProjectRepo.Repo
} else {
imageName = j.Pipeline.JobName
}
harborURLHost := path.Join(j.getImageHost(), defaultImageProject)
ciConfig.Vars = map[string]interface{}{
"ImageName": imageName,
"ImageRepoAddress": harborURLHost,
}
j.CIConfig = ciConfig
}
25 changes: 25 additions & 0 deletions internal/pkg/plugininstaller/jenkins/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,29 @@ var _ = Describe("JobOptions struct", func() {
})
})
})

Context("buildCIConfig method", func() {
When("jenkinsfilePath is local path", func() {
BeforeEach(func() {
jobOptions.Pipeline.JenkinsfilePath = "test/local"
})
It("should use localPath", func() {
jobOptions.buildCIConfig()
Expect(jobOptions.CIConfig.LocalPath).Should(Equal(jobOptions.Pipeline.JenkinsfilePath))
Expect(jobOptions.CIConfig.RemoteURL).Should(BeEmpty())
})
})
When("jenkinsfilePath is remote url", func() {
BeforeEach(func() {
jobOptions.Pipeline.JenkinsfilePath = "http://www.test.com/Jenkinsfile"
})
It("should use remote url", func() {
jobOptions.buildCIConfig()
Expect(jobOptions.CIConfig.LocalPath).Should(BeEmpty())
Expect(jobOptions.CIConfig.RemoteURL).Should(Equal(jobOptions.CIConfig.RemoteURL))
Expect(string(jobOptions.CIConfig.Type)).Should(Equal("jenkins"))
})
})
})

})
33 changes: 9 additions & 24 deletions internal/pkg/plugininstaller/jenkins/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"errors"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"time"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/ci"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/common"
"github.com/devstream-io/devstream/pkg/util/jenkins"
"github.com/devstream-io/devstream/pkg/util/k8s"
Expand All @@ -24,6 +22,7 @@ const (
defaultAdminSecretName = "jenkins"
defautlAdminSecretUserName = "jenkins-admin-user"
defautlAdminSecretUserPassword = "jenkins-admin-password"
defaultImageProject = "library"
)

// SetJobDefaultConfig config default fields for usage
Expand Down Expand Up @@ -51,8 +50,7 @@ func SetJobDefaultConfig(options plugininstaller.RawOptions) (plugininstaller.Ra
if opts.Pipeline.JobName == "" {
opts.Pipeline.JobName = projectRepo.Repo
}

opts.CIConfig = buildCIConfig(opts.Pipeline.JenkinsfilePath)
opts.buildCIConfig()

basicAuth, err := buildAdminToken(opts.Jenkins.User)
if err != nil {
Expand Down Expand Up @@ -134,28 +132,14 @@ func generateRandomSecretToken() string {
return fmt.Sprintf("%x", b)[:32]
}

func buildCIConfig(jenkinsFilePath string) *ci.CIConfig {
ciConfig := &ci.CIConfig{
Type: "jenkins",
}
// config CIConfig
url, err := url.ParseRequestURI(jenkinsFilePath)
// if path is url, download from remote
if err != nil || url.Host == "" {
ciConfig.LocalPath = jenkinsFilePath
} else {
ciConfig.RemoteURL = jenkinsFilePath
}
return ciConfig
}

func SetHarborAuth(options plugininstaller.RawOptions) (plugininstaller.RawOptions, error) {
opts, err := newJobOptions(options)
if err != nil {
return nil, err
}

namespace := opts.Jenkins.Namespace
imageRepoUrl := opts.Pipeline.ImageRepo.URL
imageRepoUrl := opts.getImageHost()
imageRepoUser := opts.Pipeline.ImageRepo.User

imageRepoPasswd := os.Getenv("IMAGE_REPO_PASSWORD")
Expand All @@ -172,10 +156,11 @@ func createDockerSecret(namespace, url, username, password string) error {
log.Debugf("Auth string: %s.", authStr)

configJsonStrTpl := `{
"auths": {
"%s": {
"auth": "%s"
}
"auths": {
"%s": {
"auth": "%s"
}
}
}`
configJsonStr := fmt.Sprintf(configJsonStrTpl, url, authStr)
log.Debugf("config.json: %s.", configJsonStr)
Expand Down
27 changes: 0 additions & 27 deletions internal/pkg/plugininstaller/jenkins/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,6 @@ var _ = Describe("SetJobDefaultConfig func", func() {
})
})

var _ = Describe("buildCIConfig func", func() {
var (
path string
)
When("jenkinsfilePath is local path", func() {
BeforeEach(func() {
path = "/test/path"
})
It("should use localPath", func() {
ciConfigData := buildCIConfig(path)
Expect(ciConfigData.LocalPath).Should(Equal(path))
Expect(ciConfigData.RemoteURL).Should(BeEmpty())
})
})
When("jenkinsfilePath is remote url", func() {
BeforeEach(func() {
path = "http://test.com/test/path"
})
It("should use remote url", func() {
ciConfigData := buildCIConfig(path)
Expect(ciConfigData.RemoteURL).Should(Equal(path))
Expect(ciConfigData.LocalPath).Should(BeEmpty())
Expect(string(ciConfigData.Type)).Should(Equal("jenkins"))
})
})
})

var _ = Describe("generateRandomSecretToken func", func() {
It("should return random str", func() {
token := generateRandomSecretToken()
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/scm/gitlab/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Client) PushLocalFileToRepo(commitInfo *git.CommitInfo, checkUpdate boo
}
createCommitOptions := c.CreateCommitInfo(gitlab.FileCreate, commitInfo)
_, _, err := c.Commits.CreateCommit(c.GetRepoPath(), createCommitOptions)
if err != nil && pkgerror.CheckSlientErrorByMessage(err, errFileExist) {
if err != nil && !pkgerror.CheckSlientErrorByMessage(err, errFileExist) {
return true, c.newModuleError(err)
}
return false, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/scm/gitlab/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

var (
errRepoNotFound pkgerror.ErrorMessage = "Project Not Found"
errRepoExist pkgerror.ErrorMessage = "name already exists"
errRepoNotFound pkgerror.ErrorMessage = "Project Not Found"
// errRepoExist pkgerror.ErrorMessage = "name already exists"
errWebHookInvalid pkgerror.ErrorMessage = "invlid url given"
errFileExist pkgerror.ErrorMessage = "A file with this name already exists"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/scm/gitlab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

mapset "github.com/deckarep/golang-set/v2"

"github.com/devstream-io/devstream/pkg/util/pkgerror"
"github.com/devstream-io/devstream/pkg/util/scm/git"
)

func (c *Client) DeleteFiles(commitInfo *git.CommitInfo) error {
deleteCommitoptions := c.CreateCommitInfo(gitlab.FileDelete, commitInfo)
_, _, err := c.Commits.CreateCommit(c.GetRepoPath(), deleteCommitoptions)
if err != nil {
if err != nil && !pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
return c.newModuleError(err)
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/scm/gitlab/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) InitRepo() error {
p.NamespaceID = gitlab.Int(groupId)
}
_, _, err = c.Projects.CreateProject(p)
if err != nil && pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
if err != nil && !pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
return err
}

Expand All @@ -59,7 +59,7 @@ func (c *Client) InitRepo() error {

func (c *Client) DeleteRepo() error {
_, err := c.Projects.DeleteProject(c.GetRepoPath())
if err != nil && pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
if err != nil && !pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
return err
}
return nil
Expand Down Expand Up @@ -100,7 +100,7 @@ func (c *Client) AddWebhook(webhookConfig *git.WebhookConfig) error {

func (c *Client) DeleteWebhook(webhookConfig *git.WebhookConfig) error {
projectHook, err := c.getWebhook(webhookConfig)
if err != nil && pkgerror.CheckSlientErrorByMessage(err, errRepoExist) {
if err != nil && !pkgerror.CheckSlientErrorByMessage(err, errRepoNotFound) {
return err
}
if projectHook == nil {
Expand Down
10 changes: 5 additions & 5 deletions staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version="1.0.0"
repository="devstream/sprintboot-test"
version="1.0.${env.BUILD_NUMBER}"
repository="[[ .ImageRepoAddress ]]/[[ .ImageName ]]"
tag="latest"
image="${repository}:${version}.${env.BUILD_NUMBER}"
image="${repository}:${version}"

podTemplate(containers: [
containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'),
Expand All @@ -27,8 +27,8 @@ podTemplate(containers: [
container('buildkit') {
stage('build a Maven project') {
sh """
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${image},push=true
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${repository}:${tag},push=true
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${image},push=true,registry.insecure=true
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${repository}:${tag},push=true,registry.insecure=true
"""
}
}
Expand Down