Posted on Leave a comment

Powershell handle Invoke-WebRequest 404 error

In some cases you may need to use Invoke-WebRequest of powershell as a correct result with status code of 404. A use case for this scenario would be a smoke test for a particular service or URL. In newer powershell versions you can use SkipHttpErrorCheck in order to stop powershell from failing the script.

The below example is a simple Web request that will fail on Powershell <7 with an exception if a 404 response is returned from the webpage.

$req = Invoke-WebRequest -Uri $url -Method GET

The parameter that you could use with Powershell 7++ and you would not have the powershell task failed would be.

$req = Invoke-WebRequest -Uri $url -Method GET -SkipHttpErrorCheck

In order to bypass this issue you could handle the exception on Powershell <7 and add your logic inside the catch block. For example

try {
    $resultApi = MakeWebRequest("http://localhost:1234/api/console") 
}
catch {
    if( $_.Exception.Response.StatusCode.Value__ -eq 404 ) 
    {
        Write-Host "API console Test 404 , proceeding ..."
    }
    else {
        Write-Host "False response..."
    }
}
Posted on Leave a comment

Push multiple docker container images using a loop – Azure DevOps

On a pipeline that I was creating I wanted to push multiple docker images on an Azure container registry based on a list. In order to do that I used the docker@2 task on a loop providing the images that I had to push as a parameter. Code is attached below.

trigger:
– none
pr: none
parameters:
– name: containerlist
type: object
default: ["core/image1","core/image2","core/image3","core/image4"]
– name: DockerPushID
type: string
pool:
name: demo-app
stages:
– stage: containers
displayName: Push containers to container registry $(registry)
jobs:
– job: pushcontainers
displayName: Push containers on testexample.azurecr.io
steps:
– checkout: none
– ${{ each container in parameters.containerlist }}:
– task: Docker@2
displayName: pushing image ${{container}}
inputs:
containerRegistry: 'registryconnection'
repository: '${{container}}'
command: 'push'
tags: |
current-${{parameters.DockerPushID}}
current-latest

This task will run steps based on the images you provide on the parameters list. An important note is that you need to have the image named accordingly in order to get a successful result. For example if you need to push on geralexgr.azurecr.io you will need to have your images named as below.

geralexgr.azurecr.io/image1:current-latest
geralexgr.azurecr.io/image2:current-latest

Else you may notice some failures indicating the below.

The push refers to repository [***/kati/image1] 
An image does not exist locally with the tag: ***/kati/image1

A successful run of the pipeline.

Additional information regarding loops and expressions on Azure DevOps pipelines:

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#functions

Video tutorial on YouTube: