Jenkins实现扫码安装(四)

增加二维码,以供扫描安装

先看一下下载安装的网页

《Jenkins实现扫码安装(四)》 下载网页.jpeg

截止到现在,Jenkins里已经可以打包了,但是安装的话,除了上传蒲公英,http://fir.im这些,我们还可以自己生成二维码,一般情况下,我觉得不愿意把ipa包传到外网上,当然你也可以传,要拿到二维码需要看网站的API文档,按照文档去调用。下面我要讲一下怎么在自己的服务器上生成二维码。
我选用的是Python,用到的库是Python的Pillow库和QRCode模块,Mac本身应该安装有Python。

一、安装Pillow模块

用终端执行以下语句,安装pillow,先安装multiprocessing模块,再安装pillow模块,因为pillow对multiprocessing有依赖

sudo pip install multiprocessing
sudo pip install Pillow

二、安装QRCode模块

在QRCode官网下载模块包https://pypi.python.org/pypi/qrcodehttps://github.com/lincolnloop/python-qrcode
解压,终端进入到解压的文件夹下,执行以下语句来安装

python setup.py install

三、配置sublime成Python编译器

打开sublime—>Tools—>Build Systems—>Python
测试Python是否能正常工作,输入下面的句子,并且先保存成hello.py文件到桌面(要先保存,不然编译器不知道你这是个Python文件),command+b,编译以下,看打印台有没有输出hello,如果输出了hello,说明正常,可以直接使用

print("hello")

四、编写输出二维码的Python语句

二维码其实就是一个网址,一个连接,把它变成二维码,所以一扫描就可以进入到这个网址

import qrcode
img=qrcode.make("/Users/hongjie.xia/Desktop/index.html") // 这个路径是你放index.html文件的地方,上一节讲的,https服务器访问的根目录下放置index.html的地方
img.save("/Users/hongjie.xia/Desktop/QRCode.png")  // 这个是你要存储二维码的地址,根据自己的情况填写

我得实际配置Jenkins的二维码地址不是上面的,上面的是测试用的,到时候写到shell脚本里可以根据情况来修改地址

五、用shell脚本来创建可以生成二维码的Python文件,并且运行

!/bin/bash,#!/bin/python这两句必须写,告诉编译器是个shell脚本、Python脚本

#!/bin/bash
mkdir /Users/hongjie.xia/Desktop

cat > /Users/hongjie.xia/Desktop/GeverateQRCode.py << EOF // 这一句的意思是创建一个GeverateQRCode.py文件,并且往里面写内容,EOF是开头,编辑的末尾还要顶格写一个EOF
#!/bin/python
import qrcode
img=qrcode.make("/Users/hongjie.xia/Desktop/index.html")
img.save("/Users/hongjie.xia/Desktop/QRCode.png")

EOF

python  /Users/hongjie.xia/Desktop/GeverateQRCode.py  // 运行Python脚本

上面这段代码,大家可以放到sublime里进行编写,然后存成qrcode.sh文件,在终端里运行,运行的时候,需要授权,进入到放qrcode.sh的目录下,先输入以下命令

chmod 777 ./qrcode.sh

然后运行

./qrcode.sh

这个脚本就可以跑起来了

六、在Jenkins的构建脚本里写入生成二维码的命令

下面是我的构建脚本,大家可以直接改参数使用,我项目叫test,hongjie.xia是我的机子名称,192.168.1.1是我得服务器(配置https的本机)地址

project_path="/Users/hongjie.xia/Desktop/test"
hostAddress="https://192.168.1.1"

#指定项目地址
workspace_path="$project_path/test.xcworkspace"

#指定项目的scheme名称
scheme="test"
#指定要打包的配置名
configuration="Release"
#指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id,即xcodebuild的method参数
export_method='enterprise'

#服务器访问的地址
serverDocumentPath="/Users/hongjie.xia/ServerDocument"
#每次构建使用的plist文件地址
plistFileName="Plist_${BUILD_NUMBER}"
plistPath="$serverDocumentPath/${JOB_NAME}/${VERSION}/Plists/${plistFileName}"
#plist文件的名字
plistName="${JOB_NAME}Install.plist"
#plist文件的地址
plist_path="${plistPath}/${plistName}"

#每次构建后生成二维码的链接地址
htmlFileName="Html_${BUILD_NUMBER}"
htmlPath="$serverDocumentPath/${JOB_NAME}/${VERSION}/Htmls/${htmlFileName}"

#每次构建生成的二维码地址
qrCodeFileName="QRCode_${BUILD_NUMBER}"
qrCodePath="$serverDocumentPath/${JOB_NAME}/${VERSION}/QRCodes/${qrCodeFileName}"

#ipa和archive的输出路径
ipaFileName="Ipa_${BUILD_NUMBER}"
ipaPath="${serverDocumentPath}/${JOB_NAME}/${VERSION}/Ipas/${ipaFileName}"
#指定输出ipa名称
ipaName="${JOB_NAME}.ipa"
ipa_path="${ipaPath}/${ipaName}"

#指定输出archive名称
archiveName="${JOB_NAME}.xcarchive"
#指定输出归档文件地址
archive_path="${ipaPath}/${archiveName}"

#服务器打开本地html的地址
serverHtmlPath="${hostAddress}/${JOB_NAME}/${VERSION}/Htmls/${htmlFileName}"

#服务器打开的plist文件地址
serverPlistPath="${hostAddress}/${JOB_NAME}/${VERSION}/Plists/${plistFileName}/${plistName}"
#服务器打开的ipa文件地址
serverIpaPath="${hostAddress}/${JOB_NAME}/${VERSION}/Ipas/${ipaFileName}/${ipaName}"
#指定打包配置
export_plist_path="$project_path/ExportOptions.plist"
#获取执行命令时的commit message
#commit_msg="$(cat $project_path/build_log.txt)"

#输出设定的变量值
echo "===workspace path: ${workspace_path}==="
echo "===archive path: ${archive_path}==="
echo "===ipa path: ${ipaPath}/${ipaName}==="
echo "===export method: ${export_method}==="
#echo "===commit msg: ${commit_msg}==="

#先清空前一次build
xcodebuild clean -workspace ${workspace_path} -scheme ${scheme} -configuration ${configuration}
xcodebuild archive -workspace ${workspace_path} -scheme ${scheme} -archivePath ${archive_path}
xcodebuild -exportArchive -archivePath ${archive_path} -exportPath ${ipaPath} -exportOptionsPlist ${export_plist_path}

#生成二维码
#第一步,生成对应的plist文件
mkdir ${plistPath}
# html的位置应该为构建版本的信息
cat > ${plist_path} << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>items</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>${serverIpaPath}</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>com.xxx.xxx</string>
                <key>bundle-version</key>
                <string>1.0</string>
                <key>kind</key>
                <string>software</string>
                <key>releaseNotes</key>
                <string>v1(可以随意填)</string>
                <key>title</key>
                <string>写你的标题</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>

EOF

#第二步,获取本机的server地址
mkdir ${htmlPath}
# html的位置应该为构建版本的信息
cat > ${htmlPath}/index.html << EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>写你的标题</title>
</head>
<body>
<h1 style="font-size:40pt">写你的标题<h1/>
<h1 style="font-size:40pt">
<a title="iPhone" href="itms-services://?action=download-manifest&url=${serverPlistPath}">Iphone Download</a>
<h1/>
<a title="iPhone" href="${hostAddress}/server.crt">ssl 证书安装</a>
<h1/>
</body>
</html>

EOF

#第三步,生成二维码
mkdir ${qrCodePath}

cat > ${qrCodePath}/GeverateQRCode.py << EOF
#!/bin/python
import qrcode
img=qrcode.make("${serverHtmlPath}/index.html")
img.save("${qrCodePath}/QRCode.png")

EOF

python ${qrCodePath}/GeverateQRCode.py

注意到,构建脚本里,动态生了plist文件,这个plist文件是为了itms-services来调用,用来配置下载选项的,和上一节讲的打包plist是两回事。
这个原理就是,你自己建立一个网页,网页上有下载按钮,点击下载按钮,调用itms-services,去plist文件里进行下载匹配,根据plist文件,查找ipa包的位置,然后进行下载安装。

下面我来理一下整个这个文件夹的分布

《Jenkins实现扫码安装(四)》 Jenkins存储的文件结构.jpeg

七、把二维码显示到Jenkins上

需要一个插件,到Jenkins的首页上,点击系统管理–管理插件–可选插件–选择description setter plugin,进行安装。安装好以后,就可以再次打开工程,点击配置,在构建后配置里,增加如下选项

《Jenkins实现扫码安装(四)》 构建后配置.png

之后再Description里写如下语句,同样是换成你自己的配置,${JOB_NAME},${VERSION},${BUILD_NUMBER}都是Jenkins的自带环境变量

<img src='https://192.168.1.1/${JOB_NAME}/${VERSION}/QRCodes/QRCode_${BUILD_NUMBER}/QRCode.png' height="200" width="200" />

点击保存

这个时候就可以构建了,点击Build with Parameters,开始构建,就OK了,这时就会出现二维码了,直接扫描就可以进入到下载页面。

八、先进行证书的下载和信任

首先点击证书的下载,下载了以后,安装,根据操作步骤一步步进行信任等操作之后,需要到手机的设置—通用—关于本机—证书信任设置,打开对你自己根证书的信任,之后就可以直接点击下载进行下载安装应用了~

    原文作者:小雨hahaha
    原文地址: https://www.jianshu.com/p/308c7c476649
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞