Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jenkins Pipeline with Code from Git

Tags:

I want to use following Pipeline script from git in jenkins

#!groovy pipeline {     agent any      stages {         stage('Build') {             steps {                 echo 'Building..'             }         }         stage('Test') {             steps {                 echo 'Testing..'             }         }         stage('Deploy') {             steps {                 echo 'Deploying....'             }         }     } } 

I set the Repository URL right, under "Additional Behaviors" i added "Check out to a sub-directory" and wrote my sub directory there.

At "Script-Path" I wrote: mysubdirectory/Jenkinsfile

When I try to run it I get following ERROR:

java.io.FileNotFoundException     at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)     at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)     at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:162)     at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)     at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:71)     at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:158)     at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)     at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)     at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)     at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)     at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:262)     at hudson.model.ResourceController.execute(ResourceController.java:97)     at hudson.model.Executor.run(Executor.java:415) Finished: FAILURE 

What am I doing wrong?

How can I run a Jenkins Script from git correctly?

like image 833
J.Doe Avatar asked Sep 18 '17 11:09

J.Doe


2 Answers

To run the Jenkinsfile Successfully from Git repo Jenkinsfile should be available in main directory path,but not in the sub directory. For Example:

. ├── .setting ├── project └── Jenkinsfile 

Jenkinsfile should not be in the Sub Directory.

like image 190
Chandra Sekhar Y Avatar answered Oct 09 '22 00:10

Chandra Sekhar Y


Jenkins does 2 checkouts when it looks for a pipeline script. With git, the first one is often a lightweight checkout that only gets the Jenkinsfile, rather than the whole repo, but it is 2 separate checkouts. The second checkout is the real checkout to run the Jenkinsfile.

The reason it does 2 checkouts is because it has to first look at the Jenkinsfile to see what it is you want to do and validate syntax, etc. If you are skipping an SCM checkout in your script so you can do it later or differently, then it needs to know not to do the "real" checkout. For that matter, you could theoretically pull your Jenkinsfile from one repo, skip the SCM checkout, and pull a completely different repo (or branch, or tag) and build against that, but using the Jenkinsfile from the first checkout.

So by telling Jenkins to look in the subdirectory for the Jenkinsfile, you are telling it to look in someplace in the original checkout that actually doesn't exist, because your Jenkinsfile is really in the root of your git repo.

When the second checkout is done into a subdirectory, you will need to take this into account in your Jenkinsfile, because the Jenkinsfile runs from the root of the workspace. You would need to set into the directory i.e. dir("mysubdirectory") {}, to find build files, etc.

like image 43
Rob Hales Avatar answered Oct 09 '22 01:10

Rob Hales