How to publish logs in Jenkins? - jenkins

I am trying to use Jenkins to run a script on Linux. The script will run on a Linux slave, generate some log files in Jenkins working directory and tar file in a dedicated place on the slave build server.
Is there a way to publish these log files and tar file to the build result, so they can be inspected/downloaded from the build result page.

Use the "Archive the artfacts" option in the post-build steps of your Jenkins job configuration and specify the paths to the files you want to save. They will then be linked on the build page under "Last successful artifacts".

"Archive the artfacts" is one option, or you could copy the logs to %JenkinsDir%\%BUILD_ID%\archive folder as a post build action. Then you can see them as build artifacts.

You may need Log Parser plugin for extracting info from log

Related

How to add a script file in Jenkins job workspace remotely and run it as part of build step

Is it possible to add a file to Jenkins Job workspace and run it from build step.
The Jenkins is on a remote folder and I cannot directly access workspace as a folder structure.
Is there any way to achieve this from Jenkins dashboard?
Yes you can do that. In order to achieve that you can place your file in Git repository and then in the Jenkins job you can pull it and then you can execute it as a part of Jenkins job

Change working directory during Jenkins build (not gradle, not maven)

Can I change the working directory during a Jenkins job for all successive steps?
My job's first step checks out a git project. Unfortunately this project has a mix of technologies; it's not a java/maven project (so the trick of 'mvn -f subdir/pom.xml' doesn't apply) nor is it a gradle project. So I'd like to change to a subdirectory of the checked-out project and start running Jenkins plugins, like invoking shell scripts, like running tox to test python code, like running docker to build images, etc.
Maybe Jenkins wants every step to begin in $WORKSPACE, and allowing a directory change during the job would break some vital assumptions?
I know this has been asked before. Similar questions but answers specific to maven:
Jenkins Maven Build -> Change Directory and
Jenkins: How To Build multiple top-level projects from a git repository?
Similar question but answer specific to gradle:
Change directory during a build job on jenkins
You can separate out job based on sub folders and use filter to checkout in you SCM configuration so only sub folder that you want for that job will get cloned in your workspace. As your first step of your build use batch/shell command to move all file from sub folder to workspace. and then run all the steps that you want.

Copying files from one directory to another in 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.
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:
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.
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.

Jenkins - Copy build log from master to a shared drive

Can someone direct me here? I have a simple job configured in Jenkins on a WINDOWS environment (master and all slaves running on windows) and the job is supposed to run on a particular slave. When you build the job, the build log ( log.log) gets stored in ” %JENKINS_HOME%\jobs\\builds\%BUILD_NUMBER%\” on the master.
I do have a Jenkins workspace (which is required when you add a slave node) set on the slave for this job–where nothing gets stored when the job runs.
With this scenario, I would like to copy the build log (log.log file that’s available on the master) to a share drive. Please advise me the way to get this done. I have tried few plugins “Copy to slave”, “Copy Artifact Plugin” and ArtifactDeployer Plugin…I could not get them working to meet what I need.
Use a second build action with the execute batch option. Put the copy command there to copy the log to another location.
The following command kind-of works:
curl ${BUILD_URL}consoleFull -o ${TargetDir}/Log.txt
where
TargetDir="${WORKSPACE}/Directory/target"
BUILD_URL and WORKSPACE are set by Jenkins. Unfortunately Jenkins doesn't copy the whole log. I've tried consoleText and gotten the same result: partial logs files. :-(

Execute Shell script from workspace on remote machine after build successful (Jenkins)

The scenario is - I have a job A which runs my ant script and packages the artifact's for me.
I am also using parametrized Triggered plug in to Trigger my "Job B" which will deploy my artifact on remote machine.
The job A is working fine and also Job B.
The tasks that i have to perform with Job B are
GIT checkout (which contains my deployment scripts) (successfully doning).
Copying artifacts from previous build to Remote machine. (successfully doing)
Run shell script on remote machine(script present in workspace folder )- Facing issues.
I browsed various plug ins for the same but no one is allowing me to run shell script after , "SCP to remote machine" which is present in Post build action.
I would like to execute the same sequence, however if you guys have any other suggestions please share.
Thanks in Advance.!
As part of Publish Over SSH Plugin, you can execute a script after the files had been copied over.
Under Post-build Actions
Add Send build artifacts over SSH
Select a preconfigured server (done in global configuration)
Select files to copy from workspace
Enter Exec command
If one of the files you copy is your shell script, you can enter it here as an "exec command"
To solve my query i used Jenkins SSH Plugin. This provides a configuration tab where i can add multiple hosts and after that used them in my job level configuration.
Link to Plugin
you get privilege to execute shell script on remote host as pre-build step or post build step.
updated the path of publish over ssh it worked for me

Resources