我正在尝试完成 Github 操作,以使用 Docker 将我的 .Net8 Web API 构建并部署到 Kubernetes。这是我当前用于工作流程的 yml 文件:
name: Deploy to DigitalOcean Kubernetes
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
env:
NUGETUSERNAME: ${{ secrets.NUGETUSERNAME }}
NUGETPASSWORD: ${{ secrets.NUGETPASSWORD }}
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Log in to DigitalOcean Container Registry
uses: docker/login-action@v1
with:
registry: registry.digitalocean.com
username: _ # DigitalOcean requires username to be `_`
password: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Replace Github Username In Nuget.Config
run: sed -i 's/%NUGETUSERNAME%/${{ secrets.NUGETUSERNAME }}/g' x/x.Api/nuget.config
- name: Replace Github Password In Nuget.Config
run: sed -i 's/%NUGETPASSWORD%/${{ secrets.NUGETPASSWORD }}/g' x/x.Api/nuget.config
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: x/x.Api
file: x/x.Api/Dockerfile
push: true
tags: registry.digitalocean.com/x-api/aggregator-api:latest # Replace with your registry and image name
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Install Kompose
run: |
curl -L https://github.com/kubernetes/kompose/releases/download/v1.26.0/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv kompose /usr/local/bin/kompose
- name: Convert Docker Compose to Kubernetes manifests
run: kompose convert -f docker-compose.yml -o ./k8s/
- name: Set up Kubectl
uses: azure/setup-kubectl@v1
- name: Configure Kubernetes context
uses: azure/k8s-set-context@v1
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Deploy to Kubernetes
run: |
kubectl apply -f ./k8s/
- name: Verify deployment
run: kubectl rollout status deployment/x
这是API的Docker文件
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["x.Api.csproj", "x.Api/"]
COPY ./nuget.config ./
RUN dotnet restore "./x.Api/./x.Api.csproj" --configfile nuget.config
COPY . .
WORKDIR "/src/x.Api"
RUN dotnet build "./x.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./x.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "x.Api.dll"]
当 Github Action 运行时,它会失败,并且我得到以下信息(在 dotnet build 上):“程序不包含适合入口点的静态‘Main’方法”
我已更新 Docker 文件以使用此行更具体(添加 -v=q /property:StartupObject=x.Api.Program):
RUN dotnet build "./x.Api.csproj" -v=q /property:StartupObject=x.Api.Program -c $BUILD_CONFIGURATION -o /app/build
它再次失败:“找不到为主方法指定的‘x.Api.Program’...”
这个项目使用 docker-compose 在本地构建得很好。
我还打开了Visual Studio中的项目设置,查看设置了哪个启动对象,发现没有设置任何内容,并且下拉列表中没有任何可用的内容:
有人知道这是怎么回事吗?
找到答案:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["x.Api.csproj", "x.Api/"]
COPY ./nuget.config ./
RUN dotnet restore "./x.Api/./x.Api.csproj" --configfile nuget.config
WORKDIR "/src/x.Api"
COPY . .
RUN dotnet build "./x.csproj" -c $BUILD_CONFIGURATION -o /app/build
将“WORKDIR”/src/x.Api“”移至“COPY . .”上方