Skip to content

Commit 4304009

Browse files
committed
feat: jenkins pipeline support offline
Signed-off-by: Meng JiaFeng <[email protected]>
1 parent b7d1842 commit 4304009

File tree

6 files changed

+95
-12
lines changed

6 files changed

+95
-12
lines changed

internal/pkg/plugin/installer/ci/ci.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@ func (p *PipelineConfig) BuildCIFileConfig(ciType server.CIServerType, repoInfo
4949
ConfigLocation: downloader.ResourceLocation(p.ConfigLocation),
5050
}
5151
// update ci render variables by plugins
52-
rawConfigVars := p.generateCIFileVars(repoInfo)
53-
rawConfigVars.Set("AppName", repoInfo.Repo)
52+
rawConfigVars := p.GenerateCIFileVars(repoInfo)
5453
CIFileConfig.Vars = rawConfigVars
5554
log.Debugf("gitlab-ci pipeline get render vars: %+v", CIFileConfig)
5655
return CIFileConfig
5756
}
5857

59-
func (p *PipelineConfig) generateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFileVarsMap {
58+
func (p *PipelineConfig) GenerateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFileVarsMap {
6059
// set default command for language
6160
p.setDefault()
6261
varMap, _ := mapz.DecodeStructToMap(p)
@@ -67,6 +66,7 @@ func (p *PipelineConfig) generateCIFileVars(repoInfo *git.RepoInfo) cifile.CIFil
6766
if err != nil {
6867
log.Warnf("cifile merge CIFileVarsMap failed: %+v", err)
6968
}
69+
varMap["AppName"] = repoInfo.Repo
7070
return varMap
7171
}
7272

internal/pkg/plugin/installer/ci/ci_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var _ = Describe("PipelineConfig struct", func() {
8080
})
8181
})
8282
It("should return file Vars", func() {
83-
varMap := a.generateCIFileVars(r)
83+
varMap := a.GenerateCIFileVars(r)
8484
var emptyDingtalk *step.DingtalkStepConfig
8585
var emptySonar *step.SonarQubeStepConfig
8686
var emptyBool *bool
@@ -90,6 +90,7 @@ var _ = Describe("PipelineConfig struct", func() {
9090
"DingTalkSecretToken": "DINGTALK_SECURITY_TOKEN",
9191
"ImageRepoSecret": "IMAGE_REPO_SECRET",
9292
"ImageRepoDockerSecret": "image-repo-auth",
93+
"AppName": "test_repo",
9394
"StepGlobalVars": "",
9495
"RepoType": "gitlab",
9596
"imageRepo": map[string]interface{}{

internal/pkg/plugin/jenkinspipeline/option.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ func (j *jobOptions) extractPlugins() []step.StepConfigAPI {
9999
return stepConfigs
100100
}
101101

102+
// check config need offline config
103+
func (j *jobOptions) needOfflineConfig() bool {
104+
// since we use github as default config location
105+
// we use this to check whether this pipeline need default offline Jenkinsfile
106+
const githubContentHost = "raw.githubusercontent.com"
107+
return j.Jenkins.Offline && strings.Contains(string(j.Pipeline.ConfigLocation), githubContentHost)
108+
}
109+
102110
// jenkins jobName, can be like folder/jobName or jobName
103111
type jenkinsJobName string
104112

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
podTemplate(containers: [
2+
containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'),
3+
containerTemplate(name: 'buildkit', image: 'moby/buildkit:master', ttyEnabled: true, privileged: true),
4+
], volumes: [
5+
secretVolume(secretName: '[[ .ImageRepoDockerSecret ]]', mountPath: '/root/.docker')
6+
]) {
7+
node(POD_LABEL) {
8+
stage("Get Project") {
9+
checkout scm
10+
}
11+
stage('Run Maven test') {
12+
gitlabCommitStatus("test") {
13+
container('maven') {
14+
stage('run mvn test') {
15+
sh 'mvn -B test'
16+
}
17+
}
18+
}
19+
}
20+
stage("Build Docker image") {
21+
gitlabCommitStatus("build image") {
22+
container('buildkit') {
23+
stage('build a Maven project') {
24+
String opts = ""
25+
String imageRepo = "[[ .imageRepo.user ]]/[[ .AppName ]]"
26+
String imageURL = "[[ .imageRepo.url ]]"
27+
if (imageURL) {
28+
imageRepo = "${imageURL}/${imageRepo}"
29+
}
30+
if (imageRepo.contains("http://")) {
31+
opts = ",registry.insecure=true"
32+
imageRepo = imageRepo.replace("http://", "")
33+
}
34+
String version
35+
if (env.GIT_COMMIT) {
36+
version = env.GIT_COMMIT.substring(0, 8)
37+
} else {
38+
sh "git config --global --add safe.directory '*'"
39+
String gitCommitLang = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
40+
version = gitCommitLang.substring(0, 8)
41+
}
42+
sh """
43+
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:latest,push=true${opts}
44+
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${version},push=true${opts}
45+
"""
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}

internal/pkg/plugin/jenkinspipeline/validate.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package jenkinspipeline
22

33
import (
4+
_ "embed"
5+
46
"github.com/devstream-io/devstream/internal/pkg/configmanager"
7+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/ci/cifile"
58
"github.com/devstream-io/devstream/pkg/util/log"
69
"github.com/devstream-io/devstream/pkg/util/types"
710
"github.com/devstream-io/devstream/pkg/util/validator"
811
)
912

13+
//go:embed tpl/Jenkinsfile_offline.tpl
14+
var offlineJenkinsScript string
15+
1016
// setJenkinsDefault config default fields for usage
1117
func setJenkinsDefault(options configmanager.RawOptions) (configmanager.RawOptions, error) {
1218
opts, err := newJobOptions(options)
@@ -19,7 +25,18 @@ func setJenkinsDefault(options configmanager.RawOptions) (configmanager.RawOptio
1925
return nil, err
2026
}
2127
opts.ProjectRepo = projectRepo
22-
opts.CIFileConfig = opts.Pipeline.BuildCIFileConfig(ciType, projectRepo)
28+
// if jenkins is offline, just use offline Jenkinsfile
29+
if opts.needOfflineConfig() {
30+
opts.CIFileConfig = &cifile.CIFileConfig{
31+
Type: ciType,
32+
ConfigContentMap: map[string]string{
33+
"Jenkinsfile": offlineJenkinsScript,
34+
},
35+
Vars: opts.Pipeline.GenerateCIFileVars(projectRepo),
36+
}
37+
} else {
38+
opts.CIFileConfig = opts.Pipeline.BuildCIFileConfig(ciType, projectRepo)
39+
}
2340
// set field value if empty
2441
if opts.Jenkins.Namespace == "" {
2542
opts.Jenkins.Namespace = "jenkins"

staging/dtm-jenkins-pipeline-example/springboot/Jenkinsfile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
version="1.0.${env.BUILD_NUMBER}"
2-
repository="[[ .ImageRepoAddress ]]/[[ .ImageName ]]"
3-
tag="latest"
4-
image="${repository}:${version}"
5-
61
podTemplate(containers: [
72
containerTemplate(name: 'maven', image: 'maven:3.8.6-openjdk-18', command: 'sleep', args: '99d'),
83
containerTemplate(name: 'buildkit', image: 'moby/buildkit:master', ttyEnabled: true, privileged: true),
@@ -26,9 +21,20 @@ podTemplate(containers: [
2621
gitlabCommitStatus("build image") {
2722
container('buildkit') {
2823
stage('build a Maven project') {
24+
String imageRepo = "[[ .imageRepo.user ]]/[[ .AppName ]]"
25+
String imageURL = "[[ .imageRepo.url ]]"
26+
if (imageURL) {
27+
imageRepo = "${imageURL}/${imageRepo}"
28+
}
29+
if (imageRepo.contains("http://")) {
30+
opts = ",registry.insecure=true"
31+
imageRepo = imageRepo.replace("http://", "")
32+
}
33+
gitUtil = new Git()
34+
version = gitUtil.getCommitIDHead()
2935
sh """
30-
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${image},push=true,registry.insecure=true
31-
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${repository}:${tag},push=true,registry.insecure=true
36+
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${defaultTag},push=true${opts}
37+
buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${version},push=true${opts}
3238
"""
3339
}
3440
}

0 commit comments

Comments
 (0)