改用显式Git完成AGC检出
在Windows节点通过sshagent调用已安装的Git可执行文件 保留分支与指定commit校验
This commit is contained in:
@@ -28,40 +28,40 @@ pipeline {
|
||||
stages {
|
||||
stage('Checkout') {
|
||||
steps {
|
||||
withEnv(["PATH=${env.AGC_WINDOWS_PATH}"]) {
|
||||
checkout([
|
||||
$class: 'GitSCM',
|
||||
branches: [[name: "*/${params.SOURCE_BRANCH}"]],
|
||||
doGenerateSubmoduleConfigurations: false,
|
||||
extensions: [
|
||||
[$class: 'CleanBeforeCheckout'],
|
||||
[$class: 'CloneOption', shallow: false, noTags: false, timeout: 30],
|
||||
],
|
||||
userRemoteConfigs: [[
|
||||
url: env.GIT_REMOTE_URL,
|
||||
credentialsId: env.GIT_REMOTE_CREDENTIAL_ID,
|
||||
refspec: "+refs/heads/${params.SOURCE_BRANCH}:refs/remotes/origin/${params.SOURCE_BRANCH}",
|
||||
]],
|
||||
])
|
||||
sshagent(credentials: [env.GIT_REMOTE_CREDENTIAL_ID]) {
|
||||
powershell '''
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$git = 'C:\\Tools\\Git\\cmd\\git.exe'
|
||||
if (-not (Test-Path $git)) { throw "找不到 Git: $git" }
|
||||
if (Test-Path '.git') {
|
||||
& $git fetch --tags --force --prune origin "+refs/heads/$($env:SOURCE_BRANCH):refs/remotes/origin/$($env:SOURCE_BRANCH)"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Git fetch 失败' }
|
||||
& $git clean -fdx
|
||||
& $git reset --hard "origin/$($env:SOURCE_BRANCH)"
|
||||
} else {
|
||||
& $git clone --no-single-branch $env:GIT_REMOTE_URL .
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Git clone 失败' }
|
||||
& $git fetch --tags --force origin "refs/heads/$($env:SOURCE_BRANCH):refs/remotes/origin/$($env:SOURCE_BRANCH)"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Git fetch 分支失败' }
|
||||
& $git checkout --detach "origin/$($env:SOURCE_BRANCH)"
|
||||
}
|
||||
$commit = $env:COMMIT_HASH.Trim()
|
||||
if ($commit) {
|
||||
if ($commit -notmatch '^[0-9a-fA-F]{7,64}$') {
|
||||
throw 'COMMIT_HASH 必须是 7-64 位十六进制 Git commit。'
|
||||
}
|
||||
git cat-file -e "${commit}^{commit}"
|
||||
& $git cat-file -e "${commit}^{commit}"
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "COMMIT_HASH 不存在: $commit"
|
||||
}
|
||||
$branchRef = "refs/remotes/origin/$($env:SOURCE_BRANCH)"
|
||||
git merge-base --is-ancestor $commit $branchRef
|
||||
& $git merge-base --is-ancestor $commit $branchRef
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "COMMIT_HASH 不属于 SOURCE_BRANCH: $($env:SOURCE_BRANCH)"
|
||||
}
|
||||
git checkout --detach $commit
|
||||
& $git checkout --detach $commit
|
||||
}
|
||||
$resolved = (git rev-parse HEAD).Trim()
|
||||
$resolved = (& $git rev-parse HEAD).Trim()
|
||||
Set-Content -Path '.jenkins-source-commit' -Value $resolved -NoNewline
|
||||
Write-Host "源码 commit=$resolved"
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user