Answers: <p>Or you could use the <code>beforeInput</code> flag</p>
  <pre><code>stage('Example Deploy') {
          when {
              beforeInput true
              branch 'production'
          }
          input {
              message &quot;Deploy to production?&quot;
              id &quot;simple-input&quot;
          }
          steps {
              echo 'Deploying'
          }
      }
  </code></pre>
  <p><a href="https://www.jenkins.io/doc/book/pipeline/syntax/#when" rel="nofollow noreferrer">Jenkins pipeline | When</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2021-10-07T11:35:51.813Z
Answers: 2021-10-07T11:35:51.813Z
Answers: Thusitha Jayasundara
Answers: <p>I see two approaches  :</p>
  
  <h2>Save current branch name on a variable and check it in a if statement</h2>
  
  <pre><code>CURRENT_BRANCH = '...' // some value or parameter
  
  stage('only on master') {
    if (CURRENT_BRANCH == 'master') {
    input {
      ...
    }
  }
  
  // be sure to check if input was set or define a default value
  </code></pre>
  
  <p>or </p>
  
  <h2>Run a shell script to know if current folder is a git repo on master</h2>
  
  <p>The following command check if current folder is a git repository  printing "true" ( or returning a error if does not have repo)</p>
  
  <pre><code>git rev-parse --is-inside-work-tree
  </code></pre>
  
  <p>The next, print the current branch name </p>
  
  <pre><code>git branch | grep \* | cut -d ' ' -f2
  </code></pre>
  
  <p>Use both to create a script that sets an environment variable.
  Check the value on your pipeline, to show the input</p>
  
  <pre><code>stage('only on master') {
  
    // Set true on IS_ON_MASTER if current branch is master, 
    // otherwise set false
  
    sh ./check-branch-master.sh 
  
    if (env.IS_ON_MASTER) {
    input {
      ...
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-10T14:05:34.907Z
Answers: Don't forget to add closing bracket "}", which are missing in the first and last code snippets.
Answers: Chris
Answers: CC BY-SA 3.0
Answers: 2018-04-24T21:03:28.290Z
Answers: 2018-04-24T21:03:28.290Z
Answers: Rodrigo Medeiros
Body: <p>I want to ask for input but only if we are on a certain branch.  But the input is run before the when. Can this be done with declarative syntax?</p>
  
  <p>For example this will always prompt even when the branch is not master</p>
  
  <pre><code>stage('only on master') {
     when {
        branch 'master'
     }
     input { ... }
  }
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-02-06T23:22:37.050Z
Comments: Use beforeInput like specified in the docs https://jenkins.io/doc/book/pipeline/syntax/
Comments: awattar
Tags: <jenkins-pipeline>
Title: Jenkins Pipeline - input only if branch matches
UserDisplayName: Dave
ThreadText: # Jenkins Pipeline - input only if branch matches
  
  (Asked by: username_0 on 2018-04-24)
  
  I want to ask for input but only if we are on a certain branch. But the input is run before the when. Can this be done with declarative syntax?
  
  For example this will always prompt even when the branch is not master
  
  ```
  stage('only on master') {
     when {
        branch 'master'
     }
     input { ... }
  }
  
  ```
  
  **Tags:** jenkins-pipeline
  
  > **Comments:**
  > **username_1**: Use beforeInput like specified in the docs https://jenkins.io/doc/book/pipeline/syntax/  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2018-04-24. Score: 4 | Accepted answer
  
  I see two approaches :
  
  ## Save current branch name on a variable and check it in a if statement
  
  ```
  CURRENT_BRANCH = '...' // some value or parameter
  
  stage('only on master') {
    if (CURRENT_BRANCH == 'master') {
    input {
      ...
    }
  }
  
  // be sure to check if input was set or define a default value
  
  ```
  
  or
  
  ## Run a shell script to know if current folder is a git repo on master
  
  The following command check if current folder is a git repository printing "true" ( or returning a error if does not have repo)
  
  ```
  git rev-parse --is-inside-work-tree
  
  ```
  
  The next, print the current branch name
  
  ```
  git branch | grep \* | cut -d ' ' -f2
  
  ```
  
  Use both to create a script that sets an environment variable.
  Check the value on your pipeline, to show the input
  
  ```
  stage('only on master') {
  
    // Set true on IS_ON_MASTER if current branch is master, 
    // otherwise set false
  
    sh ./check-branch-master.sh 
  
    if (env.IS_ON_MASTER) {
    input {
      ...
    }
  }
  
  ```
  
  > **Comments:**
  > **username_4**: Don't forget to add closing bracket "}", which are missing in the first and last code snippets.  
  
  ---
  
  ### Answer by username_2 on 2021-10-07. Score: 2
  
  Or you could use the `beforeInput` flag
  
  ```
  stage('Example Deploy') {
          when {
              beforeInput true
              branch 'production'
          }
          input {
              message "Deploy to production?"
              id "simple-input"
          }
          steps {
              echo 'Deploying'
          }
      }
  
  ```
  
  [Jenkins pipeline | When](https://www.jenkins.io/doc/book/pipeline/syntax/#when)
  
  
  ---
  
  