【问题记录】Jenkins Pipeline读取变量的各种方法

时间:2024-07-06 19:37:28

方法一

输出docker image镜像标签给IMAGE_TAG

def IMAGE_TAG = sh(script: '''docker images | grep ${SERVER_NAME} | grep $NAME_SPACE | awk '{print $2}'|grep ${BUILD_NUMBER}''', returnStdout: true) 

另外,如果想输出docker image命令执行结果给IMAGE_TAG

def IMAGE_TAG = sh(script: '''docker images | grep ${SERVER_NAME} | grep $NAME_SPACE | awk '{print $2}'|grep ${BUILD_NUMBER}''', returnStatus: true) 

方法二

其实就是拆分上面的语句

先使用shell把执行后的结果赋值到一个文本,然后读取文本内容赋值给IMAGE_TAG

sh '''docker images | grep ${SERVER_NAME} | grep $NAME_SPACE | awk '{print $2}'|grep ${BUILD_NUMBER} > image.txt'''
def IMAGE_TAG = readFile('image.txt').trim()