[FIXED] Has anyone successfully built Xamarin.Forms with gitlab-ci?

Issue

I am working on a pet project with Xamarin.Forms and am wondering if anyone has had experience with configuring gitlab-ci.yml builds successfully. There seems to be limited material on configuring for .NET builds in general, trying my luck at building one successfully before stringing together both builds. Have tried per-project .csproj for build path.

Any insight and experience would be appreciated.

Current .gitlab-ci.yml

image: mono

variables:
   Solution: Solution.sln

stages:
  - build
  - test
  - deploy

before_script:
  - nuget restore $Solution

build:
  stage: build
  script:
    - MONO_IOMAP=case xbuild /p:Configuration="Release" /p:Platform="iPhone" /t:Build $Solution

Solution

Yes, we got it to work perfectly without needing to use boots or AzureDevops. As @AkashKava mentioned, I had to make it run on a Mac build agent/runner though, and I used AppCenter’s CLI commands for the distribution part of it, where I also stored my certs, keystore and provisioning profiles.

So before everything runs, make sure you restore nuget packages and installs the necessary libraries nuget, msbuild, appcenter,…:

before_script:
  - nuget restore

Then, for creation of an Android QA apk file:

android_dev_apk:
  stage: build
  dependencies: []
  tags:
    - xamarin
  script:
    - msbuild {AppName}.sln $BUILD_VERBOSITY /t:Clean /p:Configuration=Dev
    - msbuild {AppName}.sln $BUILD_VERBOSITY /t:Build /p:Configuration=Dev
    - msbuild {AppName}.Android/{AppName}.Android.csproj $BUILD_VERBOSITY /t:PackageForAndroid /t:SignAndroidPackage /p:Configuration=Dev /p:AndroidKeyStore=True

Just replace {AppName} with your App’s folder name/app name which was the same in my case. Similarly for iOS

ios_qa_app:
  stage: build
  dependencies: []
  tags:
   - xamarin
  script:
   - rm -rf {AppName}.iOS/bin/iPhone/QA
   - msbuild {AppName}.sln $BUILD_VERBOSITY /t:Clean /p:Platform=iPhone /p:Configuration=QA
   - msbuild {AppName}.sln $BUILD_VERBOSITY /t:Build /p:Platform=iPhone /p:ArchiveOnBuild=true /p:Configuration=QA
  artifacts:
    paths:
     - {AppName}.iOS/bin/iPhone/QA/{AppName}.ipa
     - {AppName}.iOS/bin/iPhone/QA/{AppName}.app.dSYM
    expire_in: 10 day
    when: on_success
  only:
    - schedules
  except:
    variables:
      - $ProdBuild == "true"

Note that under script, everything acts like it would when you use the Terminal, so you can also just type stuff like ls just to print the list of files in that folder in the output log, or cd .. or cd DirectoryName to change folders.

So to distribute the Android artifact, add this in your Android script:

    - appcenter distribute release --app {CompanyInAppCenter}/{AndroidAppNameInAppCenter} --group "Collaborators" --file {AppName}.Android/bin/QA/{BundleIdentifier}-Signed.apk --token=${APPCENTER_API_TOKEN}

Finally, to distribute the iOS artifact, add this in your iOS script:

    - appcenter distribute release --app {CompanyInAppCenter}/{iOSAppNameInAppCenter} --group "Collaborators" --file {AppName}.iOS/bin/iPhone/QA/{AppName}.ipa --token=${APPCENTER_API_TOKEN}

PS: I have written an article on how to do some of this stuff using GitHub Actions without using your own Build Agent.

Answered By – Saamer

Answer Checked By – Clifford M. (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published