davjfish
davjfish

Reputation: 345

running migrations as a part of an MS Azure app service release pipeline for a Django web app

I am wondering if anybody has experience with integrating a python manage.py migrate command into a MS Azure release pipeline. The app is being deployed using CI/CD pipeline through DevOps. In the release pipeline portion, the app is being deployed to three different stages (dev, test and prod). I have not been successful in being able to integrate the migrate command into the deployment process. I have tried achieving this by using a post deployment inline script:

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

If I run the above commands in the sandbox environment via SSH they are carried out successfully. However, including them in the release pipeline as a post deployment script raises the following error:

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

I also attempted running the above in-line script as:

manage.py collectstatic
manage.py migrate

But to no avail.

Based on the Oryx documentation, it seems as though manage.py collectstatic is being run, but not manage.py migrate

Any ideas or suggestions would be very much appreciated! Thanks in advance.

Upvotes: 4

Views: 3447

Answers (2)

davjfish
davjfish

Reputation: 345

Since we want to be able to use the release pipeline infrastructure on Azure DevOps, we cannot use startUpCommand: python3.6 manage.py migrate because there is no YAML file associated with the release in devops (at least as of yet). Instead, what finally worked was:

  1. Creating a script file in the project repository. I named the file Procfile.sh. In this file I added the following two lines of code:
python manage.py migrate
python manage.py collectstatic --no-input
  1. Add a new variable in the webapp configuration that points to that file:
 {
    "name": "POST_BUILD_SCRIPT_PATH",
    "slotSetting": false,
    "value": "Procfile.sh"
  }

If you are running the collectstatic command in your script, you will want to disable the Oryx engine from running it as well:

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

See Oryx documentation for more details.

Upvotes: 3

Hugh Lin
Hugh Lin

Reputation: 19461

When using Django, you typically want to migrate the data models using manage.py migrate after deploying the app code. You can add startUpCommand with post-deployment script for this purpose:

startUpCommand:  python3.6 manage.py migrate

For details ,please refer to this official document.

Upvotes: 0

Related Questions