Gitlab CI 和 Xamarin 构建失败

Gitlab CI and Xamarin build fails

我在 Visual Studio 中为 Mac 创建了一个完整的新 Xamarin Forms App 项目,并将其添加到 GitLab 存储库。之后我创建了一个 .gitlab-ci.yml 文件来设置我的 CI 构建。但问题是我收到错误消息:

error MSB4019: The imported project "/usr/lib/mono/xbuild/Xamarin/iOS/Xamarin.iOS.CSharp.targets" was not found. Confirm that the expression in the Import declaration "/usr/lib/mono/xbuild/Xamarin/iOS/Xamarin.iOS.CSharp.targets" is correct, and that the file exists on disk.

Xamarin.Android.Csharp.targets 也会弹出此错误。

我的 YML 文件如下所示:

image: mono:latest

stages:
    - build

build:    
    stage: build
    before_script:
        - msbuild -version
        - 'echo BUILDING'
        - 'echo NuGet Restore'
        - nuget restore 'XamarinFormsTestApp.sln'
    script:        
        - 'echo Cleaning'
        - MONO_IOMAP=case msbuild 'XamarinFormsTestApp.sln' $BUILD_VERBOSITY /t:Clean /p:Configuration=Debug

一些帮助将不胜感激 ;)

根据对您问题的评论,mono:latest 图像中似乎没有 Xamarin,但这没关系,因为您可以创建自己的 docker 图像以在 Gitlab 中使用 CI。您将需要访问注册表,但如果您使用 gitlab.com(与自托管实例相对),则注册表会为所有用户启用。您可以在文档中找到更多相关信息:https://docs.gitlab.com/ee/user/packages/container_registry/

如果您使用的是自托管,注册表仍然可用(即使是免费版本),但必须由管理员启用(此处文档:https://docs.gitlab.com/ee/administration/packages/container_registry.html)。

另一种选择是使用 Docker 自己的注册表,Docker Hub。使用哪个注册表并不重要,但您必须能够访问其中之一,以便您的跑步者可以拉下您的形象。如果您使用的是您(或您的管理员)无法直接控制的共享运行器,则尤其如此。如果您可以直接控制您的跑步者,另一种选择是在所有需要它的跑步者上构建 docker 图像。

我不熟悉 Xaramin,但您可以按照以下方法创建基于 mono:latest 的新 Docker 图像:

# ./mono-xamarin/Dockerfile
FROM mono:latest # this lets us build off of an existing image rather than starting from scratch. Everything in the mono:latest image will be available in this image
RUN ./install_xamarin.sh # Run whatever you need to in order to install xamarin or anything else you need.
RUN apt-get install git # just an example

一旦你的Docker文件被写入,你可以像这样构建它:

docker build --file path/to/Dockerfile --tag mono-xamarin:latest

如果您在跑步者身上构建图像,您可以立即使用它,例如:

# .gitlab-ci.yml
image: mono-xamarin:latest

stages:
  - build
...

否则,您现在可以将其推送到您要使用的任何注册表。

您将需要 mac os host 来构建 Xamarin.iOS 应用程序,据我所知,GitLab 中还没有它。您可以找到 discussion here and private beta here。现在,我建议您使用自己的 MacOS Host 并在该 host:

上注册 GitLab runner

https://docs.gitlab.com/runner/

您可以在需要的地方(VM 或物理设备)设置 host 并在那里安装 GitLab 运行器和 Xamarin 环境,对其进行标记并与 GitLab 管道一起使用,就像与任何其他共享运行器一样.