'How to stop build from executing from the pipeline script?

everyone! Sorry if the question is stupid, I'm just learning how to deal with Jenkins :) For the testing task I'm doing I need to start server, start Tomcat, then perform some actions, after which I need to stop tomcat and server from running to continue with another task. Otherwise, they run forever. That's why I need to kill build job from the same pipeline that started it. Can anyone advise how can I do this?

parallel ("Start tomcat" : {
    sleep(10)
    build job: 'tomcat_master'


Solution 1:[1]

I honestly think what you are trying to achieve the way you are trying to achieve it will be hard and unstable, or not achievable. And as a general rule, I would strongly advise you not to call jobs from jobs.

If you need to reuse steps between pipelines, you should think about making a jenkins shared library https://jenkins.io/doc/book/pipeline/shared-libraries/ there are lots of examples of libraries on github. It can be scary at first to try a new thing but I promise it is simple to do.

If you do not fill comfortable with creating a jenkins library, a good way is to factorize your steps as shell scripts, version them and fetch them at the beginning of your pipeline. You could have for instance one script that starts the tomcat server in background and store its PID, another one who kills the process based on the PID you stored

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fredericrous