我有一个 Django 应用程序,我正在通过 github 操作将其部署到 Azure。
在我的工作流程文件中,我有一个收集静态文件的步骤,但它会跳过它,引用
No module named 'decouple'
,即使在之前添加了特定步骤来安装解耦。我正在使用诗歌进行依赖管理。
这是我的工作流程文件的摘录
name: Build and deploy Python app to Azure Web App - {{appname}}
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.13'
- name: Set up Python version
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Cache node modules
uses: actions/cache@v4
env:
cache-name: node-modules-cache
with:
path: ~/.npm
key: build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
restore-keys: |
build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}
build-${{ env.cache-name }}
- name: Cache pip
uses: actions/cache@v4
env:
cache-name: pip-cache
with:
path: ~/.cache/pip
key: build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
restore-keys: |
build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}-${{ steps.vars.outputs.sha_short }}
build-${{ env.cache-name }}-${{ steps.vars.outputs.branch }}
build-${{ env.cache-name }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry==1.8.3
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs --upgrade
poetry install --with dev --no-root --no-interaction --no-ansi
poetry add python-decouple
- name: Install dependencies and build static files with webpack
run: |
npm install
npm run build
- name: Collect static files
run: |
cd backend
python manage.py collectstatic --noinput 2>&1 | sed '/^Copying/d;/^$/d;/^ /d'
在服务器中,我无法运行该应用程序,因为它找不到静态文件。任何帮助将不胜感激。
使用 pip install python-decouple
安装
de Couple包。
或者在DJango应用程序根目录下创建
requirements.txt
文件并添加所需的python包进行安装。
需求.txt:
Django
python-decouple
并添加步骤
pip install -r requirements.txt
在 GitHub 工作流程中安装依赖项:
- name: Install dependencies
run: pip install -r requirements.txt
我创建了一个 Django 应用程序并使用了
python-decouple
:
.env:
DEBUG=True
TEMPLATE_DEBUG=True
SECRET_KEY=ARANDOMSECRETKEY
DATABASE_URL=mysql://myuser:mypassword@myhost/mydatabase
PERCENTILE=90%
#COMMENTED=42
应用=>我的网站=>settings.py:
from pathlib import Path
from decouple import config
config.encoding = 'cp1251'
SECRET_KEY = config('SECRET_KEY')
使用 GitHub 操作部署到 Azure:
name: Build and deploy Python app to Azure Web App - appname
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python version
uses: actions/setup-python@v1
with:
python-version: '3.11'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: pip install -r requirements.txt
# Optional: Add step to run tests here (PyTest, Django test suites, etc.)
- name: Zip artifact for deployment
run: zip release.zip ./* -r
- name: Upload artifact for deployment jobs
uses: actions/upload-artifact@v3
with:
name: python-app
path: |
release.zip
!venv/
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: python-app
- name: Unzip artifact for deployment
run: unzip release.zip
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v2
id: deploy-to-webapp
with:
app-name: 'appname'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_937EAXXXXE9B }}