Search code examples
curlazure-devopsazure-pipelinesazure-pipelines-yaml

How to use the curl output to fail the Azure Pipelines job?


Team,

In Azure Devops yaml pipeline, where Im connecting to a server using ssh and running a curl command to look for an image presence in registry,

It works fine and gives back the result, but I want to fail the job if the curl command couldn't find the specific image.

Curl -s -X GET "https://registry/api/v2.0/Projects/test/repositories/repository%2Ftest-module/artifacts/1.0.0/tags?page=1&page_size=10&with_signature=false&with_immutable_status=false" -H "accept: application/json"

Image is available in the registry - The job succeeds with the artifactid, tag output

Image not available in the registry - Still the job succeeds with the output Errors code not found ..,

Basically, I need help to fail the job if no image in repository


Solution

  • You can achieve that by checking the exit code of the curl command. If the response is not 200 (exit code is not 0), you can fail the pipeline (exit 1).

    Here is the script:

    - task: CmdLine@2
      inputs:
        script: |
    
          Curl -s -X GET "https://registry/api/v2.0/Projects/test/repositories/repository%2Ftest-module/artifacts/1.0.0/tags?page=1&page_size=10&with_signature=false&with_immutable_status=false" -H "accept: application/json"
    
          if [ $? -ne 0 ]; then
            echo "Failed to get image: Image not available in the registry"
            exit 1
          fi
    

    $? is a special variable in Unix-like operating systems that holds the exit status of the last executed command. In your case, it represents the exit status of the curl command.