Configure iOS continuous integration stomp log using Jenkins

I will write before the article gradually transferred to the nuggets, but also hope that more people can see my article, learn together.

Jenkins is a continuous integration tool developed in Java. Here is how to use Jenkins for continuous integration of iOS. The pits are all marked in bold.

Install the pit

Before installing Jenkins, you need to install JDK first. Note that the JDK version must be 1.8, too high or too low.

Brew is recommended to install Jenkins.

brew install jenkins
Copy the code

Configuration of the pit

The first step

After the installation is complete, enter the following command:

jenkins
Copy the code

Note here that Jenkins’ default port is 8080. If this port is occupied, you can use the following command to switch ports:

jenkins -httpPort 9090
Copy the code

You can then enter Jenkins by typing localhost:9090 in your browser. The start page uses a start password to unlock it, which can be found on the command line or in the specified directory. Once you’ve entered, you’ll be asked to create an account. Just follow the instructions.

The second step

Once you have created your SSH key, you should first unbind it as follows:

Jenkins -> Credentials -> global -> add Credentials
Copy the code

The third step

After binding, start installing the following plug-ins:

  1. Keychains and Provisioning Profiles Management

Do not use the Xcode plugin for iOS configuration, because after Xcode 8, to generate ipA from Archive, you need to include an exportoption. Plist file, which is not provided in the plugin, so use the script to configure. Instead of being configured through the XCode plug-in.

After installing the plug-in, configure the plug-in directly through the information of various channels on the Internet. There is basically no big problem in configuring the plug-in. The following is the path of KeyChains and provision in the system.

keychain: /Users/zcating/Library/Keychains
provision: /Users/zcating/Library/MobileDevice/Provisioning Profiles
Copy the code

Login. keychain-db is the same file as login.keychain-db. You only need to copy it. Then change login.keychain-db to login.keychain and upload.

The fourth step

Once the upload is complete, you can start the build process as follows:

1. Create a new project and choose a free style. 2. Select Keychain and provision. 3. Add Git configuration. 4. Add build steps and select Execute Shell.Copy the code

Then add the following script:

#Custom parameters are required
xcode_project_path="/path/to/your/xcode/project"
export_path="/path/to/your/export/path"
ipa_name="ipa-name"
provision="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
#Build method, you can enter the following options: app-Store, ad-hoc, Enterprise, development
build_method="enterprise"
#The bundle_id must be the same as that in the project
bundle_id="com.yourCompany.yourApp"
bundle_name="provision_name"
#The signature type can be:"iOS Developer"."iOS Distribution"
sign_cer="iOS Distribution"
#It's the username in your team's certificate
team_id="XXXXXXXXXX"

#Completion of customization

export_option_path="$xcode_project_path/ExportOptions.plist"

#Write ExportOption. Fileecho "<? The XML version = \ \ "1.0" encoding = \ "utf-8 \"? > <! DOCTYPE plist PUBLIC \ "- / / / / DTD plist Apple / 1.0 / EN \" \ "http://www.apple.com/DTDs/PropertyList-1.0.dtd\" > < plist Version =\"1.0\"> <dict> <key>compileBitcode</key> <false/> <key>method</key> <string>$build_method</string> <key>provisioningProfiles</key> <dict> <key>$bundle_id</key> <string>$bundle_name</string> </dict> <key>signingCertificate</key> <string>$sign_cer</string> <key>signingStyle</key> <string>manual</string> <key>stripSwiftSymbols</key> <true/> <key>teamID</key> <string>$team_id</string> <key>thinning</key> <string>&lt; none&gt; </string> </dict> </plist>" > $export_option_path; xcodebuild archive \ -archivePath "$xcode_project_path/build/${ipa_name}.xcarchive" \ -project $xcode_project \ -sdk Iphoneos \ -scheme $scheme \ -configuration $build_configuration \ CODE_SIGN_IDENTITY=" Name of signature in key string "\ PROVISIONING_PROFILE=$provision xcodebuild -exportArchive \ -archivePath "$xcode_project_path/build/${ipa_name}.xcarchive" \ -exportPath $export_path \ -exportOptionsPlist $export_option_path \ -allowProvisioningUpdates \ CODE_SIGN_IDENTITY=" Name of the signature in the keystring "\ PROVISIONING_PROFILE=$provision MV ${export_PATH}/*.ipa ${export_path}/ipa_name.ipaCopy the code

Note that the XCode project needs to turn off autosignature configuration.

At this point, as long as the project opens without errors in XCode and can be built, you should have no problems.

Unity in particular article

There are a few things to be aware of when building Unity with Jenkins.

Disable automatic signature and set provision.

In Assets directory under the new directories and files/Assets/editor/ProcessBuild. Cs, and add the following code in the cs file.

using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System;

class ProjectBuild : Editor{

	// Find all the scenario files for the current project here.
	static string[] GetBuildScenes()
	{
		List<string> names = new List<string> ();foreach(EditorBuildSettingsScene e in EditorBuildSettings.scenes)
		{
			if(e==null)
				continue;
			if(e.path == "Dont_Add" || e.path == "post")
			if(e.enabled)
				names.Add(e.path);
		}
		return names.ToArray();
	}

	// Get the project name
	public static string projectName
	{
		get
		{ 
			foreach(string arg in System.Environment.GetCommandLineArgs()) 
			{
				if(arg.StartsWith("project"))
				{
					return arg.Split("-"[0[])1]; }}return "test"; }}// The shell script calls this static method directly
	static void BuildForIPhone()
	{ 
		PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, "USE_SHARE");
		// The core method of building xcode projects,
		// All scenarios that need to be packaged for parameter 1
		// Parameter 2 needs to be packaged by name
		// Parameter 3 package platform
		// Parameter 4 compiler options
		BuildPipeline.BuildPlayer(GetBuildScenes(), "ios-build", BuildTarget.iOS, BuildOptions.None); }}Copy the code

Use the following commands to automate the build.

project_dir=""
/Applications/Unity/Unity.app/Contents/MacOS/Unity \
-batchmode \
-projectPath $project_dir \
-executeMethod ProjectBuild.BuildForIPhone \
-ios \
-quit \
-logFile $project_dir/BuildXCodeProject.log 

Copy the code

This will generate the XCode project in the directory you specify.

conclusion

I used Jenkins to generate the final IPA for iOS and felt the build flow was silky smooth. With one click, you can upload Testflight, fir. Im or dandelion. Feeling that later packaging really will be convenient many many times, eliminating all kinds of complicated steps. The solution was to write a Shell script, which saved a lot of plug-in configuration problems.