0

So the question is a follow up to another question asked on stackoverflow - How to upload file from command line as file parameter in jenkins

Is there a way to make the job to wait till the file upload is complete? In my case, I have some command line args in the jenkins job which takes file input as parameter. Now I am able to hit the job successfully, but before my file is uploaded, job moves ahead to execute the command line params which cause the job to fail because they need the file to be available.

So my question again - is there a way so that I can make the job to wait for upload to complete before executing cmd params? I am do this thru java and RestAssured lib.

EDIT:

  1. RestAssured.given() .auth().basic("USERNAME", "PASSWORD") // .contentType(ContentType.URLENC) .when() .post("https://JENKINS_HOST/hudson/job/RISTSA/job/JOB_NAME/buildWithParameters" + "?file=C:/PATH_TO_FILE/SOMETHING.json&JenkinsStringParamName=test") .then() .statusCode(201);

// Already tried using file0, JenkinsFileParamName in query params

  1. RestAssured.given().log().all() .contentType(ContentType.URLENC) .auth().basic("USERNAME", "PASSWORD") // .headers(httpHeaders("USERNAME", "PASSWORD")) .formParam("file0", "C:/PATH_TO_FILE/SOMETHING.json") .formParam("json", "{\"parameter\":[{\"name\":\"JenkinsFileParamName\",\"file\":\"file0\"},{\"name\":\JenkinsStringParamName\",\"value\":\"test\"}]}") .when() .post("https://JENKINS_HOST/hudson/job/RISTSA/job/JOB_NAME/buildWithParameters") .then() .statusCode(201);

Now I have tried the following-

  1. providing the content type as url encoded.
  2. Passing credentials as headers (My jenkins job does not need a token
  3. tried using "build" instead of "buildWithParameters" - does not work
  4. tried adding wait in jenkins job to see if it was a network upload issue

Create the second approach from official curl reference provided on jenkins docs

ashkaps
  • 61
  • 6

1 Answers1

0

Since you are already doing a curl to push the file, why not continue with that :)

The below code will continuously check if the file exists.

until $(curl --output /dev/null --silent --head --fail http://website.url/directory/fileExists.txt); do
    printf '.'
    sleep 5
done;

Once the file Exists it will break the loop and continue with your jenkins build.
Hope it helps :)

rohit thomas
  • 2,302
  • 11
  • 23
  • I am actually not using curl. I mentioned in my question that I am using java and restassured to remote access jenkins api. – ashkaps Sep 26 '18 at 06:41
  • jenkins is configured to have two parameters - one File parameter and second String parameter. Is there any other configuration that is required? – ashkaps Sep 27 '18 at 12:55
  • No, not that.... `job moves ahead to execute the command line params which cause the job to fail because they need the file to be available` the point where the above situation happens you need to add the answer which i have provided before this scenario... it's simple in pipeline project...If you are using a freeStyle Project then you will have to add it in the Build Step(Execute shell script on remote host ).... – rohit thomas Sep 28 '18 at 03:08
  • Your answer is based on I am using curl. I am not. I am using java and restassured. After my hit and trial, I am pretty sure that it's the syntax I have got wrong because the file is not getting uploaded (i tried putting in sleep to test). I have added alot of details to the description. Can you tell what is wrong with the code part? – ashkaps Sep 28 '18 at 06:54
  • The Code that you have written will run the job and in the jenkins side-jenkins has to wait till the file is uploaded so you will have to do something from jenkins side not rest assured..... – rohit thomas Sep 28 '18 at 09:45
  • In my case file is not getting uploaded. The jenkins job is triggered but no file is uploaded to workspace. I tried this by giving a good delay. My file is only 2kb. So something is wrong with the way i am hitting the api. – ashkaps Sep 28 '18 at 10:08
  • Again ....it's not wrong... you have mentioned before that the job is building through the api....so that part is done.... the file name which you provide is causing the issue or the jenkins job itself is causing the issue... the file upload is done by jenkins right ? if so, do a build directly from jenkins and see if the file gets upload... if it does this step correctly then you have to see what gets passed as a parameter to jenkins from rest assured. – rohit thomas Sep 28 '18 at 10:20
  • That's where I am facing issue. The jenkins job is success if I upload file manually. I tried using curl from cmd and it triggers and uploads correctly. Challenge is I need to get this into a common utility and not every system that uses that utility will have curl installed. There is just something wrong with the way I am using restassured, – ashkaps Sep 28 '18 at 12:57
  • Can you add an echo in the jenkins build of the parameters that get passed... that way you can see the difference on what is getting passed from rest assured and what is getting passed from jenkins build...From there you just have to match it to what you pass it when doing a manual build – rohit thomas Oct 03 '18 at 02:57