【问题标题】:Building docker image with C# Source Generators inside GitHub Actions在 GitHub Actions 中使用 C# Source Generators 构建 docker 镜像
【发布时间】:2023-02-17 13:36:53
【问题描述】:

我遇到了一个问题,我无法构建具有使用源生成器的 C# 项目的 docker 映像。 C# dotnet build 在我的机器上本地运行良好,在构建容器 (docker build --target apis .) 时在本地运行良好。但是,在 GHA 中构建时,它会失败并显示一个暗示源代码生成器没有工作的错误。他们要么从不运行/不允许运行/不允许将文件写入磁盘。

以下是详细信息:

  • GitHub 操作工作流程:
name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:

  docker-ci:
    name: Docker
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - apis
          - scripts
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up dotnet
        uses: actions/setup-dotnet@v3
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          registry: my-docker.jfrog.io
          username: ${{ secrets.ORG_JFROG_USERNAME }}
          password: ${{ secrets.ORG_JFROG_ACCESS_TOKEN }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: my-project-${{ matrix.target }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=sha
      - name: test docker build # testing if `docker buildx build` might be the problem. It's not.
        run: docker build --target ${{ matrix.target }} .
      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          push: ${{ github.ref == 'refs/heads/master' }}
          platforms: linux/amd64,linux/arm64
          target: ${{ matrix.target }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
  • Docker 文件:
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base

COPY . /
WORKDIR /

FROM base as build

# Install dependencies
RUN dotnet restore --configfile ./NuGet.Config MyProject.sln

ARG config=Release

# Create dotnet artifacts, output to /app
RUN dotnet publish --no-restore --configuration ${config} --output /app MyProject.sln

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS apis

# So we can healthcheck
RUN apt-get -y update
RUN apt-get -y install curl

# Set up the app to run
WORKDIR /app
COPY --from=build /app .

EXPOSE 5009
ENTRYPOINT ["dotnet", "My.Project.Api.dll"]

我不能在这里分享整个 C# 解决方案,但我可以向您保证源代码生成器工作得很好。该解决方案在本地构建。

运行GHA报错如下:

Error: /My.Project.Api/Controllers/MyController.cs(11,15): error CS0234: The type or namespace name 'SourceGeneratedClassThatShouldWork' does not exist in the namespace 'SourceGenerated' (are you missing an assembly reference?) [/My.Project.Api/My.Project.Api.csproj]
Error: /My.Project.Api/Controllers/MyController.cs(65,56): error CS0246: The type or namespace name 'SourceGeneratedClassThatShouldWork' could not be found (are you missing a using directive or an assembly reference?) [/My.Project.Api/My.Project.Api.csproj]

【问题讨论】:

    标签: c# .net docker github-actions sourcegenerators


    【解决方案1】:

    天啊。这是一个愚蠢的。事实证明,这只是源代码生成器使用来自 git 子模块的内容,https://github.com/actions/checkout 操作默认情况下不会下拉子模块。这就是这一切,没什么特别的。

    【讨论】:

      最近更新 更多