Interview Prep/CI/CD/Jenkins Pipelines and Agents

A stage requires a long-running process (a test server) that must be stopped even if tests fail.

IntermediateScenarioJenkins Pipelines and Agents
Answer

Situation read → Background process lifecycle inside a stage.

What I'd do →

  1. Start with sh 'nohup ./start-server.sh > server.log 2>&1 & echo $! > server.pid', wait for the port, run tests.
  2. Stop in post { always { sh 'kill $(cat server.pid) || true' } }.
  3. Better: run the server as a container started in docker.image(...).withRun { c -> ... }, which cleans up automatically.

Why → withRun ties resource lifetime to the block even on failure.

Interview tip → Note Jenkins' BUILD_ID process killer: set JENKINS_NODE_COOKIE=dontKillMe only when a process must outlive the build.

All CI/CD questions

More on Jenkins Pipelines and Agents