Jenkins: Trigger another job with branch name - jenkins

I am using Jenkins Pipeline via declarative and I would like to trigger another job with branch name.
For instance, I have two different pipeline(PipelineA -PipelineB) with stages JobA and JobB.
One of the stage for JobA should trigger the JobB via paramater using env.GIT_BRANCH. What I mean, if we trigger the JobA via origin/develop, then it should trigger the 'JobB' and run the stages where it has origin/develop condition.
Meanwhile, we also making some separate changes on JobB and it also has its own GIT_BRANCH expression.Thus I could not able to find a way to manage this separately without affecting JobA. To be clarify, when JobA trigger JobB with origin/stage parameter, due to latest changes on JobB is origin/development whereas GIT_BRANCH is origin/development, I can not able to run the stages which has stage condition.
Here is my script.
stage ('Job A') {
steps {
script {
echo "Triggering job for branch ${env.GIT_BRANCH}"
ret = build(job: "selenium_tests",
parameters: [
string(name: "projectName", value: "Project1"),
string(name: "branchName", value: "env.GIT_BRANCH")
],
propagate: true,
wait: true)
echo ret.result
currentBuild.result = ret.result
}
}
}
parameters {
string(defaultValue: "project1", description: 'Which project do you want to test?', name: 'projectName')
string(defaultValue: "origin/development", description: 'Environment for selenium tests', name:'branchName')
}
stage ('Job B') {
when {
beforeAgent true
expression { params.projectName == 'Project1' }
expression { params.branchName == "origin/stage"}
expression{ return env.GIT_BRANCH == "origin/stage"}
}
steps {
script {
//Do something
}
}
}

Pass down one more param for branch when trigger Job B
stage('Trigger Job A') {}
stage('Trigger Job B') {
when {
allOf {
beforeAgent true
expression { params.projectName == 'Project1' }
expression{ return env.GIT_BRANCH == "origin/stage"}
}
}
steps {
build(job: "selenium_tests/Job B",
parameters: [
string(name: "projectName", value: "Project1")
strint(name: "branchName", value: "${env.GIT_BRANCH}")
],
propagate: true,
wait: true)
}
}
In Job B' Jenkinsfile add one stage as the first stage to switch to desired branch
pipeline {
parameters {
string(name: 'branchName', defaultValue: 'develop')
}
stages {
stage('Switch branch') {
steps {
sh "git checkout ${params.branchName}"
}
}
// other stages
}
}

Related

Is it possible to get build number even if build is unstable but not failed?

When building a job in a scripted pipeline, I would like to keep the external build number even if that build is unstable but not failed.
pipeline {
agent any
stages {
stage('Job1') {
steps {
script {
Job1 = build job: 'Job1'
}
}
}
stage('Job2') {
steps {
build job: 'Job2',
parameters: [
string(
name: 'Job1_ID'
value: "${Job1.number}"
)
]
}
}
}
}
I have tried with a catchError() around the job1 build, but still have that problem if the build is unstable.
I have also tried with propagate:false parameter, but I can never see the actual status of the build visually, plus, I don't want the second build to be triggered if the first is failed.
Is there any solution for that ?
What you can do is set propagate: false and then conditionally execute your second Job. Please see the pipeline below.
pipeline {
agent any
stages {
stage('Job1') {
steps {
script {
Job1 = build job: 'Job1', propagate: false
}
}
}
stage('Job2') {
when { expression { return Job1.resultIsBetterOrEqualTo("SUCCESS")}}
steps {
build job: 'Job2',
parameters: [
string(name: 'Job1_ID',value: "${Job1.number}")
]
}
}
}
}

Groovy script for a Jenkins multijob pipeline

I have a multijob pipeline that triggers several other builds to run. It takes in a couple of choice parameters (PRODUCT and BRANCH) to separate the builds into different groups. The UI was easy to set up and works well. Now I need to transfer the same functionality onto a Groovy script instead of using the UI. I have the script below, but it's not working. I'm getting the following error message:
eCaught: groovy.lang.MissingMethodException: No signature of method...
I'm pretty sure my syntax is way off:
pipeline {
parameters {
choice(
choices: ['A', 'B'],
description: 'Select which set of artifacts to trigger',
name: 'PRODUCT')
choice(
choices: ['develop', 'release'],
description: 'Select which branch to build the artifacts from',
name: 'BRANCH')
}
stages {
stage ('Build') {
when {
expression {
env.PRODUCT == 'A' && env.BRANCH == 'release'
}
}
steps {
parallel (
build (job: '../../Builds/artifact1/release'),
build (job: '../../Builds/artifact2/release'),
build (job: '../../Builds/artifact3/release'),
)
}
when {
expression {
env.PRODUCT == 'A' && env.BRANCH == 'develop'
}
}
steps {
parallel (
build (job: '../../Builds/artifact1/develop'),
build (job: '../../Builds/artifact2/develop'),
build (job: '../../Builds/artifact3/develop'),
)
}
when {
expression {
env.PRODUCT == 'B' && env.BRANCH == 'release'
}
}
steps {
parallel (
build (job: '../../Builds/artifact4/release'),
build (job: '../../Builds/artifact5/release'),
build (job: '../../Builds/artifact6/release'),
}
when {
expression {
env.PRODUCT == 'B' && env.BRANCH == 'develop'
}
}
steps {
parallel (
build (job: '../../Builds/artifact4/develop'),
build (job: '../../Builds/artifact5/develop'),
build (job: '../../Builds/artifact6/develop'),
)
}
}
}
}
Inside the steps , run the build in a script segment, it might help
script{
j1BuildResult = build job: "Compile", propagate: true, wait: true, parameters: [
//booleanParam(name: 'portal', value: env.PORTAL),
string(name: 'BRANCH', value: params.BRANCH),
booleanParam(name: 'portal', value: false),
string(name: 'db', value: params.SCHEME_NAME),
]
}

Jenkins - matrix jobs - variables on different slaves overwrite each other?

I think i dont get how matrix builds work. When i set some variable in some stage depending on which node i run, then on rest of the stage sometimes this variable is set as it should and sometimes it gets values from other nodes (axes). In example below its like job which runs on ub18-1 sometimes has VARIABLE1='Linux node' and sometimes is VARIABLE1='Windows node'. Or gitmethod sometimes it is created from LinuxGitInfo and sometimes WindowsGitInfo.
Source i based on
https://jenkins.io/doc/book/pipeline/syntax/#declarative-matrix
Script almost exactly the same as real one
#Library('firstlibrary') _
import mylib.shared.*
pipeline {
parameters {
booleanParam name: 'AUTO', defaultValue: true, description: 'Auto mode sets some parameters for every slave separately'
choice(name: 'SLAVE_NAME', choices:['all', 'ub18-1','win10'],description:'Run on specific platform')
string(name: 'BRANCH',defaultValue: 'master', description: 'Preferably common label for entire group')
booleanParam name: 'SONAR', defaultValue: false, description: 'Scan and gateway'
booleanParam name: 'DEPLOY', defaultValue: false, description: 'Deploy to Artifactory'
}
agent none
stages{
stage('BuildAndTest'){
matrix{
agent {
label "$NODE"
}
when{ anyOf{
expression { params.SLAVE_NAME == 'all'}
expression { params.SLAVE_NAME == env.NODE}
}}
axes{
axis{
name 'NODE'
values 'ub18-1', 'win10'
}
}
stages{
stage('auto mode'){
when{
expression { return params.AUTO }
}
steps{
echo "Setting parameters for each slave"
script{
nodeLabelsList = env.NODE_LABELS.split()
if (nodeLabelsList.contains('ub18-1')){
println("Setting params for ub18-1");
VARIABLE1 = 'Linux node'
}
if (nodeLabelsList.contains('win10')){
println("Setting params for Win10");
VARIABLE1 = 'Windows node'
}
if (isUnix()){
gitmethod = new LinuxGitInfo(this,env)
} else {
gitmethod = new WindowsGitInfo(this, env)
}
}
}
}
stage('GIT') {
steps {
checkout scm
}
}
stage('Info'){
steps{
script{
sh 'printenv'
echo "branch: " + env.BRANCH_NAME
echo "SLAVE_NAME: " + env.NODE_NAME
echo VARIABLE1
gitinfo = new GitInfo(gitmethod)
gitinfo.init()
echo gitinfo.author
echo gitinfo.id
echo gitinfo.msg
echo gitinfo.buildinfo
}
}
}
stage('install'){
steps{
sh 'make install'
}
}
stage('test'){
steps{
sh 'make test'
}
}
}
}
}
}
}
Ok i solved the problem by defining variables maps with node/slave names as keys. Some friend even suggested to define variables in yml/json file in repository and parse them. Maybe i will, but so far this works well
example:
before the pipelines
def DEPLOYmap = [
'ub18-1': false,
'win10': true
]
in stages
when {
equals expected: true, actual: DEPLOYmap[NODE]
}

How to build an pipeline jobs in parallel based on choice parameters values?

In Jenkins, right now i am configuring the pipeline job that can run based on choice parameters values, for each choice values there is an certain jobs need to run in parallel. for example here i need to build Job1 parameter then its only need to build Job1's parallel jobs. but i tried it here its building all the jobs, is there an way to build the jobs based on parameter values?
Choice Parameter
Name: Param
Value: Job1
Job2
import jenkins.model.*
import hudson.model.*
node('') {
String
stage ('Parallel-Job1'){
parallel(Job1: {
stage ('Parallel-test1'){
build job: 'test1', propagate: false
def jobname1 = "test1"
}
}, Job1: {
stage ('Parallel-test2'){
build job: 'test2', propagate: false
def jobname2 = "test2"
}
})
stage ('Parallel-Job2'){
parallel(Job2: {
stage ('Parallel-test3'){
build job: 'test3', propagate: false
def jobname1 = "test3"
}
})
}
}
}
if (param == "Job1") {
stage('Parallel-Job1') {steps ..}
PA: in this case you won't see the skipped pipeline stage on the general view
Or:
stage('conditional stage') {
agent label:'my-node'
when {
expression {
return ${Param} != 'Job1';
}
}
steps {
echo 'foo bar'
}
}

Jenkins Pipeline, downstream job and Agent label

I have a Jenkins Pipeline that executes Job A and Job B. I have 10 agents/nodes on which Job A is executed.
If I specify Agent1, when I Build Pipeline, then Job A should execute on Agent1.
Issue:
Pipeline is running on Agent1 and JobA is getting picked up on any random available agent.
Script:
pipeline {
agent none
stages {
stage('JOB A') {
agent { label "${machine}" }
steps {
build job: 'JOB A', parameters: [a,b,c,d,e,f]
}
}
stage('JOB B') {
agent { label 'xyz' }
steps {
build job: 'JOB B', parameters: [a,b,c,d,e,f,]
}
}
}
}
I'm using different label for every agent.
Can someone help me understand how and where the Pipeline and downstream jobs are running?
Thanks!
As rightly pointed by #yong, I 'specified agent label for stage, not for the JOB A'.
So I declared a label parameter in JOB A and passed it downstream via the Pipeline. It's now correctly executing on the specified Agent.
pipeline {
agent { label 'master' }
stages {
stage('JOB A') {
steps {
build job: 'JOB A', parameters: [a, [$class: 'LabelParameterValue', name: 'Agent', label: "${Agent}" ], b, c, d]
}
}
stage('JOB B') {
steps {
build job: 'JOB B', parameters: [x,y,z]
}
}
}
}

Resources