如何通过 Azure DevOps 管道部署 Python 控制器

问题描述 投票:0回答:1

我正在创建一个基本的天蓝色管道,预计将执行以下任务:

  1. 从 github 拉取 Python 代码
  2. 通过gunicorn运行python控制器

部署服务器是私有云中的Linux VM(不属于azure),代理也运行在同一服务器上。

trigger:
  branches:
    include:
      - master
      - refs/tags/*
      - feature*

stages:
  - stage: Deploy
    condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/tags'), startsWith(variables['Build.SourceBranch'], 'refs/heads/feature')))
    jobs:
      - job: 'RunDeploymentCommandJob'
        displayName: Deploying job
        pool:
          name: Agent-Pool
        steps:
          - script: |
              # Install dependencies if needed
              pip3 install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir --user -r requirements.txt
              # Run the Python code
              /usr/local/bin/gunicorn -w 1 -b 0.0.0.0:8081 controller:app --daemon
            displayName: 'Run Python Code'

当上述管道完成执行时,我没有发现服务器上运行任何gunicorn进程。似乎一旦管道执行完成,守护进程就会停止。

如果没有

--daemon
,我可以在管道日志中看到控制器已成功启动并侦听8081,但管道不会继续完成并继续等待,因为gunicorn正在前台运行。

任何人都可以建议如何处理这个问题,或者是否有更好的方法来自动部署公开 REST API 的 python 应用程序。

谢谢

python-3.x azure-pipelines gunicorn daemon cicd
1个回答
0
投票

当上述管道完成执行时,我没有发现服务器上运行任何gunicorn进程。似乎一旦管道执行完成,守护进程就会停止。

问题的原因可能是 Azure Pipeline 中的 Finalize 作业将在运行 Pipeline 任务后清理进程。

例如:

管道完成后,gunicorn进程将在服务器上被清理。

为了避免 Finalize 作业清理进程,您可以在管道中设置变量:process.clean: false

例如:

trigger:
  branches:
    include:
      - master
      - refs/tags/*
      - feature*

stages:
  - stage: Deploy
    variables:
      process.clean: false
    condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/tags'), startsWith(variables['Build.SourceBranch'], 'refs/heads/feature')))
    jobs:
      - job: 'RunDeploymentCommandJob'
        displayName: Deploying job
        pool:
          name: Agent-Pool
        steps:
          - script: |
              # Install dependencies if needed
              pip3 install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir --user -r requirements.txt
              # Run the Python code
              /usr/local/bin/gunicorn -w 1 -b 0.0.0.0:8081 controller:app 
            displayName: 'Run Python Code'

然后您可以删除

--daemon
并检查gunicorn进程是否可以在服务器上保持活动状态。

© www.soinside.com 2019 - 2024. All rights reserved.