skip to Main Content

I have a pipeline part like following:

stages:
 - stage: A
   jobs:
     - job: build
       steps:
         - script: docker image build repo/myimage:1
         - script: docker  push repo/myimage:1

And I want to add a new step to check if "repo/myimage:1" pushed to repo as success.
So I read that "docker image inspect repo/myimage:1" returns a result "1" or "0". if 0 success.

How can I add this step and check in pipeline? If docker inspect returns 1, build pipeline should fail.

3

Answers


  1. How can I add this step and check in pipeline? If docker inspect returns 1, build pipeline should fail.

    You can add a bash task in devops, after docker inspect related command, check return code if it’s 1(non-zero), fail the task with logging command echo "##vso[task.complete result=Failed;]Failed"

        - task: Bash@3
          inputs:
            targetType: 'inline'
            script: |
              docker image inspect ....  # the related command which will return code.
              if [ $? -ne 0 ]; then
                  echo "##vso[task.complete result=Failed;]Failed"
              fi
    
    Login or Signup to reply.
  2. Another option is to use the failOnStderr

    stages:
     - stage: A
       jobs:
         - job: build
           steps:
             - script: docker image build repo/myimage:1
             - script: docker push repo/myimage:1
             - script: docker image inspect repo/myimage:1
               failOnStderr: true
    

    This parameter will fail the step if anything is written to the Standard Error (Stderr). If docker image inspect command fails, it writes the error to Stderr, so the step will fail.

    Login or Signup to reply.
  3. Run the docker image inspect command and store its result in a variable.
    Use a custom script step to evaluate the variable and control the pipeline outcome based on its value.

    stages:
     - stage: A
       jobs:
         - job: build
           steps:
             - script: docker image build repo/myimage:1
             - script: docker push repo/myimage:1
             - script: |
                 # Run the docker image inspect command and store the result in a variable
                 docker_result=$(docker image inspect repo/myimage:1 | grep -c '"Id": ""')
                 echo "docker_result=$docker_result"
                 # Set an Azure DevOps pipeline variable based on the docker result
                 echo "##vso[task.setvariable variable=DockerResult;isOutput=true]$docker_result"
               displayName: 'Check Docker Image'
    
         - script: |
             # Get the DockerResult variable
             docker_result=$(echo $DockerResult)
             # Check the result and control the pipeline outcome
             if [ $docker_result -eq 0 ]; then
               echo "Docker image inspect succeeded."
             else
               echo "Docker image inspect failed."
               echo "##vso[task.setvariable variable=PipelineResult;isOutput=true]failed"
             fi
           displayName: 'Check Docker Result'
    
     - stage: B
       jobs:
         - job: deploy
           dependsOn: A
           steps:
             - script: echo "Deploy your application"
               condition: ne(variables['PipelineResult'], 'failed')

    The first script step runs the docker image inspect command and sets the result in the DockerResult variable.

    This way, the pipeline will only continue with the deploy job if the docker image inspect command succeeds. If it fails, the pipeline will stop and be marked as failed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search