Git 在 Jenkins 管道中推送失败?

Git push fails in Jenkins pipeline?

我有一个包含多个阶段的 Jenkinsfile。它是使用 BitBucket Pull Request Notfier 插件从 BitBucket 服务器触发的,该插件提供一些参数。第一阶段在结帐前进行清理、预合并,然后结帐到本地分支。

script {
  def scmVars = checkout changelog: true,
                         poll: true,
                         scm: [$class: 'GitSCM', branches: [[name: '${PULL_REQUEST_FROM_BRANCH}']],
                             browser: [$class: 'Stash', repoUrl: '${PULL_REQUEST_TO_SSH_CLONE_URL}'],
                             doGenerateSubmoduleConfigurations: false,
                             extensions: [
                                 [$class: 'CleanCheckout'],
                                 [$class: 'LocalBranch',localBranch:'${PULL_REQUEST_TO_BRANCH}'],
                                 [$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'origin', mergeStrategy: '< object of type org.jenkinsci.plugins.gitclient.MergeCommand.Strategy >', mergeTarget: '${PULL_REQUEST_TO_BRANCH}']]],
                             submoduleCfg: [],
                             userRemoteConfigs: [[credentialsId: 'eae6ff22-2ea6-4300-adf7-ca1efd2ef7eb', url: '${PULL_REQUEST_TO_SSH_CLONE_URL}']]]
}

稍后,如果管道成功,我有一个 post 步骤来推送合并。

post {
  success {
    script {
      powershell 'git push origin "${PULL_REQUEST_TO_BRANCH}"'
    }
  }
  always {
    script {
      currentBuild.result = currentBuild.result ?: 'SUCCESS'
      notifyBitbucket()


    }
    nunit testResultsPattern: "NUnitResult.xml"
  }
}

但是推送总是失败。

powershell.exe : fatal: The current branch develop has no upstream branch.

At C:\Jenkins\workspace\Website-Integrate@tmp\durable-64c064d3\powershellWrapper.ps1:5 char:3

+   & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Fi ...

+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (fatal: The curr...pstream branch.:String) [], RemoteException

    + FullyQualifiedErrorId : NativeCommandError

To push the current branch and set the remote as upstream, use

    git push --set-upstream origin develop

script returned exit code 128

我在从站工作区的 powershell 中执行以下操作:

PS C:\Jenkins\workspace\Website-Integrate> git remote show origin
* remote origin
  Fetch URL: ssh://bitbucket.teamsinspace.com:7999/dev/sitecore.git
  Push  URL: ssh://bitbucket.teamsinspace.com:7999/dev/sitecore.git
  HEAD branch: develop
  Remote branches:
    Team1-Integration               tracked
    Team2-Integration               tracked
    Team3-Integration               tracked
    develop                         tracked
    feature-NEON-10-setup-toll-gate tracked
    master                          tracked
  Local refs configured for 'git push':
    develop                         pushes to develop                         (fast-forwardable)
    feature-NEON-10-setup-toll-gate pushes to feature-NEON-10-setup-toll-gate (up to date)
PS C:\Jenkins\workspace\Website-Integrate>

我也可以从 powershell 推送:

PS C:\Jenkins\workspace\Website-Integrate> git push origin develop
Total 0 (delta 0), reused 0 (delta 0)
To ssh://bitbucket.teamsinspace.com:7999/dev/sitecore.git
   09e22ba528..316dea6c08  develop -> develop

为什么这在管道中总是失败?

We cannot substitute Jenkins variables in single quote strings。请参考read this。在您的代码中,行

 powershell 'git push origin "${PULL_REQUEST_TO_BRANCH}"'

不会替换变量${PULL_REQUEST_TO_BRANCH}。这是导致错误的原因。请将行设为

powershell "git push origin \"${PULL_REQUEST_TO_BRANCH}\""

这样它就会起作用。希望这是你需要的。