When deploying a Python project from a User Acceptance Testing (UAT) environment to Production, it’s essential to ensure that all dependencies and configurations remain consistent. Particularly in our situation where this was going to be the first deployment of AI semantic search functionality to Production. In some cases, the UAT environment may not include a requirements.txt file, making it necessary to generate one before transferring the project. This article outlines the steps required to achieve a seamless deployment using Git and Python virtual environments.
Cloning or updating the repository on production
The first step is to ensure the Production server has the latest version of the application from the Git repository hosted on Azure.
1. Navigate to the project directory on Production
cd /var/www/html/open-ai/
2. **Clone the repository if it does not exist
git clone https://your-azure-repo-url.git .
If the repository is already cloned, update it instead:
git pull origin main # Adjust 'main' to the correct branch
Generating a `requirements.txt` file on UAT
If a `requirements.txt` file does not exist in the UAT environment, it must be generated to ensure the same Python packages are installed on Production.
1. Activate the virtual environment on UAT (if one exists)
source /var/www/html/open-ai/venv/bin/activate
If no virtual environment exists, the system-wide Python installation will be used.
2. Check installed Python packages
pip freeze
3. Generate the `requirements.txt` file
pip freeze > /var/www/html/open-ai/requirements.txt
Transferring the dependencies to production
Once the `requirements.txt` file has been generated, it must be copied to the Production environment.
1. Transfer the file using `scp` (secure copy)
scp /var/www/html/open-ai/requirements.txt user@production-server:/var/www/html/open-ai/
Setting up the Python environment on production
Now that the project and dependencies are in place, the next step is to ensure that Production has the same Python environment as UAT.
1. Navigate to the project directory on Production
cd /var/www/html/open-ai/
2. Create a virtual environment if it does not already exist
python3 -m venv venv
3. Activate the virtual environment
source venv/bin/activate
4. Install dependencies from requirements.txt
pip install -r requirements.txt
5. Deactivate the virtual environment after installation
deactivate
Ensuring the application runs correctly
Once dependencies are installed, ensure the application is running correctly:
- Check that environment variables match UAT by transferring or manually setting up a `.env` file
- Restart any application services if necessary:
sudo systemctl restart your-app.service
- If using Gunicorn or another WSGI server, reload the service:
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
Automating future deployments
To streamline future deployments, consider setting up a Git deployment workflow:
1. Use Git to pull the latest changes
git pull origin main
2. Set up an Azure DevOps pipeline to deploy changes automatically to Production when new commits are pushed.
3. Use a webhook on Azure to trigger an SSH-based deployment.
The wrap
Deploying a Python project from UAT to Production requires careful attention to dependencies and configurations. By generating a `requirements.txt` file, setting up a virtual environment, and ensuring the correct system configurations, you can maintain consistency between environments and avoid potential deployment issues. Implementing automation for future updates can further enhance efficiency and reduce manual workload.