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 1 commit
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
9 changes: 9 additions & 0 deletions internal/pkg/plugininstaller/jenkins/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jenkins
import (
"context"
"fmt"
"net/url"
"os"
"path"

Expand Down Expand Up @@ -149,3 +150,11 @@ func (j *JobOptions) deleteJob(client jenkins.JenkinsAPI) error {
func (j *JobOptions) getJobPath() string {
return path.Join(j.JobFolderName, j.JobName)
}

func (j *JobOptions) getHarborHost() string {
harborURL, err := url.ParseRequestURI(j.HarborURL)
if err != nil {
return j.HarborURL
}
return harborURL.Host
}
27 changes: 18 additions & 9 deletions internal/pkg/plugininstaller/jenkins/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
defaultAdminSecretName = "jenkins"
defautlAdminSecretUserName = "jenkins-admin-user"
defautlAdminSecretUserPassword = "jenkins-admin-password"
defaultHarborProject = "library"
)

// SetJobDefaultConfig config default fields for usage
Expand All @@ -40,7 +41,7 @@ func SetJobDefaultConfig(options plugininstaller.RawOptions) (plugininstaller.Ra
if opts.JobName == "" {
opts.JobName = projectRepo.Repo
}
opts.CIConfig = buildCIConfig(opts.JenkinsfilePath)
opts.CIConfig = buildCIConfig(opts)
basicAuth, err := buildAdminToken(opts.JenkinsUser)
if err != nil {
return nil, err
Expand Down Expand Up @@ -111,18 +112,25 @@ func generateRandomSecretToken() string {
return fmt.Sprintf("%x", b)[:32]
}

func buildCIConfig(jenkinsFilePath string) *ci.CIConfig {
func buildCIConfig(opts *JobOptions) *ci.CIConfig {
jenkinsFilePath := opts.JenkinsfilePath
ciConfig := &ci.CIConfig{
Type: "jenkins",
}
// config CIConfig
url, err := url.ParseRequestURI(jenkinsFilePath)
jenkinsfileURL, err := url.ParseRequestURI(jenkinsFilePath)
// if path is url, download from remote
if err != nil || url.Host == "" {
if err != nil || jenkinsfileURL.Host == "" {
ciConfig.LocalPath = jenkinsFilePath
} else {
ciConfig.RemoteURL = jenkinsFilePath
}
harborURLHost := opts.getHarborHost() + "/"
steinliber marked this conversation as resolved.
Show resolved Hide resolved
ciConfig.Vars = map[string]interface{}{
"Project": defaultHarborProject,
"ImageName": opts.ProjectRepo.Repo,
"ImageRepoAddress": harborURLHost,
}
return ciConfig
}

Expand All @@ -132,7 +140,7 @@ func SetHarborAuth(options plugininstaller.RawOptions) (plugininstaller.RawOptio
return nil, err
}
namespace := opts.JenkinsNamespace
harborURL := opts.HarborURL
harborURL := opts.getHarborHost()
harborUser := opts.HarborUser

harborPasswd := os.Getenv("HARBOR_PASSWORD")
Expand All @@ -149,10 +157,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
18 changes: 11 additions & 7 deletions internal/pkg/plugininstaller/jenkins/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,29 @@ var _ = Describe("SetJobDefaultConfig func", func() {

var _ = Describe("buildCIConfig func", func() {
var (
path string
options *JobOptions
)
When("jenkinsfilePath is local path", func() {
BeforeEach(func() {
path = "/test/path"
options = &JobOptions{
JenkinsfilePath: "/test/path",
}
})
It("should use localPath", func() {
ciConfigData := buildCIConfig(path)
Expect(ciConfigData.LocalPath).Should(Equal(path))
ciConfigData := buildCIConfig(options)
Expect(ciConfigData.LocalPath).Should(Equal(options.JenkinsfilePath))
Expect(ciConfigData.RemoteURL).Should(BeEmpty())
})
})
When("jenkinsfilePath is remote url", func() {
BeforeEach(func() {
path = "http://test.com/test/path"
options = &JobOptions{
JenkinsfilePath: "/test/path",
}
})
It("should use remote url", func() {
ciConfigData := buildCIConfig(path)
Expect(ciConfigData.RemoteURL).Should(Equal(path))
ciConfigData := buildCIConfig(options)
Expect(ciConfigData.RemoteURL).Should(Equal(options.JenkinsfilePath))
Expect(ciConfigData.LocalPath).Should(BeEmpty())
Expect(string(ciConfigData.Type)).Should(Equal("jenkins"))
})
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 ]][[ .Project ]]/[[ .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