代码改变世界

【Devops】【jenkins】jenkins上下游工程传递参数,文件传参

2022-09-27 14:11  码上起舞  阅读(851)  评论(0编辑  收藏  举报

一、背景

Devops打通上下游,从开发编译打包到测试自动化到运维部署,最后通知结果

采用将参数写入profile.txt文件中,传递到下游工程,下游工程读取profile.txt文件中的变量进行赋值

上游工程A----下游工程B

 

二、上游工程A配置

2.1 将所有的变量写入profile.txt,格式如下

这里

echo "buildtag=${BUILD_TAG}" >>profile.txt 
echo "noticeduser=${TEST_GROUP_USERS},${otheruser}" >>profile.txt
echo "subject=${subject}" >>profile.txt
即定义了buildtag这个变量的值,可以用于下游工程使用
其中profile默认生成在workspace目录下
#定义buildtag,从上游工程传递下来,若没有,则为当前工程的buildtag
if [[ "${buildtag}" = "" ]];then
    echo "buildtag=${BUILD_TAG}" >>profile.txt
else echo "buildtag=${buildtag}" >>profile.txt
fi

#定义消息subject,从上游工程传递下来,若没有,则为当前工程的${JOB_NAME}/${BUILD_NUMBER}
if [[ "${subject}" = "" ]];then
    echo "333333333333333333"
    echo "subject=${JOB_NAME}/${BUILD_NUMBER}" >>profile.txt
else echo "subject=${subject}" >>profile.txt
fi

#定义消息发送人
echo "noticeduser=${TEST_GROUP_USERS},${otheruser}" >>profile.txt

2.2 触发下游工程,并传参,配置如下

 

 

三、下游工程B配置

下游工程接收profile.txt中的变量(下游工程接收变量,只需要在参数化构建中定义profile设置好的变量名即可)效果如下:

 

具体配置如下,举一个例子;

 

 即可完成变量传递