运行 迁移作为 Django 网络应用程序的 MS Azure 应用程序服务发布管道的一部分
running migrations as a part of an MS Azure app service release pipeline for a Django web app
我想知道是否有人有将 python manage.py migrate
命令集成到 MS Azure 发布管道的经验。该应用程序正在通过 DevOps 使用 CI/CD 管道进行部署。在发布管道部分,应用程序被部署到三个不同的阶段(开发、测试和生产)。我未能成功将迁移命令集成到部署过程中。我已尝试通过使用 post 部署内联脚本来实现此目的:
/antenv/bin/python /home/site/wwwroot/manage.py collectstatic
/antenv/bin/python /home/site/wwwroot/manage.py migrate
如果我 运行 通过 SSH 在沙箱环境中执行上述命令,它们将成功执行。
但是,将它们作为 post 部署脚本包含在发布管道中会引发以下错误:
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
如有任何想法或建议,我们将不胜感激!提前致谢。
使用 Django 时,您通常希望在部署应用代码后使用 manage.py migrate
迁移数据模型。为此,您可以使用 post-部署脚本添加 startUpCommand
:
startUpCommand: python3.6 manage.py migrate
有关详细信息,请参阅此 official 文档。
由于我们希望能够在 Azure DevOps 上使用发布管道基础结构,因此我们不能使用 startUpCommand: python3.6 manage.py migrate
,因为在 devops 中没有与发布关联的 YAML 文件(至少到目前为止)。相反,最终起作用的是:
- 正在项目存储库中创建脚本文件。我将文件命名为
Procfile.sh
。在这个文件中我添加了以下两行代码:
python manage.py migrate
python manage.py collectstatic --no-input
- 在指向该文件的 webapp 配置中添加一个新变量:
{
"name": "POST_BUILD_SCRIPT_PATH",
"slotSetting": false,
"value": "Procfile.sh"
}
如果您是 运行 脚本中的 collectstatic 命令,您还需要从 运行 中禁用 Oryx 引擎:
{
"name": "DISABLE_COLLECTSTATIC",
"slotSetting": false,
"value": "true"
},
有关详细信息,请参阅 Oryx documentation。
我想知道是否有人有将 python manage.py migrate
命令集成到 MS Azure 发布管道的经验。该应用程序正在通过 DevOps 使用 CI/CD 管道进行部署。在发布管道部分,应用程序被部署到三个不同的阶段(开发、测试和生产)。我未能成功将迁移命令集成到部署过程中。我已尝试通过使用 post 部署内联脚本来实现此目的:
/antenv/bin/python /home/site/wwwroot/manage.py collectstatic
/antenv/bin/python /home/site/wwwroot/manage.py migrate
如果我 运行 通过 SSH 在沙箱环境中执行上述命令,它们将成功执行。 但是,将它们作为 post 部署脚本包含在发布管道中会引发以下错误:
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
如有任何想法或建议,我们将不胜感激!提前致谢。
使用 Django 时,您通常希望在部署应用代码后使用 manage.py migrate
迁移数据模型。为此,您可以使用 post-部署脚本添加 startUpCommand
:
startUpCommand: python3.6 manage.py migrate
有关详细信息,请参阅此 official 文档。
由于我们希望能够在 Azure DevOps 上使用发布管道基础结构,因此我们不能使用 startUpCommand: python3.6 manage.py migrate
,因为在 devops 中没有与发布关联的 YAML 文件(至少到目前为止)。相反,最终起作用的是:
- 正在项目存储库中创建脚本文件。我将文件命名为
Procfile.sh
。在这个文件中我添加了以下两行代码:
python manage.py migrate
python manage.py collectstatic --no-input
- 在指向该文件的 webapp 配置中添加一个新变量:
{
"name": "POST_BUILD_SCRIPT_PATH",
"slotSetting": false,
"value": "Procfile.sh"
}
如果您是 运行 脚本中的 collectstatic 命令,您还需要从 运行 中禁用 Oryx 引擎:
{
"name": "DISABLE_COLLECTSTATIC",
"slotSetting": false,
"value": "true"
},
有关详细信息,请参阅 Oryx documentation。