diff --git a/internal/pkg/plugininstaller/ci/option.go b/internal/pkg/plugininstaller/ci/option.go index 8b64ef8ab..e0d3822a8 100644 --- a/internal/pkg/plugininstaller/ci/option.go +++ b/internal/pkg/plugininstaller/ci/option.go @@ -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 } diff --git a/internal/pkg/plugininstaller/jenkins/option.go b/internal/pkg/plugininstaller/jenkins/option.go index 39b955a53..46a8d83e0 100644 --- a/internal/pkg/plugininstaller/jenkins/option.go +++ b/internal/pkg/plugininstaller/jenkins/option.go @@ -3,7 +3,9 @@ package jenkins import ( "context" "fmt" + "net/url" "os" + "path" "strings" "github.com/mitchellh/mapstructure" @@ -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 +} diff --git a/internal/pkg/plugininstaller/jenkins/option_test.go b/internal/pkg/plugininstaller/jenkins/option_test.go index ff831cb33..e9ff87f3a 100644 --- a/internal/pkg/plugininstaller/jenkins/option_test.go +++ b/internal/pkg/plugininstaller/jenkins/option_test.go @@ -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")) + }) + }) + }) + }) diff --git a/internal/pkg/plugininstaller/jenkins/validate.go b/internal/pkg/plugininstaller/jenkins/validate.go index ef04d83b4..d13fb1606 100644 --- a/internal/pkg/plugininstaller/jenkins/validate.go +++ b/internal/pkg/plugininstaller/jenkins/validate.go @@ -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" @@ -24,6 +22,7 @@ const ( defaultAdminSecretName = "jenkins" defautlAdminSecretUserName = "jenkins-admin-user" defautlAdminSecretUserPassword = "jenkins-admin-password" + defaultImageProject = "library" ) // SetJobDefaultConfig config default fields for usage @@ -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 { @@ -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") @@ -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) diff --git a/internal/pkg/plugininstaller/jenkins/validate_test.go b/internal/pkg/plugininstaller/jenkins/validate_test.go index 532feb796..3ac41883b 100644 --- a/internal/pkg/plugininstaller/jenkins/validate_test.go +++ b/internal/pkg/plugininstaller/jenkins/validate_test.go @@ -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() diff --git a/pkg/util/scm/gitlab/commit.go b/pkg/util/scm/gitlab/commit.go index 2b695d2ff..197a724cb 100644 --- a/pkg/util/scm/gitlab/commit.go +++ b/pkg/util/scm/gitlab/commit.go @@ -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 diff --git a/pkg/util/scm/gitlab/errors.go b/pkg/util/scm/gitlab/errors.go index 98b12236d..a754169e3 100644 --- a/pkg/util/scm/gitlab/errors.go +++ b/pkg/util/scm/gitlab/errors.go @@ -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" ) diff --git a/pkg/util/scm/gitlab/file.go b/pkg/util/scm/gitlab/file.go index e1b201285..2f98b76b8 100644 --- a/pkg/util/scm/gitlab/file.go +++ b/pkg/util/scm/gitlab/file.go @@ -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 diff --git a/pkg/util/scm/gitlab/repo.go b/pkg/util/scm/gitlab/repo.go index 0b76ee1fb..9030e677b 100644 --- a/pkg/util/scm/gitlab/repo.go +++ b/pkg/util/scm/gitlab/repo.go @@ -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 } @@ -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 @@ -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 { diff --git a/staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile b/staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile index 03e9a49ed..4976eb98f 100644 --- a/staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile +++ b/staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile @@ -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'), @@ -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 """ } }