SVN checkout with credentials in groovy - jenkins

I want to perform svn checkout like this-
sh """ svn checkout svn://yaabba/dabba/doop --username flint --password stone """
This works fine but I want to add this to it as well workspaceUpdater: [$class: 'UpdateUpdater']
I know we can just use Pipeline syntax to generate a default subversionSCM checkout step, but for a particular reason I have to use the method I have mentioned.
I tried adding it like this-
sh """ svn checkout ([ workspaceUpdater: [$class: 'UpdateUpdater'], svn://yaabba/dabba/doop, additionalCredentials: [--username flint --password stone] ])"""
But this didn't worked.
Anyone know how we can add workspace updater in a regular svn checkout ?

Related

Jenkinsfile syntax: Commit to Subversion SCM repository from workspace

Somehow, I could not find an example of declarative pipeline syntax (Jenkinsfile) for this case.
The idea is very simple. There is some local file in Jenkins pipeline workspace that was checked out from Subversion SCM repository at the beginning of a pipeline stage, it was checked out from under some branch.
I modified this file and at the end of a pipeline stage I need to commit this file back into SCM repository.
Does anybody have Jenkins pipeline DSL syntax example for such commit? This commit should be able to overwrite the file available under the branch in SCM repository.
You don't want to "override" the commit but to add another commit that will update the file, right? Indeed it's a good practice to know where the changes on your repository come from.
For Subversion you should do something like
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:'mycreds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh "svn commit --message 'jenkins update' --non-interactive --no-auth-cache --username $USERNAME --password $PASSWORD"
}
Note: mycreds would be the id of a username/password credential you stored in jenkins
Finally making jenkins do commit is something you want to discuss with your team before implementing it, it can be hard to maintain if not done properly

Using Jenkins global credentials in a pipeline (Jenkinsfile)

I am currently automating a project in Jenkins. I am using a pipeline that reads and executes a Jenkinsfile from a Source Management Tool (GIT in my case). For it to happen, I give the git URL and supply credentials with 'Jenkins Credentials Provider'and execute the build. It reads the Jenkinsfile and checks out the code, but fails at the next stage:
pipeline{
agent any
stages{
...
stage('Cloning GIT Repo'){
steps{
echo 'Cloning the repository'
git url: 'http://test.git'
}
}
...
It gives the error:
No credentials specified
It there a way for me to use the global credentials, I specified in the Jenkins UI earlier?
You can use credentialsId param
git(
url: 'http://test.git',
credentialsId: 'jenkins-credentials',
branch: "${branch}"
)
https://jenkins.io/doc/book/pipeline/jenkinsfile/#optional-step-arguments
https://jenkins.io/doc/pipeline/examples/#push-git-repo

How do I configure a Jenkins multi-branch pipeline to build as a submodule?

I have a project kuma organized with submodules:
kuma
Jenkinsfile (configured to test kuma)
locales
kumascript
Jenkinsfile (configured to test kumascript)
A bunch of other files
I'd like to configure a multi-branch pipeline in Jenkins that watches for branches on the kumascript repo, to:
Check out the master branch of kuma
Update the locales to the commit in the master branch (a regular git submodule update --init)
Update the kumascript submodule to the branch to test
Run the Jenkinsfile in the kumascript branch
Is this possible? Is there a better way to do this?
Here's what worked for me.
First, the Jekinsfile is read from the commit, before checkout, so it is easy to use the one in the kumascript submodule, and much, much harder (impossible?) to read it from a different repo.
In Jenkins 2.68 with Git plugin 3.4.1, I setup a multibranch pipeline. The one source is Git, pointing to the kumascript repository:
"Discover branches" finds branches in the repository and starts builds for them.
"Wipe out repository and force clone" works around an issue where jgit doesn't fetch a submodule repository before checking it out, and thus the target commit isn't available. It causes an error that looks like this in the Jenkins logs:
> git fetch --no-tags --progress https://github.com/mdn/kumascript +refs/heads/*:refs/remotes/origin/*
Checking out Revision 998d9e539127805742634ef1c850221cf04ca2c7 (build-with-locales-1340342)
org.eclipse.jgit.errors.MissingObjectException: Missing unknown 998d9e539127805742634ef1c850221cf04ca2c7
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:158)
at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:227)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:859)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:772)
This issue appears to be reported in https://issues.jenkins-ci.org/browse/JENKINS-45729, and is reported fixed in Git client plugin 2.5.0.
Wiping out the repo appears to force the full fetch, and may be necessary when installing in a parent project.
Jenkins is now configured to create a build for each branch in the repository. To check it out as a submodule, the parent project will need to be manually checked out in the Jenkinsfile. I used Jenkin's "Pipeline Syntax" tool to help construct the command
After some formatting, this goes in my Jenkinsfile:
stage("Prepare") {
// Checkout Kuma project's master branch
checkout([$class: 'GitSCM',
userRemoteConfigs: [[url: 'https://github.com/mozilla/kuma']],
branches: [[name: 'refs/heads/master']],
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: false,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: []
])
// Checkout KumaScript in subfolder
dir('kumascript') {
checkout scm
}
}
This checks out the kuma project and its submodules, and then uses the "vanilla" checkout to checkout the requested branch, but in the submodule directory:
From then on, if I want to run a command in the kuma repo, I run it:
stage('Build') {
sh 'make build-kumascript VERSION=latest'
}
and if I want to run it in the kumascript submodule, I wrap it in dir:
stage('Lint') {
dir('kumascript') {
sh 'make lint VERSION=latest'
sh 'make lint-macros VERSION=latest'
}
}

How to execute a svn checkout in a Jenkins Pipeline in a Groovy file?

I have a script in groovy and I need to execute a svn Checkout. Here's what I have :
"cmd /c \"svn checkout PATHtoSVN PATHtoDIRECTORY\"".execute()
But it doesn't work. Can someone help me ?
Thx.
Emile, why don't you try to use the general checkout step in your pipeline code?
checkout([$class: 'SubversionSCM', locations: [[credentialsId: 'svn-server', local: '.', remote: '<SVN URL>']]])
See https://JENKINS_SERVER_HOSTNAME/job/PIPELINE_NAME/pipeline-syntax/ for full step syntax.
Please mind that the svn Step is easier but does not allow to specify potentially needed credentials...

Perform a git fetch in pipeline 2.0 groovy script

There is an open bug on jenkins 2.0 pipeline scripts relating to included regions in git, so it means for a large mono-repo as in my case each checkin to master will cause multiple pipelines to be kicked off which is not the desired behavior.
So to visualize:
top-level:
->application folder 1
->application folder 2
What I want to do is to do a git fetch first so then I can do a git diff to see if anything in the particular folder has changed and if it has then run the pipeline for that particular folder and not do anything if nothing changed
Code I have is below:
node{
git credentialsId: 'cred id', url: 'ssh://git#git-repo:1234/app/mono-repo.git'
ret = sh(script: 'git fetch; git diff origin/master remotes/origin/master | grep "folder-name"', returnStatus: true)
if(ret == 0){
doSomething()
}else{
doNothing()
}
}
The issue I have that the git fetch fails due a permissions error, I can use a the checkout but then I cannot get the diff before hand which is not what. Is there a way of using the u tiling the git fetch using the credentias?
It might help to simply get the references to tags. Note, I believe this is not equivalent to git fetch --tags. See Does "git fetch --tags" include "git fetch"? for example.
git([branches: [
[name: '*/master'],
[name: 'refs/tags/*:refs/tags/*']],
credentialsId: CREDENTIALS_ID_GIT,
url: REPO])
I noticed that on the Jenkin's console the Git plugin is performing a git fetch --tags, so by default the Git Plugin may already provide this functionality. Please check on this.
I'd like to also add this solution:
withCredentials(
[usernamePassword(
credentialsId: CREDENTIALS_ID_GIT,
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USERNAME')]) {
sh("git fetch --tags https://${GIT_USERNAME}:${GIT_PASSWORD}#${REPO}")
}
Make sure you are using SSH credentials with the correct user. You can check this answer, which is summarized by the capture below :
In particular, make sure that in the url ssh://git#git-repo:1234/app/mono-repo.git the git# part is your actual SSH user. In my case it is the jenkins user, so I would use ssh://jenkins#git-repo:1234/app/mono-repo.git instead.

Resources