You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function连接PostgreSQL:本地/GitHub Actions部署失败排查

通过GitHub Actions CI/CD构建连接PostgreSQL的Azure Function的正确配置方案

正常部署场景

使用以下Python代码和依赖,通过CLI执行远程构建部署命令,可成功连接PostgreSQL:

函数代码

import os
import azure.functions as func
import logging

import psycopg2

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="ping_db", auth_level=func.AuthLevel.FUNCTION)
def ping_db(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    sample_sql = "SELECT * FROM table limit 1;"

    cnx_str = os.environ.get("POSTGRES_CONNECTION_STRING")
    with psycopg2.connect(cnx_str) as cnx:
        cursor = cnx.cursor()
        cursor.execute(sample_sql)
        result = cursor.fetchone()
        cursor.close()

    return func.HttpResponse(f"{result}")


@app.route(route="sanity_check", auth_level=func.AuthLevel.FUNCTION)
def sanity_check(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    return func.HttpResponse("sanity_check")

依赖文件(requirements.txt)

azure-functions
psycopg2-binary

有效部署命令

func azure functionapp publish <func-app-name> --build remote

遇到的异常问题

1. 本地构建失败

执行本地构建部署命令时报错:

There was an error restoring dependencies. ERROR: cannot install psycopg2_binary-2.9.9 dependency: binary dependencies without wheels are not supported when building locally. Use the "--build remote" option to build dependencies on the Azure Functions build server, or "--build-native-deps" option to automatically build and configure the dependencies using a Docker container. More information at https://aka.ms/func-python-publish

执行的命令:

func azure functionapp publish <func-app-name> --build local

2. GitHub Actions流水线部署异常

使用Azure Portal生成的默认GitHub Actions流水线部署,任务显示成功,但Azure门户中无可用函数;注释掉psycopg2相关代码后,sanity_check函数可正常运行。

解决方案:GitHub Actions CI/CD配置方案

默认流水线未处理psycopg2-binary的依赖构建兼容性问题,可通过以下两种方案解决:

方案1:启用Azure远程构建(推荐)

修改GitHub Actions工作流的部署步骤,指定使用Azure远程构建,依赖会在Azure构建服务器上安装,规避本地构建的兼容性限制。

修改后的关键步骤示例:

- name: 'Deploy to Azure Functions'
  uses: Azure/functions-action@v1
  with:
    app-name: '<func-app-name>'
    package: '.'
    publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}
    build-remote: true # 开启远程构建

方案2:使用Docker容器构建依赖

若需在GitHub Actions中完成本地构建,可通过Docker容器处理psycopg2-binary的原生依赖:

  1. 项目根目录添加Dockerfile(适配Azure Functions Python环境):
FROM mcr.microsoft.com/azure-functions/python:4-python3.11

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt

COPY . /home/site/wwwroot
  1. 修改GitHub Actions工作流,实现镜像构建与部署:
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: 'Login to Azure Container Registry'
        uses: azure/docker-login@v1
        with:
          login-server: '<your-acr-name>.azurecr.io'
          username: ${{ secrets.AZURE_ACR_USERNAME }}
          password: ${{ secrets.AZURE_ACR_PASSWORD }}

      - name: 'Build and push image'
        run: |
          docker build -t <your-acr-name>.azurecr.io/<func-app-name>:${{ github.sha }} .
          docker push <your-acr-name>.azurecr.io/<func-app-name>:${{ github.sha }}

      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        with:
          app-name: '<func-app-name>'
          image: '<your-acr-name>.azurecr.io/<func-app-name>:${{ github.sha }}'
          publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}

额外注意事项

  • 确保Azure Function App的应用设置中已配置POSTGRES_CONNECTION_STRING环境变量,值为PostgreSQL的连接字符串。
  • 方案1无需额外配置Docker,操作更简便;方案2需提前创建Azure容器注册表(ACR),并在GitHub Secrets中配置ACR的用户名和密码。

内容的提问来源于stack exchange,提问作者Joey Baruch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 10:20:35