Jenkins pipeline works in UI but not from SCM - jenkins-pipeline

I decided to port all my Jenkins job over to Jenkins-pipeline. I did a simple test with the following Jenkinsfiles in to the UI:
pipeline {
agent any
stages {
stage('Clone Repo') {
steps {
git changelog: false, credentialsId: 'xxxxxxxxxx ', url: 'https://github.com/xxxxx/xxx.git'
}
}
}
}
This works fine. I created a repo in github and checked in this Jenkinsfile. I changes Jenkins to Pipeline script from SCM and it is finding the Jenkinsfile but falling over with the error message below. I know I've missed something basic, but reading all the documentations; I couldn't work it out. Any help is appreciated.
Here's the Jenkins job. There's a jenkinsfile the ndh_poc.

In the Script Path field, specify the location (and name) of your Jenkinsfile. This location is the one that Jenkins checks out/clones the repository containing your Jenkinsfile, which should match that of the repository’s file structure.
In your case if your jenkinsfile name is Jenkinsfile and is present within the directory ndh_poc at the root of your repository then the Script Path should be ndh_poc/Jenkinsfile

Related

Jenkins pipeline - use same location and credentials as Jenkinsfile definition

When we define a Jenkinsfile location in the "Pipeline script from SCM" we define the Jenkinsfile location and credetials that should be used to the SCM.
But within the Jenkins file we actually put the same url and credentialsId. Seems like code dublication. Another problem, we dont want users that editing the Jenkinsfile put any credentialsId but use the one that defined in the Jenkins job step under "SCM definitions".
Is it possible to get SCM location and credentialsId from the job?
I am getting branch and git scm location configured in the job like below
def pipelineRepoBranchName = scm.branches[0]
def pipelineRepositoryUrl = scm.userRemoteConfigs[0].url
If you dig deeper you can find the way to access credential id as well.

Build only changes in Jenkins Multibranch pipeline

I am working on Multibranch pipelines in Jenkins.We use SVN as repository and maven for build. I have placed the jenkins file in each branch.
In jenkins file,
After initial checkout, I wrote a function to iterate over the workspace and add the folder names to the list that needs to be build ahead.
In Build stage, I used the list size to iterate using for loop, navigate to the directory and run maven command inside the folder where the POM file is located.
Issue i am trying to solve is, when there is changes in one of the projects under branch, it runs build for all projects due to above configuration. I am looking for some guidance to run build only for changes using pipelines.
Code Snippet -
for ( int i = 0; i < size; i++) {
dir(dirsl[i]) {
try {
withMaven(jdk: 'Java7', maven: 'localmaven') {
bat "mvn clean package"
}
}
catch (e) {
println e
}
}
Also not sure if this is the right way to build using Maven.

what is the shell script command to checkout a project from github

I'm running a Jenkins job on a GitHub project (Project A), as part of this job I want to checkout another different GitHub project (Project B) using shell script command
If you really want to use a shell command for that you could go for
$ git clone https://<YOUR_REPOSITORY_URL>.
However, if you are using a Jenkins pipeline job you might consider using following command in your Jenkinsfile:
stage('Checkout') {
git branch: '<BRANCH_NAME>', credentialsId: '<JENKINS_CREDENTIAL_ID>', url: 'git#<URL_OF_REPOSITORY>'
}

Jenkins Multi-Branch Pipeline -- include another script into local Jenkinsfile

We are just starting out using Jenkins Multi-branch pipelines. I like the idea of Jenkins automatically creating a new Jenkins job when a new branch is created. It will make sure that all releasable development is being built in Jenkins. We have about 40 or 50 projects that get branched for almost every release, and creating those 40 or so jobs every time we branch is error prone work.
However, I see there are two types of pipeline builds in Jenkins:
Regular Pipeline builds: You specify the location and branch in your Jenkins job. However, you can specify whether you want to use the script inside your Jenkins job configuration, or a script from your source repository. This would allow us to maintain a single Jenkinsfile for all of our jobs. If we change something in the build procedure, we only have to edit a single Jenkinsfile.
Multi-Branch Pipeline builds: Jenkins will automatically create a new Jenkins job for you when a new branch is created. This means we no longer have to create dozens of new Jenkins projects when a new branch occurs. However, it looks like the Jenkinsfile must be located on the root of the project. If you make a basic change in your build procedure, you have to update all Jenkins projects.
I'd like to be able to use the Multi-branch Pipeline build, but I want to either specify where to pull up the Jenkinsfile from our repository, or include a master Jenkinsfile from a repository URL.
Is there a way to do this with Jenkins Multi-branch pipelines?
If you have common build logic across repos, you can move most of the pipeline logic to a separate groovy script. This script can then be referenced in any Jenkinsfile.
This could be done either by checking another checkout of the repo that the the groovy script is in to another directory and then doing a standard groovy load or, probably the better approach would be by storing it as a groovy script in the Jenkins Global Script Library - which is essentially a self-contained git repo within Jenkins
(see https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/README.md for more details).
We had a similar requirement, and created a global groovy method in a script that was maintained in Git and deployed to Jenkins' Global script library under /vars/ when it changed:
e.g. the script 'scriptName.groovy' has
def someMethod(){
//some build logic
stage 'Some Stage'
node(){
//do something
}
}
That way the common function could be called in any Jenkinsfile via
scriptName.methodName()

Can't seem to get Multi-branch Pipeline to work with JenkinsFile

I'm sure I'm doing something stupid that most of you are going to laugh at, but I can't seem to get Jenkins to build with multi-branch pipeline. I've simplified the problem down to the very essence. A single branch (master) with an empty node JenkinsFile. I have a second project, a standard Freestyle project that basically runs true to verify it builds.
Here's my Freestyle configuration:
Repository URL: ssh://git#rgit.vegicorp.net:7999/dw/foobar.git
Credentials: None
BUILD
Execute Shell
Command: True
And the build works. (Basically, the repo checks out, true runs, and the build is reported as a success.
Here's my Multibranch build configuration:
BRANCH SOURCES
Project Repository: ssh://git#rgit.vegicorp.net:7999/dw/foobar.git
Credentials: None
BUILD CONFIGURATION
Mode: By JenkinsFile
When I do Branch Indexing, I get a failure and the log says:
Started
Setting origin to ssh://git#cmstash.travelclick.net:7999/dw/foobar.git
Fetching origin...
FATAL: Failed to recompute children of test » Jenkinsfile Test \
java.lang.IllegalStateException: Cannot open session, connection is \
not authenticated.
at com.trilead.ssh2.Connection.openSession(Connection.java:1127)
at org.jenkinsci.plugins.gitclient.trilead.TrileadSession.exec(TrileadSession.java:32)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:262)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:161)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1138)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:130)
at org.jenkinsci.plugins.gitclient.JGitAPIImpl.fetch(JGitAPIImpl.java:678)
at jenkins.plugins.git.AbstractGitSCMSource.retrieve(AbstractGitSCMSource.java:174)
at jenkins.scm.api.SCMSource.fetch(SCMSource.java:146)
at jenkins.branch.MultiBranchProject.computeChildren(MultiBranchProject.java:294)
at com.cloudbees.hudson.plugins.folder.computed.ComputedFolder.updateChildren(ComputedFolder.java:157)
at com.cloudbees.hudson.plugins.folder.computed.FolderComputation.run(FolderComputation.java:122)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
Finished: FAILURE
My JenkinsFile (which sits in the root of the Repo) says:
node {
}
This works when I am not doing a multi-branch pipeline and just put this in as a Jenkins build script. I just want to verify that Jenkins is able to pick up my JenkinsFile.
Found the issue: You must have credentials set even if you don't need to use credentials. The freestyle job has no credentials set and has access to this particular Git repository. In our old Jenkins server, we use ssh:// and setup public/private keys in the $HOME/.ssh directory for our Git projects. None of our Git projects use credentials and don't need them.
However, Multibranch Pipeline does require credentials -- and a non-null JenkinsFile. When I finally set the credential, the job I had to add at least one step in the JenkinsFile or else it wouldn't trigger a single build.)
i think you are also hitting BUG like me ..
{`https://issues.jenkins-ci.org/browse/JENKINS-35567
Jenkins workflow-multibranch with git - IllegalStateException  Tried adding jgit and that didn’t help
`}
Did you got this resolved ?

Resources