Enabling LFS in JobDSL

Ryan Graham
2 min readMar 29, 2023

--

I spent a lot of time piecing this together. So let me save you the time.

LFS for Github Org — multibranchPipelineJob

organizationFolder('MyOrg') {
displayName('MyOrg')

String includedBranches = [
"main",
"master",
"PR-*"
].join(" ")

properties {
noTriggerOrganizationFolderProperty {
branches('main|master|PR-\\d+')
}
}

organizations {
github {
repoOwner("MyOrg")
apiUri("https://api.github.com")
credentialsId('my-api-token')
buildOriginPRHead(true)
traits {
gitHubBranchDiscovery {
// 3 => Check all branches
strategyId(3)
}
gitHubPullRequestDiscovery {
// 2 => Discover head of PR without merging target
strategyId(2)
}
headWildcardFilter {
includes(includedBranches)
excludes("")
}
gitLFSPullTrait()
}
}
}
projectFactories {
workflowMultiBranchProjectFactory {
scriptPath("Jenkinsfile-MyOrg")
}
}
}

Pretty simple, right? Just add gitLFSPullTrait() to traits and you’ll stop seeing this error: smudge filter lfs failed

LFS for pipelineJob

Enabling LFS pull after checkout for a basic pipelineJob is slightly more difficult. There is no direct DSL shorthand. So I need to use configure{} to add it to config.xml for the job.

To figure out what exactly to add I had to enable LFS in the UI for an existing job and then look at config.xml. You can view config.xml in the browser like this: https://my-jenkins-url/job/My%20Folder/job/my-job/config.xml

<extensions>
<hudson.plugins.git.extensions.impl.CleanCheckout>
<deleteUntrackedNestedRepositories>false</deleteUntrackedNestedRepositories>
</hudson.plugins.git.extensions.impl.CleanCheckout>
<hudson.plugins.git.extensions.impl.GitLFSPull/>
</extensions>

I found this under the GitSCM node after enabling LFS. So I knew I needed to add hudson.plugins.git.extensions.impl.GitLFSPull in my JobDSL.

configure { node ->
node / 'extensions' << 'hudson.plugins.git.extensions.impl.GitLFSPull' {}
}

Since configure is under git, node represents GitSCM, and I just need to add to extensions.

pipelineJob('My Folder/my-job') {
definition {
cpsScm {
scm {
git {
remote {
github('MyOrg/MyRepo', 'https')
credentials('my-api-token')
}
branch('main')
extensions {
cleanAfterCheckout()
}
configure { node ->
node / 'extensions' << 'hudson.plugins.git.extensions.impl.GitLFSPull' {}
}
}
}
scriptPath('Jenkinsfile-my-pipeline')
lightweight(true)
}
}
}

This is what the complete job looks like with LFS enabled.

--

--

No responses yet