Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13723

Environment variables are not found in Jenkins

I want to set quite a few variables in Jenkins. I have tried putting them in .bashrc, .bash_profile and .profile of the jenkins user but Jenkins cannot find them when a build is happening.

The only way that is working is to put all the env variables inside the Jenkinsfile like this:

env.INTERCOM_APP_ID = '12312'
env.INTERCOM_PERSONAL_ACCESS_TOKEN = '1231'
env.INTERCOM_IDENTITY_VERIFICATION_KEY='asadfas'

But I don't think this is a good way of doing it.

What is the correct way of setting env variables in Jenkins?

Upvotes: 3

Views: 7154

Answers (2)

Talha Junaid
Talha Junaid

Reputation: 2405

To me, it seems very normal. INTERCOM_PERSONAL_ACCESS_TOKEN and INTERCOM_IDENTITY_VERIFICATION_KEY should be considered as text credentials and you can use the environment directive to add environment variables.

stages {
    stage('Example') {
        environment {
            INTERCOM_APP_ID = '12312'
            INTERCOM_PERSONAL_ACCESS_TOKEN = credentials('TokenCrednetialsID')
            INTERCOM_IDENTITY_VERIFICATION_KEY = credentials('VerificationCrednetialsID')
        }
        steps {
            echo "Hello ${env.INTERCOM_APP_ID}"
        }
    }
}

If you need to keep environment variables separate from JenkinsFile you can create a groovy file which contains all of those and then load that file into Jenkinsfile using

load "$JENKINS_HOME/.envvars/stacktest-staging.groovy"

For more information take a look at following links

https://jenkins.io/doc/pipeline/steps/workflow-cps/

SO: Load file with environment variables ...

Upvotes: 2

Łukasz Jakubek
Łukasz Jakubek

Reputation: 1013

Jenkins resets environment variables to some defaults for their jobs. Best way to set them is in jenkins configuration. You can set global vars, local for project or local for node.

Now i do not remember if this feature is build in or provided by some plugin.

Upvotes: 0

Related Questions