在詹金斯中找不到环境变量 [英] Environment variables are not found in Jenkins

查看:119
本文介绍了在詹金斯中找不到环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在詹金斯中设置很多变量.我试过将它们放在jenkins用户的.bashrc.bash_profile.profile中,但是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.

唯一可行的方法是将所有env变量放入Jenkinsfile内,如下所示:

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.

在Jenkins中设置环境变量的正确方法是什么?

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

推荐答案

在我看来,这很正常. INTERCOM_PERSONAL_ACCESS_TOKENINTERCOM_IDENTITY_VERIFICATION_KEY应该被视为文本凭据,您可以使用 environment 指令添加环境变量.

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}"
        }
    }
}

如果需要将环境变量与JenkinsFile分开,则可以创建一个包含所有这些变量的groovy文件,然后使用

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:使用环境变量加载文件...

这篇关于在詹金斯中找不到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆