Last active
February 17, 2022 07:46
-
-
Save ivan-pinatti/57582806426cf529af8c4debd82d5c7a to your computer and use it in GitHub Desktop.
Jenkins - Create a Jenkins job via groovy script that; cleans before checkout, shallow clone, sparse checkout only the Jenkinsfile and set lightweight - #jenkins #groovy #jenkins-job #jenkins-git #git #jenkinsfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
// imports | |
import hudson.plugins.git.* | |
import hudson.plugins.git.extensions.* | |
import hudson.plugins.git.extensions.impl.* | |
import jenkins.model.Jenkins | |
// parameters | |
def jobParameters = [ | |
name: 'MyJob', | |
description: 'Build of my STG environment : https://stg.mycompany.com', | |
repository: '[email protected]:my-company/my-repo.git', | |
branch: 'master', | |
credentialId: 'jenkins-key' | |
] | |
// define repo configuration | |
def branchConfig = [new BranchSpec(jobParameters.branch)] | |
def userConfig = [new UserRemoteConfig(jobParameters.repository, null, null, jobParameters.credentialId)] | |
def cleanBeforeCheckOutConfig = new CleanBeforeCheckout() | |
def sparseCheckoutPathConfig = new SparseCheckoutPaths([new SparseCheckoutPath("Jenkinsfile")]) | |
def cloneConfig = new CloneOption(true, true, null, 3) | |
def extensionsConfig = [cleanBeforeCheckOutConfig,sparseCheckoutPathConfig,cloneConfig] | |
def scm = new GitSCM(userConfig, branchConfig, false, [], null, null, extensionsConfig) | |
// define SCM flow | |
def flowDefinition = new org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition(scm, "Jenkinsfile") | |
// set lightweight checkout | |
flowDefinition.setLightweight(true) | |
// get Jenkins instance | |
Jenkins jenkins = Jenkins.getInstance() | |
// create the job | |
def job = new org.jenkinsci.plugins.workflow.job.WorkflowJob(jenkins,jobParameters.name) | |
// define job type | |
job.definition = flowDefinition | |
// set job description | |
job.setDescription(jobParameters.description) | |
// save to disk | |
jenkins.save() | |
jenkins.reload() |
Hi Ivan, can u help me for writing a jenkinsfile for the multibranch pipeline? I have written but I m facing one issue in the multibranch pipeline, there no feature for sparse checkout in Jenkins for multibranch, I have gitlab repository repo structure like for the master branch like
dir test1>folder A>microservice 1> dockerfile & jenkinsfilemicroservice 2>
folder B
folder C
dir test2
dir test3
Hi @psingh2525,
Please check this https://stackoverflow.com/questions/43293334/sparsecheckout-in-jenkinsfile-pipeline, I think is what you are looking for.
Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Ivan, can u help me for writing a jenkinsfile for the multibranch pipeline? I have written but I m facing one issue in the multibranch pipeline, there no feature for sparse checkout in Jenkins for multibranch, I have gitlab repository repo structure like for the master branch like
dir test1>folder A>microservice 1> dockerfile & jenkinsfile
>microservice 2>
>folder B
>folder C
dir test2
dir test3