Jenkins – Checking if a string value is set to a parameter

 

Check if the value of the variable "GIT_COMMIT_ID" is set, if so, then do a checkout by the hash of the commit, if not, then do a checkout by the name of the branch. The variable "GIT_BRANCH_NAME" is taken from the Active Choice parameter.

Jenkinsfile:

pipeline {
  agent any
  parameters {
    string(defaultValue: '', description: 'If you need to build a specific commit, enter it in this field.', name: 'GIT_COMMIT_ID')
  }
  stages {
    stage('Checkout') {
      steps {
        script {
          if ( env.GIT_COMMIT_ID.isEmpty() ) {
            sh "echo Checkout branch: $GIT_BRANCH_NAME"
            checkout([
              $class: "GitSCM",
              branches: [[name: "${GIT_BRANCH_NAME}"]],
              userRemoteConfigs: [[url: "${GIT_URI}"]]
            ])
          }
          else {
            sh "echo Checkout commit: $GIT_COMMIT_ID"
            checkout([
              $class: "GitSCM",
              branches: [[name: "${GIT_COMMIT_ID}"]],
              userRemoteConfigs: [[url: "${GIT_URI}"]]
            ])       
          }
        }
      }
    }
  } 
}  

Tagged: Tags

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments