Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git parameter pipeline script

I'm trying to get a Parametrized Pipeline Job in Jenkins (2.19.4) to work with the following specs:

  • BRANCH_TO_BUILD Git parameter that retrieves all available branches from a gitlab repository for the user to pick one
  • Groovy Pipeline script that runs different stages that works perfectly when used as a Jenkinsfile from SCM.

The error I'm getting is related to Git Parameter:

net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.WorkflowJobWrapper getSCMFromDefinition
SEVERE: Get repo scm from Workflow job fail
java.lang.NoSuchMethodException: org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition.getScm()

As far as I know Jenkins is not capable of retrieving the SCM configuration from pipeline script before asking for parameter's input.

I know there is a new feature request JENKINS-39530 but is there a different approach to accomplish this?

like image 673
xOUe Avatar asked Nov 08 '22 04:11

xOUe


1 Answers

From what I understand you want a job where the user selects a branch. The branches should be kept in sync with the branches in GitLab.

One way to do that is with the Job DSL plugin.

  1. Create a job, perhaps called job-creator, that runs every X minutes, or is triggered from GitLab.
  2. Let job-creator run a DSL build step.
  3. The DSL may ask GitLab with REST to get the branches. Loop through the branches to create the pipeline-job.

The job DSL would look similar to this:

...
 pipelineJob("the pipeline job") {
  parameters {
   def branches = ['[Choose]']
   getJson(server+ "/rest/request/to/gitlab...")
    .values
    .each { branch ->
    if (branch.displayId.startsWith('feature')) {
     branches.push(branch.displayId)
    }
   }
   choiceParam(
    'branch',
    branches,
    'Pick a branch.')
  }
...
like image 196
Tomas Bjerre Avatar answered Nov 15 '22 04:11

Tomas Bjerre