0

My code is as follows:

    stage('Infracost'){
      agent {
        docker {
            image 'infracost/infracost:ci-latest'
            args "--user=jenkins --entrypoint=''"
            alwaysPull true
            reuseNode true
        }
      }
      environment {
          INFRACOST_API_KEY = credentials('INFRACOST_API_KEY')
          USAGE_FILE = '/tmp/infracost-usage.yml'
          SYNC_USAGE_FILE = true          
      }
      steps {
        dir('./services/' + serviceType + '/terraform'){
          sh script:'infracost breakdown --path .', label: "Infracost output"
          sh script:'infracost diff --path plan.json', label: "Infracost diff"
        }
      }
    }

What I would like to happen is if any part of this stage fails, then continue the pipeline. I have used this question for reference - Show a Jenkins pipeline stage as failed without failing the whole job

I have tried using catchError in the steps of the stage and that answers my question partially. What I am seeing is that if the docker part fails for some reason, then the entire pipeline fails.

How do I allow for any part of this stage to fail and for the job to continue?

fraserc182
  • 237
  • 1
  • 4
  • 13

1 Answers1

1

Instead of using an agent block, try executing the commands like shown below.

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo "${params.Environment}"
        echo "${params.Host}"
        script{
        docker.image("ubuntu:20.04").withRun('--user=ubuntu') {c -> 
            sh 'infracost breakdown --path .'
        }
        }
      }
    }
  }
}

References:

https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow

https://www.jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline

ycr
  • 12,828
  • 2
  • 25
  • 45
  • Thanks but this errors with: ``` WorkflowScript: 161: Expected a symbol @ line 161, column 15. docker.image('infracost/infracost:ci-latest').withRun("--user=jenkins --entrypoint=''") {c -> ^ WorkflowScript: 161: Arguments to "error" must be explicitly named. @ line 161, column 15. docker.image('infracost/infracost:ci-latest').withRun("--user=jenkins --entrypoint=''") {c -> ^ ``` – fraserc182 Jun 14 '22 at 19:29
  • 1
    @fraserc182 Sorry I just added the block as a reference. Please find the updated answer. Since don't have your images I used ubuntu image. – ycr Jun 14 '22 at 19:43