Chethan L
Chethan L

Reputation: 55

How to trigger multiple down stream jobs in jenkins dynamically based on some input parameter

Scenario: I want to trigger few down stream jobs(Job A and Job B ....) dynamically based on the input parameter received by the current job.

Upvotes: 3

Views: 684

Answers (2)

anshul Gupta
anshul Gupta

Reputation: 1270

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 upstream system and I splitted them as individaul string which is internally jobs. Making a call by passing each individual strings.

Upvotes: 1

burnettk
burnettk

Reputation: 14047

this Jenkinsfile would do that:

#!/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'
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions