<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>

CC BY-SA 3.0
2017-07-06T08:02:37.190Z
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
Dharanidhar
CC BY-SA 3.0
2017-07-04T07:44:58.000Z
2017-07-04T07:44:58.000Z
Jesper Olsson
<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>

CC BY-SA 4.0
2018-09-14T18:59:32.767Z
2018-09-14T18:59:32.767Z
Elektrolyte
<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>

CC BY-SA 3.0
2017-05-11T12:38:16.370Z
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
Dharanidhar
CC BY-SA 3.0
2017-05-09T16:01:44.693Z
2017-05-09T16:01:44.693Z
Michael Pereira
<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>

<jenkins><jenkins-plugins><jenkins-pipeline><jenkinsfile>
Using Artifactory plugin in Jenkins declarative pipeline
Dharanidhar
# 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 {  }
    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  

---

