如何在 Jenkins 管道中以编程方式和自动化方式停止 Appium 服务器

How to stop Appium server programmatically and automated in Jenkins Pipeline

Appium 服务器有时在您尝试关闭它时不会停止,服务器和端口仍然永远挂起并且 Appium CLI 没有内置命令来停止服务器,这使得以编程方式管理变得更加困难

假设您想通过 CI/CD 管道中的自动化流程以编程方式管理它 比如詹金斯,这可能是一个非常痛苦的故事

appium
or
appium & (as background process)

启动Appium服务器的命令,只有当你终止它时才会停止,但有时它不会停止

我在 Whosebug 上搜索了很长时间的答案,其中 none 直接回答了我的问题

到目前为止,似乎可行的是您必须在 shell 中使用特定进程 ID

手动终止服务器进程

为了让它简单地与管道一起工作,我们可以有一个简短版本的命令

 kill $(lsof -t -i :4723)
 kill $(lsof -t -i :${APPIUM_PORT}) [In Jenkinsfile]

其中APPIUM_PORT是你的Appium端口,默认端口是4723
lsof 命令应该在类 Unix 系统中工作,例如苹果操作系统,Linux

lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them

通过运行这个命令,它应该return进程的ID运行在那个特定端口上用于kill信号

准备实施 在你的 Jenkinsfile

末尾添加这一步
    post{
        always{
            ...
            echo "Stop appium server"
            sh "kill $(lsof -t -i :${APPIUM_PORT})"
        }
        success{
            ...
        }
        failure{
            ...
        }
        cleanup{
            ...
        }
    }

它应该会杀死挂起的 Appium 服务器进程,您可以使用相同的端口再次启动新的 Appium 服务器!

要查看更多详细信息,我已经在此处发布了博客
How to start/stop Appium server in Jenkins Pipeline

希望对你有帮助