-->

GitLab CI with Fastlane and Cocoapods not working

2020-05-06 18:02发布

问题:

I can't get GitLab CI to work with Fastlane and Cocoapods.

Here is the error messages I get during the Gitlab-CI:

[!] You cannot run CocoaPods as root.

Here the screenshot of the GitLab CI Log:

Here is my gitlab-runner register code that I write into a MacOS Terminal:

sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "ABCDEFG21sadfSAEGEAERE" \
  --description "MyApp runner with shell" \
  --tag-list ios \
  --executor "shell"

After that I type inside the terminal:

sudo gitlab-runner start

and...

sudo gitlab-runner run

After the run cmd, any git push will cause the GitLab CI to start a new Pipeline. So far, so good :)

But after a few seconds, the above error occurs. And it has to do with the Cocoapods.

How can you make Gitlab (and/or Fastlane) to recognise the Podfile during CI ??

Here is my Fastfile:

update_fastlane

default_platform(:ios)

platform :ios do

    def install_pods
        cocoapods(
          clean: true,
          podfile: "./Podfile",
          try_repo_update_on_error: true
        )
    end

    lane :tests do
        install_pods()
        gym(configuration: "Release",
            workspace: "MyApp.xcworkspace",
            scheme: "MyApp",
            clean: true,
            output_name: "MyApp.ipa")  
        # increment_build_number
        scan(workspace: "MyApp.xcworkspace",
            devices: ["iPhone SE", "iPhone XS"],
            scheme: "MyAppTests")
    end

I tried plenty of other Podfile paths, and even tried to run everything on a docker container. But same thing, the error [!] You cannot run CocoaPods as root remains. What is wrong here ??

Here is my .gitlab-ci.yml file:

stages:
  - unit_tests

variables:
  LC_ALL: "en_US.UTF-8"
  LANG: "en_US.UTF-8"

before_script:
  - gem install bundler
  - bundle install

unit_tests:
  dependencies: []
  stage: unit_tests
  artifacts:
    paths:
      - fastlane/screenshots
      - fastlane/logs
  script:
    - bundle exec fastlane tests
  tags:
    - ios

Any help appreciated. How do you make the Podfile being recognised by the CI ???

回答1:

I finally found the solution:

Also your gitlab-runner register is not allowed to have sudo.

For some reason there are many tutorials out there that show differently (i.e. with sudo) - such as this video or others...

Anyway, as @Scriptable pointed out, on a Mac you absolutely need to leave sudo out !

Here is the best tutorial I have found on how to Gitlab CI an iOS project.