【问题标题】:running migrations as a part of an MS Azure app service release pipeline for a Django web app作为 Django Web 应用程序的 MS Azure 应用程序服务发布管道的一部分运行迁移
【发布时间】:2020-03-23 03:09:26
【问题描述】:

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

/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.py collectstatic 正在运行,而不是manage.py migrate

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

【问题讨论】:

    标签: python django azure azure-devops kudu


    【解决方案1】:

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

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

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

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

    更多详情请见Oryx documentation

    【讨论】:

      【解决方案2】:

      使用 Django 时,您通常希望在部署应用代码后使用 manage.py migrate 迁移数据模型。为此,您可以在部署后脚本中添加 startUpCommand

      startUpCommand:  python3.6 manage.py migrate
      

      详情请参考此official文档。

      【讨论】:

      • 感谢您的回复。我尝试了上面的建议并在日志输出中收到以下错误:2020-03-24T11:46:54.0274181Z ##[error]Error: Unable to run the script on Kudu Service. Error: Error: Executed script returned '127' as return code. Error: /home/site/VSTS_PostDeployment_1401585050340838/kuduPostDeploymentScript.sh: 1: /home/site/VSTS_PostDeployment_1401585050340838/kuduPostDeploymentScript.sh: startUpCommand:: Permission denied
      • 您可以尝试使用 chmod 为脚本授予执行权限作为解决方法。请参考这个document和类似的case
      猜你喜欢
      • 2023-04-04
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多