Jenkins自动化打包(Gitlab)并上传蒲公英

看以下最新的文档 亲测可用

https://www.jianshu.com/p/531c959b8cf8

 调试过程中常见的问题可以看下面文档的后面

https://www.jianshu.com/p/68a19f28c51a

别人如何访问Jenkins地址

https://blog.csdn.net/sinat_37865456/article/details/123844525

/Users/hzs/Library/LaunchAgents/ 前往文件夹找 ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist 找不到的

切记  http://localhost:8080/restart 重启Jenkins不行,必须用下面指令重启

brew services restart jenkins

 

 这个插件要安装  用于选择分支  Git Parameter

 

Fastfile文件

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:iOS)

#蒲公英api_key和user_key
api_key = "9e10d6552c7bfac5f3c48196058a732e"
#user_key = "23ad8dc66ff14718fb55897eae138c55"
configuration = "Debug"#Debug_Production_Server

platform :iOS do
  desc "🚀🚀🚀🚀🚀打包并上传蒲公英🚀🚀🚀🚀"
  lane :debug do
  #指定项目的scheme名称
  scheme = "QuFangApp"
  gym(
    #输出的ipa名称
    output_name:"#{scheme}",
    # 是否清空以前的编译信息 true:是
    clean:true,
    scheme:scheme,
    # 指定打包方式,Release 或者 Debug
    configuration:"#{configuration}",
    # 指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development
    export_method:"development",
    # 指定输出文件夹
    output_directory:"./build/pgy",
    )
  pgyer(api_key: "#{api_key}")
  end
end

 

Jenkins脚本

#!/bin/bash
 
export LANG=en_US.UTF-8
 
export LANGUAGE=en_US.UTF-8
 
export LC_ALL=en_US.UTF-8
 
set -e
 
#输出错误
function echo_error() {
    echo "$1"
}
 
#输出信息
function echo_info() {
    echo "$1"
}
 
#打包
function packaging() {
 
    echo_info "====================开始===================="
 
    #参数校验 $#  添加到Shell的参数个数
    if [ $# != "1" ] || ([ $1 != "debug" ] && [ $1 != "release" ] && [ $1 != "appStore" ]); then
       echo_error '请指定打包类型:debug, release, appStore'
       exit 1
    fi
 
    #参数配置
    fastlane_output_path="./build/pgy"
    
    #你的蒲公英账号 API Key
    PGY_API_KEY="9e10d6552c7bfac5f3c48196058a732e"
 
    #定义一些符变量
    export v_env=$1
    export v_project_name="QuFangApp"
    export v_build_number="${BUILD_NUMBER}"
    export v_git_branch="${GIT_BRANCH}"
    export v_commit_node="$(git log -n 1 --pretty=format:"%h")"
    export v_download_URL="https://www.pgyer.com/ckcj"
    export v_QRCode_URL="https://www.pgyer.com/ckcj"
    export v_last_10_logs="$(git log -n 10 --pretty=format:"%h %ci %cn%n%s%n")"
 
    echo_info "====================安装pod依赖===================="
    #安装pod依赖
    #rm -rf './Pods/Local Podspecs'
    #pod update
    cd ${WORKSPACE}/QuFang-App/QuFangApp
    pod install
 
    #删除构建输出
    rm -rf "${fastlane_output_path}"
    
    #一个项目里面多个target设置
    target="QuFangApp"
    if [ $1 == "debug" ]; then
        target="QuFangApp"
    fi
    #版本号
    export v_app_version=$(echo "$(fastlane run get_version_number xcodeproj:QuFangApp.xcodeproj target:${target}))" | tr -d '\n' | sed -E 's/.*Result: ([0-9\.]*).*/\1/g')
    
 
    #构建
    echo_info "====================构建===================="
    ipa_name="QuFangApp"
    
    #fastlane打包
    fastlane ${v_env}
 
    #上传二进制包到蒲公英
    if [ $1 != "appStore" ]; then
        echo_info "====================上传二进制包到蒲公英===================="
        resp=$(curl -F "file=@${fastlane_output_path}/${ipa_name}.ipa" -F "_api_key=${PGY_API_KEY}" -F "buildInstallType=1" https://www.pgyer.com/apiv2/app/upload)
        echo "pgy_resp=${resp}"
 
        #解析数据,生成下载地址
        build_key=$(echo "$resp" | tr "\n" " " | sed -E 's/.*"buildKey" *: *"([^"]*)".*/\1/g')
        v_QRCode_URL=$(echo "$resp" | tr "\n" " " | sed -E 's/.*"buildQRCodeURL" *: *"([^"]*)".*/\1/g' | sed -E 's/\\\//\//g')
        v_download_URL="https://www.pgyer.com/apiv2/app/install?_api_key=${PGY_API_KEY}&buildKey=${build_key}"
    
        v_download_URL="https://www.pgyer.com/${build_key}"
 
        echo_info "二维码地址:${v_QRCode_URL}"
 
        echo_info "app下载地址:${v_download_URL}"
            
    fi
    
}
 
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120
 
#执行打包
packaging ${BuildType}

 

 

 

=================================================

下面的不用看 几年前的文档了

=================================================

整个过程详见:https://www.jianshu.com/p/91e8f571fc2b

以下是遇到的问题及解决过程

一、安装homebrew(网速很慢很慢……被墙了)

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

查看 Homebrew 版本

brew -v

详见:https://www.jianshu.com/p/117424d09d4c

 

homebrew换源,不然慢的要死

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git 
cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git 
echo 'export HOMEBREW_BOTTLE_DOMAIN=[https://mirrors.ustc.edu.cn/homebrew-bottles'](https://mirrors.ustc.edu.cn/homebrew-bottles' rel=) >> ~/.zshrc 
source ~/.zshrc

详见:https://www.jianshu.com/p/766c047f625f

 

二、安装jenkins(网速很慢很慢……被墙了,查看上面的homebrew换源)

brew install jenkins

A.如果出问题

Updating Homebrew...
jenkins: Java 1.8 is required to install this formula.
JavaRequirement unsatisfied!
You can install with Homebrew-Cask:
 brew cask install homebrew/cask-versions/java8
You can download from:
 https://www.oracle.com/technetwork/java/javase/downloads/index.html
Error: An unsatisfied requirement failed this build.

根据提示安装Java

brew cask install homebrew/cask-versions/java8

安装完成后再次执行

brew install jenkins

B.如果出问题

jenkins: Java 1.8 is required to install this formula. Install AdoptOpenJDK 8 with Homebrew Cask: brew cask install homebrew/cask-versions/adoptopenjdk8 Error: An unsatisfied requirement failed this build.

根据提示安装

brew cask install homebrew/cask-versions/adoptopenjdk8

安装完成后再次执行

brew install jenkins

 

三、启动jenkins

jenkins

在浏览器打开 http://localhost:8080 如果页面有内容就是安装成功了

 

四、获得账号密码

账号 admin

1.finder 前往文件夹 导航到/Users/Shared/Jenkins/Home文件夹;
2.右键单击secrets文件夹,选择显示简介
3.滚动到最下面,然后单击右下角的锁定图像>输入您的密码>确定;
4.点击弹出窗口左下角的“+”,将当前电脑用户添加到共享与权限中,设置为只读读与写;
5.打开secrets文件夹,找到该initialAdminPassword文件以获取初始的管理员密码。如果您没有该文件的权限,则需要右键单击该文件并选择显示简介,然后重复上述步骤3和4访问该文件。

 

也可以mac显示隐藏文件,找到secrets文件夹

defaults write com.apple.finder AppleShowAllFiles -boolean true

killall Finder

五、安装jenkins插件

目前安装的插件如下(安装插件时有时会报错,必须依赖其他插件时,按照提示优先安装需要的插件,再次重新安装即可):

branch-api.hpi

build-token-root.hpi

git-client.hpi

git-parameter.hpi

git.hpi

gitlab-api.hpi

gitlab-branch-source.hpi

gitlab-hook.hpi

gitlab-logo.hpi

gitlab-oauth.hpi

gitlab-plugin.hpi

locale.hpi

publish-over-ssh.hpi

ruby-runtime.hpi

xcode-plugin.hpi

 

 

A.jenkins系统管理>>管理插件>>高级 菜单

下将升级站点中的URL设为
http://mirror.xmission.com/jenkins/updates/current/update-center.json
保存并且重启Jenkins

进入 系统管理>插件管理>可选插件,右上角搜索框输入需要的插件名字,选中点击安装。经常会下载失败。可以通过下面B的方式,先下载再上传即可解决。

B.如果A的在线下载失败,可以在此http://updates.jenkins-ci.org/download/plugins 搜索需要的hpi文件,下载后然后进行以下操作

系统管理>插件管理>高级>上传插件(选择文件上传即可)

详见:https://blog.csdn.net/ZNWhahaha/article/details/81269041 

因为jenkins版本过低导致插件安装失败

详见:https://www.cnblogs.com/sxdcgaq8080/p/10489326.html

详见:下载后手动更换 jenkins.war 文件 https://www.cnblogs.com/huangzs/p/11719581.html 

 

六、jenkins修改显示语言为中文

下载插件 Localization: Chinese(Simplified)

安装后 系统管理 -> 系统设置 -> Locale

设置为 zh_cn  简体中文

重启jenkins

详见:https://www.cnblogs.com/ycyzharry/p/11664654.html

 

七、新建项目 

新建Item 输入项目名字,构建一个自由风格的软件项目即可

 

八、配置git

配置Repository URL是http连接后如果报以下错误,git仓库总是连不上,可以在gitlab配置SSH认证,然后jenkins里git仓库地址改为SSH地址,输入git的账号密码,设置分支即可解决。

Failed to connect to repository : Command "/usr/local/git/git-2.12.2/git ls-remote -h https://github.com/Frodez/jenkinsTest.git HEAD" returned status code 128:
stdout:
stderr: fatal: unable to access '/root/etc/gitconfig':

解决方式:

Git - 生成 SSH 公钥

ssh-keygen
cd ~/.ssh
open ~/.ssh

打开文件夹,就可以找到id_rsa.pub文件了,里面内容就是公钥。

gitlab 点击账号头像>Settings>SSH Keys>Add an SSH key (Title写邮箱,Key写id_rsa.pub的内容)

详见:https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E7%94%9F%E6%88%90-SSH-%E5%85%AC%E9%92%A5

 

九、配置Xcode

1.Xcode Schema File 配置为工程里Target名字

2.Configuration 配置Release 或者Debug

3.勾选 Pack application, build and sign .ipa?

Export method 配置为证书方式 ad-hoc

.ipa filename pattern 配置为 Target名字,方便上传蒲公英找到ipa位置

4.Code signing & OS X keychain options

勾选 Manual signing

然后配置Bundle ID 和 配置文件的UUID(找到配置文件,右键显示简介即可看到UUID)

4.Advanced Xcode build options

Xcode Workspace File 配置为 ${WORKSPACE}/Target名字/Target名字

 

十、上传到蒲公英

uKey 和 apiKey 从以下网址获取

http://www.pgyer.com/doc/api

IPA_PATH路径中***改为jenkins中生成的ipa路径名字

 

#蒲公英上的User Key
uKey="******"
#蒲公英上的API Key
apiKey="*******"
#要上传的ipa文件路径
IPA_PATH="${WORKSPACE}/build/Release-iphoneos/***.ipa"
#密码
MSG="${commitMsg}"
#执行上传至蒲公英的命令
echo "++++++++++++++upload+++++++++++++"
curl -F "file=@${IPA_PATH}" -F "uKey=${uKey}" -F "_api_key=${apiKey}" -F "updateDescription=${MSG}" -F "password=${PASSWORD}" https://qiniu-storage.pgyer.com/apiv1/app/upload

 

十一、开始构建 Build Now

构建历史中 蓝色为成功 红色为失败 灰色为取消

posted @ 2019-10-28 17:13  黄增松  阅读(1318)  评论(0编辑  收藏  举报