上接iOS自动化实践——jenkins持续集成打包(一),已经实现了命令行打包,以后想要实现在jenkins页面展示扫描二维码和安装链接。
1、二维码获取
选择通过蒲公英上传app的api,然后python语言实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| def payload(file, userkey, apikey): url = 'https://www.pgyer.com/apiv1/app/upload' data={ 'uKey':userkey, '_api_key':apikey } file_ipa = file+'/MiFit.ipa' ipa_file={'file':open(file_ipa, 'rb')} headers={ 'enctype':'multipart/form-data' } try_times = 0 while try_times < 5: try: response = requests.post(url, headers=headers, files=ipa_file ,data=data) assert response.status_code == requests.codes.ok result = response.json() appDownloadPageURL = parseUploadResult(result) return appDownloadPageURL except requests.exceptions.ConnectionError: print 'requests.exceptions.ConnectionError occured' time.sleep(50) try_times += 1 except Exception as e: time.sleep(50) try_times += 1 if try_times >= 5: raise Exception('Upload ipa failed')
|
获取二维码的短链接
1 2 3 4 5 6 7 8
| appQRCodeURL = parseQRCodeImageUrl(data) response = requests.get(appQRCodeURL) build_image = build_path+'/image' if not os.path.exists(build_image): os.makedirs(build_image) QR_image_file_path = os.path.join(build_image, 'MiFit.png') with open(QR_image_file_path, 'wb') as f: f.write(response.content)
|
会发现每次上传安装包后返回的二维码图片是一个短链接,这个短连接居然是固定的(对同一个账号而言)。这个短连接总是指向最近生成的二维码图片,但是对于二维码图片的唯一URL地址,平台并没有在响应中进行返回。在这种情况下,我们每次构建完成后保存二维码图片的URL链接就没有意义了。
我的做法是将返回的二维码图片短链接将二维码图片下载并保存到本地,然后在build描述信息中引用该图片的Jenkins地址。
2、二维码在jenkins上展示
二维码图片的URL链接有了,那要怎样才能将二维码图片展示在Jenkins项目的历史构建列表中呢?
这里需要用到另外一个插件,description setter plugin。安装该插件后,在【Post-build Actions】栏目中会多出description setter功能,可以实现构建完成后设置当次build的描述信息。这个描述信息不仅会显示在build页面中,同时也会显示在历史构建列表中。
有了这个前提,要将二维码图片展示在历史构建列表中貌似就可以实现了,能直观想到的方式就是采用HTML的img标签,将<img src='QR_image_file_path'>
写入到build描述信息中。
这个方法的思路是正确的,不过这么做以后并不会实现我们预期的效果。
这是因为Jenkins出于安全的考虑,所有描述信息的Markup Formatter默认都是采用Plain text模式,在这种模式下是不会对build描述信息中的HTML编码进行解析的。
要改变也很容易,Manage Jenkins -> Configure Global Security,将Markup Formatter的设置更改为Safe HTML即可。
更改配置后,我们就可以在build描述信息中采用HTML的img标签插入图片了。
最后的效果
遇到的问题
1、<img src='QR_image_file_path'>
写入本地的png生成路径,总是不能展示图片,类似于这样:
解决方案:放入png的Jenkins地址。这里的地址可以在工作目录中查看。