0

My use case: I want to set up a Jenkins configuration via the Jenkins Helm chart, using the JCasC plugin. I would also like to define a number of jobs via the Pipeline plugin in a series of Jenkinsfiles, so that the entire setup is configured in code, with a clean, complete installation able to be performed just by running helm install.

However, I'm having trouble loading my Pipeline scripts. In my JCasC, I have defined a Job DSL seed script as follows:

job('seedJob') {
  scm {
    git {
      remote {
        url 'ssh://git@foo/bar.git'
        credentials 'creds'
      }
    }
  }
  steps {
    dsl {
      external 'jenkins/jobs/*.groovy'
    }
  }
}

This successfully pulls the scripts from the repo, an example of which is:

pipeline {
   // hello.groovy
   // Do stuff
}

However, the job fails when parsing the Pipeline script with the following error:

ERROR: (hello.groovy, line 1) No signature of method: hello.pipeline() is applicable for argument types: (hello$_run_closure1) values: [hello$_run_closure1@4c6f43b6]
Possible solutions: pipelineJob(java.lang.String), pipelineJob(java.lang.String, groovy.lang.Closure)
Finished: FAILURE

My suspicion is that Pipeline scripts can't be read this way via Job DSL. If this is the case, is there are way I can achieve the loading of multiple Pipeline scripts from a single seed job?

Matt Dunn
  • 5,106
  • 6
  • 31
  • 55

1 Answers1

0

Seed jobs should look like:

pipelineJob('job_name_here') {
  definition {
    cpsScm {
      scm {
        git {
          branches('*/master')
      branches('*/release')
          remote {
            credentials('credentials_id_from_jenkins_here')
            name('name')
            url('git@gitlab_repo_here.git')
          }
        }
      }
    }
  }
  triggers {
    gitlab {
      ciSkip(true)
      triggerOnPush(true)
      triggerOnMergeRequest(false)
      triggerOnClosedMergeRequest(true)
      branchFilterType('RegexBasedFilter')
      targetBranchRegex('(.*master.*|.*release.*)')
      secretToken('paste_secret_token_for_webhook_here')
    }
  }
}
Dmitriy Tarasevich
  • 1,082
  • 5
  • 6
  • Can this load multiple pipeline jobs from the same repository? I added the `scriptPath` property and tried a wildcard (i.e. `scriptPath('jobs/*.groovy')`), but it didn't seem to do the expansion, complaining of a file not found. Setting this to a single filename worked however, which suggests that `pipelineJob` can only load one pipeline script. – Matt Dunn Feb 09 '21 at 11:19
  • Actually I used JCasC plugin 1,5-2 years ago when it didn't have 'job' steps (you will need to add all jobs to jenkins.yaml, not cool I think)). I have following resolution: use JCasC to configure jenkins itself, install needed plugins and configure manually only one freestyle seed job which will be processing dsl script and will be pointed to a repo with *.groovy files, which contains other jobs' configs (take one from my answer as an example). Suppose I explained it clear) – Dmitriy Tarasevich Feb 09 '21 at 13:30