python - 将迁移作为 Django Web 应用程序的 MS Azure 应用程序服务发布管道的一部分运行

标签 python django azure azure-devops kudu

我想知道是否有人有将 python manage.py migrate 命令集成到 MS Azure 发布管道中的经验。该应用程序正在通过 DevOps 使用 CI/CD 管道进行部署。在发布管道部分,应用程序被部署到三个不同的阶段(开发、测试和生产)。我未能成功地将迁移命令集成到部署过程中。我尝试通过使用部署后内联脚本来实现此目的:

/antenv/bin/python /home/site/wwwroot/manage.py collectstatic
/antenv/bin/python /home/site/wwwroot/manage.py migrate

如果我通过 SSH 在沙箱环境中运行上述命令,它们会成功执行。 但是,将它们作为部署后脚本包含在发布管道中会引发以下错误:

2020-03-22T19:00:32.8641689Z Standard error from script: 
2020-03-22T19:00:32.8727872Z ##[error]/home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: 1: /home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: /antenv/bin/python: not found
/home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: 2: /home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: /antenv/bin/python: not found

2020-03-22T19:01:34.3372528Z ##[error]Error: Unable to run the script on Kudu Service. Error: Error: Executed script returned '127' as return code. Error: /home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: 1: /home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: /antenv/bin/python: not found
/home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: 2: /home/site/VSTS_PostDeployment_1321584903618191/kuduPostDeploymentScript.sh: /antenv/bin/python: not found

我还尝试运行上述内联脚本:

manage.py collectstatic
manage.py migrate

但是没有效果。

基于Oryx documentation ,似乎正在运行 manage.pycollectstatic,但没有运行manage.py migrate

任何想法或建议将不胜感激!提前致谢。

最佳答案

由于我们希望能够在 Azure DevOps 上使用发布管道基础设施,因此我们无法使用 startUpCommand: python3.6 manage.py migrate 因为 devops 中没有与发布关联的 YAML 文件(至少到目前为止)。相反,最终起作用的是:

  1. 在项目存储库中创建脚本文件。我将该文件命名为 Procfile.sh。在此文件中,我添加了以下两行代码:
python manage.py migrate
python manage.py collectstatic --no-input
  • 在 Web 应用配置中添加指向该文件的新变量:
  •  {
        "name": "POST_BUILD_SCRIPT_PATH",
        "slotSetting": false,
        "value": "Procfile.sh"
      }
    

    如果您在脚本中运行collectstatic 命令,您还需要禁止 Oryx 引擎运行它:

    {
        "name": "DISABLE_COLLECTSTATIC",
        "slotSetting": false,
        "value": "true"
      },
    

    参见Oryx documentation了解更多详情。

    关于python - 将迁移作为 Django Web 应用程序的 MS Azure 应用程序服务发布管道的一部分运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60807485/

    相关文章:

    python - 使用日期时间创建新列或列表

    django - 跟踪对象的 "page views"或 "hits"的编号?

    asp.net - 创建新的 Azure 应用服务时出现问题

    Django 访问自定义管理器中登录的用户

    python - Django对象查询集通过匹配日期时间过滤

    python - 基于ServiceBus触发Azure Function并写回不起作用(Python)

    Azure (PowerShell) 如何向多个现有网络安全组添加额外的 IP(源地址前缀字段)?

    python - 带有群图的 Seaborn PairGrid

    python - 字符串可以变成变量吗?

    python - Django 调试工具栏是否适用于 DRF?