22

I am executing parameterised build in jenkins to count no. of lines in file which has 1 file parameter. Its file location is pqr. The name of the script file is linecount.sh which is saved at remote server. When i tried to execute it using command sh linecount.sh filename, it works perfectly from jenkins. But as i remove filename from the argument and execute same script as parameterised build it is showing below error on console :

Started by user Prasoon Gupta
[EnvInject] - Loading node environment variables.
Building in workspace users/Prasoon/sample_programs
Copying file to pqr
[sample_programs] $ /bin/sh -xe /tmp/hudson3529902665956638862.sh
+ sh linecount.sh
PRASOON4
linecount.sh: line 15: parameterBuild.txt: No such file or directory
Build step 'Execute shell' marked build as failure
Finished: FAILURE

I am uploading file (parameterBuild.txt) from my local machine. Why is it giving this error?

My doubt is in shell script I used argument as $1. How can I refer this when I am taking file as parameter.

Jon S
  • 15,846
  • 4
  • 44
  • 45
Prasoon Gupta
  • 305
  • 1
  • 3
  • 11
  • 1
    Make sure that the parameter file is inserted as you expect into the workspace, e.g. do a `ls -l` in your shell command. – Jon S Feb 14 '17 at 11:47
  • Thank you but i want to upload it directly from my local machine, want to make my job more user friendly. – Prasoon Gupta Feb 14 '17 at 12:21
  • Yes, understand that. But in order to isolate the issue, what happens if you run `ls -l` as part of your shell command in the job configuration? E.g. does it list `parameterBuild.txt` there or any other files? – Jon S Feb 14 '17 at 14:06
  • No, it doesn't lists parameterBuild.txt there but a new file named same as logical name of my file location (here pqr) is created there having all the content as in parameterBuild.txt. – Prasoon Gupta Feb 15 '17 at 04:39
  • Ah, I see, my bad, misread some of your question. I've posted an answer. – Jon S Feb 15 '17 at 06:27

4 Answers4

36

The uploaded file will not retain the same name as it has on your local computer. It will be named after the File location argument specified in the file parameter settings: enter image description here In this example I will get a file called file.txt in my workspace root, regardless of what I call it on my computer. So if I now build my job and enter the following in the parameter dialog (note that my local filename is table.html):

enter image description here

Then I get the following in the log (I have a build step which does ls -l):

Building on master in workspace /var/lib/jenkins/workspace/fs
Copying file to file.txt
[fs] $ /bin/sh -xe /tmp/hudson845437350739055843.sh
+ ls -l
total 4
-rw-r--r-- 1 jenkins jenkins 292 Feb 15 07:23 file.txt
Finished: SUCCESS

Note that table.html now is called file.txt, e.g. what I entered as File location.

So in you're case the command should be:

sh linecount.sh pqr
Jon S
  • 15,846
  • 4
  • 44
  • 45
  • @JonS Thanks for the clarification. Additionaly from a Job A, I wanted to trigger Job B in a Remote Server. Job B is parameterized and takes file as an input. I use Parameterized Remote Trigger Plugin. If I pass the file in Load parameters from external file field of Job A, its not getting read properly in the Job B. Any suggestions ?? – Santhosh Oct 12 '17 at 07:08
  • @Santhosh Sorry, I have no idea, I suggest that you post a separate question – Jon S Oct 12 '17 at 08:34
  • can this file parameter filed be made mandatory.So that, without uploading the file, the build should fail. – vishal Sep 04 '19 at 13:41
  • 1
    @vishal, probably a separate question, but, just a shot in the dark, I think you simply could check if the file exists, and if not fail the script – Jon S Sep 04 '19 at 14:05
  • i have exactly that flow - parent project passes file with parameters, child project has input file parameter with the `File location` as scan.txt (it is property file with lines key=value) , there is a build step - powershell with ```#read input file $props = convertfrom-stringdata (get-content ./scan.txt -raw) write-host "params1: xx=$($props.xx) yy=$($props.yy)"``` and it prints values properly. as indicated above it worth to check workspace of you child project if it has that file parameter; in your case - `file.txt` should be present – Sasha Bond Sep 15 '22 at 21:11
2

There is a a bug since ages that makes impossible to use fileParameter:

GalloCedrone
  • 4,869
  • 3
  • 25
  • 41
  • 2
    Note there's a solution in the comments after you configure the needed plugin & signatures. Something along the lines `def inputFile = input message: 'Upload file', parameters: [file(name: 'data.zip')] unzip dir: '', glob: '', zipFile: inputFile.remote` – Federico Sep 30 '19 at 01:20
  • The comments on your first link say in Feb 2021 a plugin was released for this functionality: https://plugins.jenkins.io/file-parameters/ – Noumenon Apr 19 '21 at 04:31
2

There is a workaround for this issue https://github.com/janvrany/jenkinsci-unstashParam-library and in a pipeline script you do:

library "jenkinsci-unstashParam-library"
node {
  def file_in_workspace = unstashParam "file"
  sh "cat ${file_in_workspace}"
}
0

If it's to do with Free-Style job & if your configuration looks similar to this - https://i.stack.imgur.com/vH7mQ.png then you can run simply do sh linecount.sh ${pqr} to get what you are looking for?

vinWin
  • 509
  • 1
  • 5
  • 18