为什么这个脚本变量一直显示为“null”?

为什么这个脚本变量一直显示为“null”?

我有以下脚本:

def String Service_version = sh "cd ./projects/onehome && pwd && cat package.json | grep version"
             echo "Service_Version_update: $Service_version"
             echo "My_Version is : ${Service_version}"

输出是:

cd ./projects/onehome
13:47:43  + pwd
13:47:43  /home/jenkins/agent/workspace/_apps_onehome_feature-AOTF-11540/projects/onehome
13:47:43  + cat package.json
13:47:43  + grep version
13:47:43  "version": "1.0.400",
13:47:43  [Pipeline] echo
13:47:43  Service_Version_update: null
13:47:43  [Pipeline] echo
13:47:43  My_Version is : null

为什么${Service_version}打印为null

答案1

让我们清理一下:

## Ensure were in projects directory
cd projects/onehome/

## Switch to jenkins directory
$jenkins = pwd
$jenkins_output = "The current Jenkins Directory is: $jenkins"
echo $jenkins_output

## Create version Number
versionNumber = grep version package.json 
$serviceVersion = "The Current Service Version is: $versionNumber"
echo $serviceVersion

我相信您正在尝试比较myVersionserviceVersion查看是否需要更新。根据我如何解释您要对当前代码执行的操作,您的服务版本将始终等于我的版本。您可能想使用另一种方法来确定 myVersion。

相关内容