Skip to content

Commit

Permalink
Merge pull request #1293 from merico-dev/feat-jenkins-pipeline-suppor…
Browse files Browse the repository at this point in the history
…t-offline

feat: jenkins pipeline support offline
  • Loading branch information
aFlyBird0 authored Dec 2, 2022
2 parents b91239a + 4304009 commit 461af71
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 deletions.
6 changes: 3 additions & 3 deletions internal/pkg/plugin/installer/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ func (p *PipelineConfig) BuildCIFileConfig(ciType server.CIServerType, repoInfo
ConfigLocation: downloader.ResourceLocation(p.ConfigLocation),
}
// update ci render variables by plugins
rawConfigVars := p.generateCIFileVars(repoInfo)
rawConfigVars.Set("AppName", repoInfo.Repo)
rawConfigVars := p.GenerateCIFileVars(repoInfo)
CIFileConfig.Vars = rawConfigVars
log.Debugf("gitlab-ci pipeline get render vars: %+v", CIFileConfig)
return CIFileConfig
}

func (p *PipelineConfig) generateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFileVarsMap {
func (p *PipelineConfig) GenerateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFileVarsMap {
// set default command for language
p.setDefault()
varMap, _ := mapz.DecodeStructToMap(p)
Expand All @@ -67,6 +66,7 @@ func (p *PipelineConfig) generateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFil
if err != nil {
log.Warnf("cifile merge CIFileVarsMap failed: %+v", err)
}
varMap["AppName"] = repoInfo.Repo
return varMap
}

Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/plugin/installer/ci/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("PipelineConfig struct", func() {
})
})
It("should return file Vars", func() {
varMap := a.generateCIFileVars(r)
varMap := a.GenerateCIFileVars(r)
var emptyDingtalk *step.DingtalkStepConfig
var emptySonar *step.SonarQubeStepConfig
var emptyBool *bool
Expand All @@ -90,6 +90,7 @@ var _ = Describe("PipelineConfig struct", func() {
"DingTalkSecretToken": "DINGTALK_SECURITY_TOKEN",
"ImageRepoSecret": "IMAGE_REPO_SECRET",
"ImageRepoDockerSecret": "image-repo-auth",
"AppName": "test_repo",
"StepGlobalVars": "",
"RepoType": "gitlab",
"imageRepo": map[string]interface{}{
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/plugin/jenkinspipeline/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func (j *jobOptions) extractPlugins() []step.StepConfigAPI {
return stepConfigs
}

// check config need offline config
func (j *jobOptions) needOfflineConfig() bool {
// since we use github as default config location
// we use this to check whether this pipeline need default offline Jenkinsfile
const githubContentHost = "raw.githubusercontent.com"
return j.Jenkins.Offline && strings.Contains(string(j.Pipeline.ConfigLocation), githubContentHost)
}

// jenkins jobName, can be like folder/jobName or jobName
type jenkinsJobName string

Expand Down
51 changes: 51 additions & 0 deletions internal/pkg/plugin/jenkinspipeline/tpl/Jenkinsfile_offline.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
podTemplate(containers: [
containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'),
containerTemplate(name: 'buildkit', image: 'moby/buildkit:master', ttyEnabled: true, privileged: true),
], volumes: [
secretVolume(secretName: '[[ .ImageRepoDockerSecret ]]', mountPath: '/root/.docker')
]) {
node(POD_LABEL) {
stage("Get Project") {
checkout scm
}
stage('Run Maven test') {
gitlabCommitStatus("test") {
container('maven') {
stage('run mvn test') {
sh 'mvn -B test'
}
}
}
}
stage("Build Docker image") {
gitlabCommitStatus("build image") {
container('buildkit') {
stage('build a Maven project') {
String opts = ""
String imageRepo = "[[ .imageRepo.user ]]/[[ .AppName ]]"
String imageURL = "[[ .imageRepo.url ]]"
if (imageURL) {
imageRepo = "${imageURL}/${imageRepo}"
}
if (imageRepo.contains("http://")) {
opts = ",registry.insecure=true"
imageRepo = imageRepo.replace("http://", "")
}
String version
if (env.GIT_COMMIT) {
version = env.GIT_COMMIT.substring(0, 8)
} else {
sh "git config --global --add safe.directory '*'"
String gitCommitLang = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
version = gitCommitLang.substring(0, 8)
}
sh """
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:latest,push=true${opts}
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${version},push=true${opts}
"""
}
}
}
}
}
}
19 changes: 18 additions & 1 deletion internal/pkg/plugin/jenkinspipeline/validate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package jenkinspipeline

import (
_ "embed"

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/types"
"github.com/devstream-io/devstream/pkg/util/validator"
)

//go:embed tpl/Jenkinsfile_offline.tpl
var offlineJenkinsScript string

// setJenkinsDefault config default fields for usage
func setJenkinsDefault(options configmanager.RawOptions) (configmanager.RawOptions, error) {
opts, err := newJobOptions(options)
Expand All @@ -19,7 +25,18 @@ func setJenkinsDefault(options configmanager.RawOptions) (configmanager.RawOptio
return nil, err
}
opts.ProjectRepo = projectRepo
opts.CIFileConfig = opts.Pipeline.BuildCIFileConfig(ciType, projectRepo)
// if jenkins is offline, just use offline Jenkinsfile
if opts.needOfflineConfig() {
opts.CIFileConfig = &cifile.CIFileConfig{
Type: ciType,
ConfigContentMap: map[string]string{
"Jenkinsfile": offlineJenkinsScript,
},
Vars: opts.Pipeline.GenerateCIFileVars(projectRepo),
}
} else {
opts.CIFileConfig = opts.Pipeline.BuildCIFileConfig(ciType, projectRepo)
}
// set field value if empty
if opts.Jenkins.Namespace == "" {
opts.Jenkins.Namespace = "jenkins"
Expand Down
20 changes: 13 additions & 7 deletions staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
version="1.0.${env.BUILD_NUMBER}"
repository="[[ .ImageRepoAddress ]]/[[ .ImageName ]]"
tag="latest"
image="${repository}:${version}"

podTemplate(containers: [
containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'),
containerTemplate(name: 'buildkit', image: 'moby/buildkit:master', ttyEnabled: true, privileged: true),
Expand All @@ -26,9 +21,20 @@ podTemplate(containers: [
gitlabCommitStatus("build image") {
container('buildkit') {
stage('build a Maven project') {
String imageRepo = "[[ .imageRepo.user ]]/[[ .AppName ]]"
String imageURL = "[[ .imageRepo.url ]]"
if (imageURL) {
imageRepo = "${imageURL}/${imageRepo}"
}
if (imageRepo.contains("http://")) {
opts = ",registry.insecure=true"
imageRepo = imageRepo.replace("http://", "")
}
gitUtil = new Git()
version = gitUtil.getCommitIDHead()
sh """
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
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${defaultTag},push=true${opts}
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${version},push=true${opts}
"""
}
}
Expand Down

0 comments on commit 461af71

Please sign in to comment.