jenkinsjenkins-pipelinejenkins-job-dsl

Getting error pipeline@tmp/durable-81eb936b/script.sh: line 5: command not found


I'm creating a pipeline in jenkins to build a binary using ANT. After creating I'm using some shell commands to store md5sum of the generated war file. But when using the shell commands I'm getting error.

        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"
                }
            }


        }

I'm getting below error ++ md5sum /opt/codecheckout/pipeline/ABC/XYZ/dist/ABC.war

I have tried using script syntax in pipeline as well, but getting the same error.

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

Solution

  • The issue actual is about Bash syntax.

    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"
    

    Fix your issue is simple

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