github.com/jfrog/jfrog-cli-core/v2@v2.51.0/general/cisetup/jenkinsfiledslgenerator.go (about) 1 package cisetup 2 3 import ( 4 "fmt" 5 "golang.org/x/text/cases" 6 "golang.org/x/text/language" 7 "strings" 8 9 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 10 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 11 ) 12 13 const ( 14 JenkinsDslFileName = "Jenkinsfile" 15 resolverIdTemplate = "%s_RESOLVER" 16 deployerIdTemplate = "%s_DEPLOYER" 17 homeEnv = "%[1]s_HOME = '/full/path/to/%[1]s' // Set to the local %[1]s installation path." 18 19 jenkinsfileTemplate2 = `pipeline { 20 21 // More info about the Declarative Pipeline Syntax on https://www.jfrog.com/confluence/display/JFROG/Declarative+Pipeline+Syntax 22 // Declarative syntax is available from version 3.0.0 of the Jenkins Artifactory Plugin. 23 24 agent any 25 26 %s 27 28 %s 29 }` 30 31 environmentsTemplate = ` 32 environment { 33 %s 34 }` 35 36 allStagesTemplate = ` 37 stages { 38 %s 39 }` 40 41 stageTemplate = ` 42 stage (%q) { 43 steps {%s 44 } 45 } 46 ` 47 48 cloneStepsTemplate = ` 49 git branch: %q, 50 url: %q 51 // credentialsId: 'git_cred_id'. If cloning the code requires credentials, set the credentials to your git in Jenkins > Configure System > credentials > "username with password" > ID: "git-cred-id"` 52 53 rtConfigServerStepTemplate = ` 54 rtServer ( 55 id: %[1]q, 56 url: %[2]q, 57 credentialsId: 'rt-cred-id', // Set the credentials to your JFrog instance in Jenkins > Configure System > credentials > "username with password" > ID: "rt-cred-id" 58 59 // bypassProxy: true, (If Jenkins is configured to use an http proxy, you can bypass the proxy when using this Artifactory server) 60 // timeout: 300 , (Configure the connection timeout (in seconds). The default value (if not configured) is 300 seconds) 61 ) 62 rt%[3]sDeployer ( 63 id: %[4]q, 64 serverId: %[1]q, 65 %[5]s, 66 67 // threads: 6, (Optional - Attach custom properties to the published artifacts) 68 // properties: ['key1=value1', 'key2=value2'], (Optional - Attach custom properties to the published artifacts) 69 ) 70 rt%[3]sResolver ( 71 id: %[6]q, 72 serverId: %[1]q, 73 %[7]s 74 )` 75 76 mavenRepoTemplate = `releaseRepo: %q, 77 snapshotRepo: %q` 78 79 singleRepoTemplate = `repo: %q` 80 81 mavenRunStepTemplate = ` 82 rtMavenRun ( 83 pom: 'pom.xml', // path to pom.xml file 84 goals: %q, 85 resolverId: %q, 86 deployerId: %q, 87 88 // tool: {build installation name}, (Maven tool installation from jenkins from : Jenkins > Manage jenkins > Global Tool Configuration > Maven installations) 89 // useWrapper: true, (Set to true if you'd like the build to use the Maven Wrapper.) 90 // opts: '-Xms1024m -Xmx4096m', (Optional - Maven options) 91 )` 92 93 gradleRunStepTemplate = ` 94 rtGradleRun ( 95 buildFile: 'build.gradle', 96 tasks: %q, 97 rootDir: "", 98 resolverId: %q, 99 deployerId: %q, 100 101 // tool: {build installation name}, // Jenkins > Gradle jenkins > Global Tool Configuration > Gradle installations 102 )` 103 104 npmInstallStepTemplate = ` 105 rtNpmInstall ( 106 resolverId: %q, 107 108 // tool: {build installation name}, (Npm tool installation from jenkins from : Jenkins > Manage jenkins > Global Tool Configuration > NodeJS installations 109 )` 110 111 npmPublishStepTemplate = ` 112 rtNpmPublish ( 113 deployerId: %q, 114 115 // tool: {build installation name}, (Npm tool installation from jenkins from : Jenkins > Manage jenkins > Global Tool Configuration > NodeJS installations 116 // path: '', (Optional path to the project root. If not set, the root of the workspace is assumed as the root project path.) 117 // javaArgs: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005' , (Jenkins spawns a new java process during this step's execution. You have the option of passing any java args to this new process.) 118 )` 119 120 publishBuildInfoStepsTemplate = ` 121 rtPublishBuildInfo ( 122 serverId: %q, 123 )` 124 ) 125 126 type JenkinsfileDslGenerator struct { 127 SetupData *CiSetupData 128 } 129 130 func (jg *JenkinsfileDslGenerator) GenerateDsl() (jenkinsfileBytes []byte, jenkinsfileName string, err error) { 131 serviceDetails, err := config.GetSpecificConfig(ConfigServerId, false, false) 132 if err != nil { 133 return nil, "", err 134 } 135 // Generate environments sections 136 environments := generateEnvironments(jg.SetupData.BuiltTechnology.Type) 137 // Generate Stages Section 138 cloneStage := generateStage("Clone", fmt.Sprintf(cloneStepsTemplate, jg.SetupData.GitBranch, jg.SetupData.VcsCredentials.Url)) 139 rtConfigStage := generateStage("Artifactory configuration", generateRtConfigSteps(jg.SetupData.BuiltTechnology, serviceDetails.ArtifactoryUrl)) 140 execBuildStage := generateBuildStages(jg.SetupData.GetBuildCmdForNativeStep(), jg.SetupData.BuiltTechnology.Type) 141 publishBuildInfoStage := generateStage("Publish build info", fmt.Sprintf(publishBuildInfoStepsTemplate, ConfigServerId)) 142 // Combine all stages together 143 stagesString := generateAllStages(cloneStage, rtConfigStage, execBuildStage, publishBuildInfoStage) 144 145 return []byte(fmt.Sprintf(jenkinsfileTemplate2, environments, stagesString)), JenkinsDslFileName, nil 146 } 147 148 func generateStage(stageName, steps string) (stageString string) { 149 return fmt.Sprintf(stageTemplate, stageName, steps) 150 } 151 152 func generateAllStages(stages ...string) (allStagesString string) { 153 allStagesString = "" 154 for _, stage := range stages { 155 allStagesString += stage 156 } 157 return fmt.Sprintf(allStagesTemplate, allStagesString) 158 } 159 160 func generateEnvironments(buildType coreutils.Technology) string { 161 envs := "" 162 switch buildType { 163 case coreutils.Maven: 164 fallthrough 165 case coreutils.Gradle: 166 envs += fmt.Sprintf(homeEnv, strings.ToUpper(buildType.String())) 167 default: 168 envs += "" 169 } 170 if envs == "" { 171 return "" 172 } 173 return fmt.Sprintf(environmentsTemplate, envs) 174 } 175 176 func generateRtConfigSteps(techInfo *TechnologyInfo, rtUrl string) string { 177 var deployerRepos string 178 var resolverRepos string 179 switch techInfo.Type { 180 case coreutils.Maven: 181 deployerRepos = fmt.Sprintf(mavenRepoTemplate, techInfo.LocalReleasesRepo, techInfo.LocalSnapshotsRepo) 182 resolverRepos = fmt.Sprintf(mavenRepoTemplate, techInfo.VirtualRepo, techInfo.VirtualRepo) 183 case coreutils.Gradle: 184 fallthrough 185 case coreutils.Npm: 186 deployerRepos = fmt.Sprintf(singleRepoTemplate, techInfo.LocalReleasesRepo) 187 resolverRepos = fmt.Sprintf(singleRepoTemplate, techInfo.VirtualRepo) 188 default: 189 deployerRepos = "//Build type is not supported at the moment" 190 resolverRepos = "//Build type is not supported at the moment" 191 } 192 buildType := string(techInfo.Type) 193 resolverId := fmt.Sprintf(resolverIdTemplate, strings.ToUpper(buildType)) 194 deployerId := fmt.Sprintf(deployerIdTemplate, strings.ToUpper(buildType)) 195 return fmt.Sprintf(rtConfigServerStepTemplate, ConfigServerId, rtUrl, cases.Title(language.Und, cases.NoLower).String(buildType), deployerId, deployerRepos, resolverId, resolverRepos) 196 } 197 198 func generateBuildStages(buildCmd string, buildType coreutils.Technology) (buildStages string) { 199 buildStages = "" 200 resolverId := fmt.Sprintf(resolverIdTemplate, strings.ToUpper(buildType.String())) 201 deployerId := fmt.Sprintf(deployerIdTemplate, strings.ToUpper(buildType.String())) 202 switch buildType { 203 case coreutils.Maven: 204 buildStages += generateStage("Exec Maven", fmt.Sprintf(mavenRunStepTemplate, buildCmd, resolverId, deployerId)) 205 case coreutils.Gradle: 206 buildStages += generateStage("Exec Gradle", fmt.Sprintf(gradleRunStepTemplate, buildCmd, resolverId, deployerId)) 207 case coreutils.Npm: 208 buildStages += generateStage("Exec Npm install", fmt.Sprintf(npmInstallStepTemplate, resolverId)) 209 buildStages += generateStage("Exec Npm publish", fmt.Sprintf(npmPublishStepTemplate, deployerId)) 210 default: 211 buildStages = "//Build type is not supported at the moment" 212 } 213 return buildStages 214 }