if condition not working in jenkins pipeline job? - jenkins-pipeline

Here I am using an choice parameters plugin, i need to build the jobs as per the parameter values in choices
for Example:
CHOICE PARAMETER
NAME: Project
VALUE: Job1
Job2
Here Each parameter value has 6 jobs builds need to run in parallel.
import jenkins.model.*
import hudson.model.*
node('') {
if (Project == 'Job1'){
stages ('Parallel-A'){
parallel(firstTask: {
stage ('Parallel-test1'){
build job: 'test1'
}
}, secondTask: {
stage ('Parallel-test2'){
build job: 'test2'
}
})
}
}
if (Project =='Job2'){
stages ('Parallel-B'){
parallel(firstTask: {
stage ('Parallel-test3'){
build job: 'test3'
}
}, secondTask: {
stage ('Parallel-test4'){
build job: 'test4'
}
})
}
}
}
but its not working here, Thanks in advance.

Try $Project
As it was set as parameter, "$" shall be added before the variable.

Related

Running Jenkins job with different maven opts in parallel

community.
I would like to run Jenkins job, same job but with diferent maven opts values and in parallel. How can i achieve that? I was trying to use different Jenkins plugins, but with no luck.
Trying to configure pipelines using groovy scripts, but i am so amateur that i can't figure out how to achieve what i want. The goal is to run same jenkins job in parallel, but the only thing that must be different is environment where my tests should run.
Maybe there is already a solution so you could point me to that.
You should be able to use parallel blocks for this. Following is a sample.
pipeline {
agent none
stages {
stage('Run Tests') {
parallel {
stage('Test On Dev') {
agent {
label "IfYouwantToChangeAgent"
}
steps {
sh "mvn clean test -Dsomething=dev"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
stage('Test On QA') {
agent {
label "QA"
}
steps {
sh "mvn clean test -Dsomething=qa"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
}
}
}
}

Can we use triggers directive for multiple stage

Hi I want to know whether multiple stages can have a triggers directive, if no then I need some way to schedule each stage
pipeline{
stages{
stage{
triggers{cron (#some_exp)}
steps{
# Some steps
}
}
stage{
triggers{cron (#some_exp)}
steps{
# Some steps
}
}
}
}
If you want to trigger each stage separately that means that each stage is probably a separate business logic unit and therefore you should seriously consider creating a separate job for each step in order to avoid packing them all into a single job with multiple triggers and packed logic.
However if you still want to achieve what you want you can do it with the Parameterized Scheduler Plugin which enables you to define cron triggers that trigger the job with a specific environment variable, you can then use this variable as a condition to determine which step to execute.
Here is an example for implementing it:
pipeline {
agent any
parameters {
string(name: 'STAGE', defaultValue: 'setup', description: 'Which stage to run')
}
triggers {
parameterizedCron('''
*/2 * * * * %STAGE=setup
*/3 * * * * %STAGE=build
''')
}
stages {
stage('Setup') {
when {
expression { STAGE== 'setup' }
}
steps {
echo "In Setup stage - STAGE parameter is ${STAGE}"
...
}
}
stage('Build') {
when {
expression { STAGE== 'build' }
}
steps {
echo "In Build stage - STAGE parameter is ${STAGE}"
...
}
}
...
}
}

Jenkinsfile - a way to skip the whole pipeline?

I use the declarative syntax for developing the (multibranch) pipeline script and I'm looking for a way to skip the whole pipeline based on some condition, without having to modify the when on every single stage.
Current use case: I'm setting up a cron to trigger builds at night, but I only want let's say the branches release/v1 and develop to go through the pipeline at night, not the dozen of other branches.
triggers {
cron('H 21 * * 1-5')
}
// SKIP PIPELINE if triggered by timer AND branch not 'release/v1' OR 'develop'
stages {
stage('build') {
when { ... }
}
stage('UT') {
when { ... }
}
etc...
}
any hints would be appreciated.
You can nest stages, if you have the pipeline-definition-plugin 1.3 or later (changelog).
Using that, you can nest your whole job in a parent stage, and use a when directive on the parent stage. All child stages will be skipped if the parent stage is skipped. Here is an example:
pipeline {
agent any
stages {
stage('Parent') {
when {
//...
}
stages {
stage('build') {
steps {
//..
}
}
stage('UT') {
steps {
//...
}
}
}
}
}
}
You can use the SCM Skip plugin. Then use it in the pipeline like so:
scmSkip(deleteBuild: true, skipPattern:'.*#skip-ci.*')
This also enables to delete the build and also use a regex expression easily. The problem is, it aborts the next builds, and then ignores them for the relevant PR if you're connected with GitHub organization.

Locking resource in Jekinsfile pipeline for parallel and sequential stages at one time

I'm trying to run the following process in my Jenkinsfile:
Build the app
Trigger deploy of two components to the test environment, in parallel
foo deploy
bar deploy
Run tests on a deployed app
The steps 2 and 3 require locking a resource because I have only one test environment available.
There's no problem to achieve that w/o running step 2 in parallel, however, when I configure Jenkinsfile to run them together I get the following error from Jenkins:
WorkflowScript: 19: Parallel stages or branches can only be included in a top-level stage. # line 19, column 7.
stage('Deploy Foo') {
^
Here's the full Jenkinsfile:
pipeline {
agent any
stages {
stage('Build') {
steps {
powershell(script: '.\\ci\\build.ps1 -Script .\\ci\\build\\build.cake')
}
}
stage('Deploy and run tests') {
when {
branch('develop')
}
options {
lock('test-env')
}
stages {
stage('Deploy') {
parallel {
stage('Deploy Foo') {
steps {
build(job: 'Deploy_Foo')
}
}
stage('Deploy Bar') {
steps {
build(job: 'Deploy_Bar')
}
}
}
}
stage('Run tests') {
steps {
powershell(script: '.\\ci\\build.ps1 -Script .\\ci\\test\\build.cake')
}
}
}
}
}
}
I have also tried a solution with locking test-env resource separately for Deploy and Test stages, however, this increases the risk of race condition when some other running job may wait for that resource and "jump in" between Deploy and Test stages of the current job.
Is there any way to achieve such mix of sequential and parallel stages as described above in Jenkinsfile?

Get environment variable of Jenkins Pipeline inside a Job

How can I get the Build workspace for a inside job. I have a pipeline which builds jobs in the following:
node('TEST_UST') {
stage('test01') {
build job: 'test01'
}
stage('test02') {
build job: 'test02'
}
}
How Could I get the workspace of job test01?

Resources