Using ssh credentials in post-build of Jenkins matrix job - jenkins

We have a Jenkins matrix job with "SSH Agent" enabled in "Build Environment" with SSH credentials and a post-build action of "Execute Scripts On Matrix" with a shell command that runs ssh expecting to use the credentials stored by ssh-agent.
We recently upgraded from Jenkins v2.249.3 to v2.263.1 (and potentially upgraded some plugins at the same time, though I don't believe that we upgraded any of the ssh-related ones.) The aforementioned shell command now fails because it no longer has access to the ssh credentials it requires.
Comparing the build logs, we see a new call to ssh-agent -k in the Jenkins v2.263.1 parent job log immediately after the matrix children complete and before "[PostBuildScript] - [INFO] Executing post build scripts." that wasn't present with Jenkins v2.249.3.
It would appear that the agent is being killed before running the post-build operations by Jenkins v2.263.1 whereas it wasn't with Jenkins v2.249.3. I was unable to find a setting that controls this.
I entered JENKINS-64394 for this, but I wasn't really sure which components to label it with which I suspect mean that the right people haven't seen it. Does anyone here have any ideas?

Related

Jenkins freestyle project running as SYSTEM conversion to Pipeline project

When I run a freestyle project in Jenkins server, here are the first two lines from the logs:
Started by user {username}
Running as SYSTEM
Question: What is SYSTEM referring to here? Is it root user?
I am trying to write pipeline script for the above mentioned freestyle project in Jenkins and here is the first few lines of its log.
Started by user {username}
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
Question: I don't think it is Running as SYSTEM. It must be running as JENKINS user. I see some projects that I am trying to convert from free style to pipeline scripts are failing due to reasons like "acl not found" or "npm install issues". When I run free style projects, they run without any issues. Any idea how I can let the pipeline scripts Run as SYSTEM?
"By default, builds run as the internal SYSTEM user that has full permissions to run on any node, create or delete jobs, start and cancel other builds, etc." See https://www.jenkins.io/doc/book/security/build-authorization/
The "Started by" text is indicating the cause of the job being run, and does not indicate who the job is running as. Examples are "Started by user xxx" means that user xxx pressed a button or caused the event to trigger. Other examples "Push event to branch main", and "Started by timer".
Its a little odd that I only see the "Running as SYSTEM" when the job is submitted by a user from the Jenkins console. But by inserting a couple of debug prints in my automated jobs, it appears they run as the same user as the manually started jobs by default, SYSTEM.

Jenkins Ranorex plugin not executing test

I am new to Jenkins. I have master and slave configuration done and I have installed Jenkins on my master and want to run my ranorex test on my slave machine. All files needed for running ranorex scripts are present on my slave. When the job from the master runs, it gives error
[2019/11/18 17:04:51.686][Debug ][Logger]: Console logger starting.
[2019/11/18 17:04:51.845][Failure][TestSuite]: This operation requires an interactive window station
[2019/11/18 17:04:51.858][Debug ][Logger]: Console logger stopping.
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
i tried workarounds like going to Jenkins serve and selection option of user interaction.
If i only have bat file on slave of copying one folder to another and if i trigger job from master, it works. So master slave configuration does not have issue. Issue is running Ranorex GUI Test on Slave.
Jenkins needs to be running as an application not a service (which is default). Services cannot show anything with a GUI nor interact with anything with a GUI.
Disable the Jenkins service
Run Jenkins via Command Line java -jar jenkins.war
For a more detailed guide, check out the Infrastructure section of the Ranorex Jenkins integration blog.

Can I tell jenkins agent to execute job under a configured user?

I'm training a MacBook to be a jenkins agent for automation testing. On the MacBook, I logged in as user bob and installed everything needed (npm, yarn, xcode etc). Then I connected to a remote master using Java Web Start.
Because I launched the java thing in user bob, I assume jenkins pipeline jobs will also be under bob, which seems true - whoami command in pipeline echos bob. However, all dependencies cannot be found. If I echo $PATH I see a completely different result compared to echo $PATH directly on the MacBook.
I googled a bit, the problem might be that Jenkins create a new shell on everyjob. Can I tell jenkins pipeline to not to do this? I want the pipeline to use everything I configured manually for bob.
EDIT:
The main problem is jenkins pipeline's user has a different PATH. I ended up using environment directives to specify which path to use.
environment {
PATH = "/Users/bappo/.nvm/versions/node/v8.11.3/bin:$PATH"
}
What where you trying to configure? If you have parameters/configuration for your pipeline I strongly recommend to use pipeline parameters. They can either configured manually (including default values) or from the jenkinsfile.
example

Send command from TeamCity to run automation tests on Jenkins

Is there a way to add a build step in TeamCity which sends a request to Jenkins server, run some automation test scripts in Jenkins and sends back a response to Teamcity.
The idea basically is to automate the whole deployment process which also includes running of some automation tests created using python scripts (which will be done on Jenkins).
I am not sure if this is the best way of doing it but are there any better ways to achieve this? Also any hints on how to send command from Teamcity to Jenkins?
You can make an http request, as mentioned in the comment to start tests on Jenkins.
As for publishing the results bach to TeamCity, the possible solution might be:
after tests are done on Jenkins, publish the results that can be accessed externally (by TeamCity) and interpreted / reported by TeamCity (either in any of the supported formats), or manually, by the script, that will be run by TeamCity, using service messages
create a build configuration that will process the tests after the Jenkins build
set up a URL build trigger plugin, configure the trigger for the created build configuration. Point the trigger to the address where results are published. As soon as the content published is changed, the build will start and you will be able to download the tests results to TeamCity and process them
Got an easy to implement solution for the first part i.e. sending command from Teamcity to Jenkins
Using CURL:
Install/copy Curl to the Teamcity agent.
and then in your TC build configurations, create a new Command line build step similar to below (modify the parameters to your needs)
curl --user %jenkins_user%:%jenkins_pwd% -X POST http://%jenkins_instance_withport%/job/%jenkins_jobs_name%/buildWithParameters?token=%jenkins_token% --data "Build_Number=%build.number%"
e.g: curl --user admin:password -X POST http://jenkinssever:2123/job/test-build-image/buildWithParameters?token=rtbuild --data "Build_Number=1.2.0"
Here i could even pass the build number to Jenkins by using "-- data"
Do the below under Jenkins build configuration:
In Jenkins configuration:
In Jenkins configuration, update the below values:
"This project is parameterized"
Name: Build_Number
Default Value: 1.2.0
"Trigger builds remotely"
Authentication Token: rtbuild
Optional: for setting build number
"Set Build Name"
Build Name: #${Build_numuber}
done and you are good to go.please do let me know you if you have more questions.
the above is the implementation of the Initial comment
I think I found a way while trying to solve similar use-case, did it for batch files in Teamcity build steps. for Jenkins, we have to modify accordingly.
Also is there any specific reason for using Teamcity and Jenkins simultaneously, unless you are making use of already created Jenkins build?
Steps:
Get CLI based command for Jenkins:
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
you can achieve in two ways
Method 1:
As build step is in current build.
Create a build step before your current step and trigger the Jenkins build using CLI
Based on the return value of the Jenkins build step, next step will execute
Method 2:
create a new build with above CLI step and add a dependency in your primary build.
so whenever the primary build is started, it will start the dependent CLI jenkins build. and once the dependent build is completed, will return success/failure, based on that the primary build will start.
i haven't tested the CLI of Jenkins but as Teamcity supports the steps and dependencies structure, expecting this will work.
will keep posted once i implement it.

Execute Shell script from workspace on remote machine after build successful (Jenkins)

The scenario is - I have a job A which runs my ant script and packages the artifact's for me.
I am also using parametrized Triggered plug in to Trigger my "Job B" which will deploy my artifact on remote machine.
The job A is working fine and also Job B.
The tasks that i have to perform with Job B are
GIT checkout (which contains my deployment scripts) (successfully doning).
Copying artifacts from previous build to Remote machine. (successfully doing)
Run shell script on remote machine(script present in workspace folder )- Facing issues.
I browsed various plug ins for the same but no one is allowing me to run shell script after , "SCP to remote machine" which is present in Post build action.
I would like to execute the same sequence, however if you guys have any other suggestions please share.
Thanks in Advance.!
As part of Publish Over SSH Plugin, you can execute a script after the files had been copied over.
Under Post-build Actions
Add Send build artifacts over SSH
Select a preconfigured server (done in global configuration)
Select files to copy from workspace
Enter Exec command
If one of the files you copy is your shell script, you can enter it here as an "exec command"
To solve my query i used Jenkins SSH Plugin. This provides a configuration tab where i can add multiple hosts and after that used them in my job level configuration.
Link to Plugin
you get privilege to execute shell script on remote host as pre-build step or post build step.
updated the path of publish over ssh it worked for me

Resources