简体   繁体   English

How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application?

[英]How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application?

I have a Flask application that spins up my web application to a specific port (eg, 8080 ).我有一个 Flask 应用程序,它将我的 web 应用程序启动到特定端口(例如8080 )。 It launches by running python run.py .它通过运行python run.py启动。 I also have a Python file that runs Selenium end-to-end tests for the web app.我还有一个 Python 文件,它为 web 应用程序运行 Selenium 端到端测试。

This is part of a GitHub repo, and I have it hooked up so that whenever I push/commit to the GitHub repo, it initiates a Jenkins test.这是 GitHub 存储库的一部分,我将其连接起来,以便每当我推送/提交到 GitHub 存储库时,它都会启动 Jenkins 测试。 Jenkins is using my machine as the agent to run the commands. Jenkins 使用我的机器作为代理来运行命令。 I have a series of commands in a Pipeline on Jenkins to execute.我在 Jenkins 的管道中有一系列命令要执行。

Ideally, the order of commands should be:理想情况下,命令的顺序应该是:

  1. Spin up the Flask web application启动 Flask web 应用程序
  2. Run the Selenium tests运行 Selenium 测试
  3. Terminate the Flask web application终止 Flask web 应用程序

在 Jenkins 流水线中并行运行 Flask 和 Selenium

Now, my issue is this: I can use Jenkins to clone the repo to the local workspace on my computer (the agent), and then run the Python command to spin up my web app.现在,我的问题是:我可以使用 Jenkins 将 repo 克隆到我的计算机(代理)上的本地工作区,然后运行 Python 命令来启动我的 Z2567A5EC9705EB7AC2C984033E0618 应用程序。 I can use Jenkins to run a task in parallel to do the Selenium tests on the port used by the web app.我可以使用 Jenkins 并行运行任务,在 web 应用程序使用的端口上执行 Selenium 测试。

BUT I cannot figure out how to end the Flask web app once the Selenium tests are done.但是,一旦 Selenium 测试完成,我无法弄清楚如何结束 Flask web 应用程序。 If I don't end the run.py execution, it is stuck on an infinite loop, endlessly serving up the application to the port.如果我不结束 run.py 执行,它就会陷入无限循环,无休止地为端口提供应用程序。 I can have Jenkins automatically abort after some specified amount of time, but that flags the entire process as a failure.我可以让 Jenkins 在指定时间后自动中止,但这会将整个过程标记为失败。

Does anyone have a good idea of how to end the Flask hosting process once the Selenium tests are done?完成 Selenium 测试后,是否有人知道如何结束 Flask 托管过程? Or, if I am doing this in a block-headed way (I admit I am speaking out of copious amounts of ignorance and inexperience), is there a better way I should be handling this?或者,如果我以一种笨拙的方式来做这件事(我承认我是出于大量的无知和缺乏经验而说话),有没有更好的方法来处理这个问题?

I'm not aware of an official way to cause the development server to exit under Selenium control, but the following route works with Flask 1.1.2我不知道导致开发服务器在 Selenium 控制下退出的官方方法,但以下路线适用于 Flask 1.1.2

from flask import request
...

if settings.DEBUG:
    @app.route('/quit')
    def quit():
        shutdown_hook = request.environ.get('werkzeug.server.shutdown')
        if shutdown_hook is not None:
            shutdown_hook()
            return "Bye"
        return "No shutdown hook"

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

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