Special Character in Property setting in artifactory through jenkins pipeline - jenkins

I am following this link to add properties to a file in artifactory. But I am not able to find a way to add special characters in values of Property. Is there any escape character to do this.
I have tried using \ and %5C as suggested for api in artifactory. But it is not working for pipeline.
This is my pipeline script
node('master'){
stage('test'){
def arti_server = Artifactory.server 'Artifactory_Server'
def setPropsSpec = """{
"files": [{
"pattern": "test/test.groovy"
}
]
}"""
arti_server.setProps spec: setPropsSpec, props: "p1=%5C;1;p2=test2"
}
}
Error I am getting is because it is not taking ; as escape character. Instead it is taking it as another property. Here is my ERROR
java.io.IOException: Setting properties: Every property must have at least one value.
at org.jfrog.build.extractor.clientConfiguration.util.EditPropertiesHelper.validateSetProperties(EditPropertiesHelper.java:93)

Related

How to fix Jenkins String Interpolation with downstream job?

I have a parent pipeline job that takes parameters and passes them to a downstream job. I've achieves this in multiple ways with no issue however I keep getting a string interpolation warning that I am trying to fix, but am unable to do so. Based on the documentation (https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation) in most cases using single quotes should work, however this passes the literal name rather than the value (e.g if I set my variable as SECRET_PWD and call it like '${SECRET_PWD}' it shows up as ${SECRET_PWD} on the downstream job instead of the value passed to the parameter.
Here's what I've tried to so far:
Parent Pipeline
pipeline {
agent any
parameters {
password(defaultValue: "", description: 'The admin password', name: 'SUPER_SECRET_ADMIN_PWD')
stages {
stage("Stage1") {
steps {
build job: "secret_job/${env.BRANCH}", propagate: true, wait: true, parameters: [
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD', value: "${params.SUPER_SECRET_ADMIN_PWD}" ]
]
}
}
This gives the following error in when calling the downstream job.
Warning: A secret was passed to "build" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [SUPER_SECRET_ADMIN_PWD]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
The parameter 'SUPER_SECRET_ADMIN_PWD' did not have the type expected by secret_job ยป secret_branch. Converting to Password Parameter.
Note: I am aware that the StringParameterValue is the reason for the first error. I have changed this in a few different ways to solve that but i still get the interpolation issue.
The other ways I've tried are:
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This works but still interpolation issue
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD', value: '${params.SUPER_SECRET_ADMIN_PWD}' = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
I've also used ${env.SUPER_SECRET_ADMIN_PWD} similar to ${params.SUPER_SECRET_ADMIN_PWD}
Note that I've changed my downstream job to use single quotes and i'm doing a simple sh script something like below with no interpolation errors (I have them before though).
stages{
stage("test"){
steps{
script{
sh '''
echo ${SUPER_SECRET_ADMIN_PWD}
'''
}
}
}
How do I go about solving interpolation and still passing the password parameter down to a downstream job?

Dynamic assignment of values as default parameters in Jenkinsfile

Whenever I run this pipeline in Jenkins I have to manually copy-paste some values from a YAML file in a remote Gitlab repository. What I would like to achieve is an auto-fill of the values that .
This is how my Jenkinsfile and the YAML look like:
Jenkinsfile
pipeline {
agent {
docker {
image 'artifactory...'
args "..."
}
}
parameters {
string(name: 'BACKEND_TAG_1', defaultValue: '', description: 'Tag...')
string(name: 'BACKEND_TAG_2', defaultValue: '', description: 'Tag...')
}
stage('prepare') {
steps {
script {
dir('application') {
git url: env.PIPELINE_APPLICATION_GIT_URL, branch: env.PIPELINE_APPLICATION_GIT_BRANCH
}
Values = readYaml file: 'application/values.yaml'
values.yaml
version:
default: 0.1.2
company_tag_1: 0.1.124
company_tag_2: 0.1.230
So I need to loop into the parameters and assign the corresponding values:
Values.each { Value ->
Value.version.minus('company')
/* This value should be assigned to the corresponding parameter BACKEND_TAG_* parameter.
e.g.: BACKEND_TAG_1.default=company_tag_1
BACKEND_TAG_2.default=company_tag_2
*/
}
Reading the YAML works fine but I don't know how to proceed in the assignment of the values.
I presume you would like to populate all parameters before click Build button. I mean after clicking "Build with Parameters" button, you basically would like to see your parameters are populated from your YAML file.
If this is the case You can use Active Choice Parameter or Extended Choice Parameter plugins for this purpose. These Plugins are able to run Groovy Script, so you can develop a small Groovy Script read and select parameters automatically.

Multiple Jenkins parameterized build value pass value to Jenkinsfile for declarative pipeline

Hi my Jenkins project is parameterised build. I have 3 variables. 1 choice and 2 string parameter. The choise perameter is do_you_want_to_deploy and string parameter is git_tag and git_branch. I want to know how can i pass this value to a jenkinsfile?
In freestyle project, I selecft 'Extra Variables' and then got Key and Value. So key i put deploy_location, value is ${do_you_want_to_deplo}. Key is which_tag, value is ${git_tag}. Key is which_ranch, value is ${git_branch}. I am performing for ansible. How can i add verbos -vvv as well? This for pipelin project. Below is my code
ansiblePlaybook(
vaultCredentialsId: 'VaultId',
inventory: 'host-inventory.yml',
playbook: 'myPlaybook.yml'
)
``
I also need pass same value to downstream project. How can this be done?
Hi my Jenkins project is parameterised build. I have 3 variables. 1
choice and 2 string parameter. The choise perameter is
do_you_want_to_deploy and string parameter is git_tag and git_branch.
I want to know how can i pass this value to a jenkinsfile?
In Jenkinsfile there is parameters block to define variables. As per your use case, parameters definition may look like below. Here, by choice in your explanation I was assuming you need a toggle but if you need a list of items then use choice parameter type.
pipeline {
...
parameters {
booleanParam(name: 'do_you_want_to_deploy', defaultValue: false, description: 'Description of do_you_want_to_deploy')
string(name: 'git_tag', defaultValue: '', description: 'Description of git_tag')
string(name: 'git_branch', defaultValue: '', description: 'Description of git_branch')
}
stages {
stage('Example') {
steps {
ansiblePlaybook(
...
)
}
}
}
}
In freestyle project, I selecft 'Extra Variables' and then got Key and
Value. So key i put deploy_location, value is ${do_you_want_to_deplo}.
Key is which_tag, value is ${git_tag}. Key is which_ranch, value is
${git_branch}. I am performing for ansible. How can i add verbos -vvv
as well?
Ansible plugin has an option extraVars that can be used to pass number of variables from the pipeline. There is another option named extras that takes a string and can be used to pass additional variables, switches etc.
Together, ansiblePlaybook may look like below,
ansiblePlaybook (
vaultCredentialsId: 'VaultId',
inventory: 'host-inventory.yml',
playbook: 'myPlaybook.yml',
extras: '-vvv',
extraVars: [
deploy_location: params.do_you_want_to_deploy,
which_tag: params.git_tag,
which_branch: params.git_branch
]
)
I also need pass same value to downstream project. How can this be
done?
As you can see from the example of ansiblePlaybook above, the parameters can be accessed via params object.

Looking for a Jenkins plugin to allow per-branch default parameter values

I have a multi-branch pipeline job set to build by Jenkinsfile every minute if new changes are available from the git repo. I have a step that deploys the artifact to an environment if the branch name is of a certain format. I would like to be able to configure the environment on a per-branch basis without having to edit Jenkinsfile every time I create a new such branch. Here is a rough sketch of my Jenkinsfile:
pipeline {
agent any
parameters {
string(description: "DB name", name: "dbName")
}
stages {
stage("Deploy") {
steps {
deployTo "${params.dbName}"
}
}
}
}
Is there a Jenkins plugin that will let me define a default value for the dbName parameter per branch in the job configuration page? Ideally something like the mock-up below:
The values should be able to be reordered to set priority. The plugin stops checking for matches after the first one. Matching can be exact or regex.
If there isn't such a plugin currently, please point me to the closest open-source one you can think of. I can use it as a basis for coding a custom plugin.
A possible plugin you could use as a starting point for a custom plugin is the Dynamic Parameter Plugin
Here is a workaround :
Using the Jenkins Config File Provider plugin create a config json with parameters defined in it per branch. Example:
{
"develop": {
"dbName": "test_db",
"param2": "value"
},
"master": {
"dbName": "prod_db",
"param2": "value1"
},
"test_branch_1": {
"dbName": "zss_db",
"param2": "value2"
},
"default": {
"dbName": "prod_db",
"param2": "value3"
}
}
In your Jenkinsfile:
final commit_data = checkout(scm)
BRANCH = commit_data['GIT_BRANCH']
configFileProvider([configFile(fileId: '{Your config file id}', variable: 'BRANCH_SETTINGS')]) {
def config = readJSON file:"$BRANCH_SETTINGS"
def branch_config = config."${BRANCH}"
if(branch_config){
echo "using config for branch ${BRANCH}"
}
else{
branch_config = config.default
}
echo branch_config.'dbName'
}
You can then use branch_config.'dbName', branch_config.'param2' etc. You can even set it to a global variable and then use throughout your pipeline.
The config file can easily be edited via the Jenkins UI(Provided by the plugin) to provision for new branches/params in the future. This doesn't need access to any non sandbox methods.
Not really an answer to your question, but possibly a workaround...
I don't know that the rest of your parameter list looks like, but if it is a static list, you could potentially have your static list with a "use Default" option as the first one.
When the job is run, if the value is "use Default", then gather the default from a file stored in the SCM branch and use that.

How to access all nodes from NodeParameterDefinition in jenkins pipeline?

I'm writing a Jenkinsfile that use the NodeLabel Parameter Plugin for jenkins. Here I use the NodeParameterDefinition to give the user the ability to select nodes where the build should happen. I have enabled allowMultiSelectionForConcurrentBuilds, but I only still get a string with only one node name when accessing the parameter value in the Jenkinsfile. The parameter value type is also a string, how can I get all the nodes the user selected for the parameter?
paramter definition:
[
$class: 'NodeParameterDefinition',
allowedSlaves: ['ALL (no restriction)'],
defaultSlaves: ['master'],
description: 'What nodes to run the build on.',
name: 'BUILD_NODE',
nodeEligibility: [$class: 'AllNodeEligibility'],
triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
]
So if I select multiple nodes when executing, I still only get one node name when accessing this parameter value.
echo "Will build on $BUILD_NODE";
Is multi node selection was enabled not possible with pipeline scripts?
How I access the parameter value:
echo "Will build on $BUILD_NODE";
node("$BUILD_NODE")
{
...
}
NodeLabel Parameter Plugin doesn't work smoothly with Pipeline and Blue Ocean, just as it is not updated frequently (see the revision history). Jenkins Plugins must follow requirements in order to be compatible with Pipeline.
Unfortunately the issue is still unresolved (unknown when it will be resolved):
https://issues.jenkins-ci.org/browse/JENKINS-43720
The problem is that I can not use env.NODE_PARAM or NODE_PARAM to get
multiple selection of nodes, as those are only a string representation
of a single node.
You can vote for this jira-task JENKINS-43720 (click "Vote for this issue"), or participate in the plugin development.
So far I found my clumsy way to imitate the plugin behavior by using another parameter option choice (but this works in Blue Ocean!):
properties([
parameters([
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_1"),
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_2")
])
])
// here you can write your behaviour
// e.g. validation of params, e.g. if 'none' is selected, then use the default node_X
node(env.NODE_1) { }
node(env.NODE_2) { }
or you can use the option string:
properties([
parameters([
string(defaultValue: "node_1, node_2", description: "", name: "NODE", trim: false)
])
])
// parse here the param env.NODE

Resources