// 通过jenkins pipeline-syntax语法生成器获取SSH私钥
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'gituser', keyFileVariable: 'SSH_KEY_FOR_GIT', passphraseVariable: '', usernameVariable: '')]) {
// 自定义ssh参考<https://git-scm.com/docs/gitfaq>"How do I use multiple accounts with the same hosting provider using SSH"
sh "echo 'Host gitlab.repostory.com' >> ~/.ssh/config" // gitlab地址或域名
sh "echo '\tStrictHostKeyChecking no' >> ~/.ssh/config" // 是否检查指纹 即初次ssh忽略确认,否则构建任务将卡住
sh "echo '\tHostname gitlab.repostory.com' >> ~/.ssh/config" // gitlab地址或域名
sh "echo '\tUser git' >> ~/.ssh/config" // 仓库虚拟用户 可根据clone地址用户修改
sh "echo '\tPort 22' >> ~/.ssh/config" // 默认22 可自定义修改
sh "echo '\tPreferredAuthentications publickey' >> ~/.ssh/config" // 认证类型
sh "echo '\tIdentityFile ~/.ssh/id_devops' >> ~/.ssh/config" // 文件位置
sh "cp ${SSH_KEY_FOR_GIT} ~/.ssh/id_devops"
sh "chmod 600 ~/.ssh/id_devops" // 权限必须600 否则会报权限过大
dir("${PIPELINE_ROOT}/destproject"){
// 设置gitlab账户信息
sh "git config --global user.email 'git@mail'"
sh "git config --global user.name 'gituser'"
sh "git checkout master" // 必须先checkout,否则报分支错误
sh "git pull origin master" // 请提前确认默认分支或操作的分支名称
sh "rm -rf ${PIPELINE_ROOT}/destproject/*"
sh "cp -a ${PIPELINE_ROOT}/${sourcedir}/any/* ${PIPELINE_ROOT}/destproject/"
sh "git add ."
sh "git commit -m \"Tag_Val\" "
sh "git push origin master"
sh "git checkout master" // 必须存在,否则报分支错误或无任何提交
sh "git tag -a \"Tag_Val\" -m \"Tag_Val\" " // 参考<https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE>
sh "git push origin \"Tag_Val\" "
}
}
jenkinsfile通过ssh推送代码至gitlab
最新推荐文章于 2023-05-23 10:38:11 发布