设为首页 收藏本站
查看: 796|回复: 0

[经验分享] Jenkins 六: 构建中执行shell或者 windows的批处理程序

[复制链接]

尚未签到

发表于 2018-1-7 13:53:18 | 显示全部楼层 |阅读模式
Shell/ bat
  Jenkins 可以在构建中执行shell命令或者windows的batch 命令。
  1. 选择一个项目,点击“配置”。
  2. 找到“构建” –> “增加构建步骤”。选择 “Execute shell” 或者 “Execute Windows batch command”。
  具体选择哪一个取决于你的执行环境,如果是linux环境,选择 “Execute shell”。如果是windows环境,选择 “Execute Windows batch command”。我这边选择的是“Execute Windows batch command”。
  3. 在 “命令”栏中输入你要执行的命令。
  

echo "Hello World"  
echo %BUILD_TAG%
  
python hello_python.py
  

  备注:
  在命令栏下方,可以看到“参阅可用环境变量列表”。点击之后,可以看到所有可以用的变量名列表及其解释。
  比如上面我们使用的 BUILD_TAG。
  

BUILD_NUMBER  
The current build number, such as
"153"  
BUILD_ID
  
The current build>for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds  
BUILD_DISPLAY_NAME
  
The display name of the current build,
which is something like "#153" by default.  
JOB_NAME
  
Name of the project of this build, such as
"foo" or "foo/bar". (To strip off folder paths from a Bourne shell script, try: ${JOB_NAME##*/})  
BUILD_TAG
  
String of
"jenkins-${JOB_NAME}-${BUILD_NUMBER}". Convenient to put into a resource file, a jar file, etc for easier>
EXECUTOR_NUMBER

  
The unique number that>in the "build executor status", except that the number starts from 0, not 1.  
NODE_NAME
  
Name of the slave
if the build is on a slave, or "master" if run on master  
NODE_LABELS
  
Whitespace
-separated list of labels that the node is assigned.  
WORKSPACE
  
The absolute path of the directory assigned to the build as a workspace.
  
JENKINS_HOME
  
The absolute path of the directory assigned on the master node
for Jenkins to store data.  
JENKINS_URL
  
Full URL of Jenkins, like http:
//server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)  
BUILD_URL
  
Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)
  
JOB_URL
  
Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)
  
SVN_REVISION
  
Subversion revision number that's currently checked out to the workspace, such as "12345"
  
SVN_URL
  
Subversion URL that's currently checked out to the workspace.
  

  4. 在Jenkins的 workspace中的该项目下,我的路径是“D:\Jekins\workspace\Ant_project”,放置 hello_python.py。内容如下:
  

print("8"*30)  

print("this is easy")  

  备注:
  因为我的python是python3.0,所以print语句是这个样子。
  因为命令的默认执行路径是在 Jenkins的workspace下面,所以脚本需要放置在该路径。当然也可以放置到其他路径,执行脚本时前面加上路径即可。
  5. 点击“立即构建”。
  查看“Console output”:
  

BUILD SUCCESSFUL  
Total time: 1 second
  
[Ant_project] $ cmd /c call C:\Users\xxx\AppData\Local\Temp\hudson6570782408589408145.bat
  

  

D:\Jekins\workspace\Ant_project>echo "Hello World"  
"Hello World"
  

  
D:\Jekins\workspace\Ant_project>echo jenkins-Ant_project-8
  
jenkins-Ant_project-8
  

  
D:\Jekins\workspace\Ant_project>python hello_python.py
  
888888888888888888888888888888
  
this is easy
  

  
D:\Jekins\workspace\Ant_project>exit 0
  
Finished: SUCCESS
  

  从“cmd /c call C:\Users\xxx\AppData\Local\Temp\hudson6570782408589408145.bat ”可以看出,Jenkins自动生成了一个 hudson6570782408589408145.bat 脚本,保存要被执行的命令,该脚本保存在临时路径下。
  后边具体执行了bat命令。
  这里给出了Jenkins执行bat的实例,同时在bat中调用了 python脚本,扩展开来,当然也可以调用其他语言,比如ruby ,perl。

python
  除了上面执行python命令的方式,Jenkins还给出了另外一种使用python命令的方式,那就是使用插件。
  1. 安装 Python Plugin插件。
  进入“系统管理” –> “管理插件”-> “可选插件”,在“过滤”中输入“Python Plugin”,可以看到页面显示出该插件,选中并点击“直接安装”即可。

Python Plugin
Adds the ability to execute python scripts as build steps.  2. 任选一个项目,点击“配置”。
  3. 找到“构建” –> “增加构建步骤”。选择 “Execute Python script”。
  4. 在 “Script”栏中输入你要执行的命令。
  比如:
  

print('hello'*8)  

print('3+2')  

  5. 点击“立即构建”。
  查看“Console output”:
  

[Ant_project] $ python C:\Users\xxx\AppData\Local\Temp\hudson7702020379517348753.py  
hellohellohellohellohellohellohellohello
  

3+2  
Finished: SUCCESS
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-432577-1-1.html 上篇帖子: Jenkins备份与恢复 下篇帖子: 解决Jenkins 2.0 初始化界面卡住的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表