Can you pass environment variables into xcodebuild? - ios

I have two Jenkins jobs that run our functional tests. One job is for whenever something is submitted for code review and the other job runs whenever something is pushed to master.
Because these are functional tests they test entire flows of the application which end up modifiying the user state. The issue we are having right is that every job uses the same account so whenever two Jenkins jobs are running in parallel they modify the same account which can put them in an unexpected state and fail the test.
My plan was to use Jenkins' BUILD_NUMBER environment variable and by applying a bit of arthimetic to it I could have a guaranteed unique number for the job. This unique number could then be passed into xcodebuild as an environment variable and the tests could use this number to ensure that every Jenkins' is working on a unique account.
The problem is that I cannot find any way to pass environment variables into xcodebuild. I know it is possible for you to pass in user-defined build settings via xcodebuild (or xcargs if you're using Fastlane) but those values do not seem to be accessible as environment variables. They are accessible by the preprocessor and so you could use it to export the value to your Info.plist and then read it from there. But then you have baked these value into your binary and it cannot be changed unless you rebuild it which is not ideal. Also at this point in time I could just have Jenkins write to a file on disk and have the tests read from it. It is essentially the same functionality and saves me from having to pass in build settings.

I remember using something like GCC_PREPROCESSOR_DEFINITIONS to pass my own var
I had to shell escape the quotes. I ended up coding it into my fastlane build file.
in ruby it looked like this:
tmp_other_flags = {
GCC_PREPROCESSOR_DEFINITIONS: '"DISABLE_PUSH_NOTIFICATIONS=1"',
TARGETED_DEVICE_FAMILY: '1',
DEBUG: '1'
}
other_flags = tmp_other_flags.map do |k, v|
"#{k.to_s.shellescape}=#{v.shellescape}"
end.join ' '
puts "___ Custom Flags also know as xcargs:"
puts other_flags
gym(
clean: true,
silent: false,
project: proj_xcodeproj_file,
archive_path: "build-ios-xcarchive",
destination: 'generic/platform=iOS',
use_legacy_build_api: true,
output_directory: 'build-ios',
output_name: "MyApp.ipa",
export_method: 'ad-hoc',
codesigning_identity: 'iPhone Distribution: company (12345)',
provisioning_profile_path: './dl_profile_com.company.myapp.iphone.prod_ad_hoc.mobileprovision',
scheme: 'MyApp',
configuration: 'Debug',
xcargs: other_flags
)
it ended up getting called in the shell something like this:
set -o pipefail && xcodebuild -scheme 'MyApp' -project 'platforms/ios/MyApp.xcodeproj' -configuration 'Debug' -destination 'generic/platform=iOS' -archivePath 'build-ios-xcarchive.xcarchive' GCC_PREPROCESSOR_DEFINITIONS=\"DISABLE_PUSH_NOTIFICATIONS\=1\" TARGETED_DEVICE_FAMILY=1 DEBUG=1 clean archive CODE_SIGN_IDENTITY='iPhone Distribution: My Company (Blah)' | tee '/Users/andxyz/Library/Logs/gym/MyApp-MyApp.log' | xcpretty
xcodebuild - how to define preprocessor macro?
So, perhaps you could pull in your own environment variable using ruby inside of fastlane. by adding your var into the GCC_PREPROCESSOR_DEFINITIONS section
ruby can access the environment, for example:
ENV.fetch('TERM_PROGRAM') #returns "iTerm.app" on my machine
so following along with above:
tmp_other_flags = {
GCC_PREPROCESSOR_DEFINITIONS: "MY_VARIABLE=#{ENV.fetch('MY_VARIABLE')}" ,
TARGETED_DEVICE_FAMILY: '1',
DEBUG: '1'
}
HTH

Via #alisoftware, you can use xcargs to pass additional variables in:
gym(
scheme: scheme,
xcargs: {
:PROVISIONING_PROFILE => 'profile-uuid',
:PROVISIONING_PROFILE_SPECIFIER => 'match AppStore com.bigco.App'
},
codesigning_identity: "iPhone Distribution: BigCo, Inc. ()",
)
emits this during the build:
+---------------------+-------------------------------------------------------------------------------------------------+
| Summary for gym 2.53.1 |
+---------------------+-------------------------------------------------------------------------------------------------+
| scheme | Bespoke-iOS |
| xcargs | PROVISIONING_PROFILE=profile-uuid PROVISIONING_PROFILE_SPECIFIER=match\ AppStore\ com.bigco.App |
…

Related

How to use Fastlane with a CMake generated XCode project with dependency on WebP?

I have a project written in C++ where CMake is used to generate the build system for various platforms including iOS. The project has a dependency on WebP. You can find an example project on GitHub here that can be used to reproduce things & I've included the relevant source files at the end of this post for completeness.
The Xcode build system for iOS is generated using CMake as follows:
cmake -G Xcode -DCMAKE_TOOLCHAIN_FILE=third_party/ios-cmake/ios.toolchain.cmake -DPLATFORM=OS64 -DDEPLOYMENT_TARGET=15.0 -DENABLE_BITCODE=0 -S . -B cmake-build-release
We can now attempt to build/archive the app using Fastlane with the command from within the generated cmake-build-release directory:
bundle exec fastlane ios beta
However this fails due to being unable to locate various webp object files (that based on console output it appears to have previously successfully compiled):
...
▸ Compiling buffer_dec.c
▸ Compiling alpha_dec.c
▸ Building library libwebpdsp.a
...
** ARCHIVE FAILED **
▸ The following build commands failed:
▸ Libtool /Users/dbotha/Library/Developer/Xcode/DerivedData/CMakeFastlaneWebpTest-dlwvukebfiwjqvaqiepshuxqklhh/ArchiveIntermediates/CMakeFastlaneWebpTest/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/libwebpdecoder.a normal (in target 'webpdecoder' from project 'CMakeFastlaneWebpTest')
▸ (1 failure)
▸ ❌ error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't open file: /Users/dbotha/CLionProjects/CMakeFastlaneWebpTest/cmake-build-release/third_party/libwebp/CMakeFastlaneWebpTest.build/Release-iphoneos/webpdecode.build/Objects-normal/arm64/alpha_dec.o (No such file or directory)
▸ ❌ error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't open file: /Users/dbotha/CLionProjects/CMakeFastlaneWebpTest/cmake-build-release/third_party/libwebp/CMakeFastlaneWebpTest.build/Release-iphoneos/webpdecode.build/Objects-normal/arm64/buffer_dec.o (No such file or directory)
...
Internally Fastlane attempted to build/archive the project with the following command:
xcodebuild -scheme CMakeFastlaneWebpTest -project ./CMakeFastlaneWebpTest.xcodeproj -configuration Release -destination 'generic/platform=iOS' -archivePath ./out.xcarchive archive
Interestingly an archive can be successfully generated if I use the following xcodebuild command (note how -target flag is used instead of -scheme):
xcodebuild -project CMakeFastlaneWebpTest.xcodeproj archive -target CMakeFastlaneWebpTest -configuration Release
After this successful attempt bundle exec fastlane ios beta will now also succeed as the compiled object files are where it expected them to be.
Now I'd happily workaround this issue using my xcodebuild + -target flag approach and then use the fastlane command to push to Testflight, etc. but the real project (not this toy example) takes a very long time to build so building it twice is really wasteful from a cost point of view on CI platforms.
Does anyone have any idea what's going on here & how I can successfully build things in the first instance using fastlane without my own explicit call to xcodebuild first? Or alternatively how can I have Fastlane use the successfully built objects from my workaround so it doesn't need to rebuild the entire project from scratch?
CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(CMakeFastlaneWebpTest)
set(CMAKE_CXX_STANDARD 14)
add_executable(CMakeFastlaneWebpTest src/main.cpp)
# Skip building of unused webp tools which fail for me under ios:
set(WEBP_BUILD_ANIM_UTILS OFF)
set(WEBP_BUILD_CWEBP OFF)
set(WEBP_BUILD_DWEBP OFF)
set(WEBP_BUILD_GIF2WEBP OFF)
set(WEBP_BUILD_IMG2WEBP OFF)
set(WEBP_BUILD_VWEBP OFF)
set(WEBP_BUILD_WEBPINFO OFF)
set(WEBP_BUILD_WEBPMUX OFF)
set(WEBP_BUILD_EXTRAS OFF)
set(WEBP_BUILD_WEBP_JS OFF)
add_subdirectory(third_party/libwebp EXCLUDE_FROM_ALL)
target_link_libraries(CMakeFastlaneWebpTest PRIVATE webpdecoder webpdemux)
include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/third_party/libwebp/src)
configure_file(${CMAKE_SOURCE_DIR}/fastlane/Appfile ${CMAKE_BINARY_DIR}/fastlane/Appfile COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/fastlane/Fastfile ${CMAKE_BINARY_DIR}/fastlane/Fastfile COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/Gemfile ${CMAKE_BINARY_DIR}/Gemfile COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/Gemfile.lock ${CMAKE_BINARY_DIR}/Gemfile.lock COPYONLY)
set_target_properties(CMakeFastlaneWebpTest PROPERTIES
XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET}
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/src/iOS-Info.plist.in
MACOSX_BUNDLE_GUI_IDENTIFIER com.dbotha.CMakeFastlaneWebpTest
MACOSX_BUNDLE_BUNDLE_NAME CMakeFastlaneWebpTest
MACOSX_BUNDLE_BUNDLE_VERSION "0.1"
MACOSX_BUNDLE_SHORT_VERSION_STRING "0.1"
)
set_xcode_property(CMakeFastlaneWebpTest PRODUCT_BUNDLE_IDENTIFIER "com.dbotha.CMakeFastlaneWebpTest" All)
set_xcode_property(CMakeFastlaneWebpTest CODE_SIGN_IDENTITY "iPhone Developer" All)
set_xcode_property(CMakeFastlaneWebpTest DEVELOPMENT_TEAM "GFP63373B2" All)
fastlane/Appfile
app_identifier("com.dbotha.CMakeFastlaneWebpTest") # The bundle identifier of your app
apple_id("REPLACE_ME") # Your Apple Developer Portal username
itc_team_id("REPLACE_ME") # App Store Connect Team ID
team_id("REPLACE_ME") # Developer Portal Team ID
fastlane/Fastfile
default_platform(:ios)
platform :ios do
desc "Push a new beta build to TestFlight"
lane :beta do
build_app(scheme: "CMakeFastlaneWebpTest", configuration: "Release")
upload_to_testflight
end
end
src/main.cpp
#include <iostream>
#include <webp/demux.h>
int main() {
WebPAnimDecoderOptions decOptions;
(void)decOptions;
std::cout << "Hello, World!" << std::endl;
return 0;
}
I'm not an expert in this topic but according to the documentation you should provide workspace to build your scheme.
To build an Xcode workspace, you must pass both the -workspace and
-scheme options to define the build. The parameters of the scheme will
control which targets are built and how they are built, although you may
pass other options to xcodebuild to override some parameters of the
scheme.
Scheme controls what target will be build, and guessing by your example, since you do not provide a workspace, it gets lost in the process.
The scheme is not lost anymore if you build the target manually. Since it is already build the scheme does not have to do a thing.
My proposals:
Option 1: Try adding workspace to build_app parameters in Fastfile.
Option 2: Don't bother with building scheme just use target in the
build_app parameters in Fastfile like so: build_app(target: "CMakeFastlaneWebpTest", configuration: "Release")

Fastlane iOS: Keeps building for simulator when using "iphoneos" sdk

I'm attempting to write a fastlane script that zips and uploads my app and testplan to Firebase Test Lab. It is currently failing because it keeps building the app for a simulator instead of a real device. I have no idea why because with the sdk set to "iphoneos" this shouldn't be happening?
lane :QAUITests do |options|
generate(options)
#testplan = options[:testplan]
scan(
scheme: 'QAUITests', # XCTest scheme
clean: true, # Recommended: This would ensure the build would not include unnecessary files
skip_detect_devices: true, # Required
build_for_testing: true, # Required
sdk: 'iphoneos', # Required
should_zip_build_products: true, # Must be true to set the correct format for Firebase Test Lab
buildlog_path: "./fastlane/fastlane-buildlog",
configuration: 'Debug',
derived_data_path: "./DerivedData",
testplan: #testplan
)
sh "zip -r MyTests.zip build/Build/Products/* Debug-iphoneos QAUITests_Regression_iphoneos15.4-arm64.xctestrun"
sh "gcloud firebase test ios run --test MyTests.zip --device model=iphone11pro,version=14.7,locale=en_GB,orientation=portrait"
For more context, this is what I'm trying to do https://firebase.google.com/docs/test-lab/ios/command-line. They do have a plugin to help but unfortunatly this issue prevents it from working https://github.com/fastlane/fastlane-plugin-firebase_test_lab/issues/79
Exit status of command 'zip -r MyTests.zip DerivedData/Build/Products/* Debug-iphoneos QAUITests_Regression_iphoneos15.4-arm64.xctestrun' was 12 instead of 0.
zip warning: name not matched: DerivedData/Build/Products/*
zip warning: name not matched: Debug-iphoneos
zip warning: name not matched: QAUITests_Regression_iphoneos15.4-arm64.xctestrun

The parent bundle has the same identifier (bundle.identifier) as sub-bundle

So I just recently added a POD to my project which then required me to use/load workspaces instead of the individual project. That's all fine and dandy but now the build script that we use to ultimately build the project (see below) will build successfully but upon installation, I've discovered upon examining the device console logs (some lines pasted below) while trying to install it that it fails to do so because of the parent bundle identifier is the same as the sub-bundle (I assume this is referring to this one library I added - JSONWebToken).
I suppose the question I have is how do I get around this? We have a script that appends the appropriate bundleId (we have multiple apps we build from the same code base). It seems to me xcodebuild will use this bundleId correctly for the main app but it is using it for the POD package as well. How do I get around this using xcodebuild?
xcodebuild -workspace "CompanyOne.xcworkspace" -scheme "Arrow" -configuration "Release" archive -archivePath "~/Library/Developer/Xcode/Archives/2021-12-01/Envoy Bag Drop 12-01-21 11.23 PM.xcarchive" -allowProvisioningUpdates PRODUCT_BUNDLE_IDENTIFIER=com.companyone.retail.demo PRODUCT_NAME="Envoy Bag Drop" GCC_PREPROCESSOR_DEFINITIONS='USE_SKIN_INCLUDE=1 RETAIL=1 SKIN_OVERRIDE_KEY=#\"*retail\"' | tee "/tmp/_xc-archive_envoy-demo_Release/build.log" | grep --color -E 'warning:|error:'
installd 0x16b54f000 -[MIInstallableBundle performPreflightWithError:]: 433: The parent bundle has the same identifier (com.luxerone.retail.demo) as sub-bundle at /var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.DUYvYU/extracted/Payload/Arrow.app/Frameworks/JWT.framework
default 17:31:21.582751-0800 installd 0x16b54f000 -[MIInstaller performInstallationWithError:]: Preflight stage failed
default 17:31:21.582992-0800 runningboardd Invalidating assertion 31-271-3129 (target:system) from originator [daemon<com.apple.mobile.installd>:271]

Fastlane Match environment variables are not picked up by build_app

I started using fastLane and match for codesigning on jenkins. match is able to successfully create the certificates and provisioning profile. The build_app step however fails since the pbxproj file is setting the CODE_SIGN_STYLE to Automatic. I want to achieve the build without modifying the pbxproj file since the devs use automatic signing.
FastFile
lane :upload_debug_test_flight do
setup_jenkins
match
build_app(scheme: "MyWork Dev", clean: true, export_method: "app-store")
upload_to_testflight(.....)
end
Match file:
git_url("git#github.mywork/cert_repo.git")
storage_mode("git")
type("appstore")
api_key_path("./test.json")
app_identifier(["com.mywork.mywork-test"])
username("developer#mywork.com")
In our project.pbxproj we have
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
PROVISIONING_PROFILE_SPECIFIER= ''
Also tried the following, but still gym/build_app is not picking the match env variables:
build_app(
skip_profile_detection: true,
export_method: "app-store",
export_options:{
signingStyle: "manual",
provisioningProfiles:{
"com.mywork.mywork-test": "match AppStore com.mywork.mywork-test"
}
}
)
Gym (build_app) uses the provisioning profile mapping defined by Match for archiving (export_options), but not for building the app (these are two different steps).
Thus, you need to either configure your Xcode project to use specific profiles, or update the PROVISIONING_PROFILE_SPECIFIER value before calling build_app.
If developers only use the Debug configuration, you can specify different settings between Debug and Release. This way, you can keep the Automatic option for the Debug configuration and specify the code signing identity and profile manually for the Release configuration.
If you don't want to touch your Xcode project at all and define everything dynamically from fastlane, you can use the update_code_signing_settings action to use the provisioning profile managed by Match:
match(...)
profile_mapping = Actions.lane_context[SharedValues::MATCH_PROVISIONING_PROFILE_MAPPING]
update_code_signing_settings(
use_automatic_signing: false,
path: "path/to/your/project.xcodeproj",
profile_name: profile_mapping['app_identifier'] # app_identifier could be an environment variable: ENV['APP_ID']
)
build_app(...)
Another solution is to pass the PROVISIONING_PROFILE_SPECIFIER variable to the xcargs option. Note that it will update the value in all your targets, so it may not work if you have an app extension with a different profile for example.
build_app(
...
xcargs: "PROVISIONING_PROFILE_SPECIFIER='profile_name'"
)

Specifying number of test devices when doing parallel testing using fastlane scan

I haven't been able to get more than 2 concurrent simulator test devices when testing iOS apps using "fastlane scan".
Doing this "manually" using only xcodebuild works, something like this. This would fire up at most 4 devices:
xcodebuild -workspace myapp.xcworkspace -scheme somescheme_debug -destination 'platform=iOS Simulator,OS=12.1,name=iPhone X' build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO -parallel-testing-worker-count 4
Relevant (but closed(ignored?)) thread: https://github.com/fastlane/fastlane/issues/13394
Here is the lane I'm using
platform :ios do
desc "Test"
lane :test do |values|
maxconcurrenttestingcount = 4
schemefortesting = 'somescheme_debug'
thebranch = git_branch
ensure_git_status_clean
puts "Testing, using scheme: '#{schemefortesting}'"
scan(
scheme: schemefortesting,
devices: ['iPhone X'],
# devices: ['iPhone XS Max'], #, 'iPad Air'],
max_concurrent_simulators: maxconcurrenttestingcount,
xcargs: "-parallel-testing-enabled=YES -parallel-testing-worker-count=#{maxconcurrenttestingcount}" # hmm not really working?
)
reset_git_repo
end
end
Starting with fastlane 2.142, you can now specify concurrent_workers
Specify the exact number of test runners that will be spawned during parallel testing.
Equivalent to -parallel-testing-worker-count
scan(
concurrent_workers: 2
)

Resources