运行 在 Jenkins 管道中按顺序执行多个异步步骤的最佳方法

Best approach to run a number of asynchronous steps in sequence in a Jenkins pipeline

让我先简要描述一下我要实现的用例。

上下文

实施

问题

Should I implement a simple shell script that issues an HTTP request to one of my app REST endpoints in a loop and that completes as soon as the app sends a response? Should I invoke this script in the waitUntil step?

是的,这是一种合法的方法。

您也可以简单地将其折叠到运行测试的脚本中。为了使流水线脚本简短明了,并且更容易单独测试逻辑片段,请将该脚本存储在 SCM 中。假设它在 Bash 中(但 Python 或任何可以正常工作的东西):

while :
do
  if curl http://endpoint/
  then
    echo Up and running
    break
  else
    echo Still waiting
  fi
done
make test

然后您的流水线脚本可以读取类似

的内容
node {
  stage 'build'
  checkout scm
  sh 'mvn clean install'
  stage 'test'
  sh 'docker-compose up'
  try {
    sh './run-tests-when-ready'
  finally {
    sh 'docker-compose down'
  }
}

当然,如果您愿意,甚至可以将对 docker-compose 的调用放入此类外部脚本中。在 Bash 中可靠地进行清理很棘手(可以使用 trap EXIT '…'),在真实语言中更容易。

我最终在我的 Jenkinsfile 中做了这样的事情:

stage 'Validation'
  dir("microservice") {
  sh "docker-compose down"
  sh "docker-compose up &"
}

waitUntil {
    def appIsReady = false
    try {
        echo "Checking Spring Boot status page via ${GAMEDOCK_URL}"
        sh "set +e; curl -f -sL -w \"%{http_code}\n\" ${GAMEDOCK_URL} -o /dev/null; echo $? > springBootAppStatus; return 0"
        def status = readFile('springBootAppStatus').trim()
        echo 'status: ' + status
        appIsReady = (status == '0')
    } catch (e) {
        echo 'exception: ' + e
        appIsReady = false
    }
    echo 'return appIsReady'
    return appIsReady == true

}

   echo "application is ready"