Curl(bash)命令在Jenkinsfile groovy脚本中不起作用

bgtovc5b  于 2023-06-05  发布在  Jenkins
关注(0)|答案(2)|浏览(443)

在我的Jenkinsfile Groovy脚本中,我有以下代码:

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

文件的第一部分工作正常,包括命令;

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:]' `

当我运行Jenkins构建时失败。
产生以下错误;

line 4: curl: command not found

我不明白为什么前一行运行正常,但这一行却失败了。
我是不是明显漏掉了什么?
任何帮助将不胜感激。
谢谢

omjgkv6w

omjgkv6w1#

curl可能没有安装在像/usr/bin/这样的公共位置。我建议你尝试运行curl的完整路径。

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

在你的剧本里:

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

另一种选择是编辑/etc/profile并将curl附加到PATH所在的位置。

export PATH=$PATH:/usr/somelocation/
41ik7eoe

41ik7eoe2#

非常感谢你的回复。你是对的,没有安装curl!通过它的完整路径运行curl,然后修改我的脚本,现在已经解决了这个问题。Thank you!:)

相关问题