Jenkins pipeline using anyof - jenkins

how can i use 2 anyof. What i want is both condition must be met then only execute the job
stage("Spring App") {
when {
anyOf {
environment name: 'key1', value: 'true'
environment name: 'key2', value: 'true'
}
anyOf {
environment name: 'key3', value: 'string1'
environment name: 'key4', value: 'string2'
environment name: 'key5', value: 'string3'
}
}
I need met both condition . How to use and between 2 anyof

You can wrap everything into the allOf condition, like this :
allOf {
anyOf {
environment name: 'key1', value: 'true'
environment name: 'key2', value: 'true'
}
anyOf {
environment name: 'key3', value: 'string1'
environment name: 'key4', value: 'string2'
environment name: 'key5', value: 'string3'
}
}

Related

can we run Jenkins file in pipeline?

I've Pipeline Generic Webhook from Bitbucket, this is a job to trigger another job.
currentBuild.displayName = "Generic-Job-#" + env.BUILD_NUMBER
pipeline {
agent any
triggers {
GenericTrigger(
genericVariables: [
[key: 'actorName', value: '$.actor.display_name'],
[key: 'TAG', value: '$.push.changes[0].new.name'],
[key: 'REPONAME', value: '$.repository.name'],
[key: 'GIT_URL', value: '$.repository.links.html.href'],
],
token: '11296ae8d97b2134550f',
causeString: ' Triggered on $actorName version $TAG',
printContributedVariables: true,
printPostContent: true
)
}
stages {
stage('Build Job DEVELOPMENT') {
when {
expression { return params.TARGET_ENV == 'DEVELOPMENT' }
}
steps {
build job: 'DEVELOPMENT',
parameters: [
[$class: 'StringParameterValue', name: 'FROM_BUILD', value: "${BUILD_NUMBER}"],
[$class: 'StringParameterValue', name: 'TAG', value: "${TAG}"],
[$class: 'StringParameterValue', name: 'GITURL', value: "${GIT_URL}"],
[$class: 'StringParameterValue', name: 'REPONAME', value: "${REPONAME}"],
[$class: 'StringParameterValue', name: 'REGISTRY_URL', value: "${REGISTRY_URL}"],
]
}
}
}
}
Another Pipeline
pipeline {
agent any
stages {
stage('Cleaning') {
steps {
cleanWs()
}
}
def jenkinsFile
stage('Loading Jenkins file') {
jenkinsFile = fileLoader.fromGit('Jenkinsfile', "${GIT_URL}", "${TAG}", null, '')
}
jenkinsFile.start()
}
}
can i run Jenkinsfile in Pipeline ? Because every project I make has a different Jenkinsfile, it can't be the same, but when I run this it doesn't execute the Jenkinsfile
it works for me :D
Sample Pipeline
stage 'Load a file from GitHub'
def jenkinsFile = fileLoader.fromGit('<path-jenkinsfile>', "<path-git>", "<branch>", '<credentials>', '')
stage 'Run method from the loaded file'
jenkinsFile
pipeline {
agent any
stages {
stage('Print Hello World Ke #1') {
steps {
echo "Hello Juan"
}
}
}
}
before run the pipeline, you must install plugin "Pipeline Remote Loader Plugin Version"

Jenkins pipeline, groovy map array as choises

How can I pass the array variable into the global variable in groovy?
It works perfectly without if-else.
choiceArray = []
if (appName == 'ms'){
['78', '99', '10'].each {
"${choiceArray}" << it
}
}
else if (appName == 'ms2'){
['12', '34', '56'].each {
"${choiceArray}" << it
}
}
pipeline {
parameters {
string (name: 'Branch', defaultValue: 'master', description: 'Select Branch')
choice(name: 'Environment', choices: choiceArray, description: 'Choose Environment')
if(...){
choiceArray.addAll(['78', '99', '10'])
}

Use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameter

Is there a way to use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameters?
Below attempt failed.
pipeline {
parameters {
extendedChoice description: 'Template in project',
multiSelectDelimiter: ',', name: 'TEMPLATE',
propertyFile: env.WORKSPACE + '/templates.properties',
quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
value: 'Project,Template', visibleItemCount: 6
...
}
stages {
...
}
propertyFile: '${WORKSPACE}/templates.properties' didn't work either.
The environment variable can be accessed in various place in Jenkinsfile like:
def workspace
node {
workspace = env.WORKSPACE
}
pipeline {
agent any;
parameters {
string(name: 'JENKINS_WORKSPACE', defaultValue: workspace, description: 'Jenkins WORKSPACE')
}
stages {
stage('access env variable') {
steps {
// in groovy
echo "${env.WORKSPACE}"
//in shell
sh 'echo $WORKSPACE'
// in groovy script
script {
print env.WORKSPACE
}
}
}
}
}
The only way that worked is putting absolute path to Jenkins master workspace where properties file is located.
pipeline {
parameters {
extendedChoice description: 'Template in project',
multiSelectDelimiter: ',', name: 'TEMPLATE',
propertyFile: 'absolute_path_to_master_workspace/templates.properties',
quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
value: 'Project,Template', visibleItemCount: 6
...
}
stages {
...
}
It seems that environment variables are not available during pipeline parameters definition before the pipeline actually triggered.

Unknown conditional beforeAgent. Valid conditionals are: allOf, anyOf, branch, buildingTag, changeRequest, changelog, changeset, environment, equals

when {anyOf{ beforeAgent true; environment name: "IRM_VLM_FACTSET", value: "true"; environment name: "env1", value: "true"; environment name: "env2", value: "true"; environment name: "env3", value: "true"}}
this is giving me error . Can you tell me what wrong i am doing
when {beforeAgent true; anyOf{ environment name: "IRM_VLM_FACTSET", value: "true"; environment name: "env1", value: "true"; environment name: "env2", value: "true"; environment name: "env3", value: "true"}}

Jenkins Date Parameter Plugin - How to use it in a Declarative Pipeline

What is the syntax for using the Date Parameter Plugin in a declarative pipeline.
So far I have tried this:
pipeline {
agent {
node {
label 'grange-jenkins-slave'
}
}
options { disableConcurrentBuilds() }
parameters {
date(name: 'EffectiveDate',
dateFormat: 'MMddyyy',
defaultValue: 'LocalDate.now();',
description: 'Effective Date',
trim: true)
file(name:'algo.xlsx', description:'Your algorithm file')
choice(name: 'currency',
choices: ['USD'],
description: 'Select a currency')
}
stages {
stage('genRates') {
steps {
script {
echo "test"
}
}
}
}
}
The error I get is WorkflowScript: 11: Invalid parameter type "date". Valid parameter types: [booleanParam, choice, credentials, file, text, password, run, string] # line 11, column 3.
you can define parameter as class DateParameterDefinition.
example:
properties([parameters([
string(name: 'somestring', defaultValue: 'somevalue'),
[$class: 'DateParameterDefinition',
name: 'somedate',
dateFormat: 'yyyyMMdd',
defaultValue: 'LocalDate.now()']
])])
pipeline {
...
}
I did not use date parameter plugin as I didn't find any example how to use it. I resolved this in a different way.
import java.text.SimpleDateFormat
def sdf = new SimpleDateFormat("yyyyMMdd")
def dateDefaultValue = sdf.format(new Date())
pipeline {
parameters {
string(name: 'SOMEDATE', defaultValue: "${dateDefaultValue}", description: 'Default value is current date in the format YYYYmmdd', trim: true)
}
.....
.....
}

Resources