Version Number Plugin in Jenkins declarative pipeline - jenkins

I'm trying to use version number plugin to format a version number for our
packages.
From some reason the placement for the version variable doesn't work
and when I echo the following I only get the build number, for instance: "...54"
def Version_Major = '1'
def Version_Minor = '0'
def Version_Patch = '0'
pipeline {
environment {
VERSION = VersionNumber([
versionNumberString: '${Version_Major}.${Version_Minor}.${Version_Patch}.${BUILD_NUMBER}',
worstResultForIncrement: 'SUCCESS'
]);
}
stage ('Restore packages'){
steps {
script{
echo "${VERSION}"
}
}
}
}
Edit: It does look like an issue with the plugin usage since this works:
properties([
parameters([
string(name: 'Version_Major', defaultValue: '1', description: 'Version Major'),
string(name: 'Version_Minor', defaultValue: '0', description: 'Version Minor'),
string(name: 'Version_Patch', defaultValue: '0', description: 'Version Patch')
])
])
pipeline {
agent any
environment {
VERSION = "${params.Version_Major}.${params.Version_Minor}.${params.Version_Patch}.${BUILD_NUMBER}"
}
stages{
stage ('Test'){
steps {
echo "${VERSION}"
}
}
}
}

You must define the variables inside the pipeline.
Try this:
pipeline {
environment {
Version_Major = '1'
Version_Minor = '0'
Version_Patch = '0'
VERSION = VersionNumber([
versionNumberString: '${Version_Major}.${Version_Minor}.${Version_Patch}.${BUILD_NUMBER}',
worstResultForIncrement: 'SUCCESS'
]);
}
stage ('Restore packages'){
steps {
script{
echo "${VERSION}"
}
}
}
}
If you need to use a parameter instead that's also possible via:
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
usage:
"Hello ${params.PERSON}"

Related

How to pass file parameters to upstream pipeline job in build step plugin

def BUILD_USER = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
pipeline {
agent {label "master"}
parameters {
string(name: 'BUILD', defaultValue: '123')
booleanParam(name: 'Deploy', defaultValue: 'true')
booleanParam(name: 'Upgrade_Config', defaultValue: 'true')
booleanParam(name: 'SchemaComparison', defaultValue: 'false')
booleanParam(name: 'Publish_Server', defaultValue: 'false')
}
stages {
stage ('Start Deployment') {
agent {label "master"}
steps{
script{
sh '''
rm -rf /params/parameters
cd /params
echo $BUILD_USER
python3 buildParameters.py --Build=$BUILD --Publish_VM=$Publish_Server --userName=BUILD_USER --Upgrade_Config=$Upgrade_Config
'''
file = readFile('/params/parameters.txt')
}
}
}
stage ('UpgradeConfigurations') {
when {
expression { params.Deploy == true }
}
agent {label "master"}
environment {
file = "${file}"
}
steps{
script{
println("${file}")
build(job: 'UpgradeConfigurations', parameters: [ file(name: 'parameters', file: "${file}"), string(name: 'build_uniqe_id' , value: "${BUILD_USER}") , booleanParam(name: 'Deploy' , value: "${Deploy}") , booleanParam(name: 'SchemaComparison' , value: "${SchemaComparison}")], propagate: false, wait: false )
}
}
}
}
}
buildParameters.py file generate some additional parameters in parameters.txt file on master vm and I am trying to pass it to the upstream job UpgradeConfigurations
Upstream job UpgradeConfigurations is getting started but file parameters are not getting passed as parameters to it.
I have tried using base64file as well but no luck.
Referred Build Plugin doc:
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/

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.

How to pass parameter value to stage build in Jenkins pipeline?

I have a pipeline created which takes the parameter value from the user. I want to trigger a jenkin job using this parameter.
How can I pass the parameter value to the build's parameter.
Here is my code:
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: ${params.EMail}), string(name: 'SYSTEM', value: ${params.SYSTEM})]
}
}
}
}
In the above code, I am taking email and system details from the user. Then I want to trigger my job "xyz" which would require these parameter.
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: params.EMail),
string(name: 'SYSTEM', value: params.SYSTEM)]
}
}
}
}

Extended Choice Parameter implementation

I'm setting up a new job in which we are required to select Multiple values. Need to select Service1 and Service2...
Went through link How to pass multi select value parameter in Jenkins file(Groovy)
However, I am not sure how to pass values in my Jenkinsfile
A snippet of Jenkinsfile
stage('parallel'){
parallel(
"service1": {stage('service1-deployment') {
if (params.ServiceName == 'Service1' || params.ServiceName == 'ALL'){
b = build(job: 'job name', parameters: [string(name: 'ENVIRONMENT', value: TARGET_ENVIRONMENT),string(name: 'IMAGE_TAG', value: value)], propagate: false).result
if(b=='FAILURE'){
echo "job failed"
currentBuild.result = 'UNSTABLE'
}
}
}
},
"service2": {stage('service2t') {
if (params.ServiceName == 'service2' || params.ServiceName == 'ALL'){
b = build(job: 'Job name', parameters: [string(name: 'ENVIRONMENT', value: TARGET_ENVIRONMENT),string(name: 'IMAGE_TAG', value: value)], propagate: false).result
if(b=='FAILURE'){
echo "job failed"
currentBuild.result = 'UNSTABLE'
}
}
}
},
I see that you're using declarative pipeline syntax for your job.
So, if the accepted answer for that question with booleanParam is useful for you, then you can use it inside parameters section (see the official documentation for more details):
pipeline {
agent any
parameters {
booleanParam(defaultValue: false, name: 'ALL', description: 'Process all'),
booleanParam(defaultValue: false, name: 'OPTION_1', description: 'Process option 1'),
booleanParam(defaultValue: false, name: 'OPTION_2', description: 'Process options 2'),
}
stages {
stage('Example') {
steps {
echo "All: ${params.ALL}"
echo "Option 1: ${params.OPTION_1}"
echo "Option 2: ${params.OPTION_2}"
}
}
}
}
However, if you want to use extended choice parameter with multiselect input, you need to use scripted pipeline syntax, see this example (already mentioned here).

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