Home > Net >  Execute some code and if it fails execute another in Jenkins
Execute some code and if it fails execute another in Jenkins

Time:10-26

I'm trying to download some dependencies from two different artifact repositories. I need to make it secuentially and execute the download of the second just if the code cannot download from the first. I've been trying using a try-catch but it does not seem to be the best way.

stage('My_Stage') {
    steps {
        try{
            ... download some dependencies from artifacts repository 1
        } catch(Exception e) {  // If the first fails then try to do it from the second
            ... download some dependencies from artifacts repository 2
        }
    }
}

Do you have any idea of how to do this?

CodePudding user response:

You should return a value from the download process. E.g.:

steps {
  def repository1_return_code = download_from(repository1)
  if (repository1_return_code != 0) {
    download_from(repository2)
  }
}
  • Related