测试Jenkins构建自动化部署

1.概述

在Jenkins上创建一个Job,Job里执行一个远程shell脚本,脚本展示“hello world”。

2.步骤

2.1.配置免密码登录

参考:http://www.jianshu.com/p/cd7b53f601e5

2.2.编写测试脚本

进入目标机

echo "echo "hello word"" > /home/demo.sh
chmod +x /home/demo.sh
/home/demo.sh
hello word

2.3.创建Job

==登录Jenkins 创建Job
==输入item name,选择“构建一个自由风格的软件项目”
==在“构建”处,选择“Execute shell”
==在Command栏,输入ssh root@10.11.1.30 “/bin/sh /home/demo.sh
==保存
至此,Job创建完成

2.4.立即构建

点击“立即构建”
发现构建失败,查看错误信息,提示如下:

Started by user aorise
Building in workspace /var/lib/jenkins/workspace/auto_test
[auto_test] $ /bin/sh -xe /tmp/jenkins8711967078967140627.sh
+ ssh root@10.11.1.30 '/bin/sh /home/demo.sh'
Warning: Permanently added '10.11.1.30' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
Build step 'Execute shell' marked build as failure
Finished: FAILURE

错误信息提示:“Permission denied” 无权限执行脚本
原因分析:Jenkins运行在Jenkins账户下,并无root权限
解决方案:将Jenkins添加至sudo列表下,通过sudo执行脚本
修改Job脚本,为sudo ssh root@10.11.1.30 “/bin/sh /home/demo.sh
再次点击“立即构建”
构建失败,提示信息如下:

sudo: sorry, you must have a tty to run sudo

原因分析:默认情况下sudo需要在tty终端才能被调用
解决方案:注释掉 Default requiretty 一行

visudo
# Default requiretty

再次点击“立即构建”
构建成功!

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