您的位置:首页 > 运维架构 > Shell

Jenkins 六: 构建中执行shell或者 windows的批处理程序

2016-02-29 16:48 323 查看

Shell/bat

Jenkins可以在构建中执行shell命令或者windows的batch命令。

1.选择一个项目,点击“配置”。

2.找到“构建”–>“增加构建步骤”。选择“Executeshell”或者“ExecuteWindowsbatchcommand”。

具体选择哪一个取决于你的执行环境,如果是linux环境,选择“Executeshell”。如果是windows环境,选择“ExecuteWindowsbatchcommand”。我这边选择的是“ExecuteWindowsbatchcommand”。

3.在“命令”栏中输入你要执行的命令。

echo"HelloWorld"
echo%BUILD_TAG%
pythonhello_python.py


备注:

在命令栏下方,可以看到“参阅可用环境变量列表”。点击之后,可以看到所有可以用的变量名列表及其解释。

比如上面我们使用的BUILD_TAG。

BUILD_NUMBER
Thecurrentbuildnumber,suchas"153"
BUILD_ID
ThecurrentbuildID,identicaltoBUILD_NUMBERforbuildscreatedin1.597+,butaYYYY-MM-DD_hh-mm-sstimestampforolderbuilds
BUILD_DISPLAY_NAME
Thedisplaynameofthecurrentbuild,whichissomethinglike"#153"bydefault.
JOB_NAME
Nameoftheprojectofthisbuild,suchas"foo"or"foo/bar".(TostripofffolderpathsfromaBourneshellscript,try:${JOB_NAME##*/})
BUILD_TAG
Stringof"jenkins-${JOB_NAME}-${BUILD_NUMBER}".Convenienttoputintoaresourcefile,ajarfile,etcforeasieridentification.
EXECUTOR_NUMBER
Theuniquenumberthatidentifiesthecurrentexecutor(amongexecutorsofthesamemachine)that’scarryingoutthisbuild.Thisisthenumberyouseeinthe"buildexecutorstatus",exceptthatthenumberstartsfrom0,not1.
NODE_NAME
Nameoftheslaveifthebuildisonaslave,or"master"ifrunonmaster
NODE_LABELS
Whitespace-separatedlistoflabelsthatthenodeisassigned.
WORKSPACE
Theabsolutepathofthedirectoryassignedtothebuildasaworkspace.
JENKINS_HOME
TheabsolutepathofthedirectoryassignedonthemasternodeforJenkinstostoredata.
JENKINS_URL
FullURLofJenkins,likehttp://server:port/jenkins/(note:onlyavailableifJenkinsURLsetinsystemconfiguration)
BUILD_URL
FullURLofthisbuild,likehttp://server:port/jenkins/job/foo/15/(JenkinsURLmustbeset)
JOB_URL
FullURLofthisjob,likehttp://server:port/jenkins/job/foo/(JenkinsURLmustbeset)
SVN_REVISION
Subversionrevisionnumberthat'scurrentlycheckedouttotheworkspace,suchas"12345"
SVN_URL
SubversionURLthat'scurrentlycheckedouttotheworkspace.


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

print("8"*30)
print("thisiseasy")


备注:

因为我的python是python3.0,所以print语句是这个样子。

因为命令的默认执行路径是在Jenkins的workspace下面,所以脚本需要放置在该路径。当然也可以放置到其他路径,执行脚本时前面加上路径即可。

5.点击“立即构建”。

查看“Consoleoutput”:

BUILDSUCCESSFUL
Totaltime:1second
[Ant_project]$cmd/ccallC:\Users\xxx\AppData\Local\Temp\hudson6570782408589408145.bat


D:\Jekins\workspace\Ant_project>echo"HelloWorld"
"HelloWorld"

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

D:\Jekins\workspace\Ant_project>pythonhello_python.py
888888888888888888888888888888
thisiseasy

D:\Jekins\workspace\Ant_project>exit0
Finished:SUCCESS


从“cmd/ccallC:\Users\xxx\AppData\Local\Temp\hudson6570782408589408145.bat”可以看出,Jenkins自动生成了一个hudson6570782408589408145.bat脚本,保存要被执行的命令,该脚本保存在临时路径下。

后边具体执行了bat命令。

这里给出了Jenkins执行bat的实例,同时在bat中调用了python脚本,扩展开来,当然也可以调用其他语言,比如ruby,perl。

python

除了上面执行python命令的方式,Jenkins还给出了另外一种使用python命令的方式,那就是使用插件。

1.安装PythonPlugin插件。

进入“系统管理”–>“管理插件”->“可选插件”,在“过滤”中输入“PythonPlugin”,可以看到页面显示出该插件,选中并点击“直接安装”即可。

PythonPlugin
Addstheabilitytoexecutepythonscriptsasbuildsteps.
2.任选一个项目,点击“配置”。

3.找到“构建”–>“增加构建步骤”。选择“ExecutePythonscript”。

4.在“Script”栏中输入你要执行的命令。

比如:

print('hello'*8)
print('3+2')


5.点击“立即构建”。

查看“Consoleoutput”:

[Ant_project]$pythonC:\Users\xxx\AppData\Local\Temp\hudson7702020379517348753.py
hellohellohellohellohellohellohellohello
3+2
Finished:SUCCESS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: