首页 > 解决方案 > Jenkins Pipeline Git Push 通过 Windows 代理节点

问题描述

所以我有以下场景:

Jenkins 主节点:Red Hat 7 Jenkins 代理节点:Windows Server 2019

使用 Windows 代理节点,我正在尝试签出存储库、添加/更新更改以及提交并推送回 GitHub。

我签出并提交正常工作,但在推回 GitHub 时遇到问题。我尝试了一些方法,但似乎都没有。

管道:

def NodeLabel = 'windows'
//def CustomWorkSpace = "C:/JenkinsWorkspace/${env.JOB_BASE_NAME}"
def CustomWorkSpace = "C:/Work"

// Organization
def OrganizationName = [
    'organization1',
    'organization2'
]

// Solution
def SolutionMap = [
    SolutionA: [
        SolutionName: 'SolutionA',
        RequiredVersion: '1.0.0.0',
        UnpackedFileFolder: 'FolderA/ConfigurationA/SolutionA'
    ]
]

pipeline {
    agent {
        node {
            <Node Stuff...>
        }
    }

    environment {
        RELEASE_NAME = 'Release-0'
        SOURCE_DIRECTORY = 'Source'
    }

    parameters {
        <Parameter...>
    }

    stages {
        // Check Out Repository
        stage('Check Out Repository') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [
                        [name: '*/development']
                    ],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [
                        [$class: 'RelativeTargetDirectory',
                            relativeTargetDir: SOURCE_DIRECTORY],
                        [$class: 'LocalBranch',
                            localBranch: RELEASE_NAME],
                        [$class: 'UserIdentity',
                            email: '<Email>',
                            name: '<Name>'],
                        [$class: 'CleanBeforeCheckout',
                            deleteUntrackedNestedRepositories: true]
                    ],
                    submoduleCfg: [],
                    userRemoteConfigs: [
                        [credentialsId: '<Credential>',
                            url: '<URL>']
                    ]
                ])
            }
        }

        // Solution
        stage('Solution') {
            steps {
                script {
                    SolutionMap.each { Solution ->
                        <Some Work...>

                        // Git Commit Solution Source Code
                        stage("Git Commit Solution: ${Solution.key}") {
                            dir(SOURCE_DIRECTORY) {
                                powershell(script: """
                                git config core.fileMode false
                                git config core.autocrlf false
                                git add --all
                                git commit --allow-empty --message="Commit ${Solution.value.SolutionName} version ${Solution.value.RequiredVersion}"
                                """)
                            }
                        }
                    }
                }
            }
        }

        // Git
        stage('Git Push') {
            steps {
                dir(SOURCE_DIRECTORY) {
                    powershell(script: """
                    Write-Host User Profile: ${USERPROFILE}
                    Write-Host Release Name: ${RELEASE_NAME}
                    git push origin ${RELEASE_NAME}
                    """)
                }
            }
        }
    }
}

我尝试了几件事:

如果我只是尝试

git push origin ${RELEASE_NAME}

或者

powershell(script: """ssh-add ${USERPROFILE}/.ssh/id_rsa""")
powershell(script: """git push origin ${RELEASE_NAME}""")

我会得到:

powershell.exe : Could not create directory '/work/jenkins/latest/.ssh'.
At C:\Work\Source@tmp\durable-cfdcf986\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Could not creat...s/latest/.ssh'.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Host key verification failed.
fatal: Could not read from remote repository.

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

如果我要尝试 subshel​​l 方法:

ssh-agent bash -c 'ssh-add ${USERPROFILE}/.ssh/id_rsa; git push origin ${RELEASE_NAME}'

它执行,但似乎没有做任何事情或推动任何事情。

任何帮助,将不胜感激。

标签: gitjenkinsjenkins-pipeline

解决方案


推荐阅读