python - 将 python 脚本部署和运行到 azure 资源是如何工作的?

标签 python ubuntu docker-compose azure-web-app-service github-actions

我对 DevOps 很陌生,所以这可能是一个非常愚蠢的问题。我正在尝试使用 GitHub 操作将 python Web 抓取脚本部署到 azure webapp 上。该脚本旨在长时间运行,因为它正在逐字分析网站数小时。然后它将结果记录到 .log 文件中。
我知道一点 GitHub 操作是如何工作的,我知道当我推送到 repo 时我可以触发作业。但是,例如,我对如何在 azure 资源(如 VM 或 webapp)上运行应用程序或脚本感到有些困惑。这个过程是否涉及 SSH 进入资源,然后自动运行 cli 命令“python main.py”或“docker-compose up”,还是涉及更复杂的东西?
为了获得更好的上下文,这是我的工作流文件夹中的脚本:

   on:
  [push]

env:
  AZURE_WEBAPP_NAME: emotional-news-service # set this to your application's name
  WORKING_DIRECTORY: '.'         # set this to the path to your path of working directory inside GitHub repository, defaults to the repository root
  PYTHON_VERSION: '3.9' 
  STARTUP_COMMAND: 'docker-compose up --build -d'           # set this to the startup command required to start the gunicorn server. default it is empty

name: Build and deploy Python app
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    environment: dev
    steps:
    # checkout the repo 
    - uses: actions/checkout@master
    # setup python
    - name: Setup Python
      uses: actions/setup-python@v1
      with:
        python-version: ${{ env.PYTHON_VERSION }}
    # setup docker compose
    - uses: KengoTODA/actions-setup-docker-compose@main
      with:
        version: '1.26.2'
    # install dependencies
    - name: python install
      working-directory: ${{ env.WORKING_DIRECTORY }}
      run: |
        sudo apt install python${{ env.PYTHON_VERSION }}-venv
        python -m venv --copies antenv
        source antenv/bin/activate
        pip install setuptools
        pip install -r requirements.txt
        python -m spacy download en_core_web_md

    # Azure login
    - uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}
    - uses: azure/appservice-settings@v1
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        mask-inputs: false
        general-settings-json: '{"linuxFxVersion": "PYTHON|${{ env.PYTHON_VERSION }}"}' #'General configuration settings as Key Value pairs'
    # deploy web app
    - uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        package: ${{ env.WORKING_DIRECTORY }}
        startup-command: ${{ env.STARTUP_COMMAND }}
    # Azure logout
    - name: logout
      run: |
        az logout
上面的大部分脚本取自:https://github.com/Azure/actions-workflow-samples/blob/master/AppService/python-webapp-on-azure.yml .
env.STARTUP_COMMAND 是我正在考虑的“SSH 然后运行命令”部分,还是完全不同的东西?
我还有另一个问题:有没有更好的方法来查看从 azure 资源中运行的 python 脚本的日志?我能想到的唯一方法是 ssh 进入它,然后输入“cat 'whatever.log'”。
提前致谢!

最佳答案

而不是使用 STARTUP_COMMAND: 'docker-compose up --build -d'您可以使用启动文件名。

startUpCommand: 'gunicorn --bind=0.0.0.0 --workers=4 startup:app'
或者
StartupCommand: 'startup.txt'
StartupCommand参数在 startup.py 文件中定义应用程序。默认情况下,Azure 应用服务在名为 app.py 或 application.py 的文件中查找 Flask 应用对象。如果您的代码不遵循此模式,则需要自定义启动命令。 Django 应用程序可能根本不需要定制。如需更多信息,请参阅 How to configure Python on Azure App Service - Customize startup command .
此外,因为 python-vscode-flask-tutorial 存储库在名为 startup.txt 的文件中包含相同的启动命令,您可以在 StartupCommand 中指定该文件。参数而不是命令,使用 StartupCommand: 'startup.txt' .
引用:here for more info

关于python - 将 python 脚本部署和运行到 azure 资源是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69143590/

相关文章:

python - Tornado -redis : RPOP works but BRPOP doesn't?

python : can't install new packages ubuntu 15. 10

mysql - 数据未添加到 MySQL 服务

docker - Gitlab-CI:绑定(bind)挂载失败

ruby-on-rails - docker-compose 在 bundle 中找不到 gemfile

python - 值错误: expected 2D or 3D input (got 1D input) PyTorch

Python NumPy : Convert string in to numpy array

python - 重命名未命名的列 Pandas 数据框

python - 导入 Orange 返回 "ImportError: no module named orange"

java - 无法通过服务器 IP 访问的 Spring Boot 应用程序给出 404