Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Last active February 17, 2022 07:46
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ivan-pinatti/57582806426cf529af8c4debd82d5c7a to your computer and use it in GitHub Desktop.
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
#!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: 'git@github.com: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()
@psingh2525
Copy link

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

@ivan-pinatti
Copy link
Author

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

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