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 ""
BUILD_ID
The current build ID, identical to BUILD_NUMBER 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 identification.
EXECUTOR_NUMBER
The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from , not .
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(""*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”,可以看到页面显示出该插件,选中并点击“直接安装”即可。

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

Jenkins 六: 构建中执行shell或者 windows的批处理程序的更多相关文章

  1. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...

  2. Java中执行shell笔记

    在java中执行shell有好几种方式:第一种(exec)方式一 public static synchronized void runshell2() {    File superuser = n ...

  3. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

  4. Docker中执行Shell出现乱码

    问题描述 最近遇到一个问题: 执行命令 docker exec f4af9b sh -c 'bash /tmp/build.sh' 命令在docker中执行shell,会出现中文乱码的问题.但是在do ...

  5. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  6. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  7. 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序

    原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...

  8. Jenkins自动化构建(一)执行selenium+python脚本

    Jenkins执行python写的selenium自动化脚本,通常会遇到,执行打不开浏览器,查看jenkins构建Console Output控制台输出信息,发现脚本是执行了的,但是出错了,打开浏览器 ...

  9. SQL Server 中执行Shell脚本计算本地文件的内容大小

    SQL Server 数据库中除了能执行基本的SQL语句外,也可以执行Shell脚本.默认安装后,SQL中的Shell脚本的功能是关闭的,需要手动打开, 执行以下脚本即可打开该功能. -- 允许配置高 ...

随机推荐

  1. TreeView checkbox

    C# TreeView checkbox 联动打勾 #region 将树的checkbox选中 private void setNodeTrue(Node selNode) { Node node = ...

  2. 菜鸟学开店—自带U盘的打印机

    本文旨在提供最简单.便宜.有效的解决方案,解决普通用户最困扰的问题.今天提供普通用户一个低价的小票打印机驱动安装解决方案 相信很多用户都碰到过这种情况,电脑的重装了系统,打印机的驱动没有备份,要用打印 ...

  3. Solaris用户管理(一):用户与组管理

    Solaris用户管理(一):用户与组管理  2008-07-01 09:19 用户管理是系统管理的基础.Solaris中不但支持传统Unix所支持的用户和组的概念,还从Solaris 8开始引入了基 ...

  4. (JavaScript实现)页面无操作倒计时退出

    项目前端页面需要实现,页面没人操作进入倒计时,以下为前端代码实现. //设置(倒计时功能)开关 var _mouseActiveListener_flag = true; beforecount:触发 ...

  5. Vijos1352 NOI2006 最大获利 最小权闭合图

    Orz胡伯涛<最小割模型在信息学竞赛中的应用> 建图方法: 设立源点S和汇点T,S和用户(共M个)连边,载流量为满足其要求的获利 T和中转站(共N个)连边,载流量为建立该中转站的费用 每个 ...

  6. SGU 179.Brackets light

    时间限制:0.25s 空间限制:12M 题意       给定一个合法的仅由'(',')'组成的括号序列,求它的下一个合法排列.假定'('<')'. Solution:             ...

  7. ubuntn 虚拟机NAT 静态IP 网络配置

    在虚拟机安装ubuntu12.04自动获取IP 一切都没有问题 ssh连接也正常.关机重启后郁闷的发现网络已经不通了,于是开始了以下的摸索. 1.配置静态IP 网关: ip段: 命令: Vim /et ...

  8. html5本地数据库(一)

    本地数据库 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...

  9. Bootstrap_表单_表单提示信息

    平常在制作表单验证时,要提供不同的提示信息.在Bootstrap框架中也提供了这样的效果.使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部. < ...

  10. QTP的DataTable操作整理(注---不知转载多少遍)

    返回值:数字 示例: 以下示例使用 GetRowCount 方法查找 MySheet 运行时数据表中最长的列中的总行数,并将其写入报告. rowcount = DataTable.GetSheet(& ...