how to keep process running after the stage is finished for declarative jenkins pipeline

♀尐吖头ヾ 提交于 2019-12-24 19:04:24

问题


pipeline {
  agent none
  stages {
   stage('Server') {
      agent{
          node {
            label "xxx"
            customWorkspace "/home/xxx/server"
          }
        }
      
      steps {
        sh 'node server.js &'
        //start server
      }
    }
   stage('RunCase') {
      agent{
          node {
            label 'clientServer'
            customWorkspace "/home/xxx/CITest"
          }
        }

      steps{
        sh 'start test'
        sh  'run case here'
      }
    }
  }

}

I create above Jenkins pipeline. What I want to do is:
1. start server at server node.
2. start test at test node.

However, I found the server process will be closed when second stage start. So how to keep server start until my second stage testing work is finished. I try to use &, still not working. It seems it will kill all process I started at first stage.


回答1:


One solution is to try to start the two stages in "parallel"-mode. For more informations see this two files: parallel-declarative-blog jenkins-pipeline-syntax. But be carefull, because it is not ensured, that the first stage starts before the second one starts. Maybe you need a waiting time for your tests. Here is an example Jenkinsfile:

pipeline {
agent none
stages {
    stage('Run Tests') {
        parallel {
            stage('Start Server') {
                steps {
                    sh 'node server.js &'
                }
            }
            stage('Run Tests) {
                steps {
                    sh  'run case here'
                }
            }
        }
    }
}
}

Another solution would be to start the node server in the background. For this you can try different tools, like nohup or pm2.



来源:https://stackoverflow.com/questions/47587521/how-to-keep-process-running-after-the-stage-is-finished-for-declarative-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!