刘浩的技术博客

就当是做个笔记,顺便分享一些知识,更希望业界的交流

Jenkins+fastlane持续集成

iOS持续集成

用Jenkins配置job,在合适的时机自动触发从git上拉取最新代码、自动构建、自动运行Test最后通过slack通过发送通知形式把结果反馈给team成员。

安装工具

  1. 更新brew包管理工具 $ sudo brew update
  2. 安装jenkins $ sudo brew install jenkins
  3. 安装fastlane $ sudo gem install fastlane
  4. 安装fir-cli $ sudo gem install fir-cli

brew error You can use brew with sudo, but only if the brew executable is owned by root. However, this is both not recommended and completely unsupported so do so at your own risk. 解决 $ sudo chown -R root /usr/local 参考:

根证书过期,下载重新安装: 根证书下载

fastlane自动构建、自动测试等等

  • cd [your_project_folder]
  • fastlane init
  • Follow the setup assistant, which will set up fastlane for you
  • Edit Fastfile `` fastlane_version "1.81.0" default_platform :ios platform :ios do before_all do ENV["SLACK_URL"] = "https://hooks.slack.com/services/..." #cocoapods #increment_build_number cocoaPods xcodebuild(workspace: "LHAlertCustomViewDevApp.xcworkspace",scheme: "LHAlertCustomViewDevApp",sdk: "iphonesimulator",destination: "platform=iOS Simulator,name=iPhone 6s") #gym(workspace: "LHAlertCustomViewDevApp.xcworkspace") end desc "Runs all the tests" lane :test do xctest(scheme: "LHAlertCustomViewDevApp",destination: "platform=iOS Simulator,name=iPhone 6s", reports: [ { report: "html", output: "build/reports/unit-tests.html" }, { report: "junit", output: "build/reports/unit-tests.xml" } ], clean: nil) #scan end desc "Submit a new Beta Build to Apple TestFlight" desc "This will also make sure the profile is up to date" lane :beta do # match(type: "appstore") # more information: https://codesigning.guide #gym # Build your app - more options available #pilot # sh "your_script.sh" # You can also use other beta testing services here (runfastlane actions`) end desc “Deploy a new version to the App Store” lane :appstore do

    match(type: “appstore”)

    # snapshot #gym # Build your app - more options available #deliver(force: true) # frameit end

    You can define as many lanes as you want

    after_all do |lane| # This block is called, only if the executed lane was successful slack( message: “Successfully test new App Update.” ) end error do |lane, exception| slack( message: exception.message, success: false ) end end ``` 在终端输入fastlane test测试自动构建,自动测试是否成功,也可以用xtool来构建和测试工程。

    fir-cli

  • 进入slack.com注册自己的账号,可以自己创建team,也可以加入别的team。
  • 进入https://ajiosteam.slack.com/services/B0ZRJH8UU?added=1%E7%94%9F%E6%88%90SLACK_URL%E3%80%82
  • 设置Fastfile中的slack地址。

jenkins配置

  1. 创建job
  2. 打开本地
  3. Manage Jenkins > Manage Plugins > Available 安装插件
    • HTML Publisher Plugin
    • AnsiColor Plugin
    • Rebuild Plugin
    • GIT Plugin
  4. 上一步结束点击 Back to Dashboard > New Item > 键入Item name > 选择Freestyle project > ok
  5. 配置job Source Code Management:/Users/liuhao/Documents/gittest/LHAlertCustomView Branches to build:*/master Add build step > Execute shell Command: #!/bin/bash source ~/.bash_profile cd LHAlertCustomViewDevApp/ fastlane test or #!/bin/bash export LC_ALL="en_US.UTF-8" fastlane test
    • Save job
  6. 自动job
    • Build Triggers > Build periodically 在每周的工作日的每小时的前半个小时,每过十分钟触发一次 H(0-29)/10 * * * 1-5

参考

fastlane
Jenkins Integration

Note

error:

修改文件为可读可写: –httpListenAddress=0.0.0.0替换–httpListenAddress=127.0.0.1 sudo chmod 666 ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist

Mac-mini:~ dongmeiliang$ sudo chmod o-w /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist Password: Mac-mini:~ dongmeiliang$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist: Path had bad ownership/permissions Mac-mini:~ dongmeiliang$ sudo chmod g-w /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist Mac-mini:~ dongmeiliang$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist: service already loaded Mac-mini:~ dongmeiliang$ sudo chmod o-w ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist Mac-mini:~ dongmeiliang$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist: service already loaded Mac-mini:~ dongmeiliang$ vim ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist Mac-mini:~ dongmeiliang$ vim /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist Mac-mini:~ dongmeiliang$ clear Mac-mini:~ dongmeiliang$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist /usr/local/Cellar/jenkins/1.643/homebrew.mxcl.jenkins.plist: service already loaded

注意

如果fastlane编译测试成功 但Jenkins运行编译成功测试失败并报 或者:invalid byte sequence in US-ASCII (ArgumentError)[0m

timeout connect(2)只是以上错误引起的。

可以fix错误 export LC_ALL=“en_US.UTF-8"写到你的shell里面

There are two ways to fix this. You can export language settings to your shell:

1:不推荐,这样会修改整个系统的设置。 export LANGUAGE=en_US.UTF-8 export LANG=en_US.UTF-8 export LC_ALL=en_US.UTF-8 or you can put this into your Gemfile: 2:推荐这个 you can put this into your Gemfile:(可以把一下代码加到你的Gemfile的头部) if RUBY_VERSION =~ /1.9/ Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 end 提醒:可以免密码登录 The authorized_keys file corresponds to the conventional ~/.rhosts file, and has one key per line, though the lines can be very long. After this, the user can log in without giving the password