Answers: <p>You can have conditionals in your declarative pipeline by using the <strong>when</strong>-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.</p>
  
  <pre><code> when {
      environment name: 'pushArtifact', value: 'true'
    }
    steps{
       //push artifact  
    }
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-06T08:02:37.190Z
Answers: Thanks for the hint. If i understood it right a variable has to be set after conditions have been met and then check that variable in Pre-Build step
Answers: Dharanidhar
Answers: CC BY-SA 3.0
Answers: 2017-07-04T07:44:58.000Z
Answers: 2017-07-04T07:44:58.000Z
Answers: Jesper Olsson
Answers: <p>I think your issue is rooted in the <strong>server</strong> variable not being reusable outside the pre-build stage block.</p>
  
  <p>In Jenkins declarative, you can define variables like that using the <code>script { ... }</code> block, but once you leave the stage those variables are inaccessible to other to stages.</p>
  
  <p>With the previous suggestions, I'd recommend this:</p>
  
  <p>House the artifactory deploy code to a shared library.</p>
  
  <pre>
  <i>gradle_artifactory.groovy</i>
  
      def call (Map parameters = [:]) {  //optional parameters mapping
  
          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
          def buildInfo = rtGradle.run rootDir: "projectDir/", buildFile: 
              'build.gradle', tasks: 'clean artifactoryPublish'
  
      }
  </pre>
  
  <p>Then to keep your declarative pipelines <code>D.R.Y</code></p>
  
  <pre><code>@Library('my-shared-lib') 
  ...
  stage('Artifactory Upload') {
      when { &lt;some expression with sonarqube/checkmarx&gt; }
      steps {
          script {
              gradle_artifactory()
          }
      }
  }
  </code></pre>
  
  <p>refs:</p>
  
  <p><a href="https://jenkins.io/doc/book/pipeline/syntax/#when" rel="nofollow noreferrer">https://jenkins.io/doc/book/pipeline/syntax/#when</a></p>
  
  <p><a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">https://jenkins.io/doc/book/pipeline/shared-libraries/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2018-09-14T18:59:32.767Z
Answers: 2018-09-14T18:59:32.767Z
Answers: Elektrolyte
Answers: <p>If you want to embed logic in the Jenkinsfile, the declarative syntax may not be the best method as it is not always easy to reflect it in the code.</p>
  
  <p>If you switched to the Jenkinsfile scripted pipeline, you would be able to define and use conditions in an easier way.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-05-11T12:38:16.370Z
Answers: Thanks for the reply. I am aware of scripted pipeline but since, this is a basic necessity for any project thought it would be included in declarative pipeline. Will switch back to Scripted or external groovy script then
Answers: Dharanidhar
Answers: CC BY-SA 3.0
Answers: 2017-05-09T16:01:44.693Z
Answers: 2017-05-09T16:01:44.693Z
Answers: Michael Pereira
Body: <p>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. </p>
  
  <p>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</p>
  
  <pre><code>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
              }
  
          }   
      }
  }
  </code></pre>
  
  <p>The conditional publish is not possible with above code as I cannot reuse the <strong>server</strong> variable even if I disable auto publish.</p>
  
Tags: <jenkins><jenkins-plugins><jenkins-pipeline><jenkinsfile>
Title: Using Artifactory plugin in Jenkins declarative pipeline
UserDisplayName: Dharanidhar
ThreadText: # Using Artifactory plugin in Jenkins declarative pipeline
  
  (Asked by: username_0 on 2017-05-09)
  
  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.
  
  **Tags:** jenkins, jenkins-plugins, jenkins-pipeline, jenkinsfile
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-07-04. Score: 3
  
  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  
    }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the hint. If i understood it right a variable has to be set after conditions have been met and then check that variable in Pre-Build step  
  
  ---
  
  ### Answer by username_2 on 2018-09-14. Score: 2
  
  I think your issue is rooted in the **server** variable not being reusable outside the pre-build stage block.
  
  In Jenkins declarative, you can define variables like that using the `script { ... }` block, but once you leave the stage those variables are inaccessible to other to stages.
  
  With the previous suggestions, I'd recommend this:
  
  House the artifactory deploy code to a shared library.
  
  ```
  
  gradle_artifactory.groovy
  
      def call (Map parameters = [:]) {  //optional parameters mapping
  
          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
          def buildInfo = rtGradle.run rootDir: "projectDir/", buildFile: 
              'build.gradle', tasks: 'clean artifactoryPublish'
  
      }
  
  ```
  
  Then to keep your declarative pipelines `D.R.Y`
  
  ```
  @Library('my-shared-lib') 
  ...
  stage('Artifactory Upload') {
      when { <some expression with sonarqube/checkmarx> }
      steps {
          script {
              gradle_artifactory()
          }
      }
  }
  
  ```
  
  refs:
  
  <https://jenkins.io/doc/book/pipeline/syntax/#when>
  
  <https://jenkins.io/doc/book/pipeline/shared-libraries/>
  
  
  ---
  
  ### Answer by username_3 on 2017-05-09. Score: 1
  
  If you want to embed logic in the Jenkinsfile, the declarative syntax may not be the best method as it is not always easy to reflect it in the code.
  
  If you switched to the Jenkinsfile scripted pipeline, you would be able to define and use conditions in an easier way.
  
  > **Comments:**
  > **username_0**: Thanks for the reply. I am aware of scripted pipeline but since, this is a basic necessity for any project thought it would be included in declarative pipeline. Will switch back to Scripted or external groovy script then  
  
  ---
  
  