Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying files from one directory to another in jenkins

Tags:

jenkins

I'm trying to move my test results onto a public webpage.

I set up a "Post-build action" > "Post build task" to execute a script.

The script is:

cp -r /var/lib/jenkins/jobs/instrumentation-tests/htmlreports/HTML_Report/ /var/www/html/test/

Jenkins outputs: cp: directory /var/www/html/test does not exist

If I'm logged in as user jenkins on the linux machine running jenkins, I can navigate to the source and SEE that there are files there currently. I can navigate to the destination and see that it DOES exist.

Also, I tried running that command from the terminal as the jenkins user, and the cp completed successfully.

like image 744
EGHDK Avatar asked Mar 13 '23 07:03

EGHDK


1 Answers

As you mention in the comments, your test results are being generated during a build on a slave machine. Therefore you're trying to copy the files to /var/www on the slave, not the Jenkins master server.

There are a few different ways you could solve this:

  1. Ensure that the build happens on the master server.

    You can do this by choosing "Restrict where this project can be run" on the job configuration page, and entering "master" in the text field.

    This ensures that your file copying will work at the end of a build, assuming that the Jenkins user has write permissions.

  2. Use the Publish over SSH plugin to publish the files directly to /var/www on the Jenkins master, from any other machine.

    This has the advantage that it will work, no matter which Jenkins build machine the build takes place on.

You could also split up the job into two parts: one job to run the tests and generate the results, and another job to publish the test results.

The first job could run on any machine, and would save the generated HTML files using the "Archive the artifacts" post-build action. It would then start the second job — via the "Build other jobs" post-build action — in order to do the publishing.

The second job could use either of the above approaches: either publish using SSH, or ensure that it runs on the master and use a simple shell step with cp.

In both cases, you would use the Copy Artifact plugin to copy the archived HTML files from the upstream build.

like image 188
Christopher Orr Avatar answered Mar 19 '23 01:03

Christopher Orr