How to run multiple downstream jobs in Jenkins dynamically based on some input parameter

Scenario: I want to run multiple downstream jobs (Job A and Job B ....) dynamically based on an input parameter received by the current job.

+3


source to share


2 answers


import hudson.model.*

def values = ${configname}.split(',')
def currentBuild = Thread.currentThread().executable

println ${configname}
println ${sourceBranch}

values.eachWithIndex { item, index ->
    println item
    println index

def job = hudson.model.Hudson.instance.getJob(item)
def params = new StringParameterValue('upstream_job', ${sourceBranch})  
def paramsAction = new ParametersAction(params) 
def cause = new hudson.model.Cause.UpstreamCause(currentBuild)
def causeAction = new hudson.model.CauseAction(cause) 
hudson.model.Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)
}

      



How about something like this? I was getting a comma separated list from the bottom up system and I separated them as an individual string which is an internal assignment. Making a call by transmitting each separate line.

+1


source


this Jenkinsfile will do it:



#!/usr/bin/env groovy

pipeline {
  agent { label 'docker' }
  parameters {
    string(name: 'myHotParam', defaultValue: '', description: 'What is your param, sir?')
  }
  stages {
    stage('build') {
      steps {
        script {
          if (params.myHotParam == 'buildEverything') {
            build 'mydir/jobA'
            build 'mydir/jobB'
          }
        }
      }
    }
  }
}

      

+1


source







All Articles