How to make path of File Parameter Dynamic in Jenkins Job - jenkins

I am using a file parameter in my Jenkins Job.
E.g. src/main/resources/file1.txt;
Now I have three files in the same directory (src/main/resources) : file1.txt, file2.txt, file3.txt
If I enter the path of file param as : src/main/resources/file1.txt
Then the uploaded file will be replacing the file1.txt file in my workspace.
Problem Statement :
I want the file parameter to be dynamic in nature so that I can upload file1/2/3 and it should replace the corresponding file in my workspace in directory(src/main/resources/).
Need suggestion if this can be achieved in Jenkins.

This is possible. You need to use 'Active Choice Reactive Reference Plugin'.
https://plugins.jenkins.io/uno-choice/

Related

parametrized jenkinsFile : file parameter

I want to configure a parametrized job in jenkins, who manipulate file:
parameters([
file(defaultValue: 'DEFAULT', name : 'tomcatCodesUrl' , description: 'URL of service where to find tomcat mapping json file'),
the issus is , this parameter only return the name of the file. how can I acces to this content?
Currently there is no easy way to do this. You can find discussion about this in JENKINS-27413
Yeah that parameter is as redundant as it can be. Might aswell just use string.
Anyway. You can get the files content with readFile:
def content = readFile encoding: 'utf-8', file: 'tomcatCodesUrl'
How to use file parameter in jenkins
This post might be helpful. The upshot is, when users upload a file, it will be saved into the root directory of the project's workspace. You can directly access the file using any programming language you like, given the file name. The file content is not returned to you as a parameter, but anyway, since you know its saved place (workspace dir) and file name, you are in control.

Jenkins issue with Trigger one build per property file

Job A uses "For every property file, invoke one build" parameter factory to call downstream job B.
Here is the file pattern I am using:
d:\temp*.properties
There are two files in that folder:
build0.properties
build1.properties
each file looks something like this:
modified=SampleApp
Job B fails because job A is not setting the parameters from above file. If I look at the parameters for a build of Job B, they are empty.
The process works when I use "Parameters from properties file" parameter type instead of a parameter factory, and specify the full path to one of the files, so I know the files are in the right format. I do not want to add a parameter for each file I have,
since I will have these files generated dynamically.I would prefer to use the parameter factory if possible.
Issue with the file permissions, when I pointed to workspace directory with the file pattern It started workign fine.

SCP Jenkins Plugin does not copy selectively

I want to copy only specific files in a directory to remote server using Jenkins SCP Plugin.
I have folder structure /X/Y/...Under Y, I need only the files a b c among a b c d e f. Is this possible...?
Of course, to copy all files all you need is X/Y/**. But what about copying selectively.
I was reading somewhere that this is a kind of bug in the plugin.
I have string parameter, $FILES=x,y,z highlighted in "BUILD WITH PARAMETERS"
SCP Configuration:
Source: some/path/$FILES (relative to $WORKSPACE)
Destination: /var/lib/some/path
You should be able to say X/Y/a; X/Y/b; X/Y/c
Also remember that these files have to be under the job's ${WORKSPACE}
Alternatively, you can have another build step in-between that copies only the files that you want into a staging folder, and then supplying the staging folder to SCP plugin
Edit after OP clarification:
Your $FILES variable contains x,y,z When you supply this as Source to SCP plugin, it becomes:
some/path/x,y,z
Or if we break this one item per line:
some/path/x
y
z
The first item is valid, the next two are not complete paths, therefore are not found.
Several ways to fix it (chose either one):
Full path in parameter variable.
Under your FILES string parameter, list the full path, like:
some/path/x, some/path/y, some/path/z
Under SCP Source, use only $FILES
pros: quick and stable.
cons: looks ugly with long paths.
Wildcard path in parameter variables.
Under your FILES string parameter, list the global wildcard path (files will be found under any directory), like:
**/x, **/y, **/z
Under SCP Source, use only $FILES
pros: quick and looks better than long paths.
cons: only works if files x, y and z are unique in your whole workspace. If there is $WORKSPACE/x and $WORKSPACE/some/path/x, one will end up overwriting the other.
Prepare MYFILES variable and inject it.
You need an Execute Shell build step added. In there write the following:
mypath=some/path/
echo MYFILES=${mypath}${files//,/,$mypath} > myfiles.props
Then add Inject environment variables build step (get the plugin in the link). Under Properties File Path, specify myfiles.props.
Under SCP Source, use only $MYFILES (note you are reading modified and injected variable, not the original $FILES)
pros: looks good in UI, proper and further customizable.
cons: more build steps to maintain in configuration.
p.s.
In all these cases, a multi select Extended Choice Parameter will probably look better than a string parameter.

Jenkins email ext jelly script include file contents

Can I display the contents of another file in my jelly script output?
If the file is included in the workspace of the job, declare your variable such as:
<j:set var="fileContent" value="${build.getWorkspace().child("results.html")}"/>
And call it this way:
${fileContent}
Yes, you can use the ${FILE, path} token to include the contents of a file (path is relative to your workspace directory).
This info is taken from the Content Token Reference in the email-ext part of your job configuration. Click the question mark on the right to get the full list of tokens.
Look at util:loadText which is a "tag which loads text from a file or URI into a Jelly variable."
<u:loadText var="contents" file="${filename}"/>
${contents}
Haven't used it inside of Jenkins before... let us know if it works.

Insert the content of a text file in a job description

I'm trying to insert the content of a file inside a job description. My build generates a file and I can find it easily with the following URL: http:/[my-domain]job/[my-job]/lastBuild/artifact/[my-file]. In my case, this is a text file and I would like to display it in the job description. I can easily insert a link to this file with HTML but how can I insert the content of this file ?
What is your Jenkins running on? Windows or Linux?
The Project Description Setter plugin is indeed the way to go, but you need to display the content of your file in the build log first, before the plugin will pick it up.
Like Christopher said, you don't need a job URL to access the file that you have in your workspace.
For Linux, put the following into your shell execute step:
echo -n "[DESC] " && cat myfile
For Windows, use this:
echo|set /p="[DESC] " & type myfile
This will print the content of file and prefix it with "[DESC]". We need this prefix (it can be anything you want) to identify this line to the Description Setter plugin
In the job configuration, under Set build description, type:
\[DESC\] (.*)
One note: only the first line of the file will be printed in description
The Project Description Setter plugin can do this.

Resources