How to upload a generic file into a Jenkins job? - jenkins

I am trying to find a way to prompt the user to select and upload a generic file from a local machine to a Jenkins job prior to build. The input file that user is going to upload is not necessarily a text or a property file.
I am specifically trying to get the user to "select" their desired file - browse to their file ; the user should not pass the file's path.
Thanks

Use the File Parameter:
File parameter allows a build to accept a file, to be submitted by the user when scheduling a new build. The file will be placed inside the workspace at the known location after the check-out/update is done, so that your build scripts can use this file.
If you need to verify the file has a certain extension, you would have to do that with a script as part of your job, and fail the job is extension/content-type does not match what you need.

This is kind of annoying to handle when you don't know what the file name will be or need to change its name before it reaches its destination. You kind of need to perform a hack. This is how I do it:
Use the "File parameter" parameter to upload your file
Use the OS-specific script to rename the file from whatever you named your File Parameter to whatever you want it to be, e.g., if my File Parameter had the File location value of file_name instead of an actual relative file-path, I'd then do something like this for say, Windows inside a Build-Step for "Execute Windows Batch Command":
move .\file_name .\%file_name%
And then just use ArtifactDeployer to copy everything there to your desired location.
ps: this won't remove digital signatures, so the move-operation should be considered mostly safe.

The use of the Jenkins File Parameter will not work for Jenkins pipelines. It's ridiculous that they don't disable that kind of build parameter for pipelines. It's even more ridiculous that they don't at the very least, identify this SEVERE limitation in the help documentation for that parameter.
It would have saved me a couple hours trying to figure out why it would not work in my pipeline.
Refer to this feature request for more details: https://issues.jenkins-ci.org/browse/JENKINS-27413

Related

Jenkins: define custom mime-type for viewing archived artifacts

I am saving certain log-files during a Jenkins build as archived artefacts within jenkins (e.g. via Jenkinsfile's "archiveArtifacts" command). These files are plain-text-files but unfortunately don't end in ".log". The web.xml of the jenkins.war seems to by default use the octet-stream mime-type for unknown extensions. This leads to the "problem" that i cannot open these logs in the browser easily; rather, chrome downloads the file.
I would like to be able to open the file in the browser for convenience.
Is there a way or plugin to configure/register custom mime-types?
I already tried to modify the web.xml just for a quick hack but did not continue when i noticed that the jars are signed and jenkins does not start when the war-file is modified. So, i rather do not want to go this route; also because i will have to patch the war-file after each and every jenkins-update.
Is there a smarter way? What is not an option for me is to rename my log-files to have an extension like ".log" (which Jenkins will serves as text/plain).
Thanks,
Daniel

Is there a way to overwrite a value contained within a config.properties file via Jenkins?

Is there a way to overwrite a value contained within a config.properties file via Jenkins?
I have the following config.properties file contained within my automation framework:
browser=chrome
url=http//www.example.com
If the value of chrome get changed to firefox then all tests will now execute within firefox browser.
I can manually change this value by directly accessing the config.properties file but can the value get altered via jenkins?
I use the Pipeline Utility Steps plugin to read properties files, and it looks like it can write a few other types of files, but not properties files.
It seems to me that you want to make this change in this file so you can run some tests first in one browser, then in another. If this is the case, I think a better way to handle this is to try to get your tests to point to different files. This is a little cleaner, and allows things like parallel execution and when you find that another thing needs to change in the future, you won't be writing so many things to the file in a script, which gets a little error prone.
If you can't make your tests execute against a different properties file, you could have a copy of each file you need, and then copy them to them appropriate filename to execute your tests.
But maybe I made poor assumptions as to your setup here. ;)
Yes.
You can create a build parameter as $browser to accept the value say "firefox" and using sed inside "execute shell", replace the value in config.properties.
Once done, execute your scripts.
This is just overview as you have not posted details about your config.properties file, its location, if you are using Jenkins jobs or jenkinsfile/pipeline etc.

Nunit results XML file location on jenkins master

Does anyone know where the Nunit results xml file stored on the Jenkins master (if any). We run tests on a slave and the xml file is replaced after every job run. But we need the xml file for some reasons from previous jobs. I know there is a console output and published results but we specifically need the xml.
Default state is to output to the "current directory". Where is the current directory? Look at the jenkins console output. For your stage using nunit, there is a section called Run Settings and a subsection called WorkDirectory. This directory will contain the most recent xml output. This file gets overwritten each time.
You can explicitly define another location in whatever stage this gets called in. For instance bat \path\to\nunit3-console.exe \path\to\project.dll --work=\a\different\path. You will have an xml written to \a\different\path\TestResults.xml
You should check out the command link docs for Nunit. They include options that expressly defining the output location.
http://www.nunit.org/index.php?p=consoleCommandLine&r=2.6.3
We used the
/xml:C:\testresults\testResults.xml
option.

aliasing jenkins artifact URLs

Jenkins artifact URLs allow abstracting the "last successful build", so that instead of
http://myjenkins.local/job/MyJob/38/artifact/build/MyJob-v1.0.1.zip
we can say
http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/MyJob-v1.0.1.zip
Is it possible to abstract this further? My artifacts have their version number in their filename, which can change from build to build. Ideally I'd like to have a some kind of "alias" URL that looks like this:
http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/MyJob-latest.zip
MyJob-latest.zip would then resolve to MyJob-v1.0.1.zip.
If Jenkins itself can't do this, perhaps there's a plugin?
Never seen any such plugin, but Jenkins already has a similar functionality built-in.
You can use /*zip*/filename.zip in your artifact path, where filename is anything you choose. It will take all found artifacts, and download them in a zipfile (you may end up with a zip inside a zip, if your artifact is already a zip file)
In your case, it will be:
http://myjenkins.local/job/MyJob/lastSuccessfulBuild/artifact/build/*zip*/MyJob-latest.zip
This will get you the contents of /artifact/build/ returned in zipped archive with name MyJob-latest.zip. Note that if you have more than just that zip file in that directory, other files will be returned too.
You can use wildcards in the path. A single * for a regular wildcard, a double ** for skipping any number of preceding directories.
For example, to get any file that starts with MyJob, ends with .zip, and to look for it in any artifact directory, you could use:
/lastSuccessfulBuild/artifact/**/MyJob*.zip/*zip*/MyJob-latest.zip
Edit:
You cannot do something like this without some form of a container (a zip in this case). With the container, you are telling the system:
Get any possible [undetermined count] wildcard match and place into this container, then give me the container. This is logical and possible, as there is only one single container, whether it is empty or not.
But you cannot tell the system:
Give me a link to a specific single file, but I don't know which one or how many there are. The system can't guarantee that your wildcards will match one, more than one, or none. This is simply impossible from a logic perspective.
If you need it for some script automation, you can unzip the first level zip and be still left with your desired zipped artifact.
If you need to provide this link to someone else, you need an alternative solution.
Alternative 1:
After your build is complete, execute a post-build step that will take your artifact, and rename it to MyJob-latest.zip, but you are losing versioning in the filename. You can also chose to copy instead of rename, but you end up with double the space used for storing these artifacts.
Alternative 2 (recommended):
As a post-build action, upload the artifact to a central repository. It can be Artifactory, or even plain SVN. When you upload it, it will be renamed MyJob-latest.zip and the previous one would be overwritten. This way you have a static link that will always have the latest artifact from lastSuccessfulBuild
There is actually a plugin to assign aliases to build you've run, and I have found it pretty handy: the Build Alias Setter Plugin.
You can use it for instance to assign an alias in the form of your own version number for a build, instead (or rather in addition) to the internal Jenkins-assigned build number.
I found that it is usually most practical to use it in conjunction with the EnvInject plugin (or your favorite variant): you would export an env variable (e.g. MY_VAR=xyz) with a value to the target version or moniker, and then use the form ${ENV,var="myvar"} in the "Token Macro alias" config that the plugin provides in your job config.
You can also use it to assign aliases in the form of "lastSuccesful" if you have such a need, which allows you to distinguish between different types of successful (or other state) builds.
Wait thee's more! You can also use the /*zip*/ trick in conjunction with the alias setter as well.

Get result of a build step in Hudson/Jenkins to re-use it in another one

My question may be silly but I've been trying several ways and I still can't do what I want, i.e.:
use the scp target of Ant to target a remote machine and execute
a script there
this script creates a dynamic list of files
get this list of files (only their names) back in Hudson to use it in the next build step (another scp from Ant)
I tried to use environment variables but they are interpreted by Hudson so I'm stuck here...
Globally my question would be: how to get a result from an Ant build step ?
Thanks for your ideas,
Emmanuel
You may find File parameter useful. This allows you to create an input file, pass it to build. You may need to write script/ant script to process the file though.
In the long term you may evaluate a Hudson farm. This will allow to create tasks that span multiple machines , pass results around. (https://wiki.jenkins-ci.org/display/JENKINS/Plugins)
You can get the ID(s) of the job that triggered your job via the API and fetch their status.

Resources