jenkins 获取错误pipeline@tmp/durable-81 eb 936 b/script.sh:line 5:command not found

uqxowvwt  于 2023-03-22  发布在  Jenkins
关注(0)|答案(1)|浏览(359)

我在jenkins中创建了一个管道来使用ANT构建一个二进制文件。创建后,我使用一些shell命令来存储生成的war文件的md5 sum。但是当使用shell命令时,我得到了错误。

stage('Build') {
            steps {
                    //sh "ant -f ABC/XYZ/build.xml build"
                    withAnt(installation: 'ANT_1.9.9', jdk: 'Java_1.8.0_361') {
                        sh 'ant  -f ABC/XYZ/build.xml build'
                        
                    }
                    sh 'SUM=echo $(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war)'
                    sh 'echo $SUM'
                    
            }

            post {
                success {
                    echo "build success"
                }
            }

        }

我得到下面的错误++ md5 sum/opt/codecheckout/pipeline/ABC/XYZ/dist/ABC.war

  • SUM=回波
  • 4ffaf00d7fa7dbd0398d9d4de3496992 /opt/codecheckout/pipeline/ABC/XYZ/ABC.war /opt/codecheckout/pipeline@tmp/durable-0f835202/script.sh:line 1:4ffaf00d7fa7dbd0398d9d4de3496992:未找到命令

我也试过在管道中使用脚本语法,但得到了同样的错误。

sh script:'''
                        #!/bin/bash
                        SUM=$(md5sum ${WORKSPACE}/ABC/XYZ/ABC.war)
                        echo $SUM
                        MDSUM= "$(echo $SUM | head -n1 | awk '{print $1;}')"
                        //echo $MDSUM
                        '''
owfi6suc

owfi6suc1#

问题实际上是关于Bash语法的。

SUM=echo $(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war)

# there is a space between "echo" and 
"SUM=echo $(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war)"

thus Bash interpreter will treat the line into two parts:
1) SUM=echo 
  the first part work well 

2) $(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war) 
   the second part return the md5 sum which is a string, then bash continue 
   treat it as a cmd and execute it (the md5 string: 4ffaf00d7fa7dbd0398d9d4de3496992),
   but the md5 string is not a bash cmd or executable. 

   that's why the error say "4ffaf00d7fa7dbd0398d9d4de3496992: command not found"

解决您的问题非常简单

SUM=$(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war)
// the ahead echo is unnecessary
or 
SUM=$(echo $(md5sum ${WORKSPACE}/ABC/XYZ/dist/ABC.war))

相关问题