如何在Jenkins文件中加入两个值?

发布于 2025-01-24 08:53:30 字数 2413 浏览 2 评论 0原文

**我试图通过下面的jenkins文件语法来串联两个值。也许,我无法做到。

输出值 + Jenkins构建编号应保存在新变量**

连接的控制台输出 /日志中以供参考。寻找此问题声明的输入 /建议。

pipeline {
    agent any
    environment {
        AWS_ACCOUNT_ID="xyz"
        AWS_DEFAULT_REGION="xyz" 
        IMAGE_REPO_NAME="abc/xyz"
        REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
        BLD_NUMBER = "${env.BUILD_NUMBER}"

    }
   
    stages {
        
         stage('Logging into AWS ECR') {
            steps {
                script {
                sh "aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com"
                }
            }
        }

        stage('Build Release') {          
            steps {
                script {
                    def version = sh script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true
                    sh "echo version value is $version"
                    def pom_toupdate = sh script: "echo ${version} | rev | cut -c2- | rev", returnStdout: true
                    sh "echo pom_toupdate version is $pom_toupdate"
                    def newpomversion = sh script: "echo ${pom_toupdate} + ${BLD_NUMBER}", returnStdout: true
                    sh "echo newpomversion value is $newpomversion"
            }

        }
    }
}

output_error / logs:

Login Succeeded
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build Release)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ mvn help:evaluate -Dexpression=project.version -q -DforceStdout
[Pipeline] sh
+ echo version value is 4.51.0
version value is 4.51.0
[Pipeline] sh
+ rev
+ cut -c2-
+ rev
+ echo 4.51.0
[Pipeline] sh
+ echo pom_toupdate version is 4.51.
pom_toupdate version is 4.51.
[Pipeline] sh
+ echo 4.51.
+ 195
/var/lib/jenkins/workspace/xxx@tmp/durable-d0262401/script.sh: line 2: 195: command not found
4.51.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

desired_output:

newpomversion should have 4.51.195

** I am trying to concatenate two values via below Jenkins file syntax. Perhaps, I am unable to do so.

output value + jenkins build number should be saved in a new variable **

Attached console output / logs for reference. Looking for inputs / recommendation for this problem statement.

pipeline {
    agent any
    environment {
        AWS_ACCOUNT_ID="xyz"
        AWS_DEFAULT_REGION="xyz" 
        IMAGE_REPO_NAME="abc/xyz"
        REPOSITORY_URI = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPO_NAME}"
        BLD_NUMBER = "${env.BUILD_NUMBER}"

    }
   
    stages {
        
         stage('Logging into AWS ECR') {
            steps {
                script {
                sh "aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com"
                }
            }
        }

        stage('Build Release') {          
            steps {
                script {
                    def version = sh script: 'mvn help:evaluate -Dexpression=project.version -q -DforceStdout', returnStdout: true
                    sh "echo version value is $version"
                    def pom_toupdate = sh script: "echo ${version} | rev | cut -c2- | rev", returnStdout: true
                    sh "echo pom_toupdate version is $pom_toupdate"
                    def newpomversion = sh script: "echo ${pom_toupdate} + ${BLD_NUMBER}", returnStdout: true
                    sh "echo newpomversion value is $newpomversion"
            }

        }
    }
}

OUTPUT_ERROR / LOGS:

Login Succeeded
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build Release)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ mvn help:evaluate -Dexpression=project.version -q -DforceStdout
[Pipeline] sh
+ echo version value is 4.51.0
version value is 4.51.0
[Pipeline] sh
+ rev
+ cut -c2-
+ rev
+ echo 4.51.0
[Pipeline] sh
+ echo pom_toupdate version is 4.51.
pom_toupdate version is 4.51.
[Pipeline] sh
+ echo 4.51.
+ 195
/var/lib/jenkins/workspace/xxx@tmp/durable-d0262401/script.sh: line 2: 195: command not found
4.51.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

DESIRED_OUTPUT:

newpomversion should have 4.51.195

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文