简体   繁体   English

如何在 Jenkins 管道中定义一个使用 curl 检查服务器是否启动的测试阶段?

[英]How to define a test stage in Jenkins pipeline which makes use of curl to check if the server is up or not?

I am using latest Jenkins in my Linux box.我在我的 Linux 机器中使用最新的 Jenkins。

I would like to implement a Test stage like below:我想实现如下的Test阶段:

pipeline {
    agent any
    stages {
        stage('Test') { 
            HTTP_CODE = sh (
                        script: 'echo $(curl --write-out \\"%{http_code}\\" --silent --output /dev/null http://localhost/)',
                        returnStdout: true
                    ).trim()
        }
    }
}

In this stage, I want to execute a bash script to check whether the web server is up or not.在这个阶段,我想执行一个 bash 脚本来检查 Web 服务器是否已启动。 HTTP_CODE variable will have the value 200 if everything is fine, and if there is any other value, then it can be treated as error.如果一切正常, HTTP_CODE变量的值为200 ,如果有任何其他值,则可以将其视为错误。

How can I implement this logic as a testing stage in my Jenkins pipeline?如何在我的 Jenkins 管道中将此逻辑实现为测试阶段?

Thanks.谢谢。

You should update your pipeline as follow:您应该按如下方式更新您的管道:

pipeline {
    agent any
    stages {
        stage('Test') { 
            HTTP_CODE = sh (
                        script: 'echo $(curl --write-out \\"%{http_code}\\" --silent --output /dev/null http://localhost/)',
                        returnStdout: true
                    ).trim()

            if ('200' != HTTP_CODE) {
                currentBuild.result = "FAILURE"
                error('Test stage failed!)
            }
        }
    }
}

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM