简体   繁体   English

如何从詹金斯“带密码的用户名”参数中提取用户名和密码?

[英]How to extract the username and password from a jenkins “username with password” parameter?

I have a Jenkins pipeline job. 我有一个詹金斯管道工作。 It takes several parameters, one of which is a "Username with password" parameter, which is set to a "service account" that I've had provisioned. 它带有几个参数,其中一个是“带密码的用户名”参数,该参数设置为我已配置的“服务帐户”。

This is used in the "StashNotifier" to notify BitBucket of the results of a build. 在“ StashNotifier”中使用它来将生成结果通知BitBucket。

I now have to add some "scp/ssh" steps to the job, and I'd like to use the same service account principal and credentials. 现在,我必须在工作中添加一些“ scp / ssh”步骤,并且我想使用相同的服务帐户主体和凭据。 I could manually add two parameters, redundant copies of the username and password of the service account parameter, but I would really like to just extract the pieces from the existing "Username and password" parameter. 我可以手动添加两个参数,即服务帐户参数的用户名和密码的冗余副本,但是我真的很想从现有的“用户名和密码”参数中提取片段。

Is it possible to do that? 有可能这样做吗?

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { 
    // use USERNAME and PASSWORD as envs - ${USERNAME}
    // credentialId is the credential id from Credentials tab 
}

Generally speaking if you are using ssh/scp the ssh-agent is what you want. 一般来说,如果您使用的是ssh / scp,则需要ssh-agent (And usually you also want public key authentication instead of the password, but that is another story) (通常,您还需要公钥身份验证而不是密码身份验证,但这是另一回事了)

node {
    sshagent (credentials: ['your-credential-id']) {
        sh 'ssh blaa blaa'
    }
}

As mentioned by @jil it would make things a lot easier if you'd use ssh keys for the authentication at your SSH server - assuming your jenkins server is running on a linux / unix machine. 如@jil所述,如果您在SSH服务器上使用ssh密钥进行身份验证,将会使事情变得容易得多-假设您的jenkins服务器正在linux / unix机器上运行。 You can add the keys to the system user jenkins which runs Jenkins and copy it to the machine to which you want to connect for your scp / ssh steps. 您可以将密钥添加到运行Jenkins的系统用户jenkins ,然后将其复制到要连接到您的scp / ssh步骤的计算机上。 Afterwards you can directly use any ssh / scp command without providing a password. 之后,您可以直接使用任何ssh / scp命令,而无需提供密码。

There are some security considerations though: adding the SSH key to the jenkins user enables every job on the Jenkins server to access your SSH server. 但是,有一些安全方面的考虑 :将SSH密钥添加到jenkins用户使Jenkins服务器上的每个作业都可以访问您的SSH服务器。

If this is not a problem for you - this is a good article on how to use ssh keys . 如果这对您来说不是问题-这是一篇有关如何使用ssh键好文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM