Search code examples
javajenkins-pipelinejenkins-cli

How to run a Java service in a Jenkins pipeline?


I have initialized a Jenkins pipeline with the following script. The pipeline reported a successful run, but the application has no response at all. Please help me.

This is my pipeline script

pipeline {
    agent any
    
    stages {
        stage('Checkout') {
             steps {
                script {
                   git credentialsId: '3',
                   url: '[email protected]:myusername/myproject.git'
                   sh "git checkout master"
                  }
               }
        }
        
        stage('Build') {
            steps {
                sh 'mvn clean package'
                sh 'mvn package -Dmaven.test.skip=true'
                sh 'mv target/my-project-0.0.1.jar target/my-project.jar'
            }
        }
    
        stage('Deploy') {
            steps {
                sh 'java -jar target/my-project.jar > /dev/null 2>&1 &'
            }
        }
    }
}

This is pipeline last line log:

[Pipeline] sh
+ mv target/my-project-0.0.1.jar target/my-project.jar
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ java -jar target/my-project.jar
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Solution

  • Jenkins auto-kills the process after deploy. I just add JENKINS_NODE_COOKIE=dontKillMe to the script.

    Full script:

    sh 'export JENKINS_NODE_COOKIE=dontKillMe && java -jar target/my-project.jar &'