githubjenkinsjenkins-pluginsjenkins-groovyjenkins-job-dsl

Get DSL Jobs from another GitHub Repo


I am currently learning Jenkins and I figured that it would be smarter to have the Groovy files separated from the source code.

What I am trying to do is to use "get_jenkins_files.groovy" from Source code repository to get the other groovy files from Jenkins job repository and execute them.

I changed the "get_jenkins_files.grovvy" multiple times to no avail.

As of now, it looks like this :

job( 'Get Jenkins Files' ) {
    scm {
        github( 'Julian52575/Jenkins.git' )
    }

    steps {
        shell( 'git clone git@github.com:Julian52575/Jenkins2.git a' )
        systemGroovyCommand( 'a/dsl_math.groovy' )
        systemGroovyCommand( 'a/ls_math.groovy' )
        systemGroovyCommand( 'a/null.groovy' )
    }
}

None of the cloned jobs seems to be ran because I modified the repo name to one that doesn't exist and called the "null.groovy" file, who doesn't exist either, and I still have a build SUCCESS The console prints this message :

git checkout -f eb4acb8d6f6c3ddb92a08a2bb91725a1e555f261 # timeout=10

Commit message: "test] non existing repo"

git rev-list --no-walk 2aac20ca321b33f83280d9e545af0e8771235c18 # timeout=10

Processing DSL script get_jenkins_files.groovy

Existing items:

GeneratedJob{name='Get Jenkins Files'}

Math freestyle] $ /bin/sh -xe /tmp/jenkins1914244833218209724.sh

ls

Makefile README.md get_jenkins_files.groovy src

Finished: SUCCESS

Does anyone have any idea on how to proceed ?


Solution

  • Shared libraries is exactly match for your goal

    For Dsl jobs you may use multiscm plugin (Deperecated) and i belive you need to use pipelineJob rather than groovyCommand

    In pipeline you can use checkout step

    checkout scmGit(
      branches: [[name: '*/master']],
      extensions: [],
      userRemoteConfigs: [[
       credentialsId: 'my_jenkins_git_creds',
       url: 'https://my_git_repo/service.git']])
    

    You may use it with dir step, or with checkout to subdir behavior (check it in pipeline syntax snippet generator)

    P.S. for learning purposes DSL jobs plugin - is not good. You will have some troubles and misunderstanding with moving to pipelines.