How to Install xcode.app via terminal only (without using GUI)

Viewed 1130

The goal: is to automate the macOS app notarization using Amazon EC2 with the Jenkins pipeline.

Problem statement: The EC2 macOS AMIs only has the Xcode command-line tool preinstalled in those AMIs, I can't install the Xcode.app via ssh/command line onto the EC2 machine. To install the Xcode, it has to be via the Apple App Store, using the GUI to log in first. The Xcode.app is required for the notarization.

Attempts / Experimented

I have tried the mas cli which is no longer to support sign in from the command line. https://www.funkyspacemonkey.com/use-the-mac-app-store-from-the-command-line

In macOS High Sierra (10.13) Apple removed the -[ISAccountService signInWithContext:replyBlock:] method from the StoreFoundation private Apple framework which this app relies on to authenticate users through the signin command. I have done a lot of digging and debugging but have not found another API that works to authenticate a user with the Mac App Store.

Users running older macOS versions (10.9-10.12) are unaffected.

You need to have xcode.app for not notarization https://developer.apple.com/forums/thread/126350 The documentation seems clear enough:

"Prepare Your Software for Notarization Notarization requires Xcode 10 or later. Building a new app for notarization requires macOS 10.13.6 or later. Stapling an app requires macOS 10.12 or later."

It seems the 'altool' is used for the notarization for xcode12. I tried to copy the altool from my dev machine to the Jenkins machine /Library/Developer/CommandLineTools/usr/bin and got this error when running it.

dyld: Library not loaded: @rpath/ITunesConnectFoundation.framework/Versions/A/ITunesConnectFoundation
  Referenced from: /Library/Developer/CommandLineTools/usr/bin/altool
  Reason: image not found
zsh: abort      xcrun altool

Even the otool -L doesn't show the altool has any linkage with the xcode.

/Library/Developer/CommandLineTools/usr/bin/altool:
@rpath/ITunesConnectFoundation.framework/Versions/A/ITunesConnectFoundation (compatibility version 1.0.0, current version 1.0.0)
@rpath/AppStoreService.framework/Versions/A/AppStoreService (compatibility version 0.0.0, current version 1.0.0)
/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1122.24.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1775.109.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1775.109.0)
/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 59754.100.88)
1 Answers

xcode-install is a nifty tool to install, update, and manage Xcode versions on the command line. Works great with automation tools like Ansible, too. It downloads Xcode, simulators from the Apple Developer Portal using fastlane. This means you need an ADP account.

Installation:

$ gem install xcode-install

You also need to set the environment variables XCODE_INSTALL_USER and XCODE_INSTALL_PASSWORD (use your ADP login). If your ADP account does not have 2FA enabled, you need to set SPACESHIP_SKIP_2FA_UPGRADE=1, too. If your ADP account has 2FA enabled, please see this issue.

$ xcversion list

lists all available Xcode versions. To install, for example, 13.2.1, run:

$ xcversion install 13.2.1

Ensure that you have at least 30 GB of free disk space. Otherwise the decompression of the Xcode archive fails. If you need to increase the size of your EBS volume, see this article in the AWS documentation on how to do it on a Mac.

Of course, it is also possible to use Screen Sharing (VNC) to install Xcode via the GUI. To enable Screen Sharing on AWS:

  1. Set a user password if you haven't already:

    $ sudo passwd ec2-user
    
  2. Enable Screen Sharing:

    $ sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
      -activate -configure -access -on \
      -restart -agent -privs -all
    
  3. Start an SSH tunnel on your local computer:

    $ ssh -L 5900:localhost:5900 -i /path/my-key-pair.pem ec2-user@my-instance-public-dns-name
    
  4. Connect to your AWS Mac with Screen Sharing.app from your local computer by connecting to localhost:5900.

Related