Appropriate directory to place additional files for jenkins builds? - jenkins

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?

Related

How to get build workspace path using jenkins remote api?

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

Where are Jenkins artifacts located?

I added the Archive Artifacts post-build option to my project. I can see the artifacts from the web browser interface, but I cannot find them in the filesystem.
Where are they located?
It is being archived on the master server (even if the build were on a slave) in the following folder:
$JENKINS_HOME/jobs/<job>/builds/<build>/archive
But you can configure a different location using the 'Advanced' setting of the job (where you can set a different workspace folder) or using plugins that are made for this purpose such as Copy Artifact Plugin
Just another couple of tips...
You can find jenkins home by going to the environment variables page in the job build jenkins page.
In my case JENKINS_HOME turned out to be /var/lib/jenkins
Found artifacts in:
/var/lib/jenkins/jobs/<my-job-name>-build/lastStable/archive/target
/var/lib/jenkins/jobs/<my-job-name>-build/lastSsuccessful/archive/target
as well as
/var/lib/jenkins/jobs/<my-job-name>-build/builds/8/archive/target
Path is : $JENKINS_HOME/jobs//jobs//branches//builds/$BUILD_NUMBER/archive/
You have to extract branch name, job name and repository name from JOB_NAME environment variable.

How to pass job-specific files to Jenkins as part of job configuration?

I have a jenkins job that pulls source code from GitHub public repo. I need to pass some files such as instance-specific configuration files containing secrets to the job and merge with source code prior to running build because these files are obviously inappropriate to be put in public SCM. The Jenkins instance is a multi-tenanted shared service.
The config files don't change often so I don't want to implement using file parameter which forces user manually input the file on every run. Another reason file parameter doesn't work is some builds are triggered automatically by SCM.
I don't want to use Config File Provider Plugin either, because the plugin requires jenkins admin access but I want users with job-level privileges manage the files themselves.
Ideally the uploaded files are saved alongside with job config.xml instead of in workspace, because I would like to delete workspace after each build. I can write scripts to copy the files from job config folder to workspace.
Are there any solutions available? Thanks.
If the "special" files are being placed in a folder with say some access privileges to it, couldn't you either run a Pre-SCM-Buildstep to move the files with shell commands, or introduce a regular build step (i.e. after the SCM stuff and before the other build steps) that would also use shell commands to move files?

Prevent Publish to SCP Repository from Maintaining Original Folder Structure

I'm running Jenkins v1.581 and Publish artifacts to SCP Repository v1.8.
I am able to successfully copy my artifacts over SCP to a destination directory; so I know that server names, authentication, etc... are all correct.
My configuration looks something like this:
Source: tmp/distribution/target/deploy/opt/**
Destination: opt
When Jenkins puts the file over SCP it ends up in a directory structure of opt/tmp/distribution/target/deploy/opt/rest_of_path. It looks like it's keeping the original path of the file as it existed as an artifact and appending it to the destination path. This causes my artifacts to be deployed to an unexpected path.
My expectation is that they would end up as opt/rest_of_path. How do I fix this?
I replaced the Publish to SCP Repository with the Send build artifacts over SSH plugin. This plugin has an option for Remove prefix which does exactly what I wanted.

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