How to customize file name of Jenkins archive artifact plugin post build action? - jenkins

Jenkins archive artifact plugin compress files into "archive.zip" file. It has always the same file name. Even more, Jenkins doesn't archive actually(there is no any "archive.zip" files in "builds" directories). Jenkins just map url
https://www.my-jenkins-server.com/jenkins/job/$job_name/$job_number/artifact/*zip*/archive.zip
and always return everything in job directory, those matches to pattern configured in post build action archive artifact plugin.
Problem is, that job itself generates ZIP archive, so I need to publish this archive under original name. It is important, since archive's name clarify owner of job, data inside, parameters used to run job. Let's say users ran job 10 times using different parameters and don't wait each job to finish before to run next. Later user will start download results and get
archive.zip
archive(1).zip
archive(2).zip
...
archive(10).zip
Now he needs to extract archives from those downloaded archives, to get 10 another archives with qualified names. Then delete those downloaded archive. After that, identify by qualified archive name those he needs actually and delete rest of then. Easy to make mistake here, delete or miss archive file.
Solutions for me are:
Publish generated by job archive under it's original name.
Generate my files and form file name of archive under with it should be served, skip zipping inside of job. Final step, pass this file name as parameter into archive artifact plugin post build action, so Jenkins will serve archive under special name configured by job itself.

The name of the zip file is determined from the directory that contains the artifacts (see Jenkins source).
Internally, the top-most artifact directory has the name archive, that's why you will always see archive.zip.
Conversely, this means that you can get a custom zip file xyz.zip by putting the artifacts in a (sub-)directory xyz.
There are no other options to change the name.

You can run any post-build script (shell/batch/powershell) after the archive step, and rename archive.zip to archive_${BUILD_NUMBER}.zip so that you can easily track of the archive by the last successful build number of the job. But to do this, first you need to clean the workspace to keep a track of the archive files based on the build number.

Related

Archive only the latest .png files in a katalon jenkins job

I need to archive any .png screenshots of the latest katalon test run in Jenkins as a post-build step of the same test run.
Using the "archive the artifacts" post-build action in Jenkins, I currently have the file path set to Reports/**/[test run name]/**/*.png where Reports is in the workspace directory. However this will just pull every .png file from the current and all previous test runs stored there, of which are kept stored in the workspace for a week before being cleaned out.
I've tried using the "Exclude" field but haven't been able to figure out a way to exclude older files with only being able to use a file path with wildcards.
Is there a way in Jenkins, using archive the artifacts or something else, to archive just the .png files generated by the same job without deleting all previously generated .png files?
From our experience, it's better to keep the artifacts archived with the job and not rely on files residing in the workspace together with a separate job to clean them. You can tell Jenkins to discard old builds (and artifacts) with something like this:
pipeline {
options {
buildDiscarder(logRotator(daysToKeepStr: '7', artifactDaysToKeepStr: '7'))
}
}
And any jobs (and their artifacts) will be cleaned after 7 days.
If you go with that path, you can safely remove any png files after archiving them as artifacts, and you won't need to find out which are new and which are old.
Alternatively, you can order your png-producing step to name the files starting with the job number (available as env.BUILD_NUMBER) and only archive the files starting with this number.
Finally, you can run find command with -name '*png' and -mtime predicate to produce the list of recently modified png files, and use that as an input to the archive step.

access jenkins build artifacts in postbuild-plugin

Is there any way to access the previously build artifacts in a post-build-plugin? If yes, how do I access them?
For example:
My build creates a .jar file as an artifact.
In my plugin, I would like to access that .jar file and send it to an external server. That server is going to evaluate the file and depending on the result, I'd like to mark the build as failed/unstable/successful.
All build artifacts are in the workspace, in the same path the previous step saved them, ie build/libs/foo.jar. If you click in workspace, you can find it there, and it would be available for any post-build-plugin if you dont move it or delete it. Post build steps are executed in the workspace so it should be accessible with ie ./build/libs/foo.jar

Download full workspace from Jenkins build

I am new to jenkins and I have tried downloading a zip archive of this workspace in jenkins, but I only get a part of it. Source folders like tensorflow or tools are not present inside the archive. Is this normal ?
If so, how do I get all of them inside a zip file ?
Use Archive Artifact plugin, to add your workspace into archive folder which will make it easily down-loadable.
But be aware that, an artifact in the Jenkins sense is the result of a build - the intended output of the build process.
A common convention is to put the result of a build into a build, target or bin directory.
The Jenkins archiver can use globs (target/*.jar) to easily pick up the right file even if you have a unique name per build.
putting a complete workspace into it will take lot of time.

Where is the copied artifact from the previous build saved

I have two jenkins jobs which are chained. The first job will run some testNG tests , in which we may get some failures. (A testng-failed.xml file will be generated that has all the tests that failed). In my second job, I need to run using that testng-failed.xml as my suite file. So I am using jenkins copy artifact plugin and saving the testng-failed.xml file. I am able to retrive it in the second job , where it says "Copied 1 artifact from "rerun_exp" build number 7".
But the problem is I dont know where it is saved to be used as my suite file.
You can easily check where the file is by looking in the workspace.
This can even be done directly from the Jenkins web UI, via the "Workspace" link in the sidebar of the job page.
Files matched by the "Artifacts to copy" field are copied to the same directory structure as the source build had.
You can use the "Flatten directories" option if you want the file(s) to be copied to the root of the build workspace. If you click the (?) help icon to the right of that checkbox, you can see documentation for this and for your original question.

Can a Jenkins build access the archived artifacts from itself?

I'm using Jenkins and have the "Archive the Artifacts" step at the end of my builds to archive them into a zip file.
Instead of using this step, I'd like to use a script to push the artifacts to a remote server at the end of the build. The server I'm pushing to uses a REST API / HTTP PUT request in a script to upload files.
Note that I'm looking to access the artifact created in the same build. So if I'm on build #5, I want the artifacts from build #5, not build #4.
Is there any way to access this zip file with a script, in the same build that it was created in?
I need to upload this zip remotely and don't want to create another job to do so.
You can install one of the "Publish Over..." plugins to upload your artifacts at the end of a build.
The goal of the Publish Over plugins is to provide a consistent set of
features and behaviours when sending build artifacts ... somewhere.
See also the full list of "upload" plugins for other methods of publishing your artifacts.
Like #Christopher said, you can use any of the Publish Over plugins on the Jenkins Plugins page to upload the artifact to any of the
If you want to access the archived zip file from within the build itself, you can use the following link to access it:
http://<server>/job/${JOB_NAME}/lastSuccessfulBuild/artifact/<artifact name w/folder>
For example:
server = myserver.com
job name = myproject
artifact = del/project.zip
Your URL would be:
http://myserver.com/job/myproject/lastSuccessfulBuild/artifact/del/project.zip
EDIT: Question was changed. In any case, this would work for accessing the artifact of the previous build in the current one.
There is no way that I have found to access the "Archive the Artifacts" package of the build that generates it. This step always occurs last in the build. Accessing the URL prior to the build ending (during the build via script for example) results in a blank zip file. To get around this limitation, I'm making a second linked build job to grab the zip and run my script to deploy it.

Resources