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!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Jenkins的Pipeline中,可以通过环境变量、参数化构建和全局变量等方式在不同的stage之间传递参数。以下是一些示例: 1. 环境变量 在stage中定义一个环境变量,可以通过${env.VARIABLE_NAME}在其他stage中引用。例如: ``` pipeline { agent any stages { stage('Build') { environment { IMAGE_TAG = '1.0.0' } steps { sh 'echo "Building image with tag ${IMAGE_TAG}"' } } stage('Deploy') { steps { sh 'echo "Deploying image with tag ${IMAGE_TAG}"' } } } } ``` 2. 参数化构建 通过参数化构建,可以在Pipeline执行之前让用户输入参数值,并将这些参数传递不同的stage。例如: ``` pipeline { agent any parameters { string(name: 'ENVIRONMENT', defaultValue: 'dev', description: 'Environment to deploy to') booleanParam(name: 'ENABLE_CACHE', defaultValue: true, description: 'Enable caching?') } stages { stage('Build') { steps { sh 'echo "Building application for ${params.ENVIRONMENT}"' } } stage('Deploy') { steps { sh 'echo "Deploying application to ${params.ENVIRONMENT}"' if (params.ENABLE_CACHE) { sh 'echo "Caching enabled"' } else { sh 'echo "Caching disabled"' } } } } } ``` 3. 全局变量 可以在Pipeline中定义全局变量,并在不同的stage中引用。例如: ``` pipeline { agent any options { // Define global variable envVars([ // Use Jenkins built-in variable for current build number // and assign to MY_BUILD_NUMBER envVar('MY_BUILD_NUMBER', "${BUILD_NUMBER}") ]) } stages { stage('Build') { steps { sh 'echo "Building application for build ${MY_BUILD_NUMBER}"' } } stage('Deploy') { steps { sh 'echo "Deploying application for build ${MY_BUILD_NUMBER}"' } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流柯`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值