Jenkinsfile管道如何访问“全局密码”

4

我该如何访问Jenkins全局配置中定义的密码?

默认情况下不会注入密码,我尝试了下面的代码并成功访问了“全局属性”,但无法访问密码。

def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars() 
println envVars['MY_VARIABLE']

你有检查过以下的答案吗?我知道它是在PowerShell中,但我认为相同的概念可以应用于Bash。 - Mostafa Hussein
根据此声明,您是否正在使用EnvInject或Credentials Binding插件? - StephenKing
2
澄清一些事情 - 查看免费样式作业的config.xml文件,我可以利用“全局密码”,它是plugin="envinject@2.1.6",并将injectGlobalPasswords设置为true。这部分似乎与我正在尝试设置的基于Jenkinsfile的多分支管道不太兼容,尽管“全局属性”被正确注入。因此,我期待类似于我在问题中发布的属性解决方案的解决方案。 - awattar
4个回答

2

1
正确,但需要定义额外的凭据配置 - 问题是关于访问在“全局密码”中定义的密码。 - awattar
抱歉,我不知道 Jenkins 中的“全局密码”是什么。它们在哪里定义? - StephenKing
在 Jenkins -> 全局配置 -> 配置 页面上: - awattar
我的安装中没有“全局密码”部分。这是来自插件吗? - StephenKing

1

你是在指Jenkins -> 管理Jenkins -> 全局属性吗?

如果是的话,以下是我们在groovy脚本中检索它们的方法:

import jenkins.model.*

instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()

globalNodeProperties.each {
  envVars = it.getEnvVars()
  if (envVars.get('ARTIFACTORY_USR') != null) {
   artifactory_usr = envVars.get('ARTIFACTORY_USR');
  }
  if (envVars.get('ARTIFACTORY_PSW') != null) {
   artifactory_pwd = envVars.get('ARTIFACTORY_PSW');
  }
}

ARTIFACTORY_USR和ARTIFACTORY_PSW是预定义的全局属性。

这是关于全局密码而不是属性的问题。 - awattar
似乎我们都对“全局密码”功能感到困惑,不知道该怎么用。这个问题似乎有答案...“不要混淆EnvInject插件和Credentials Binding插件。两者的作用完全不同,但都允许全局管理密码,只是方式不同。” - StephenKing

0

非流水线作业的全局密码注入选项来自EnvInject插件,该插件对于流水线作业并不完全支持: https://plugins.jenkins.io/envinject/

Jenkins Pipeline compatibility
Even though it is possible to set up the EnvInject Job Property and build step in Pipeline, the plugin does not provide full compatibility with Jenkins Pipeline.

Supported use-cases:

Injection of EnvVars defined in the "Properties Content" field of the Job Property
These EnvVars are being injected to the script environment and will be inaccessible via the "env" Pipeline global variable
Please note there is also a known compatibility issue with Durable Task 
Plugin 1.13
All other use-cases of the plugin may work incorrectly in Jenkins Pipeline. Please see JENKINS-42614 for more information about unsupported use-cases. There is no short-term plan to fix the compatibility issues though any pull requests to the plugin will be appreciated.

我有同样的使用情况,最终重新创建了全局密码作为凭据。


0
通常情况下,我会在Jenkins的凭据页面中创建凭据,以便在流水线中访问凭据。
如何在Jenkins中创建凭据: 1. 打开凭据页面(Jenkins -> 凭据) 2. 创建一个带有用户名和密码的凭据,并定义一个有效的ID(例如:myCredentialId)
如何使用withCredentials在流水线中访问凭据。
pipeline {
agent any

stages {
    stage("Access the credentials") {
        steps {
            script {
               withCredentials([[
                $class:'UsernamePasswordMultiBinding', 
                credentialsId: 'myCredentialId',
                usernameVariable:'username',
                passwordVariable:'token'
                ]]) {
                 sh '''
                  curl -u ${username}:${token} -L <replace your git URL> -o master.txt
                  cat master.txt   
                '''
                }
            }
        }
    }
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接