Remember before

In order to improve work efficiency, automatic packaging was adopted to solve the problem. At the beginning, I also found many tutorials on the Internet, and I was confident to complete the pit, but I didn’t expect the pit to be more than I imagined. Some articles are even plagiarized and outdated, leading to failure to implement directly according to the tutorial. Most of the online tutorials are Jenkins’ Xcode plug-in and certificate management tutorials. However, the most frustrating part here is that the path of the description file uploaded could not be identified, so the certificate could not be correctly configured when building the version. Later, I gave up this method. In addition, the Jenkins installation method is also a variety of problems. Permissions often occur when scripts are executed after installation using the official website package, and the problem cannot be solved by changing permissions. There are many pits, but there is no record one by one. In this paper, Jenkins + Fastlane is used for automatic packaging, and remote packaging is controlled by LAN. This article describes the configuration of some environments and the pits encountered.

Install Homebrew

  1. Check the installation script: https://raw.githubusercontent.com/Homebrew/install/master/install, and copy all the contents for brew_install. Sh file

  2. Replace the following part of the brew_install script. Note that the CORE_TAP_REPO parameter is missing from the new version of HomeBrew and is changed to: HOMEBREW_CORE_TAP. The second line below may be changed as needed

    BREW_REPO = "https://github.com/Homebrew/brew".freeze
    CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze
    Copy the code

    Change to

    BREW_REPO="https://mirrors.ustc.edu.cn/brew.git".freeze
    CORE_TAP_REPO = "https://mirrors.ustc.edu.cn/homebrew-core.git".freeze
    Copy the code
  3. Execute the brew_install script (note that XXX is replaced with the actual file path) :

    /usr/bin/ruby xxx/brew_install.sh
    Copy the code
  4. If the following occurs:

    ==> Tapping homebrew/core
    Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'.Copy the code

    Homebrew-core is a clone of homebrew-core, so we can clone homebrew-core manually:

    cd "$(brew --repo)"
    cd Library/Taps
    mkdir homebrew
    cd homebrew
    mkdir homebrew-core
    cd homebrew-core/
    git clone git clone git://mirrors.ustc.edu.cn/homebrew-core.git /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
    Copy the code
  5. Brew Doctor checks installed BREW for problems and automatically checks and installs missing parts

  6. If the computer has homebrew, execute the upgrade operation, if the following error occurs, do not bother about the permissions of the problem, please reinstall it:

    ERROR: /usr/local/ must be writeable!
    Copy the code

    Someone directly found that folder to change the permission, is it found that there is no permission 😆, so directly use the following command:

    sudo chown -R $(whoami) /usr/local
    Copy the code

    Then you find that nothing works, so you find a new command:

    sudo chown -R $(whoami) $(brew --prefix)/*
    Copy the code

    😆😆, still useless, do not think my god, in fact, the solution is very simple, the front said do not toss about so much, directly uninstall reinstall it

    Uninstall 😁 :

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
    Copy the code

    Then follow the above tutorial installation.

Installing the Java Environment

  1. Because Jenkins needs to run in Java, preferably in Java 8 (as Jenkins needs Java 8), otherwise some unexpected things may happen, such as downloading the plug-in may fail and not be able to download and install it again

  2. The download address is portal

  3. Manually installing Java

Install Jenkins

  1. Since downloading the official website installation package may lead to Jenkins runtime permission issues and the possibility of plug-in download failure, it is best to install by Homebrew.

  2. Execute the install command: brew install Jenkins. There is also a long-supported version that suffixes Jenkins with -lts and other commands with **-lts**. Long-supported installation commands: brew install Jenkins-lts

  3. Start enabling Jenkins after completion:

    First method: Jenkins second method: open /usr/local/ opt/Jenkins/libexec/Jenkins. War is the front desk to start, the former need a terminal window to display the log, will be shut down Jenkins, the latter is a hidden way, you can close the terminal window Recommended use: The brew services start Jenkins in addition can also through the Mac system bring launchctl background management system in the configuration file, stored in ~ / Library/LaunchAgents/homebrew. MXCL. Jenkins. Plist, Parameter configuration needs to Google, brew way management is through this configuration file for background management, so it is recommended to use this wayCopy the code
  4. The Jenkins directory installed via BREW is stored at: ~/.Jenkins, and the workspace path is: ~/.Jenkins /workspace/

  5. After the installation and startup is successful, the initial password will be displayed in the terminal. You need to copy and change the password to enter Jenkins, or you can copy the initial password in the initial password file:

    cat ~/.jenkins/secrets/initialAdminPassword
    Copy the code
  6. Jenkins needs to set environment variables when writing scripts. Enter the following command in the terminal and set the obtained environment variables into Jenkins’ global environment variables

    echo $PATH
    Copy the code
  7. When Jenkins writes the script, it is best to set the encoding type and specify the working path of the script

    export LANG=en_US.UTF-8
    export LANGUAGE=en_US.UTF-8
    export LC_ALL=en_US.UTF-8
    
    ...
    
    cd $WORKSPACE # CD to work path. fastlane xxxCopy the code
  8. ** Fastlane init ** cannot be done in script, so when deploying the project and it is the first project, CD to the project directory and sudo:

    cd XXXX
    sudo fastlane init
    Copy the code

    If your project uses Cocoapods to manage third-party libraries, add a line to the Gemfile configuration file:

    gem "cocoapods"
    Copy the code

    When the second project is deployed, you can copy the fastlane configuration files of the first project, including Gemfile, gemfile. lock, and fastlane directory to the new project directory and change the configuration contents of Appfile and Fastfile.

Update RubyGems

1. You may not be able to install Fastlane due to versioning, so you need to update RubyGems.

2. The image source maintained by Taobao is no longer maintained. The image source is changed to gem Sources -a http://gems.ruby-china.com/

Add new mirror source (org domain is still registered, so use com) :

gem sources -a http://gems.ruby-china.com/
Copy the code

4. View the current mirror source:

gem source -l
Copy the code

5. Delete the old mirror source.

gem source --remove https://ruby.taobao.org/
Copy the code

6. If you do not have the permission to install or upgrade, if you do not have the permission to use sudo:

ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/bin directory.
Copy the code

Run the following command to resolve the problem:

gem update -n /usr/local/bin
Copy the code

If fastlane installation fails, the following plug-in loading error occurs:

Error loading RubyGems plugin "/ Users/yuguo. RVM/rubieslast/ruby - 2.2.2 / lib/ruby/gems / 2.2.0 / gems/the executable - hooks - 1.3.2 / lib/rubygems_plugin rb": dlopen (/ Users/yuguo /. RVM/rubieslast/ruby - 2.2.2 / lib/ruby / 2.2.0 / x86_64 - darwin16 / openssl. Bundle, 9) : the Library not the loaded: /usr/local/ opt/openssl/lib/libssl 1.0.0. Dylib Referenced from: / Users/yuguo /. RVM/rubieslast/ruby - 2.2.2 / lib/ruby / 2.2.0 / x86_64 - darwin16 / openssl. Bundle "Reason: Image not found - / Users/yuguo /. RVM/rubieslast/ruby - 2.2.2 / lib/ruby / 2.2.0 / x86_64 - darwin16 / openssl. Bundle (LoadError)Copy the code

Reinstall the latest Ruby version:

curl -sSL https://get.rvm.io | bash -s stable
Copy the code

Apply article 1 if the following words appear:

Please do one of the following:
* 'rvm reload'
* open a new shell
* 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.
* 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.
Copy the code
rvm reload
Copy the code

Lists the available versions of Ruby

rvm list known 

# Choose your versionRVM install 2.6Check the version number, and if it is the version you just installed, successfully install and apply the latest version
ruby -v
Copy the code

Then do the above update once to install Fastlane.

Use the fastlane

  1. The fastlane installation process, using gem installation, installation command is:

    sudo gem install fastlane -NV -n /usr/local/bin
    Copy the code
  2. For local packaging, you can use the fastlane init command in the project directory to initialize it and get Gemfile, fastlane/ folder, and gemfile.lock if using sudo

  3. The Gemfile file is configured as follows:

    gem "fastlane"
    gem "cocoapods"` Add this line when your project relies on Cocoapods to manage third-party libraries
    Copy the code
  4. Appfile and Fastfile in the Fastlane/directory. Appfile is configured as follows:

    app_identifier("com.xxx.xx.xxx") The Bundle ID of the project
    apple_id("[email protected]") # Apple id, usually email
    Copy the code
  5. The Fastfile configuration is as follows:

    default_platform(:ios)  Declare the packaging platform
            platform :ios do
            
                desc "This is a description of a task."
                lane :AP_Debug do       This is to declare a Fastlane command named AP_Debug
                gym(
            
                    scheme: "AutoPackage".# Project target
                    # Export ipA package type, there are five schemes, respectively: app-Store, ad-hoc, package, Enterprise, development, developer-ID
                    export_method:"ad-hoc",   
                    clean: true.Is it necessary to clean before packing? Generally, it is required to clean before packing
                    output_directory:"./build".# Directory where ipA packages are stored
                    output_name:"AutoPackage-Beta.ipa".Ipa package name
                    Xcodeproj ", # the name of the packaged project, xxx.xcodeproj or xxx.xcworkspace
                    #slient: true, # Whether to not output logging mode
                    #configuration: "Debug", # sets the type of packaging
                    #buildlog_path: "XXX/XXX ", # set the directory for storing packed logs
                    # coDesigning_identity: "iPhone Developer XXX (XXX)", # set the certificate for the current packaging mode
                    # Set the export IPA option
                    export_options: {
                        # config file Settings
                        provisioningProfiles: {
                        		# Bundle ID => config file name (no. Mobileprovision suffix required)
                            "com.xxx.xxx.xxx"= >"xxx provision file name", 
                        }
                    }
                )
            end
            
            desc "This is a new project"
            lane: xx_task_name do. end endCopy the code

6. Execute the method defined above:

fastlane AP_Debug
Copy the code

7. The package script needs to import the configuration environment of the current PC. You can run echo $PATH on the terminal to copy the environment configuration into the package shell script package

export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
exportPATH=[Environment configuration above]echo $WORKSPACE;
cd $WORKSPACE;
Check whether there is a Fastlane profile
bundle exec fastlane fastfile_defined_name
Copy the code

Afterword.

Because ipA packaging must rely on XcodeBuild, which relies on Mac OS, either use a real Mac or use a highly configured server to install virtual machines and then Mac OS. There are still some details to be filled in.