从 Github Actions 中 CI/CD yml 中的 docker 映像(在 PR 创建)将 EF Core C# 数据库迁移(创建/更新数据库/表)应用到 Azure PostgreSQL 灵活服务器。
我已经能够看到 postgresSQL 服务器,但在尝试连接时出现以下错误
Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user "***"
在 GitHub Actions 中运行以下代码时
deploy-development:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Login to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.DEV_AZURE_CREDENTIALS }}
- name: Login to ACR (Development)
run: az acr login --name myAzureContainerRegistry
- name: Run Migrations (Development)
run: |
#1. Get the image that I have already built in previous step.
docker pull myAzureContainerRegistry/dbmigrator:${{ github.sha }}
#2. Create a connection string to pass into the migration container
# so it can run it against Azure PostgreSQL server.
connection_string="Server=myPostgreSQLServer.postgres.database.azure.com;
Database=dbToBeCreatedByMigration;Port=5432;
User Id=MyAdminUser;Password=LegalWorkingPassWord;Ssl Mode=Require;
Trust Server Certificate=true;"
#3. Run the migrations. This is where the error accurse
docker run --rm \
-e ConnectionStrings:Default="$connection_string" \
myAzureContainerRegistry/dbmigrator:${{ github.sha }}
我已验证并确定
1. 用户 ID 和密码正确。我更改了它们并在管道中进行了硬编码,并且还使用相同的密码从本地计算机使用此 psql 命令成功连接到服务器。
psql "--host=myPostgreSQLServer.postgres.database.azure.com" "--port=5432" "--dbname=postgres" "--username=MyAdminUser" "--set=sslmode=require"
2. 灵活的 postgresql 服务器具有作为防火墙规则所需的所有 GitHub Actions Ip´s 设置。在我添加此内容之前,GitHub Actions 无法连接
这些是ip
'192.30.252.0','185.199.108.0', '140.82.112.0', '143.55.64.0', '20.201.28.148', '20.205.243.168', '20.87.225.211', '20.248.137.49', '20.207.73.85', '20.27.177.116', '20.200.245.245','20.233.54.49'
3. 灵活的 postgresql 服务器具有以下设置
4. 通过以下查询,天蓝色日志包含更多信息(它们不)
let start_time = datetime("2023-07-28T07:42:08Z");
let end_time = datetime("2023-07-28T07:42:30Z");
AzureDiagnostics
| where TimeGenerated >= start_time and TimeGenerated <= end_time
| where Category == "PostgreSQLLogs"
//| where Message contains "FATAL: password authentication failed for user"
| project TimeGenerated, Message
5. docker 文件中发生了一些有趣的事情。基本上就是这样
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["myProject/aNuGet.Config", "."]
COPY ["myProject/myproject.DbMigrator.csproj", "myProject/myProject.DbMigrator/"]
RUN dotnet restore "myProjectDbMigrator/myProjectDbMigrator.csproj"
COPY . .
WORKDIR "/myProjectDbMigrator"
RUN dotnet build "myProject.DbMigrator.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "myProject.DbMigrator.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myProjectDbMigrator.dll"]
我的堆栈