Cannot build IPA file from Gitlab-CI

I already posted in StackOverflow but I got no answer. So I'm gonna try here.

I'm using Flutter and I've been trying to build .ipa through Gitlab CI and GitLab Runner that will be running on my MacOS machine. But I hit a dead end with this error

Error (Xcode): Building for 'iOS', but linking in dylib (/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/usr/lib/libobjc.A.tbd) built for 'macOS macCatalyst zippered(macOS/Catalyst)'
Error (Xcode): Linker command failed with exit code 1 (use -v to see invocation)
Encountered error while archiving for device.

This error unfortunately cannot be reproduced locally, when I ran this code. The build will be successful and produced IPA file normally.

flutter build ipa --release --obfuscate --split-debug-info=build/ios/ipa/debug_symbols --build-number=${CI_JOB_ID:0:8} --export-options-plist ./ios/export.plist

which make it incredibly difficult to debug. I tried searching Google and all it shown are result of people trying to build iOS simulator. Their solution was excluding architecture arm64 which obviously wouldn't work in this case. And I already excluding i386 and armv7 architecture. and somehow my Gitlab CI still tried to build with MacOS library instead.

I'm not sure if this is one of the causes. But I noticed that every time my GitLab Runner CI reach a new stage. This command is automatically declared :

declare -x SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk"

Maybe it's related? I honestly don't know what to do now.

One observation on the file paths here:

/Library/Developer/CommandLineTools/
/Applications/Xcode.app/

There is no problem with having both of these installed. However, if you have any shell scripts that execute a command, the system will choose the version of that tool from the active toolchain, which is set by xcode-select command line tool. This means that if you have a compile command issued directly by Xcode for source code attached to an Xcode target, that code will be compiled by the compile command inside of Xcode, because there's no direct command line tool invocation by a shell script, but if you ran a similar compile command manually in your build (including through a Run Script phase in your Xcode project, or commands run before or after xcodebuild is invoked), it will run the version of the tool declared by xcode-select. Thus, the tool version selected might come from the toolchain inside of Xcode, or it may be the one from the Command Line Tools package, depending on how that path is set. Also, the Command Line Tools package only contains the macOS SDK, and not the iOS SDK, and the part of the error message regarding Mac Catalyst while you're building an iOS app makes me think that you don't have the right toolchain selected in your build environment.

While your build requirements may require both toolchains, or your build environment may provide both as a setup convenience, you should verify all those details to make sure you know which toolchain is in use, and whether or not its the right one according to the requirements of your project and third-party libraries. Solving this may be as simple as setting xcode-select to point to Xcode as part of your environment setup, though I can't speak for the requirements of either GitLab CI or Flutter. You may want to also consult their support resources for more specific details on configuration expectations of their software.

— Ed Ford,  DTS Engineer

Cannot build IPA file from Gitlab-CI
 
 
Q