Run executable after jenkins pipeline build - jenkins

I have a console application that needs to be permanently running on the same machine on which I run Jenkins. After I build and publish the .exe file I need to run it but if I try to use bat "pathtofile\\filename.exe", the pipeline will wait for the process to finish, but it never will because the process is a socket server which keeps running and listening.
Is there a way to Run a Fire-and-Forget command to start the .exe?

Have you considered creating a Windows Service? If this is not an option, I would suggest using the /B flag of the start command. For example,
start /B yourapp.exe
will execute your app in the background, somewhat similar to Linux's yourapp & command form.
To check out all the options of the start command, you can type help start in a Command Line Window.

Jenkins gives guidance and examples on this for freestyle projects (but the principle should be the same for pipeline), see:
https://wiki.jenkins.io/display/JENKINS/Spawning+processes+from+build
For Windows, there are a number of options - using the at command (runs a job at specific time); creating a wrapper script to run your bat file; or creating a scheduled task (thus similar to the at command). I've used the schedule task approach (again for freestyle builds rather than pipeline).

Related

How to trigger one task present in Release Definition on multiple servers in Agent Phase?

I am using TFS 2017 update 3, In TFS 2017, I have a Release Definition in which i have a command line task thorugh which I call the cli of an automation application by passing it an arguments like below
"C:\Program Files (x86)\Auto-2019-Q1\Auto-Cli" -scriptName "Driver_Smoke.xlsx" -scriptLocation "C:\Next_Gen"
and it does run the application successfully but now i have to run the same task on multiple servers where i have already configured build agents so that the automation can be executed on multiple servers at the same time.
For this, I have added an agent phase in the release definition and have added the agent name in the demands but it works only on one server and doesn't works simultaneously on the other server.
However, I can add another agent phase option and then give the agent name of another server but then i'll have to wait for the previous agent phase task to be completed. This would take a lot of time.
I tried giving two agent.name in demands but it doesn't work like that. so, is it possible to run a task on multiple servers at the same time in release definition?
I have found one workaround for this using the existing tasks in TFS Release definition. What I did is,
I have configured two deployment agent on each server.
Copied the above mentioned cli commands into the notepad and created a batch file of it.
Created a Task in task scheduler to execute the batch file whenever the build agent service is started.
In the Release definition, once the deployment of the package is done and after which we need to run the automation using the tool, I added a Run on agent task in RD and associated one of the build agent name where i needed to run the automation tool and under the Run on agent task I added 3 tasks i.e. one to start the service and two task to stop the service, you can repeat the same set of task for multiple servers where you need to run the automation tool thorugh CI.
So, here when the deployment is done the Run on Agent task gets started and it first stops the service and then it start the service due to which the task created in tasks scheduler get triggered and the tool gets executed and then you can stop the service as you no longer need the service to run.
By using this process, i no longer need to wait for a long time except couple of seconds in order to run the automation tool on multiple servers simultaneously.

How to reboot Jenkins node using shell in Groovy

I am writing a Groovy script to perform an automatic reboot of Windows servers. In the script, I am first taking the nodes offline, then checking to see if there are any builds, if there aren't, then perform a restart.
I wanted to use the safeRestart() method but it doesn't support the import statement I am using when looping through the nodes. I have seen an execute() method which basically executes a shell line of code in groovy.
How would I execute a restart of the Windows computers using execute()?
Not sure if this will answer your question directly, but will point in the right direction ...
You can leverage this S/O question: Run a remote command on all Jenkins slaves via Masters's script console or this Gist: run_command_on_all_slaves.groovy
btw: Jenkins API does seem to support running a script directly on the server (Computer).
Your actual command should be shutdown /r`
I don't believe you can do this unless the Node is on-line. Disconnecting the node stops the Jenkins slave process, then there's nothing running on the node, so not sure what control you'd have. Instead you want to block the queue and let the existing jobs finish:
Jenkins.instance.getNode('Node-Name').toComputer().setAcceptingTasks(false)
and check:
Jenkins.instance.getNode('Node-Name').toComputer().countBusy() == 0
Then run your (work on server) restart command
When the server is available again, launch the node and open the queue.
Jenkins.instance.getNode('Node-Name').getComputer().launch()
Jenkins.instance.getNode('Node-Name').getComputer().setAcceptingTasks(true)
Hope that helps.

JMeter: Stoptest.sh/Shutdown.sh doesn't work from Jenkins

I am running some performance tests from Jenkins. I do have two Windows machines with JMeter and I can configure from Jenkins which one to use. Everything works as expected here.
My issue: I did create another job for Stop/Shutdown the tests in case something goes wrong and you have a big run time. Whenever I try to summon Stoptest.sh/Shutdown.sh on the Windows machine that run tests, nothing happens.
How can I stop tests remotely? It has anything to do with the listening port? Thank you.
PS: Tests are ran using PSExec from Jenkin's Windows slave so there is no active CMD window on the screen.
Be aware that .sh is extension for Linux shell scripts, they cannot be executed by Windows command-line interpreter (CMD or Powershell) if you're running JMeter on Windows you need to go for shutdown.cmd or stoptest.cmd instead
There is also AutoStop Listener plugin which can be used for conditional stopping of JMeter test basing on various criteria, it can be installed using JMeter Plugins Manager

How to auto-deploy Play Framework (2.4) application locally with Jenkins?

How can I auto-deploy Play Framework (2.4) application with Jenkins locally on the same server Jenkins is running? At some point we're going to set up a proper production environment separately and will probably implement test environment(s) in the same way but at this point I'd like to check out whether it is possible to set up a simple test environment to the same server Jenkins is running.
I have a Jenkins job running tests and it seems to work OK. Basically "Execute shell" running activator commands (that could be combined to one line).
./activator clean
./activator test
With Play 1 I've used play start & play stop for similar things. Trying activator start on my dev env, I get the message:
The start command is deprecated, and will be removed in a future version of Play.
To run Play in production mode, run 'stage' instead, and then execute the generated start script in target/universal/stage/bin.
To test your application using production mode, run 'testProd' instead.
So I evaluated two (incomplete) alternatives with "Execute shell" & stage:
Stage & run with nohup:
./activator clean
./activator stage
nohup target/universal/stage/bin/my-app -Dplay.evolutions.db.default.autoApply=true
-> application started OK but the Jenkins task did not stop.
Stage & run with nohup on background:
./activator clean
./activator stage
nohup target/universal/stage/bin/my-app -Dplay.evolutions.db.default.autoApply=true &
-> application seems to have started to some point but did not keep on running?
What would be the preferred (or even only working) way here?
For the particular case I ended up using Docker:
Installed Docker to the server
Created a Dockerfile based on play-docker-ci
Configured Jenkins to
build Docker image
stop existing container if running, remove existing container if exists
run Docker image
and this seems to work pretty nicely so far.
Jenkins kills all its child processes when build finishes to avoid memory leaks, thus there is no app running. The easiest way to setup jenkins with Playframework 2.4 is to use sbt tasks and sbt plugin. If you want to perform a release from jenkins, the best way would be to build debian package and install it using jenkins shell - no process would be killed. See release plugin.
I set this up by having Team City generate a startup.sh script that contains the command to start up the Play server as a background process:
nohup /pathToApp/bin/app_name -Dhttp.port=8180 &
Then the next build step just runs this shell script and starts it up. The nohup and & make this run as a background process and when the build server disconnects it will keep running. I cut out a lot of extra stuff from the startup script for clarity sake, but you can just add whatever startup parameters you want to use for your application.

How do I run, test, and terminate an HTTP server using Jenkins?

I'm working on a team that is building a RESTful HTTP service. We're having trouble with setting up a Jenkins CI job which will build the service, run it in the background, execute some tests, and then terminate the servers.
Specifics
The server is built in Node.js using the hapi framework and has some unit tests written in mocha.
The tests are written in Java using Maven. (Why not node.js-based tests? Because our testing dept. has invested time in creating a Java-based REST-testing framework.)
The build should fail if the node-based unit tests fail or if the java tests fail.
Our Jenkins box is run by a support team elsewhere in the company; our builds execute on a Linux slave.
Current Attempt
We've got something that kind-of works right now, but it's unreliable. We use 3 build steps:
The first build step is an Execute Shell step with the following commands:
npm install
npm test
node server.js ./test-config.json &
Second we do a Invoke Maven 3 step that points to the test pom.xml.
And third we run Invoke Standalone Sonar Analysis to do static code analysis.
This mostly works, but we depend on Jenkins' ProcessTreeKiller to stop the services once the job completes. We always get the warnings stating: Process leaked file descriptors. See
http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+buildfor
more information
Unfortunately, we've had cases where the service is terminated too soon (before the tests complete) or where the service doesn't get terminated at all (causing subsequent builds to fail because the port is already in use).
So we need something more reliable.
Failed Attempt
We tried setting up a single shell script which handled starting the service, running maven, killing the service, then outputting an exit code. But this didn't work out because the mvn command wasn't available on the command-line. Our Jenkins has multiple maven versions available (and jdks too) and I don't know where they live on the slaves or how to get at them without using the Invoke Maven 3 build step.
Ideas
We've toyed around with some ideas to solve this problem, but are hoping to get some guidance from others that may have solved similar problems with Jenkins.
Have the service self-terminate after some period of time. Problem is figuring out how long to let them run.
Add a build step to kill the services after we're done. Problem is that if the maven execution fails, subsequent steps won't run. (And if we tell maven to ignore test failures, then the build doesn't show as broken if they fail.)
Try killing any existing service process as the first and last steps of the build. Problem is that other teams also use these Jenkins slaves so we need to make sure that the service is terminated when we're done with our build.
Start and stop the node.js services via Maven doing something like this blog suggests. Problem is that we don't know if Jenkins will identify the spawned background task as a "leaked file descriptor" and kill it before we're done testing.
It would be nice if Jenkins had a "Post-build action" that let you run a clean-up script. Or if it had a "Execute background process" build step which would kill the background items at the end of the build. But I can't find anything like that.
Has anyone managed to get Jenkins to do anything remotely like this?
Some brainstorming:
You can turn off Jenkins ProcessTreeKiller, either globally or per invocation. I am not sure why that is not an option for you.
In response to #2, several options:
Post-build actions get executed regardless if build steps had failed or not. This would be a great way to trigger a "service cleanup" task that will run regardless of the build state.
You can setup any build step as post-build action, using Any Build Step plugin, or you can use Post Build Tasks plugin, the latter even gives options to define triggering criteria.
You can change the build state, based on RegEx criteria using Text-finder plugin
You can setup Conditional Build Steps. The "condition" could even be a result of some script execution

Resources