Every time a new version is to be released, the testing department has been urged to dog, the testing department can not bear, want to package their own, r&d just submit the code, hear this news, or very happy, finally do not need to package. After three days of torturing Jenkins with my colleagues, I finally exported the IPA package normally!!

Because most of the online tutorials rely on Github, and many of them configure Xcode parameters in Jenkins, which is quite troublesome, we use Shell script, very easy. Record the process of setting up the environment here, hoping to help others reduce a little pit.

1 installation Jenkins

Jenkins is a continuous integration tool based on Java development. So, to use Jenkins, you must install the JDK first.

The JDK installation

JDK download

Jenkins installation

Jenkins download

Note:

1. After Jenkins is installed successfully, a Jenkins user will be created, and the default working interval of Jenkins is in the [/ user/share /Jenkins /home/jobs] directory, which can be accessed by Finder–>.

2. The read and write permission of the folder under Jenkins directory is only open to Jenkins users, so the apple certificate and other documents must be installed under Jenkins users, and the IPA export of the project must also be operated under Jenkins users. (Or change the directory’s permissions to users with administrator privileges)

3, Jenkins is available to every user, so it may cause errors when building the version, so it is better to operate under Jenkins users.

Test Jenkins successfully installed

Open the browser and enter http://localhost:8080. If Jenkins can be opened normally, the Jenkins installation is successful.

2 Install the Jenkins plug-in

There are quite a few plugins in Jenkins, so install the plugins for the tools you use.

For example, if you use SVN, install the SVN plug-in. If you use Git, install the Git plug-in.

Since I already have SVN installed, I will use Git to demonstrate installing the plug-in.

3 Xcode and development certificate Settings

Because you want to use the Xcode command, you must ensure that the Xcode Command Line is installed.

3.1 Setting the Apple Development Certificate

Export the distribution certificate under the original Xcode development office user. If you want to export the enterprise package ($299) and corporate/personal package ($99), export both certificates and copy them to Jenkins’ user environment. Double-click to install them into the Mac keystring.

Note: Since the user needs permission to access the certificate in the keystring, and when building with Jenkins, neither Xcode plug-in configuration nor shell script can enter the user password, the [access control] of the certificate must be set to allow all applications to access the project.

3.2 Installing mobileProvision Description Files

Also need to install the phone description file required for packing under Jenkins user.

4 Configure the build project

The following describes the configuration of the build project. You can use the local project or the SVN project (just fill in the SVN project address), and then enter the shell script to start the build.

4.1 Build using local projects

The steps are as follows:

For other Settings, you only need to set the script. The details of the script are as follows:

# project name
APP_NAME="HelloJenkins"
# certificate
CODE_SIGN_DISTRIBUTION="iPhone Distribution: XXXXXXXXXXXX"
# info. Plist path
project_infoplist_path=". /${APP_NAME}/Info.plist"

if[!-f "$project_infoplist_path" ]
then
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "*** plist file path error! * * * *"
echo "*** plist file path error! * * * *"
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
exit
fi

Get the version number
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${project_infoplist_path}")

# build value
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${project_infoplist_path}")

DATE="$(date +%Y%m%d%H%M%S)"
IPANAME="${APP_NAME}_V${bundleShortVersion}_${DATE}.ipa"

# export path
IPA_PATH=~/"${IPANAME}"

echo "=================clean================="
xcodebuild -target "${APP_NAME}"  -configuration 'Release' clean

echo "+++++++++++++++++build+++++++++++++++++"
xcodebuild -target "${APP_NAME}" -sdk iphoneos -configuration 'Release' CODE_SIGN_IDENTITY="${CODE_SIGN_DISTRIBUTION}" SYMROOT='$(PWD)'

xcrun -sdk iphoneos PackageApplication "./Release-iphoneos/${APP_NAME}.app" -o "${IPA_PATH}"

if [ -f "$IPA_PATH" ]
then
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "* iPa exported successfully *"
echo "* iPa exported successfully *"
echo "* iPa exported successfully *"
echo "* iPa exported successfully *"
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "Installation file path:${IPA_PATH}"
# ipA file path to upload to dandelion
echo "${IPA_PATH}">> text.txt
else
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "* iPa export failed *"
echo "* iPa export failed *"
echo "* iPa export failed *"
echo "* iPa export failed *"
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "Installation file path:${IPA_PATH}"
fi
Copy the code

Note 1: [-o ~/$IPANAME] indicates that the exported IPA file is in the current user’s directory, namely [/ user/share /Jenkins/]. CODE_SIGN_IDENTITY=”iPhone Distribution: XXXXXXXXXX “is the common name in the keystring for the certificate you package. The exported IPA is called helloJenkins_v1.2_20160118.ipa.

Note 2: If you create a sh script file in the same directory as xcodeProj and use the [sh xxx.sh] command, see the next introduction. If you use Cocoapods in your project, there are a few parameters in the script that need to be adjusted, as described in the next article.

Note 3 (2016.02.17 update) : CODE_SIGN_IDENTITY can be set without setting the CODE_SIGN_IDENTITY attribute. You can set the profile directly, and the corresponding CODE_SIGN_IDENTITY will be automatically matched during compilation. It should be noted that the UUID value is set when setting the profile. For example [PROVISIONING_PROFILE=’ f035763E-E847-4DB8-AC10-0004809fdc90 ‘]

Click Save, then click the left menu to build now and start building.

4.2 Using the SVN ADDRESS for Construction

Step 1, create a new project, same as above.

Step 2, instead of copying the project to the Jobs directory, directly set the SVN address in the source management section of the configuration

In this case, if you want to build a project of a certain version on the SVN, you only need to add the @ version number after the path. For example: http://192.168.1.1:8999/svn/iOS/TestDemo@150.

Step 3, set up the shell script as above.

Step 4, build immediately.

Tip: After a successful build, you can also set some options, such as automatic uploading to dandelion or fir.im, or email notifications.

You can also set build triggers, setting conditions such as an automatic build at some point. Because these Settings are pretty simple, and we haven’t used them yet, you can explore them for yourself.