TSF 应用可以使用 Jenkins 构建持续集成方案。
准备工作
在开始持续集成之前,需要完成下述的准备工作。
1. 参见 使用 Python 脚本部署应用 获取部署脚本及使用方法的说明。
2. 安装 Jenkins ,确保 Jenkins 安装机器上的 Python 不低于
2.7.14
版本,并已安装 PIP 等 Python 包管理工具。3. 在 TSF 平台创建了 部署组。
安装和配置 Jenkins
1. 进入 Jenkins 官网 下载安装 Jenkins。
2. 安装 Maven 并配置 TSF 私服地址,请参见 下载 Maven 。
3. 在 Jenkins 控制台的菜单栏中选择系统管理 > 插件管理,安装 Maven Integration plugin 插件,如已安装请忽略。
4. 创建 Jenkins 项目
4.1 在 Jenkins 首页左侧导航栏中单击新建,创建 Jenkins 任务,并选择构建一个自由风格的软件项目。
![create jenkins item](https://qcloudimg.tencent-cloud.cn/image/document/a3425f719d8436053fbe1ec4631f011e.png)
![create jenkins item](https://qcloudimg.tencent-cloud.cn/image/document/a3425f719d8436053fbe1ec4631f011e.png)
4.2 在 源码管理 页面中选择 Git,并设置相关参数。
Repository URL:您的项目的 Git 协议地址。
Credentials:安全凭证,选择无(前提是运行 Jenkins 软件的用户的 SSH RSA 公匙已添加到该 Git 项目所在的 GitLab 或 GitHub 中,否则这里会报错)或者添加用户名密码。
![git info](https://qcloudimg.tencent-cloud.cn/image/document/469d5f6786f71a835fe74af92f0980de.png)
![add username](https://qcloudimg.tencent-cloud.cn/image/document/9e4a8ca2b9491dbda57bd71da6a903a5.png)
![git info](https://qcloudimg.tencent-cloud.cn/image/document/469d5f6786f71a835fe74af92f0980de.png)
![add username](https://qcloudimg.tencent-cloud.cn/image/document/9e4a8ca2b9491dbda57bd71da6a903a5.png)
4.3 ![GitLab hook](https://qcloudimg.tencent-cloud.cn/image/document/e14daa4bb3a98c6563e5b87c14a6a3c3.png)
单击构建触发器页签,勾选 Build when a change is pushed to GitLab。 GitLab webhook URL 中的 IP 要确保 GitLab 能访问。
![GitLab hook](https://qcloudimg.tencent-cloud.cn/image/document/e14daa4bb3a98c6563e5b87c14a6a3c3.png)
4.4 单击构建环境页签,勾选 Add timestamps to the Console Output(为控制台输出的信息添加时间戳)。![add timestamps](https://qcloudimg.tencent-cloud.cn/image/document/fe1b9fc04fe8e4b876114656a3f1fcd9.png)
![add timestamps](https://qcloudimg.tencent-cloud.cn/image/document/fe1b9fc04fe8e4b876114656a3f1fcd9.png)
4.5 单击构建页签,然后单击增加构建步骤,选择 Invoke top-level Maven targets。
![maven targets](https://qcloudimg.tencent-cloud.cn/image/document/b0d9aef960c7c9ac3fd681851bac943e.png)
目标:填入 clean package (如有其它参数,请根据实际情况填入)。
![mvn terminal](https://qcloudimg.tencent-cloud.cn/image/document/a09c7d3a1d79b4da3134033a4e51297a.png)
![maven targets](https://qcloudimg.tencent-cloud.cn/image/document/b0d9aef960c7c9ac3fd681851bac943e.png)
![mvn terminal](https://qcloudimg.tencent-cloud.cn/image/document/a09c7d3a1d79b4da3134033a4e51297a.png)
4.6 单击构建页签,然后单击增加构建步骤,选择 Execute shell。
![shell](https://qcloudimg.tencent-cloud.cn/image/document/b9e4334c90751cbbd07177a09f37adb7.png)
命令中填入应用部署准备 步骤中准备好的 Shell 命令。
![shell](https://qcloudimg.tencent-cloud.cn/image/document/b9e4334c90751cbbd07177a09f37adb7.png)
4.6.1 虚拟机如下:
python2.7.14 upload_virtual_machine_deploy.py ./consumer-demo/target/consumer-demo-1.10.0-RELEASE.jar application-qab76pxv v001 1300555551 group-gvk5pbdv '-Xms128m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m'
![python script](https://qcloudimg.tencent-cloud.cn/image/document/bd21cd4bfd1d9db032d4e05d0cf23c14.png)
![python script](https://qcloudimg.tencent-cloud.cn/image/document/bd21cd4bfd1d9db032d4e05d0cf23c14.png)
4.6.2 容器如下:
python2.7.14 upload_container_deploy.py group-zvw397wa v1
![containe shell](https://qcloudimg.tencent-cloud.cn/image/document/5a938d017e60e0c2ad89df3ac83eb141.png)
![containe shell](https://qcloudimg.tencent-cloud.cn/image/document/5a938d017e60e0c2ad89df3ac83eb141.png)
4.7
配置 GitLab 所需权限:
4.7.1 管理 Jenkins > Configure Global Security 勾选 匿名用户具有可读权限。
![add read permission](https://qcloudimg.tencent-cloud.cn/image/document/e9cceb01155615786ea5d66d0eb9c296.png)
取消防止跨站点请求伪造。
![cancel cors](https://qcloudimg.tencent-cloud.cn/image/document/b56b95f88fb4f0001bc4403aba6bac90.png)
![add read permission](https://qcloudimg.tencent-cloud.cn/image/document/e9cceb01155615786ea5d66d0eb9c296.png)
![cancel cors](https://qcloudimg.tencent-cloud.cn/image/document/b56b95f88fb4f0001bc4403aba6bac90.png)
4.7.2 管理 Jenkins > 系统配置 取消 Enable authentication for '/project' end-point。
![cancel end point](https://qcloudimg.tencent-cloud.cn/image/document/7b6fb76e9d8930ab58a82646ac156b64.png)
![cancel end point](https://qcloudimg.tencent-cloud.cn/image/document/7b6fb76e9d8930ab58a82646ac156b64.png)
5. 配置 GitLab 的 Web Hook,实现自动构建:
5.1 项目 > Settings > Integrations 进入添加 webhook 界面。
![gitlib add webhook](https://qcloudimg.tencent-cloud.cn/image/document/36cd75dabd65ae499020321a83c5f6d8.png)
![gitlib add webhook](https://qcloudimg.tencent-cloud.cn/image/document/36cd75dabd65ae499020321a83c5f6d8.png)
5.2 将 4.3 中 Jenkins 产生的 GitLab webhook URL 填入 URL,其他选项使用默认设置,单击"Add webhook"。
![gitlib add jenkens url](https://qcloudimg.tencent-cloud.cn/image/document/941419d98df0b935db17d12b1c9e3438.png)
![gitlib add jenkens url](https://qcloudimg.tencent-cloud.cn/image/document/941419d98df0b935db17d12b1c9e3438.png)
5.3 测试 WebHook。
![test webhook](https://qcloudimg.tencent-cloud.cn/image/document/55aef7b892b7987d97a3a3bc142cd5d0.png)
成功如图:
![hook execute success](https://qcloudimg.tencent-cloud.cn/image/document/d1f0708e04990044a331ac0fd20aa555.png)
如出现 403 异常,请检查 4.7 配置。
![test webhook error](https://qcloudimg.tencent-cloud.cn/image/document/61600e03f757908c0150900b11e5887d.png)
![test webhook](https://qcloudimg.tencent-cloud.cn/image/document/55aef7b892b7987d97a3a3bc142cd5d0.png)
![hook execute success](https://qcloudimg.tencent-cloud.cn/image/document/d1f0708e04990044a331ac0fd20aa555.png)
![test webhook error](https://qcloudimg.tencent-cloud.cn/image/document/61600e03f757908c0150900b11e5887d.png)
配置正确后,提交变更到 GitLab
如果上述步骤配置正确,这次提交会触发一次 GitLab Hook。 Jenkins 在接受到这个 Hook 后会构建您的 Maven 项目,并在构建过程中触发 Python 脚本自动部署TSF应用。
1. 虚拟机提交部署成功输出的日志信息(Build Number > 控制台输出)。
15:12:40 + python2.7.14 upload_virtual_machine_deploy.py ./consumer-demo/target/consumer-demo-1.10.0-RELEASE.jar application-qab76pxv v001 1300555551 group-gvk5pbdv '-Xms128m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m'15:12:41 POST15:12:41 /15:12:4115:12:41 content-type:application/json15:12:41 host:tsf.tencentcloudapi.com15:12:4115:12:41 content-type;host15:12:41 0c2f4a50b1dc4df621a84d1731ec07cbd756ac04b1f4853fa792c5dbcf104f2815:12:41 TC3-HMAC-SHA25615:12:41 157673956115:12:41 2019-12-19/tsf/tc3_request15:12:41 46cdc18f96456edef21e4b01e787030d62ce54bb7b300994fed4d5d47370970115:12:41 1d501d695675696fef1c2b2576bb21f42f6f31f55f07c7b27c5ca051d20f242e15:12:41 TC3-HMAC-SHA256 Credential=AKIDyqY82tg6GI9pVqR1xlQtAN2XtOn2rwsp/2019-12-19/tsf/tc3_request, SignedHeaders=content-type;host, Signature=1d501d695675696fef1c2b2576bb21f42f6f31f55f07c7b27c5ca051d20f242e15:12:41 {"Response":{"RequestId":"0b6aa4ee-38fe-4782-97e4-be1061e7cdb1", "Result":{"Content":[{"PkgId":"pkg-f51038e0","PkgName":"consumer-demo-1.10.0-RELEASE.jar","PkgType":"fatjar","PkgVersion":"v001","PkgDesc":"","UploadTime":"2019-12-18 11:03:26","Md5":"4ad6f32f20c1d2dcd60575fa50b16e1d","PkgPubStatus":1}],"TotalCount":1}}}15:12:41 [INFO] application-qab76pxv has uploaded version v001, no need upload15:12:41 POST15:12:41 /15:12:4115:12:41 content-type:application/json15:12:41 host:tsf.tencentcloudapi.com15:12:4115:12:41 content-type;host15:12:41 99f3e6ddeaf00f7b64315835fdd6f29d7a0a5520743184c21a43d8a609d6891a15:12:41 TC3-HMAC-SHA25615:12:41 157673956115:12:41 2019-12-19/tsf/tc3_request15:12:41 e5ebc373c1c432910ca0110e48f1fe4cf29ffcdedc21dc79dd59a99bf802ea2315:12:41 73e21ed01f4ad54668e94a2839d6de17c07e6188e1ff9e176452b80fb776be1515:12:41 TC3-HMAC-SHA256 Credential=AKIDyqY82tg6GI9pVqR1xlQtAN2XtOn2rwsp/2019-12-19/tsf/tc3_request, SignedHeaders=content-type;host, Signature=73e21ed01f4ad54668e94a2839d6de17c07e6188e1ff9e176452b80fb776be1515:12:41 20015:12:41 {"Response":{"RequestId":"86c93d7e-ee44-4c8d-8d34-537a0dbe6871","Result":{"TaskId":"task-76l2v6m7"}}}15:12:41 Finished: SUCCESS
TSF 平台可以查看虚拟机部署变更记录。
![](https://qcloudimg.tencent-cloud.cn/image/document/66a448dd0676505c3c9b08c62e29a213.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/66a448dd0676505c3c9b08c62e29a213.png)
2. 容器部署成功日志信息。
16:32:28 + python2.7.14 upload_container_deploy.py group-zvw397wa v116:32:31 {"Result": {"InstanceNum": 1, "NamespaceName": "zsf-test-jenkins-docker_default", "ApplicationType": "C", "CurrentNum": 1, "MicroserviceType": "N", "Status": "Running", "LbIp": "", "MemRequest": "128", "ClusterId": "cls-52tydz2q", "UpdateIvl": 0, "Envs": [{"Name": "tsf_consul_ip", "Value": "169.254.0.77"}, {"Name": "tsf_consul_port", "Value": "8000"}, {"Name": "PATH", "Value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, {"Name": "workdir", "Value": "/app/"}, {"Name": "jar", "Value": "consumer-demo-1.10.0-RELEASE.jar"}, {"Name": "tsf_app_id", "Value": "1300555551"}, {"Name": "tsf_token", "Value": "VjCCe9gixVmdeBfTpM74PikHFfU8ZmZVL7Z4FMGvXjI="}, {"Name": "tsf_application_id", "Value": "application-byx3zwdy"}, {"Name": "tsf_cluster_id", "Value": "cls-52tydz2q"}, {"Name": "tsf_namespace_id", "Value": "namespace-py5e9zjy"}, {"Name": "tsf_group_id", "Value": "group-zvw397wa"}, {"Name": "tsf_prog_version", "Value": "v1"}, {"Name": "tsf_region", "Value": "ap-guangzhou"}, {"Name": "tsf_ratelimit_master_ip", "Value": "169.254.0.77"}, {"Name": "tsf_ratelimit_master_port", "Value": "7000"}], "TagName": "v1", "ClusterIp": "172.18.255.122", "MemLimit": "256.00", "CpuLimit": "0.50", "ApplicationId": "application-byx3zwdy", "ApplicationName": "zsf-tsf-docker", "ProtocolPorts": [{"Protocol": "TCP", "TargetPort": 80, "Port": 80}], "UpdateType": 0, "Server": "ccr.ccs.tencentyun.com", "GroupName": "zsf-test-docker-consumer", "NodePort": 30215, "AccessType": 2, "ClusterName": "zsf-test-jenkins-docker", "NamespaceId": "namespace-py5e9zjy", "Reponame": "tsf_100011913960/zsf-tsf-docker", "CpuRequest": "0.25", "Message": "", "CreateTime": "2019-12-17 15:09:08", "GroupId": "group-zvw397wa"}, "RequestId": "aff9a2b6-24f5-44b1-8173-112a6a24b581"}16:32:31 {"Result": true, "RequestId": "263c8a64-1bb1-427e-80e2-870aa0bc7d68"}16:32:31 Finished: SUCCESS
TSF 平台可以查看容器部署变更记录。
![](https://qcloudimg.tencent-cloud.cn/image/document/47a5062d9d349b0840fdd6b823100e57.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/47a5062d9d349b0840fdd6b823100e57.png)
3. 容器部署成功日志信息如果部署失败,需要分析 Jenkins console 日志。