Skip to content

jenkins发版-检查参数阶段-增加自动读取线上文档活动配置功能

一、借助 Google Cloud Api 实现:

1、新建项目:

https://console.cloud.google.com/apis/dashboard?hl=zh-cn&orgonly=true&project=sheets-api-437103&supportedpurview=organizationId
image1

2、启用 Google Sheets API:

image2
image3

3、创建服务账号:

image4
image5

4、创建服务密钥(选择 json 格式):

image6
创建完成后,会自动将 json 文件下载到本地,这个文件将是后续脚本创建服务的基础配置文件;

5、为服务账号添加共享权限:

image7

6、编写脚本读取未来两周活动:

6.1 安装依赖:

pip install --upgrade oauth2client
pip install --upgrade google-api-python-client

6.2 读取未来两周配置:ggs.py

#!/usr/bin/env python
#coding: utf-8

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

global json_key_file
json_key_file = './server_secret.json'

def getWeeklyActivtyService(spreadsheet_id):
# 定义要使用的 scopes
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']

# 创建凭证
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_file, scopes)

# 建立服务对象
return build('sheets', 'v4', credentials=credentials)

def getWeeklyActivity():
# 表格 ID
spreadsheet_id = '1n9-hHrvvzyGcvktimZVQU9hwydTJn5bIWV-QpnPZNjw'
# 工作表名称
SHEETNAME = '未来两周活动资源'
# 要获取的数据范围(第三列)
RANGENAME = SHEETNAME + '!D1:D'

service = getWeeklyActivtyService(spreadsheet_id)

result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=RANGENAME).execute()
values = result.get('values', [])

return values

二、继承到 jenkins 发版脚本:

1、 读取resource_dirs.json,写入未来两周配置:

#!/usr/bin/env python
#coding: utf-8

import sys
import os

import ggs
import json

app_res_map = {
'res_oldvegas':0,
'res_doublehit':1
}

if len(sys.argv) <= 1:
print("useage: get-activities-sheets.py */resource_dirs.json")
exit(1)

# 解析 app_res 参数
resource_dirs_config_file = sys.argv[1]
res_dir = os.path.split(os.path.dirname(resource_dirs_config_file))[1]

# 解析数据索引
target_app_index = app_res_map[res_dir]
if target_app_index == None:
print('get activities from Google Sheet faild! Target app not support.')
exit(0)

# 获取数据
values = ggs.getWeeklyActivity()
if not values:
print('get activities from Google Sheet faild! No data found.')
exit(2)
else:
app_index = -1
activities_list = []

for row in values:
if len(row) > 0:
if u"活动资源名称" == row[0]:
app_index += 1
if len(activities_list) <= app_index:
activities_list.append([])
continue

       if app\_index \>= 0:  
           activities\_list\[app\_index\].append(row\[0\])

# 写入数据
content = activities_list[target_app_index]
if len(content) == 0:
print('get activities from Google Sheet faild! No match data found.')
exit(3)

json_data = None
with open(resource_dirs_config_file,'r') as file:
json_data = json.load(file)

json_data['activity'] = content

with open(resource_dirs_config_file,'w') as file:
json.dump(json_data,file,indent=2)

2、添加 Jenkins 脚本调用

./get-activities-sheets.py ../$workspace/$res_dir/resource_dirs.json
cd ../$workspace

git add .
git commit -m "! ${display_name}_JS_V${common_ver} 同步 smoke 活动配置"
git push

三、问题记录:

1、打包机 python 环境问题:

打包机使用 xocde 自带的 python 版本,未安装 pynev、未安装pip;无法安装依赖库;
解决:

1.1 获取python 版本、路径:
xcode 自带 python 版本 2.7;
path: /Library/Frameworks/Python.framework/Versions/2.7/bin/
link:/usr/local/bin/python

1.2 安装 pip:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo /usr/bin/python get-pip.py

1.3 添加 link 到可执行目录:
ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/pip /usr/local/bin/pip

1.4 重开终端或 export 到环境变量;

Released under the MIT License.