How to get build workspace path using jenkins remote api? - jenkins

Is it possible to get current build's workspace path using jenkins remote api? I can get a build details based on build number with api/json, but it doesn't return the workspace details.
curl http://jenkinsServer:8080/job/testing/1/api/json

As far as I know you can't do that using Jenkins remote API. However, you can probably infer the workspace from your project name. If, say, Jenkins base workspace is /var/lib/jenkins/workspace (which is the case with default install on Unix), the workspace for your project should simply be :
/var/lib/jenkins/workspace/your-project
That could be sufficient for your needs, but workspace may vary, in particular if you are checking out some other repo inside your pipeline (or loading some other pipeline script from your base script), you could notice folders such as :
/var/lib/jenkins/workspace/your-project#tmp
/var/lib/jenkins/workspace/your-project#script

Related

How can I explore Jenkins workspace?

I want to scan file stored in my Jenkins project workspace.
for example, when I make a project named my_project, I will get report pattern from Jenkins like **/report.xml. and to find report.xml file, I will explore in my own plugin.
since when I print pwd on jenkins console, it prints where my plugin written, not jenkins workspace. so I need to know how to explore jenkins workspace in my plugin.
thanks!
If you're asking where your jenkins workspace is, it is in the workspace folder where you can find all your projects. It should be relative to your plugin folder: ../workspace. You can try to change directory using the relative path. Hope this helps.

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.
I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.
After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.
In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:
For each job on Jenkins, Jenkins allocates a unique "workspace directory."
This is the directory where the code is checked out and builds happen.
Normally you should let Jenkins allocate and clean up workspace directories,
but in several situations this is problematic, and in such case, this option
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be
built on a specific location. While there's no doubt that such a build is
not ideal, this option allows you to get going in such a situation.
...
And your custom directory path would look like this:
workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123
where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".
There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.
Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.
Hope that info helps you make a decision that fits your needs best.
I also use "General/Advanced/Use custom workspace" (as in #pabloduo's answer) on a Windows machine with something like:
C:\${JOB_NAME}\${BUILD_NUMBER}
Just wanted to add a solution for getting rid of the build job's workspaces.
I use Groovy Events Listener Plugin for this.
Using the plug-in's standard configuration I just use the following Groovy script:
if (event == Event.JOB_DELETED){
new File(env.WORKSPACE).deleteDir()
}
And now the custom workspace is deleted when the build job is deleted.
Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

Appropriate directory to place additional files for jenkins builds?

I have an API key, for example, that is not stored in a git repo that I'd like to copy from a directory on a build machine. Where is the appropriate place to place these so that the jenkins user can still access them during a build. The host OS is ubuntu server and jenkins home is located at path /var/lib/jenkins.
Should these be placed and copied from /opt? from /var/lib/jenkins? from /var/lib/jenkins/userContent? What is accessible during a build?
Sounds like this should be a Jenkins build parameter.
Use a parameterized Jenkins build and specify the API key as a string param?

How to send build package back to teamcity as build artifact when building on agent/client side?

Our teamcity server uses Windows OS, so build process checkouts git source to agent Macs, and so build result package (*.ipa package) remains on agent. How to send this *.ipa build package to teamcity server, so it would appear as artifact?
At first, I have assumed teamcity should grab build result - ipa package by itself, so I'v added "OurProject/build/ipa/*.ipa" in "Artifact paths" settings in General settings of build configuration, but no artifacts appear under build result in teamcity website. Probably missing something obvious here :)
Artifact pattern like:
**/*.ipa
should publish all these files.
See also: http://confluence.jetbrains.com/display/TCD8/Configuring+General+Settings#ConfiguringGeneralSettings-artifactPaths
By default teamcity searches for artifacts inside workDir/yourCheckoutedSourceDir. My build dir is located at the root of teamcity folder on agent side and I'm using BUILD_DIR environment variable in custom scripts to set where build result should be saved. So and I'v used this environment variable in custom scripts using this format:
${BUILD_DIR}
My problem was teamcity does not recognize such format when using inside artifact paths field, so you need to use this format instead:
%env.BUILD_DIR%
If build dir is set to outside of checkout dir then the other solution would be to use relative paths like ../../Builds, but the first solution is more clearer in case the name of build dir would change.

Hudson/Jenkins PMD Configuration

I am new to Jenkins and just started configuring it. This is what i have done till now:
Installed and configured Jenkins to display the home page. Added PMD plugin.
Set the HUDSON_HOME to a specific directory > C:\Work\Jenkins
Configured a test build to run a simple do-nothing ant script. It runs successfully
Written an independent pmdbuild.xml to run checks on a set of files in C:\myview (I am using clearcase). This xml also copies the output pmd_results.xml to the workspace directory in $HUDSON_HOME/[job-name]/workspace
Now I added the pmdbuild.xml as a step in my primary build. So my build has 2 steps:
a. Run a simple script, do-nothing.
b. Run pmdbuild.xml which generate pmd_results.xml and place it in $HUDSON_HOME/[job-name]/workspace (HARD-CODED as Jenkins PMD plugin expects the file there)
Jenkins picks up the pmd_results.xml automatically with the plugin and displays warnings and everything.
Now the problem:
If I click on a filename in the PMD results, it gives a filenotfound exception as it is looking for the source file in $HUDSON_HOME/[job-name]/workspace.
My java code files are placed in C:\myview (a clearcase snapshot view)
My question is, do I need all my code files to be present inside $HUDSON_HOME/[job-name]/workspace ?? Meaning can't I tell Jenkins to look for the PMD input files in C:\myview or any other directory instead of $HUDSON_HOME/[job-name]/workspace ??
Sorry for the extremely long description.
Jenkins expects that all the code is in the workspace. Usually Jenkins is used to check out a copy of the code into the workspace, and then runs all build steps on the Sources in the Workspace.
Might seem restraining at first, but it saves you a lot of trouble if you need to move Jenkins to another server, or create a slave instance.
So I would suggest you let Jenkins check out your code (there should be a clearcase plugin) into the workspace, and run the analysis on the checked out code.
If there are compelling reasons why your code has to stay where it is (C:\myview in your case) you can still set the workspace of your build to that directory (find this in the job configuration page, you need to click on the 'extended' button to see the option).

Resources