Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否使用凭据签出Jenkins Pipeline Git SCM?_Git_Jenkins_Ssh_Jenkins Pipeline - Fatal编程技术网

是否使用凭据签出Jenkins Pipeline Git SCM?

是否使用凭据签出Jenkins Pipeline Git SCM?,git,jenkins,ssh,jenkins-pipeline,Git,Jenkins,Ssh,Jenkins Pipeline,我的发言如下: 但是,它没有说明如何添加凭据。Jenkins确实有特定的“凭证”部分,您可以在其中定义user&pass,然后获取作业中使用的ID,但如何在管道指令中使用它 我试过: git([url: 'git@bitbucket.org:company/repo.git', branch: 'master', credentialsId: '12345-1234-4696-af25-123455']) 运气不好: stderr: Host key verification failed.

我的发言如下:

但是,它没有说明如何添加凭据。Jenkins确实有特定的“凭证”部分,您可以在其中定义user&pass,然后获取作业中使用的ID,但如何在管道指令中使用它

我试过:

git([url: 'git@bitbucket.org: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.
有没有办法在管道中配置Cred,或者我必须将SSH密钥放入Jenkin的Linux用户的.SSH/authorized_keys文件中


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

您可以在管道中使用以下内容:

git branch: 'master',
    credentialsId: '12345-1234-4696-af25-123455',
    url: 'ssh://git@bitbucket.org:company/repo.git'

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

如果您要使用ssh凭据

  git(
       url: 'git@github.com<repo_name>.git',
       credentialsId: 'xpc',
       branch: "${branch}"
    )
git(
网址:'git@github.com.git',
CredentialId:'xpc',
分支:“${branch}”
)
如果要使用用户名和密码凭据,则需要使用http clone,如@Serban所述

    git(
       url: 'https://github.com/<repo_name>.git',
       credentialsId: 'xpc',
       branch: "${branch}"
    )
git(
网址:'https://github.com/.git',
CredentialId:'xpc',
分支:“${branch}”
)

使用特定凭据显式签出

    stage('Checkout external proj') {
        steps {
            git branch: 'my_specific_branch',
                credentialsId: 'my_cred_id',
                url: 'ssh://git@test.com/proj/test_proj.git'

            sh "ls -lat"
        }
    }
根据当前Jenkins作业中配置的凭据进行签出

    stage('Checkout code') {
        steps {
            checkout scm
        }
    }

您可以在一个Jenkins文件中使用这两个阶段。

对于值得添加到讨论中的内容。。。我所做的一切最终帮助了我。。。因为管道是在docker映像内的工作区内运行的,docker映像每次运行时都会被清理。我在管道中获取了对repo执行必要操作所需的凭据,并将其存储在.netrc文件中。这使我能够成功地授权git回购操作

withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
    sh '''
        printf "machine github.com\nlogin $GIT_USERNAME\n password $GIT_PASSWORD" >> ~/.netrc
        // continue script as necessary working with git repo...
    '''
}
withCredentials([usernamePassword(credentialsId:'',passwordVariable:'GIT\u PASSWORD',usernameviable:'GIT\u USERNAME')){
“嘘”
printf“machine github.com\nlogin$GIT\u USERNAME\n password$GIT\u password”>>~/.netrc
//根据需要继续使用git repo脚本。。。
'''
}

为您添加一个使用git插件的快速示例:


它解决了我使用

checkout scm: ([
                    $class: 'GitSCM',
                    userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
                    branches: [[name: 'refs/tags/${project_tag}']]
            ])

这就解决了,谢谢。我不知道SSH url和HTTPS url需要不同的凭据才能使用!这很有帮助,但是
credentialsId
来自中的id,因为我必须努力弄清楚它。@prayagupd,您应该能够从凭据页(
http://yourjenkinsinstall/credentials
)。无需搜索配置文件。您知道是否可以重复使用作业中定义的凭据吗?适用于询问“如何生成凭据ID”的用户。在这里如何找到它。[1.单击Jenkins主页上的凭据,2.然后您将看到一个包含您创建的所有凭据的表。3.ID在此表中]如何生成此credentialsId?请查看-凭据文件应存储在何处。jenkins sais:警告:找不到CredentialId“jenkins_key”。@Dinu凭据是在jenkins中创建的,如果插件已安装,您应该在主菜单中看到它。非常感谢。有人发布了整个内容,而不仅仅是在这里和这里,并希望人们神奇地知道接下来要放什么。如何生成此credentialsId?我生成的凭据如下:,我将公钥添加到我的git中,但我必须将此文件存储在何处。Jenkins说:警告:找不到凭据有效的“Jenkins_key”。@Dinuclae请在下面的链接中参阅
添加新的全局凭据->7。
。您知道如何为整个团队使用全局凭据吗?或者,有没有一种方法可以让无论哪个开发人员推动github,他们都可以提供他们的凭据,而不必在Jenkins文件中公开凭据。您可以在开发团队中管理与您自己的逻辑相关的机制,并为每个组使用不同的凭据密钥。例如:如果Github用户在“后端开发者”使用列表中,如果Github用户在“前端开发者”使用列表中,请设计与您自己的用例相关的机制。您将把这些凭据保存在哪里?使用Jenkins Credentials插件吗?使用Jenkins Credentials文档-我已经搜索了很多地方,找到了一个简单的
结帐
示例,谢谢。
    checkout([
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanCheckout']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
    ])
stage('checkout'){
    steps{
        script{
            checkout
        }
    }
}
checkout scm: ([
                    $class: 'GitSCM',
                    userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
                    branches: [[name: 'refs/tags/${project_tag}']]
            ])