Need suggetion on solving issue while using fastlane pilot

Describe your question in as much detail as possible:

I am trying to use fastlane to build and deploy Unity generated xcode project. I am using App Store Connect api_key to avoid 2FA. This works great when I run script on Mac or ssh into Mac then running script. However, once this script is run via GitLab runner, when calling pilot to deploy the ipa file to TestFlight. I keep getting error.

  • What are you seeing, and how does that differ from what you expect to see?

The error I get from log is

Authentication credentials are missing or invalid. - Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests

But when running script directly without going through cicd process, it works well.

The part to deploy to TestFlight is as the following.

app_identifier = CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier)
api_key = lane_context[SharedValues::APP_STORE_CONNECT_API_KEY]

output_name = "app.ipa"

  api_key: api_key,
  app_identifier: app_identifier,
  ipa: output_name,
  skip_submission: true,
  skip_waiting_for_build_processing: true,
  distribute_external: false,
  notify_external_testers: false,

The runner on Mac is registered as user without using sudo.

I speculate the runner may need some sort of environment variables or privilege but I just could not figure it out. Does anyone run into such issue?