Jenkins 六: 構建中執行shell或者 windows的批處理程序


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 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 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”,可以看到頁面顯示出該插件,選中並點擊“直接安裝”即可。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM