2016-07-19 74 views
23

我下面this tutorial签出Jenkins Pipeline Git SCM凭证?

node { 
    git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git' 
    ... 
} 

但是它并没有告诉如何添加凭据。詹金斯确实有特定的“证书”部分,您可以在其中定义用户用户& pass,然后获取用于作业的ID,但是如何在管道指令中使用该ID?

我试着用:

git([url: '[email protected]:company/repo.git', branch: 'master', credentialsId: '12345-1234-4696-af25-123455']) 

没有运气:

stderr: Host key verification failed. 
fatal: Could not read from remote repository. 

Please make sure you have the correct access rights 
and the repository exists. 

有没有办法配置管道vim的信任状,或做我必须把SSH密钥对詹公司的Linux用户的.ssh/authorized_keys文件?

在理想的世界中,我希望有一个管道作业和回购密钥的存储库,然后启动Docker Jenkins,并在那里动态添加这些作业和密钥,而无需在Jenkins控制台中配置任何内容。

回答

48

您可以使用下面的管道:

git branch: 'master', 
    credentialsId: '12345-1234-4696-af25-123455', 
    url: 'ssh://[email protected]:company/repo.git' 

如果你使用ssh网址,然后你的信用凭证必须是用户名+私钥。如果您使用的是https克隆url而不是ssh,那么您的凭据应该是用户名+密码。

+0

修好了,谢谢。我不知道SSH-url和HTTPS-url需要不同的凭证才能使用! – Render

+1

这很有帮助,但'credentialsId'来自['/var/lib/jenkins/credentials.xml'](https://stackoverflow.com/a/35603191/432903)中的id,因为我不得不努力去计算它出。 – prayagupd

+4

@prayagupd,你应该能够从凭证页面('http:// yourjenkinsinstall/credentials')获得凭证ID。无需拖曳配置文件。 –

5

如果你想使用ssh凭证,

git(
     url: '[email protected]<repo_name>.git', 
     credentialsId: 'xpc', 
     branch: '${branch}' 
    ) 
如果你想使用的用户名和密码凭据

,你需要使用HTTP克隆作为@Serban提及。

git(
     url: 'https://github.com/<repo_name>.git', 
     credentialsId: 'xpc', 
     branch: '${branch}' 
    )