22

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?

J.Doe
  • 586
  • 2
  • 8
  • 30

6 Answers6

27

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.

5

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.

Rob Hales
  • 5,123
  • 1
  • 21
  • 33
3

I got the same error. By disabling Lightweight checkout in the job configuration the error was solved!

Coco
  • 826
  • 1
  • 12
  • 20
1

Try to omit your subdirectory from the Sript-path.

When you specify a subdirectory to clone your project, Jenkins searches the pipeline file in that directory. In your case, Jenkins is looking for the Jenkinsfile in "mysubdirectory/mysubdirectory/Jenkinsfile"

  • make sure our Jenkins file name is just "Jenkinsfile" without any extensions and the file is in the top level location of your repository. – Carlos Lucas Sep 18 '17 at 11:51
  • It is named Jenkinsfile without extensions and is the only file in this repository – J.Doe Sep 18 '17 at 11:55
0

I had a similar problem to this, but in my case the naming mismatch was the branch in Git. In the pipeline settings, I was specifying the branch name in uppercase when in the repo, the branch name was lowercase. (Obviously not the solution to the original problem, but may prove useful to some other poor soul!)

Mike P
  • 742
  • 11
  • 26
0

I had a similar problem when I copied pipeline job.

solved: remove pipeline scm part from copied pipeline job. save changes. build one empty round. then create pipeline scm block again.

Kimi
  • 1