0

I am working on a Tekton task that makes a cURL command request, extracts a response to a JSON file, and then extracts attributes from that JSON. I have noticed a behavior with my task that even if my cURL command fails, it does not fail the task.

Below is my cURL command. When the cURL command fails while getting the pull number from the response, it does not fail my Tekton task:

curl -X GET -H "Authorization: token ${GIT_API_TOKEN}" --url "https://github.ford.com/api/v3/repos/${gitOrgName}/${gitRepoName}/commits/$GIT_COMMIT_ID/pulls" -o sonarScanResponse.json

export pullNumber=$(jq -j '.[0].number' sonarScanResponse.json)

On searching in Google, I found a suggestion to add the following snippet. Unfortunately, this is also not working:


curl -X GET -H "Authorization: token ${GIT_API_TOKEN}" -w "%{http_code}" --url "https://github.ford.com/api/v3/repos/${gitOrgName}/${gitRepoName}/commits/$GIT_COMMIT_ID/pulls" -o sonarScanResponse.txt

    # Check HTTP status code
    if [ $(cat sonarScanResponse.txt) != "200" ]; then

        echo "HTTP status code is not 200"

        exit -1

    fi

    sonarScanResponse=$(cat sonarScanResponse.txt | jq .)

   export pullNumber=$(jq -j '.[0].number' sonarScanResponse.json)

I am getting too many arguments error at this line if [ $(cat sonarScanResponse.txt) != "200" ]; then

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 18821 100 18821 0 0 62946 0 --:--:-- --:--:-- --:--:-- 62946 200/usr/bin/script.sh: line 72: [: too many arguments

Can someone help me get this resolved?

  • Your first code snippet ends with `$?` set to the status code of the `export` command, which always succeeds. You can try it with `export foo=$(false)`, which sets status code 0. Aside from this, if you really post the whole script here, it does not make sense to have a variable assignment as the last command of a script, unless the script is supposed to be sourced. Maybe you should provide a bit more detail about the context in which you want to use the pieces of code you have posted here. – user1934428 Jul 25 '23 at 07:30
  • BTW, the error message in line 32 comes from the fact that the file `sonarScanResponse.txt` contains whitespace characters, and word splitting occurs. Either quote that part, or use `[[....]]` instead of `[`. – user1934428 Jul 25 '23 at 07:32

0 Answers0