Jenkins實現不同構建之間進行參數傳遞

有的時候不同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 選項,可以查看如下信息:

The following variables are available to shell scripts

BUILD_NUMBER
    The current build number, such as "153"
BUILD_ID
    The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
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.

 

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!

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