GitHub部署Azure Linux Web App时antenv虚拟环境找不到求助
解决Azure Linux Web App部署时找不到antenv虚拟环境的问题
问题描述
将Python应用从GitHub部署到Azure Linux Web App Service时,持续收到错误提示:
WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv
已在应用设置中开启SCM_DO_BUILD_DURING_DEPLOYMENT=true,并修改GitHub Actions的YAML文件,将虚拟环境名称改为'antenv'(执行python -m venv antenv、source antenv/bin/activate),但问题仍未解决。
用户提供的Workflow YAML
name: Build and deploy Python app to Azure Web App - mdapp3 on: push: branches: - main workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - 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 antenv source antenv/bin/activate - name: Install dependencies run: pip install -r requirements.txt # Optional: Add step to run tests here (PyTest, Django test suites, etc.) - name: Upload artifact for deployment jobs uses: actions/upload-artifact@v2 with: name: python-app path: | . !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@v2 with: name: python-app path: . - name: 'Deploy to Azure Web App' uses: azure/webapps-deploy@v2 id: deploy-to-webapp with: app-name: 'mdapp3' slot-name: 'Production' publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_5CXXXXXX }}
应用日志片段
Container mdapp3_0_e7cbb7be couldn't be started: Logs = 2023-09-18T15:51:45.405744940Z _____ 2023-09-18T15:51:45.405781443Z / _ \ __________ _________ ____ 2023-09-18T15:51:45.405787144Z / /_\ \\___ / | \_ __ \_/ __ \ 2023-09-18T15:51:45.405791744Z / | \/ /| | /| | \/\ ___/ 2023-09-18T15:51:45.405796145Z \____|__ /_____ \____/ |__| \___ > 2023-09-18T15:51:45.405800545Z \/ \/ \/ 2023-09-18T15:51:45.405805046Z A P P S E R V I C E O N L I N U X 2023-09-18T15:51:45.405809046Z 2023-09-18T15:51:45.405813047Z Documentation: http://aka.ms/webapp-linux 2023-09-18T15:51:45.405817147Z Python 3.11.4 2023-09-18T15:51:45.405821147Z Note: Any data outside '/home' is not persisted 2023-09-18T15:51:48.717385073Z Starting OpenBSD Secure Shell server: sshd. 2023-09-18T15:51:48.866272740Z App Command Line not configured, will attempt auto-detect 2023-09-18T15:51:49.236537117Z Starting periodic command scheduler: cron. 2023-09-18T15:51:49.266362015Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite 2023-09-18T15:51:49.325928903Z Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml' 2023-09-18T15:51:49.325963607Z Could not find operation ID in manifest. Generating an operation id... 2023-09-18T15:51:49.327749380Z Build Operation ID: 5f2419ea-178e-4da1-a73d-cb0a8cd3e22c 2023-09-18T15:51:49.538150824Z Oryx Version: 0.2.20230707.1, Commit: 0bd28e69919b5e8beba451e8677e3345f0be8361, ReleaseTagName: 20230707.1 2023-09-18T15:51:49.550486823Z Detected an app based on Flask 2023-09-18T15:51:49.551541125Z Generating `gunicorn` command for 'app:app' 2023-09-18T15:51:49.586169290Z Writing output script to '/opt/startup/startup.sh' 2023-09-18T15:51:49.634218359Z WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv. 2023-09-18T15:51:49.634248962Z WARNING: Could not find package directory /home/site/wwwroot/__oryx_packages__. 2023-09-18T15:51:50.673759167Z [2023-09-18 15:51:50 +0000] [72] [INFO] Starting gunicorn 20.1.0 2023-09-18T15:51:50.734923310Z [2023-09-18 15:51:50 +0000] [72] [INFO] Listening at: http://0.0.0.0:8000 (72) 2023-09-18T15:51:50.757802233Z [2023-09-18 15:51:50 +0000] [72] [INFO] Using worker: sync 2023-09-18T15:51:50.757829636Z [2023-09-18 15:51:50 +0000] [75] [INFO] Booting worker with pid: 75 2023-09-18T15:51:51.464761425Z [2023-09-18 15:51:51 +0000] [75] [ERROR] Exception in worker process 2023-09-18T15:51:51.464798029Z Traceback (most recent call last): 2023-09-18T15:51:51.464804929Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker 2023-09-18T15:51:51.464810430Z worker.init_process() 2023-09-18T15:51:51.464815630Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/workers/base.py", line 134, in init_process 2023-09-18T15:51:51.464830932Z self.load_wsgi() 2023-09-18T15:51:51.464835832Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi 2023-09-18T15:51:51.464841033Z self.wsgi = self.app.wsgi() 2023-09-18T15:51:51.464845833Z ^^^^^^^^^^^^^^^ 2023-09-18T15:51:51.464850734Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/app/base.py", line 67, in wsgi 2023-09-18T15:51:51.464855734Z self.callable = self.load() 2023-09-18T15:51:51.464860535Z ^^^^^^^^^^^ 2023-09-18T15:51:51.464865235Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 58, in load 2023-09-18T15:51:51.464870236Z return self.load_wsgiapp() 2023-09-18T15:51:51.464874936Z ^^^^^^^^^^^^^^^^^^^ 2023-09-18T15:51:51.464879737Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp 2023-09-18T15:51:51.464884737Z return util.import_app(self.app_uri) 2023-09-18T15:51:51.464889538Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2023-09-18T15:51:51.464894438Z File "/opt/python/3.11.4/lib/python3.11/site-packages/gunicorn/util.py", line 359, in import_app 2023-09-18T15:51:51.464899438Z mod = importlib.import_module(module)
原因分析
- 虚拟环境兼容性问题:在GitHub Actions的Ubuntu Runner中构建的
antenv虚拟环境,其依赖包的二进制文件是针对Runner系统编译的,与Azure Web App的Linux容器环境不兼容,导致Oryx无法识别该虚拟环境。 - Oryx自动构建逻辑冲突:当开启
SCM_DO_BUILD_DURING_DEPLOYMENT=true时,Azure的Oryx工具会自动尝试构建虚拟环境并安装依赖,而提前上传的虚拟环境会干扰这一过程,且无法被Oryx正常使用。
解决方案
方案一:让Oryx自动构建虚拟环境(推荐)
这种方式利用Azure原生的构建工具,确保依赖与目标环境兼容:
- 修改GitHub Actions的YAML文件,移除本地构建虚拟环境和安装依赖的步骤,仅上传源代码:
name: Build and deploy Python app to Azure Web App - mdapp3 on: push: branches: - main workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python version uses: actions/setup-python@v1 with: python-version: '3.11' # 可选:仅在Runner中安装依赖用于测试,不上传虚拟环境 - name: Install test dependencies run: pip install -r requirements.txt - name: Run tests run: pytest # 根据实际测试框架调整 - name: Upload artifact for deployment jobs uses: actions/upload-artifact@v2 with: name: python-app path: | . !__pycache__/ !.pytest_cache/ 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@v2 with: name: python-app path: . - name: 'Deploy to Azure Web App' uses: azure/webapps-deploy@v2 id: deploy-to-webapp with: app-name: 'mdapp3' slot-name: 'Production' publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_5CXXXXXX }}
- 保持Azure应用设置中的
SCM_DO_BUILD_DURING_DEPLOYMENT=true,Oryx会在部署时自动创建antenv虚拟环境,并安装requirements.txt中的依赖。
方案二:强制上传并使用本地构建的虚拟环境(不推荐)
如果必须在GitHub Actions中构建依赖,需确保虚拟环境能被Azure识别:
- 在安装依赖时,指定安装路径到虚拟环境的站点包目录:
- name: Install dependencies run: | python -m venv antenv source antenv/bin/activate pip install -r requirements.txt --target antenv/lib/python3.11/site-packages
- 修改artifact上传步骤,明确包含
antenv目录:
- name: Upload artifact for deployment jobs uses: actions/upload-artifact@v2 with: name: python-app path: | . antenv/ !venv/
注意:这种方式仍可能因环境差异导致依赖运行失败,仅在特殊场景下使用。
额外说明
日志中后续出现的gunicorn启动失败错误,是因为虚拟环境未生效导致应用依赖缺失,解决虚拟环境问题后该错误会自动消失。
内容的提问来源于stack exchange,提问作者Samirit Saha
相关产品推荐
相关产品推荐

