首先說明,我的電腦環境是windows,所以以下的示例是基於windows10 X64。
一、新建任務,填寫名稱,選擇類型,點擊左下角的【確定】
二、配置
1、General
2、源碼管理
之前在github上配置了公鑰(配置的東西看這里https://www.cnblogs.com/zichuan/p/9274785.html)這里要拉取github的代碼,需要配置私鑰,點擊Add,彈窗中填寫數據。
3、構建觸發器
這里我用的是定時任務,一般項目也用的這個選項吧。
4、構建環境
這個模塊默認即可,這個示例用不到
5、構建
6、構建后操作
我這里設置的比較簡單,就是發送郵件。
三、查看構建的結果
以上設置完成之后,點擊【保存】,點擊左邊菜單欄的【立即構建】,可以查看構建的結果。
以上,在整個示例過程中,我遇到了一個坑,說大不大,說小不小。我只能說,windows和Linux還是區別的,下面來說說這個坑的發生過程:
1、我是windows環境,然后在構建的時候選擇了執行shell腳本,然后改了很多地方,還是沒解決,報錯方式是這樣的:
[0716_test] $ sh -xe C:\WINDOWS\TEMP\jenkins1910652898694296835.sh The system cannot find the file specified FATAL: 命令執行失敗 java.io.IOException: CreateProcess error=2, 系統找不到指定的文件。 at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) Caused: java.io.IOException: Cannot run program "sh" (in directory "C:\Program Files (x86)\Jenkins\workspace\0716_test"): CreateProcess error=2, 系統找不到指定的文件。 at java.lang.ProcessBuilder.start(Unknown Source) at hudson.Proc$LocalProc.<init>(Proc.java:249) at hudson.Proc$LocalProc.<init>(Proc.java:218) at hudson.Launcher$LocalLauncher.launch(Launcher.java:929) at hudson.Launcher$ProcStarter.start(Launcher.java:449) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:109) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:66) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744) at hudson.model.Build$BuildExecution.build(Build.java:206) at hudson.model.Build$BuildExecution.doRun(Build.java:163) at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504) at hudson.model.Run.execute(Run.java:1794) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Build step '執行 shell' marked build as failure Finished: FAILURE
找了我半個多小時,然后在這里找到了比較明確的說明以及解決方案:https://stackoverflow.com/questions/30374998/run-a-command-shell-in-jenkins。但是我試了添加系統環境變量和更改Jenkins的shell配置這兩種方法都沒成功,還是算了,老老實實選擇“執行windows批處理命令”。有興趣的看客可以試試,成功了給我留個言即可,在此謝過。
補充:針對構建模塊中的語句,此示例中是python xx.py,如果構建之后日志中提示“python不是內部或者外部命令”。請先在cmd中嘗試執行python的命令(例python,回車。可以查看Python的版本)看是否成功。報錯的原因是執行Jenkins的系統用戶環境變量中沒有python的環境變量,所以解決方法是:在系統環境變量path中添加python.exe這個文件的路徑!
——日月盈昃,辰宿列張