0

I want to set dynamic variable in Jenkinsfile and below is my Jenkinsfile

def determineProjectByBranch(branchName) {
  String projectName = "";

  if (branchName.contains("api")) {
    projectName = "api";
  } else if (branchName.contains("auth")) {
    projectName = "auth";
  }

  return projectName;
}

pipeline {
  agent any

  stages {
    stage("Build") {
      environment {
        PROJECT_NAME = determineProjectByBranch("${GIT_BRANCH}")
      }

      steps {
        script {
          PROJECT_NAME = determineProjectByBranch("${GIT_BRANCH}")
        }
        echo "branch name: ${GIT_BRANCH}"
        echo "project name: " + PROJECT_NAME // it shows empty value
        echo "project name: ${PROJECT_NAME}"
        sh "chmod +x gradlew"
        sh "./gradlew ${PROJECT_NAME}:clean ${PROJECT_NAME}:build"
      }
    }
  }
}

As you can see the above code i want to use function so that it can set the dynamic value but i can't find the right way to set variable dynamically.

I also tried the below code but it didn't work either.

def determineProjectByBranch(branchName) {
  String projectName = "";

  if (branchName.contains("api")) {
    projectName = "api";
  } else if (branchName.contains("auth")) {
    projectName = "auth";
  }

  return projectName;
}

def projectName

pipeline {
  agent any

  stages {
    stage("Build") {
      steps {
        script {
          projectName = determineProjectByBranch("${GIT_BRANCH}")
        }
        echo "branch name: ${GIT_BRANCH}"
        echo "project name: " + projectName // it shows empty value
        echo "project name: ${projectName}"
        sh "chmod +x gradlew"
        sh "./gradlew ${projectName}:clean ${projectName}:build"
      }
    }
  }
}

You may think the function returns empty value but when i build with below code it shows expected value

def determineProjectByBranch(branchName) {
  String projectName = "";

  if (branchName.contains("api")) {
    projectName = "api";
  } else if (branchName.contains("auth")) {
    projectName = "auth";
  }

  return projectName;
}

def projectName

pipeline {
  agent any

  stages {
    stage("Build") {
      steps {
        echo "branch name: ${GIT_BRANCH}"
        echo "project name: " + determineProjectByBranch("${GIT_BRANCH}") // it shows expected value
        echo "project name: ${projectName}"
        sh "chmod +x gradlew"
        sh "./gradlew ${PROJECT_NAME}:clean ${PROJECT_NAME}:build"
      }
    }
  }
}

I'm not sure define variable depends on Jenkins version but mine is 2.361.2. Any help would be appreciate thank you in advance

1 Answers1

0

I just hardcoded the values and removed irrelevant parts and the following seems to work fine for me.

def determineProjectByBranch(branchName) {
  String projectName = "";

  if (branchName.contains("api")) {
    projectName = "api";
  } else if (branchName.contains("auth")) {
    projectName = "auth";
  }

  return projectName;
}

pipeline {
  agent any

  stages {
    stage("Build") {
      environment {
        PROJECT_NAME = determineProjectByBranch("api123")
      }

      steps {
        echo "branch name:"
        echo "project name: " + PROJECT_NAME // it shows empty value
        echo "project name: ${PROJECT_NAME}"
      }
    }
  }
}

Output

[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
branch name:
[Pipeline] echo
project name: api
[Pipeline] echo
project name: api
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Thank you for your quick comment! I just tested with hardcoded text as well and as you said yes it worked... But still i don't get it why using `${GIT_BRANCH}` not working? Do you have any idea?? – I have 10 fingers Oct 27 '22 at 08:34
  • @Ihave10fingers Probably `${GIT_BRANCH}` doesn't have any value when you are passing it. Have the checked this? – ycr Oct 27 '22 at 11:00
  • Hey... you were right the passed value wasn't in the conditions what a stupid i am Anyways i'll accept your answer thank you – I have 10 fingers Oct 27 '22 at 14:46