Jenkins不同job之間傳遞參數

原文鏈接:https://yq.aliyun.com/articles/364460

有的時候不同job直接需要傳遞一個文件名或者路徑,這個時候我們不需要傳遞文件實體,那這個路徑如何傳遞呢?比如有如下兩個項目,我想把A的工作目錄傳遞給B,讓B使用。 
這裏寫圖片描述

A job配置

首先需要安裝一個Parameterized Trigger Plugin插件:

這裏寫圖片描述

安裝後重啓。

在A項目配置面板中Post-build Actions選項中選擇Trigger parameterized build on other projects 
我選擇的參數爲預定義參數,如果想知道有哪些與定義參數,可以在Build模塊下選擇Execute shell

這裏寫圖片描述

選在文本框下的the list of available environment variables 選項,可以查看如下信息:


 
  1. The following variables are available to shell scripts

  2.  
  3. BUILD_NUMBER

  4. The current build number, such as "153"

  5. BUILD_ID

  6. The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)

  7. BUILD_DISPLAY_NAME

  8. The display name of the current build, which is something like "#153" by default.

  9. JOB_NAME

  10. 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##*/})

  11. BUILD_TAG

  12. String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". Convenient to put into a resource file, a jar file, etc for easier identification.

  13. EXECUTOR_NUMBER

  14. 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.

  15. NODE_NAME

  16. Name of the slave if the build is on a slave, or "master" if run on master

  17. NODE_LABELS

  18. Whitespace-separated list of labels that the node is assigned.

  19. WORKSPACE

  20. The absolute path of the directory assigned to the build as a workspace.

  21. JENKINS_HOME

  22. The absolute path of the directory assigned on the master node for Jenkins to store data.

  23. JENKINS_URL

  24. Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)

  25. BUILD_URL

  26. Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)

  27. JOB_URL

  28. Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)

  29. SVN_REVISION

  30. Subversion revision number that's currently checked out to the workspace, such as "12345"

  31. SVN_URL

  32. Subversion URL that's currently checked out to the workspace.

  33.  
  •  

ok,回到正題,具體配置如下:

這裏寫圖片描述

我將A項目的工作目錄傳遞給了自定義參數TEST_WORKSPACE.到這裏A項目的配置就完成了。

B job配置

我們在A項目配置的TEST_WORKSPACE參數,如果在B job中使用,首先我們在配置界面中,勾選The build is parameterized。具體配置信息如下所示:

這裏寫圖片描述

這樣我們就在當前工作環境中得到了由A傳遞過來的參數。至於你怎麼使用,那是你的事了。比如我在shell腳本中首先切換到該目錄下(需要在一臺機器上),然後在A項目的工作目錄下生成一個hello.txt文檔,我們可以在shell命令下配置如下信息:

這裏寫圖片描述

ok,配置完成了,這個時候我們構建A,看看能不能讓B在A的工作目錄下生成一個hello.txt文檔。

這裏寫圖片描述

Done!

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章