diff --git a/Jenkinsfile b/Jenkinsfile index 0815d07..2a57def 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -93,8 +93,17 @@ pipeline { stage('SNAPSHOT: Push main') { when { expression { params.SNAPSHOT } } steps { - sshagent(credentials: ['jenkins-ssh-key']) { - bat "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 "git push origin HEAD:main" + } + } finally { + bat 'if exist git-ssh.cmd del /f /q git-ssh.cmd' + } + } } echo "SNAPSHOT 已推送到 main 分支" echo "应用端 project.yml 保持 branch: main 即可自动获取最新代码" @@ -109,9 +118,18 @@ pipeline { bat "git add version.properties" bat """git commit -m "ci: release %RELEASE_TAG% [%CHANGED_MODULES%]" """ bat "git tag %RELEASE_TAG%" - sshagent(credentials: ['jenkins-ssh-key']) { - bat "git push origin HEAD:main" - bat "git push origin %RELEASE_TAG%" + 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 "git push origin HEAD:main" + bat "git push origin %RELEASE_TAG%" + } + } finally { + bat 'if exist git-ssh.cmd del /f /q git-ssh.cmd' + } + } } } } diff --git a/README.md b/README.md index 6293508..9b003c0 100644 --- a/README.md +++ b/README.md @@ -397,3 +397,8 @@ pod repo add xuqm-specs https://xuqinmin.com/xuqinmin12/xuqm-specs.git pod spec lint XuqmSDK.podspec --allow-warnings pod repo push xuqm-specs XuqmSDK.podspec --allow-warnings ``` + +### Jenkins SSH + +Windows Jenkins 节点通过 `jenkins-ssh-key` 私钥凭据生成构建期临时 +`git-ssh.cmd` 执行主分支和标签推送,作用域结束后立即删除该脚本。