diff --git a/Jenkinsfile b/Jenkinsfile index 31869a9..9330b5d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -148,15 +148,24 @@ print('versions.json updated: ${svcName}=${svcVer}') stage('Commit Version') { steps { - sshagent(credentials: ['jenkins-ssh-key']) { - bat """ - chcp 65001 >nul - git config user.email "jenkins@xuqm.com" - git config user.name "Jenkins CI" - git add ${env.VERSION_FILE} - git diff --cached --quiet || git commit -m "ci: bump ${env.IMAGE_NAME} to ${env.SERVICE_VERSION} [skip ci]" - git push origin HEAD:main - """ + script { + withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-ssh-key', keyFileVariable: 'GIT_SSH_KEY')]) { + writeFile file: 'git-ssh.cmd', text: '@echo off\r\nssh -i "%GIT_SSH_KEY%" -o IdentitiesOnly=yes %*\r\n', encoding: 'UTF-8' + try { + withEnv(["GIT_SSH=${pwd()}\\git-ssh.cmd"]) { + bat """ + chcp 65001 >nul + git config user.email "jenkins@xuqm.com" + git config user.name "Jenkins CI" + git add ${env.VERSION_FILE} + git diff --cached --quiet || git commit -m "ci: bump ${env.IMAGE_NAME} to ${env.SERVICE_VERSION} [skip ci]" + git push origin HEAD:main + """ + } + } finally { + bat 'if exist git-ssh.cmd del /f /q git-ssh.cmd' + } + } } } } diff --git a/README.md b/README.md index 61e31a7..aa67f84 100644 --- a/README.md +++ b/README.md @@ -183,3 +183,8 @@ location /api/ { proxy_pass http://127.0.0.1:8081/api/; } ``` + +### Jenkins SSH + +Windows Jenkins 节点通过 `jenkins-ssh-key` 私钥凭据生成构建期临时 +`git-ssh.cmd` 执行版本提交推送,作用域结束后立即删除该脚本。