Using Artifactory plugin in Jenkins declarative pipeline I am using Jenkins Declarative Pipeline to automate my build process. We want to publish our artifacts to a remote JFrog repository only if certain conditions (Sonar,Checkmarx) pass. 

After a bit of research, I found that Artifactory plugin is useful for this. But I am unable to find any document on how to integrate in declarative pipeline. Below is the code snippet from Jenkinsfile

stages{

    stage('Pre-Build'){
        steps{

             script{
                def server = Artifactory.server 'LocalJfrog'
                def rtGradle = Artifactory.newGradleBuild()
                rtGradle.resolver server: server, repo: 'gradle-dev-local'
                rtGradle.deployer server: server, repo: 'gradle-release-local'
                rtGradle.useWrapper = true
            }

        }   
    }
}


The conditional publish is not possible with above code as I cannot reuse the server variable even if I disable auto publish.
You can have conditionals in your declarative pipeline by using the when-block inside a stage. There is a plugin called "environment injector" which lets you set variables outside of the pipeline-script which is nice. Also if you put the step below the other steps, it won't execute if they fail.

 when {
    environment name: 'pushArtifact', value: 'true'
  }
  steps{
     //push artifact  
  }