Curl (bash) command not working in Jenkinsfile groovy script

回眸只為那壹抹淺笑 提交于 2019-12-11 21:47:30

问题


In my Jenkinsfile Groovy script, I have the following code;

stage('Test the 500 log URLs') {
      steps {
        script {
          echo 'Testing the URLs from the 500 error access log...'
                sh '''#!/bin/bash
                    while IFS= read -r line; do
                      URL=`echo $line | awk '{print $2}' | sed 's/http:/https:/' `
                      RESULT=`curl -LI $URL -o /dev/null -w "%{http_code}\n" -s | tr -d '[:space:]' `
                      if [[ $RESULT != "200" ]]
                        then
                          echo "$RESULT $URL"   
                      fi
                    done < tests/Smoke/logs_testing/500errors.txt
                  '''
        }
      }
    }

The first parts of the file work correctly, including the command;

URL=`echo $line | awk '{print $2}' | sed 's/http:/https:/' `

However, the following line;

RESULT=`curl -LI $URL -o /dev/null -w "%{http_code}\n" -s | tr -d '[:space:]' `

fails when I run the Jenkins build.

The following error is produced;

line 4: curl: command not found

I can't see why the previous line ran ok, but this line is failing.

Is there something obvious I'm missing?

Any help would be greatly appreciated.

thanks


回答1:


It is possible that curl was not installed in a common location like /usr/bin/. I'd suggest you try to run curl via its full path.

$ which curl
/usr/somelocation/curl # <- just an example

Then in your script:

RESULT=`/usr/somelocation/curl -LI $URL...`

Another option is to edit your /etc/profile and append to PATH wherever curl is located.

export PATH=$PATH:/usr/somelocation/



回答2:


Many thanks for you reply. You were right, curl wasn't installed! Running curl via it's full path and then amending my script has now resolved the problem. Thank you! :)



来源:https://stackoverflow.com/questions/53283775/curl-bash-command-not-working-in-jenkinsfile-groovy-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!