1

In Jenkins1 we had a script to set environment variables, something like that :

//...imports
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

newEnvVarsNodeProperty = null
envVars = null

if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
  newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
  globalNodeProperties.add(newEnvVarsNodeProperty);
  envVars = newEnvVarsNodeProperty.getEnvVars();
  envVars.put(NEW_VAR, "toto");
} else {
  for (property in envVarsNodePropertyList) {
    envVars = property.getEnvVars();
    envVars.put("EXISTING_VAR","tata");
  }
}
instance.save()

Today we're using a Jenkins2, with the jenkinsfile feature and I wanted to know how to do the same thing into a jenkinsfile?

does this would work, if yes does there is a better way ? :

def setEnvVar() {
    script {
    //all the script above
    }
}
pipeline {
agent any
tools {
    maven 'Maven3_6'
    jdk 'Jdk8'
}
stages {
    stage ('Initialize') {
        steps {
            setEnvVar()
        }
    }
...

EDIT : let's say I have an existing env variable, LAST_DEPLOY = "0.1" using :

environment {
    LAST_DEPLOY = "0.2"
 } 

will only modify the variable for the current execution and somehow at the end it will be reset to "0.1", I want the modification to last for the next builds.

Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
  • 1
    I am not aware of any intrinsic functionality that would achieve this, so you would probably be better off putting that method you wrote into a shared library and invoking it within the `Jenkinsfile`. – Matthew Schuchard Mar 01 '19 at 12:06
  • have you checked my answer below ? – Mostafa Hussein Mar 01 '19 at 13:11
  • @MostafaHussein `env.variable` is not working, I just tried and it's as I explained in my question EDIT, I will try soon enough `def variable = "smt"` – Logan Wlv Mar 01 '19 at 13:17
  • `env.variable` should be working. Just to confirm i am using this with declarative syntax and i can pass the variable from stage to another. regarding `environment{}` i have not used it before so i am not sure if that is the same as `env.variable` – Mostafa Hussein Mar 01 '19 at 13:19
  • yes a stage to another within the same execution, but if you relaunch the pipeline any modifications done on `env.variable` will be lost – Logan Wlv Mar 01 '19 at 13:21
  • Another approach, may not the best but might be useful. can't you read the value from a file saved with your project ? instead of saving it to a pipeline job ? So for example reading from config file or version.txt then populate it to your jenkins pipeline while running as it will be easier to maintain and less code also does not makes you vulnerable to lose the saved data somehow – Mostafa Hussein Mar 01 '19 at 13:24

2 Answers2

0

just look into official docs, https://jenkins.io/doc/pipeline/tour/environment/

   environment {
        NODE_ENV = 'sandbox'
        DB_NAME    = 'db_common'
    }

and then you can use it in sh step like ${NODE_ENV}

Denys
  • 38
  • 8
  • 1
    But I tested this and if I do something like environment { TITI = "TITI_V0" } TITI will be visible for the current jenkins execution only and not in the next ones – Logan Wlv Mar 01 '19 at 09:39
  • I would like to be able to create a Env variable in the jenkins file that will still exist at the end of the jenkins execution – Logan Wlv Mar 01 '19 at 09:39
0

So the current solution I found was to add this in the jenkins file :

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import hudson.slaves.*

def updateEnvVar() {
    script {
    instance = Jenkins.getInstance()
    globalNodeProperties = instance.getGlobalNodeProperties()
    envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

    newEnvVarsNodeProperty = null
    envVars = null

    if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
      newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
      globalNodeProperties.add(newEnvVarsNodeProperty);
      envVars = newEnvVarsNodeProperty.getEnvVars();
      envVars.put(NEW_VAR, "toto");
    } else {
      for (property in envVarsNodePropertyList) {
        envVars = property.getEnvVars();
        envVars.put("EXISTING_VAR","tata");
      }
    }

    instance.save()
    }
}
pipeline {
...

At first Jenkins will send some security errors that you can fix (AT your own risk) by going to Manage Jenkins -> In Process Script Approval -> Approve

Cf: Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject https://jenkins.io/doc/book/managing/script-approval/

Logan Wlv
  • 3,274
  • 5
  • 32
  • 54