title: In jenkins declarative pipeline, how can I set environment variable based on method?
body: <p>In jenkins declarative pipeline, how can I set the value of an environment variable based on custom groovy/powershell method? For instance, if I have a delcarative pipeline as follows, can I use a shared library method to set this value? 
  Essentially I am trying to use a multibranch Declarative Pipeline jenkins job which has a deploy stage, but I need to ensure that develop branches are deployed to DEV, Release branches are deploying to STG, but using the same pipeline. My thought was to create an environment variable that is set based on a custom method (in perhaps Groovy in shared library), and that method would simply look at the current value for env.BRANCH and simply have a little logic to set the value of the target deploy environment. Here is an example of what I envision</p>
  
  <pre><code>pipeline {
  environment {
      DEPLOY_ENV = mapBranchToDeployEnvironment(${BRANCH})
  }
  </code></pre>
  
  <p>And then in my deploy stage I would use this value in two powershell invocations </p>
  
  <pre><code>bat "powershell .\\Deploy-Service -Environment ${DEPLOY_ENV}"
  
  bat "powershell .\\Deploy-ServiceProxy -Environment ${DEPLOY_ENV}"
  </code></pre>
  
  <p>Otherwise, How are people current solving the problem of using the same pipeline to deploy to different environments while using the variables across many other function invocations? What is the recommended approach from Jenkins on mapping a branch name that triggered the build to an environment (if any) it should be deployed to? 
  Based on my understanding, the Declarative Pipeline allows a pipeline to be "multibranch", which, if the job deploys as well, it needs to map to an deploy environment. How else would a pipeline deploy using multibranch to multiple environments when all the global jenkins pipeline environment variables are the same value for every job /branch execution?</p>
  
  <p>In the above scenario, the pipeline variable 'DEPLOY_ENV' is derived from other environment variables that are set by the job and are available typically at the stage level, but here we are looking to set the value globally so that we can use it across stages</p>
  
  <p>Update: My issue was that I didnt realize how simple it was and instead thought that I had to pass in a stage or script object into a groovy shared library function, when in fact its as simple as creating a shared library, then directly referencing the environment variables in the method. Easy. Thank you. </p>