透過 Jenkins 從 Gitlab 拉 Source 自動建置 Xamarin iOS Mobile App

公司中許多的專案都有 Auto Build 的機制,那 Mobile App 也有 Auto Build 的機制嗎?
ios 的 App 需要 Mac 的環境。本文將介紹如何透過 Jenkins 從 Gitlab 拉 Source 自動建置  Xamarin iOS Mobile App。

架構

Mac 環境建置

1.安裝所需憑證,我們公司是使用 Enterprise Store ,如下,

2.安裝 Visual Studio 2017 for mac

註:如果在建置時,有發生 csc.exe exited with code 1 的錯誤,可以參考「Visual Studio for Mac “csc.exe exited with code 1” when building Xamarin.iOS project.」,從下載舊版的 Mono   4.8.1.0 ,重啟OS後,再用 Xamarin Studio 去建置。

3.安裝 Git ,因為要從 Gitlab 拉 Source 下來。

4.驗證專案是否可以正常建置並發佈 ipa 。

 

SSH Command 測試

當透過 IDE 工具可正常建置並發佈後,就可以透過 SSH 去拉原始碼下來為建置及發行。

1.先透過 ssh 登入到 Mac (windows 請用 putty.exe)

2.從 Gitlab 將 Source Clone 到 Local

想要不用再輸入密碼從Gitlab clone 下來可以使用

git clone https://username:password@remote folderName; 

或是

git clone git@remot folderName;

我們選擇後面這種方式(因為http方式,密碼是明碼,而且密碼有特殊字元,也不行),詳細可以參考(多重 SSH Keys 與 Github 帳號)。

2.1.所以我們就來建立 sshkey (可以 -C 設定檔案名稱), 如下,

cd ~/.ssh;
ssh-keygen -t rsa -b 4096;

2.2.將 .pub 檔案內容(public key)  新增到 Gitlab 之中(profile/keys),如下,

2.3.建立 ~/.ssh/config (vim ~/.ssh/config),

#公司工作用的 GitHub 帳號
Host gss
    HostName 655git.655.com.tw(這裡可用ip or fqdn)
    User rainmaker
    IdentityFile ~/.ssh/id_rsa

2.4.將 Source 從 Gitlab 拉下來(@gss,就是對應到 .ssh/config 中的 Host ),

git clone git@gss:BeaconSystem/iBeep-iOS.git iBeep-iOS;

3.透過 xbuild 來建置產生 ipk 檔案

切換到專案的目錄,輸入  xbuild 來建置,如下,

xbuild /p:Configuration=Release /p:Platform=iPhone /p:ArchiveOnBuild=true /p:BuildIpa=true

這時會發生 codesign FAILED 的問題,訊息如下,

Codesign Task
CodesignAllocate: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
DisableTimestamp: False
Entitlements: obj/iPhone/Release/Entitlements.xcent
Keychain: <null>
Resources:
bin/iPhone/Release/GSS_Beacon.app
ResourceRules: <null>
SigningKey: 23A1BADAF10F41
ExtraArgs: <null>
IsAppExtension: False
Tool /usr/bin/codesign execution started with arguments: -v –force –sign 23A1BADAF10F –entitlements /Users/gsstds/iBeep-iOS/obj/iPhone/Release/Entitlements.xcent /Users/gsstds/iBeep-iOS/bin/iPhone/Release/GSS_Beacon.app
bin/iPhone/Release/GSS_Beacon.app: error : /Users/gsstds/iBeep-iOS/bin/iPhone/Release/GSS_Beacon.app: unknown error -1=ffffffffffffffff
Task “Codesign” execution – FAILED

直接拿 xbuild 中的 codesign 去 sing app 檔,還是一樣的錯誤,如下,

透過 codesign -vv -d 會發現 app 檔並沒有被 sign 成功,如下,

這個是因為透過 ssh 連進去時,Keychain 被 lock 了,所以需要再 unlock ,請下以下的 command

security -v unlock-keychain -p <<登入者的密碼>> ~/Library/Keychains/login.keychain;

再將 bin 目錄似 obj 目錄刪掉,重新再 run 一次 xbuild 就可以正常產出 ipa 檔了。

我們可以在專案目錄下新增一個 ipa 的Folder來放建置完成的 ipa 檔,如下,

mkdir ipa;
xbuild /p:Configuration=Release /p:Platform=iPhone /p:ArchiveOnBuild=true /p:BuildIpa=true /p:IpaPackageDir=./ipa/;

 

透過 Jenkins 來執行 SSH Command

所以透過 ssh 來建置 ios 的 command 如下(透過 Jenkins 使用 nuget, xbuild 需要輸入 full path),

rm -rf ~/iBeep-iOS;
git clone git@gss:BeaconSystem/iBeep-iOS.git iBeep-iOS; 
cd iBeep-iOS;
/Library/Frameworks/Mono.framework/Commands/nuget restore;
mkdir ipa;
security -v unlock-keychain -p <<登入者的密碼>> ~/Library/Keychains/login.keychain;
/Library/Frameworks/Mono.framework/Commands/xbuild /p:Configuration=Release /p:Platform=iPhone /p:ArchiveOnBuild=true /p:BuildIpa=true /p:IpaPackageDir=./ipa/;

其中 security unlock-keychain 需要密碼,所以可以設定在 Jenkins Job  「建置環境」中的 Inject passwords to the build as environment variables ,如下,

所以 Jenkins ssh 那就可以使用到它 ${keychainPW}

rm -rf ~/iBeep-iOS;
git clone git@gss:BeaconSystem/iBeep-iOS.git iBeep-iOS; 
cd iBeep-iOS;
/Library/Frameworks/Mono.framework/Commands/nuget restore;
mkdir ipa;
security -v unlock-keychain -p ${keychainPW} ~/Library/Keychains/login.keychain;
/Library/Frameworks/Mono.framework/Commands/xbuild /p:Configuration=Release /p:Platform=iPhone /p:ArchiveOnBuild=true /p:BuildIpa=true /p:IpaPackageDir=./ipa/;

另外,如果透過 Jenkins 連進去有 Algorithm negotiation fail 的錯誤,可依以這篇(JSch连接SSH问题Exception:Algorithm negotiation fail)去設定哦!

註:雖然 Command 沒幾行,但是卡在 codesign 那卻卡了很久,能順利完成,非常感謝 ISBG 的 Henry Kuo 、 Alice 及 QAC 的 Nita ^_^

參考資料

Visual Studio for Mac “csc.exe exited with code 1” when building Xamarin.iOS project.

多重 SSH Keys 與 Github 帳號

JSch连接SSH问题Exception:Algorithm negotiation fail

代码签名探析

iOS Code Signing 学习笔记

作者: 亂馬客

亂馬客 @叡揚資訊 rainmaker_ho@gss.com.tw https://rainmakerho.github.io/ https://www.slideshare.net/rainmakerho

透過 Jenkins 從 Gitlab 拉 Source 自動建置 Xamarin iOS Mobile App 有 “ 1 則迴響 ”

  1. Trying To Find The Best Diet Pill?

    Trying to find the best diet pill may seem like an impossible task, especially with the multitude of diet pills available for purchase. Many people purchase a diet pill only to find out that the pill makes them feel jittery, nervous, or often has no effect at all.

    Diet pills frequently contain the same or similar combination of ingredients and rarely contain anything new, innovative, or undiscovered to the supplement / weight loss industry. So, how can you find the best diet pill when most diet pills are made with similar ingredients?

    One of the most common problems associated with taking diet pills is that the person taking the diet pill is uneducated about the dosage, effects, and promises offered as they relate to each diet pill. The research at website finds that there are three factors that should be taken into consideration when deciding to take a diet pill.

    Dosage:
    It is important to take the pill exactly as recommended on the product label. Some people choose to increase the dosage thinking that the product will work faster or better. This is not the case, and many people become sick in response to the large dose. Reviewers at website often suggest that the recommended dosage be cut in half to give the body time to adjust to the stimulant in the diet pill. After the body has adjusted, it is fine to begin taking the regular dosage as recommended on the product label.

    Effects:
    The effects listed on the product label are there because these are the effects that the product has had on ‘some’ of the test group. Some of the diet pill testers may be fine taking the product, while others may have adverse effects. The diet pill companies print this information to educate the buyer as well as to protect themselves from lawsuits. The consumer needs to read the label and educate themselves before taking the product. Many people who are sensitive to caffeine are surprised when the diet pill makes them feel nervous or nauseous, but this information is likely printed on the product, so with a little research these affects can be avoided.

    Promises:
    If you read the fine print on product claims for diet pills and other weight loss supplements, you will see ‘results not typical’ printed very small somewhere where you are not expected to look. The diet pills advertised on television are responsible for some of the most outlandish claims. The results claimed in these advertisements are often unattainable within the given amount of time outlined in the ad. Don’t expect to see results in two weeks like a lot of ads claim.

    Wouldn’t it be great if you could read reviews for diet pills from actual users of each diet pill? Diet Pill Reviews website has taken the trouble out of searching for the best diet pill. You can read reviews of over 150 of the most popular diet pills available.

    Copyright 2006, Diet Pill Reviews viagra pas cher

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *