How send folder as attachments in Jenkins Job Email Notification - jenkins

I have A Jenkins Job with Workspace C:\hello_world\test_output*
in the test output folder, 2 things are one folder and one HTML file I want to send test output folder as a zip file as attachments on Jenkin jobs but I can't able to do it, please help

Think of it as two steps: 1) zipping the files; and 2) sending the attachment.
I've done this by installing 7zip, then running the command:
"C:\Program Files\7-Zip\7z.exe" a -r C:\hellow_world\test_output.zip C:\job\test_output\* - mem=AES256
With the https://plugins.jenkins.io/email-ext plugin installed, there's a lot of flexibility, including the ability to send attachments.

Bear in mind that some mail hosts example GMAIL have started blocking things like executables, even when found within zip files. If you've got users on such a host, you might run into trouble through no fault of your own.
Apart from that, depending on the OS that Jenkins is running on, you could add a post-build Execute shell or Execute Windows Batch command step that calls the zip tool of your choice, and send an e-mail with attachments using the email-ext plugin for example

Related

sending cucumber reports via jenkins Editable mail option

please can you assist me.
I am new to jenkins, working on a POC for my organization. I have successfully integrated cucumber execution through jenkins and able to execute the scripts and generated cucumber reports using cucumber plugin successfully
Issue:
I need to send the cucumber reports(reports are getting generated under(C:\Users\username.jenkins\jobs\cucumbertest\builds) send anyone of those reports as an attachment to my manager through Editable email option in jenkins.
please can someone assist me
The following approach worked for me:
Cucumber project's pom.xml have to include maven-cucumber-reporting plugin.Because we want to create report under Target folder.How to configure your pom.xml: https://github.com/damianszczepanik/
Jenkins Manage Plugin and install Email-ext plugin & Post build task.
Report files js, html, etc. in folder but I think if we want to send them properly via mail, their extension must be zip. So, we create a batch file and put it into C:\Users\yourUser\.jenkins\workspace\yourProject.
I use 7-Zip ;
4.1 Open txt file in notepad,
4.2 Put the following script into the file
#echo off
setlocal
for /d %%x in (C:\Users\yourUser\.jenkins\workspace\yourProject\target\cucumber-html-reports*.*) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%x.zip" "%%x\"
endlocal
exit
4.3 save as .bat file. For Example I have new.bat
In Jenkins choose Post Build Task then I write C:\Users\yourUser.jenkins\workspace\yourProject\new.bat in area script
Then Editable Email Notification part Attachments part must be
**\**\cucumber-html-reports.zip

Creating artifacts in jenkins

I have been tasked with looking into using Jenkins as a build server. So far I have managed to pull a project from git, restore the Nuget packages, build the project and run the unit tests. However I am struggling to find out how to generate the artifact.
The way the business would like to have the build server generate a zip file to a directory on the build server or a remote server for the systems team then to pick up and deploy to the relevant location. E.g. given a windows service project the built bin directory would be zipped up and put in the relevant artifact directory.
I thought that in order to do this I add an archive the artifacts post-build action. However I am getting the below error:
‘Watchdog.WinService.Monitor/bin/Release/*.zip’ doesn’t match anything:
‘Watchdog.WinService.Monitor’ exists but not
‘Watchdog.WinService.Monitor/bin/Release/*.zip’
If I look in the workspace for this project I can browse to the bin directory and see all the files so I unsure what I have done wrong.
Can someone please let me know if what I am trying to accomplish is possible, and also if our approach to using Jenkins is correct?
The problem is that you try to create the artifact using the archive artifatcs step.
But the step is to collect artifacts and show them on the job page.
That means you need to create the artifact first e.g. using a shell or batch script.
You can combine this with the Flexible Publish Plugin.
When you select this as post build step you can create a conditional action that runs the artifact archive task and as condition executes the script that creates the zip file.
So if that fails the task won't be executed. Also it may causes your job to 'fail' but that may not be the case in your job.

Jenkins - Add download link to file if build fails

I'm a little new to Jenkins, and I can't seem to figure this out. I have access to a Jenkins server that uses slaves to perform build jobs.
If a build fails, it stores a generated zip archive in a persistent Workspace directory for further debugging. The zip file is generated by a python script that keeps track of only the last 3 failed builds to conserve memory (i.e. 3 failed builds will result in 3 archives in the folder, but a fourth failed build will delete the oldest archive before adding the new one).
What I'm trying to do is add a download link to a failed Jenkins Run to allow users to quickly download the zip file that was generated for that build. But I'm really confused as to how approach this!
So I guess the question is, how could I add a download link to a Jenkins Run page to a file generated during that Run if it fails?
Example usage scenario:
1. I build some code :)
2. It fails :(
3. I download the zip file (from the Run page) with the generated debug files and find the fix :)
4. Space doesn't get filled up as zip files are kept only for the last 3 builds!
Any help would be greatly appreciated! Thank you! I'm happy to provide more information if needed ^^ I am currently trying to use a system groovy script to do this, but perhaps artifacts would be more appropriate? I really can't seem to find good documentation on this!
There are built in methods in Jenkins to allow this workflow:
you can archive any artifact (in that case the zip) as post build step
data retention strategy can be configured in the job via Discard old builds (Advanced).
in order to send out customized mails on build failure with embedded download link you should review Email Ext Plugin; it allows you to configure individual texts for e.g. build failures where you could add the link to downloading the artifact.

in jenkins, how to copy artifacts from another server?

I have another project from which I need to copy artifacts.
However the problem I have is that it's from another server. Is there a way to do so with the copy artifact or I'll have to go through code?
You can accomplish by either publishing your artifact and using either file transfer or secure shell.
Here is info to read upon:
Jenkins Secure Shell Plugin
Jenkins FTP Plugin
The only other possibility is to modify the ant or maven project config file.
Here is a More Reference along the same lines.
I used a wget to fetch the file in the end, with fixed paths.
This link can help for someone not used with wget.
Using wget to recursively fetch a directory with arbitrary files in it
For a long time I use this python script to download artifacts from Jenkins. It takes advantage of the JSON API layer available to any Jenkins job. The format of that API call is:
http://_YOUR_BUILD_HOST_/job/_JOBNAME_/lastSuccessfulBuild/api/json
Beware script depends on PyCurl.
Publish over ssh plugin can also be used for copying the files/artifacts from one server (local/linux) to another server. It has retries option also in case there is network issue and no. of retires and timeout also can be configured.

How to download a file from the jenkins job build folder

I have a jenkins server running, and for a job I need to download a file which is in the jobs/builds/buildname folder.
How to download that file from jenkins job?
If you would use the workspace as suggested by previous post, you can access it within a Pipeline:
sh "wget http://<servername:port>/job/<jobname>/ws/index.txt"
Or inside a script:
wget http://<servername:port>/job/<jobname>/ws/index.txt
Where index.txt is the file you want to download.
I rock a Unix based development machine and a Unix based Jenkins machine up in the cloud. This means I can use the SCP Command to download the remote file over an ssh connection. This is the anatomy of my scp commands:
scp -i <path/to/ssh.pem/file> <user>#<jenkins.remote.url>:<path/to/remote/file> <local/path/where/download/goes>
This works for directories too, for instance I use this to download backups generated by the ThinBackup Plugin
You had already been given the answer for getting the file from the workspace
http://<servername:port>/job/<jobname>/ws/filename.ext
Obviously replace stuff in <..> with values relevant to your setup, and make sure anonymous user has access to read from workspace, else you may have to login.
The only other files you could access are those that are archived from previous job runs.
http://<servername:port>/job/<jobname>/<buildnumber>/artifact/filename.ext
Where <buildnumber> is the build number you see in job build history, or one of the permalinks provided by Eldad (such as lastStableBuild). But this will only have access to archived artifacts.
You cannot arbitrarily access files from Jenkin's filesystem through the web interface... it wouldn't be very secure if it did let you.
The Jenkins job's build folder is meant for logging and plugins reports. You should not need to access it directly.
If you must, you can access it relative to the workspace: $WORKSPACE/../builds/$BUILD_ID/
You can also replace the $BUILD_ID with one of the links Jenkins creates:
lastFailedBuild
lastStableBuild
lastSuccessfulBuild
lastUnstableBuild
lastUnsuccessfulBuild
I hope this helps.
As others have pointed out this path should work, I like to highlight that the "ws" is a directory in Jenkins:
http://<servername:port>/job/<your job>/ws/<your file>
Download the Package lynx (Command line browser)
$ apt-get install lynx
or
$ yum install lynx
then use the command
# lynx http://<servername:port>/job/<jobname>/ws/file
The App Will Ask you to allow cookies and if there are authentication will direct you to login page like the browser.

Resources