Jenkinsfile`pipeline`用于使用不同的github凭据作为不同的阶段?

发布于 2025-01-27 14:07:26 字数 1164 浏览 3 评论 0原文

我有一个Jenkins管道在两个节点上并行进行了两次测试,类似:

pipeline
{
    agent { label 'MASTER' }

    stages
    {
        stage('x86 and Arm tests')
        {
            parallel
            {
                stage('x86')
                {
                    agent
                    {
                        label ('x86')
                    }
                    steps
                    {
                        sh "something"
                    }
                }
                stage('arm')
                {
                    agent
                    {
                        label ('arm')
                    }
                    steps
                    {
                        sh "something"
                    }
                }
            }
        }
    }
}

问题是GitHub凭据:我需要为这两个测试(X86 Machine vs Arm Machine)拥有两个不同的GitHub凭据。

目前,我已经配置了我的作业以通过X86计算机的SSH键进行身份验证。但是这个钥匙似乎不起作用。

我如何告诉jenkins 管道stage中使用特定的github克隆/结帐凭据?

处理凭证

I have a Jenkins pipeline running two tests in parallel on two nodes, something like:

pipeline
{
    agent { label 'MASTER' }

    stages
    {
        stage('x86 and Arm tests')
        {
            parallel
            {
                stage('x86')
                {
                    agent
                    {
                        label ('x86')
                    }
                    steps
                    {
                        sh "something"
                    }
                }
                stage('arm')
                {
                    agent
                    {
                        label ('arm')
                    }
                    steps
                    {
                        sh "something"
                    }
                }
            }
        }
    }
}

The problem are the GitHub credentials: I need to have two different GitHub credentials for the two tests (x86 machine vs Arm machine).

Currently I have configured my job to authenticate via SSH key for the x86 machine. But this key seems not to work for Arm job.

How do I tell the Jenkins pipeline to use specific GitHub clone/checkout credentials within the stage?

Handling credentials.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-03 14:07:27

从SCM运行声明的管道时,对于执行过程中使用的每个代理,用于加载管道的存储库将自动检查到工作空间。
如果要更改此默认行为并手动控制每个代理的SCM配置“ rel =“ nofollow noreferrer”> skipdefaultCheckout 选项。

skipdefaultcheckout
默认情况下,在代理指令中默认从源控件中检查代码。
例如:options {skipdefaultcheckout()}

然后在每个代理中您应该使用分支,存储库URL和凭据等所需参数查看存储库。为此,您可以使用通用 checkout>可以使用其他SCM特定步骤,例如 git 是更强大的结帐步骤子集的简化速记。
如果在其中一个代理商上,您想使用用于获取管道脚本的相同配置(例如默认行为),则可以使用Checkout SCM
这是一个有几个选项的示例:

pipeline {
    agent any
    options {
       skipDefaultCheckout()
    }
    stages {
        stage('Tests') {
            parallel {
                stage('x86') {
                    agent {
                        label 'x86'
                    }
                    steps {
                       // use the same configuration used for fetching the pipeline itself
                       checkout scm
                    }
                }
                stage('arm') {
                    agent {
                        label 'arm'
                    }
                    steps {
                        // use the checkout step - in this case for Git SCM
                        checkout([$class: 'GitSCM', branches: [[name: '*/master']],
                            userRemoteConfigs: [[credentialsId: 'my-private-key-credential-id', url: 'http://git-server/user/repository.git']]])
                    }
                }
                stage('x64') {
                    agent {
                        label 'x64'
                    }
                    steps {
                        // use the shortened git step
                        git branch: 'master', credentialsId: 'my-private-key-credential-id', url: 'https://github.com/jenkinsci/jenkins.git'
                    }
                }
            }
        }
    }
}

您现在可以选择在每个构建代理上定义不同的凭据,并且在结帐过程中将使用它们。另一个替代方法是使用SCM步骤所具有的recertialSID选项,定义jenkins(用户名和密码或ssh键)的SCM提供商的凭据所需不需要对代理商进行任何预先配置。
请参阅使用jenkins中的凭据中的凭据。

此外,所有传递给SCM步骤(分支,URL,凭据等)的所有参数都可以定义为作业参数 - 使您更加灵活性和轻松执行不同的配置。

When running a declarative pipeline from SCM, for each agent that is used during the execution the repository used for loading the pipeline will be automatically checked out to the workspace.
If you want to change this default behavior and manually control the SCM configuration for each agent you must first disable that behavior using the skipDefaultCheckout option.

skipDefaultCheckout
Skip checking out code from source control by default in the agent directive.
For example: options { skipDefaultCheckout() }

Then in each agent you should check out your repository using your required parameters like branch, repository URL and credentials. For that you can use the generic checkout step or you can use other SCM specific steps like the git step which is a simplified shorthand for a subset of the more powerful checkout step.
If, on one of the agents, you want to use the same configuration used for fetching the pipeline script (like the default behavior) you can do so by using checkout scm.
Here is an example with several options:

pipeline {
    agent any
    options {
       skipDefaultCheckout()
    }
    stages {
        stage('Tests') {
            parallel {
                stage('x86') {
                    agent {
                        label 'x86'
                    }
                    steps {
                       // use the same configuration used for fetching the pipeline itself
                       checkout scm
                    }
                }
                stage('arm') {
                    agent {
                        label 'arm'
                    }
                    steps {
                        // use the checkout step - in this case for Git SCM
                        checkout([$class: 'GitSCM', branches: [[name: '*/master']],
                            userRemoteConfigs: [[credentialsId: 'my-private-key-credential-id', url: 'http://git-server/user/repository.git']]])
                    }
                }
                stage('x64') {
                    agent {
                        label 'x64'
                    }
                    steps {
                        // use the shortened git step
                        git branch: 'master', credentialsId: 'my-private-key-credential-id', url: 'https://github.com/jenkinsci/jenkins.git'
                    }
                }
            }
        }
    }
}

You now have the option to define different credentials on each build agent, and they will be used during the checkout process. Another alternative is to use the credentialsId option that the SCM steps have, define the credentials need for the SCM provider in Jenkins (username and password or ssh key) and pass them to the step - that way you do not need any pre-configuration on the agent.
See Using Credentials in Jenkins for more info.

In addition all the parameters that are passed to the SCM steps (branch, url, credentialsId, etc.) can be define as Job parameters - allowing you more flexibility and easy execution of different configurations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文