git - 如何将文件从一个仓库复制到另一个仓库 [Jenkins]
问题描述
我创建了一个使用 groovy 脚本(gradle)下载文件的 Jenkins 作业。
stages {
stage('Import and Unzip') {
steps {
cleanWs()
checkout([$class: 'GitSCM',
branches: [[name: "$GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: 'git@github.com/repo1.git',
]]
])
script{
bat './gradlew --build-file scripts/Jenkins/build.gradle'
}
dir('scripts/Jenkins') {
bat '../../../gradlew downloadfile'
}
}
}
}
所以现在当我运行这项工作时,我在 Jenkins 工作区中有一个文件......
我想在另一个 github repo 上添加并提交它: git@github.com/repo2.git
任何想法如何做到这一点?
提前致谢
解决方案
解决方案
您应该能够执行以下操作
- 在您的工作区中创建一个新目录
- 签出您希望推送到的存储库(我称之为 repo2.git )
- 将文件从工作区复制到您创建的新目录(克隆 repo2 的目录)
- 在 bat 中运行适当的 git 命令
在不知道文件、存储库或存储库分支的名称的情况下,我无法为您提供确切的代码,但它应该类似于以下内容
示例代码
stages {
stage('clone repos') {
steps {
cleanWs()
// clones repo1 in ${WORKSPACE}/repo1
dir('repo1') {
checkout([
$class: 'GitSCM',
branches: [[name: "$GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: 'git@github.com/repo1.git',
]]
])
}
// clones repo2 in ${WORKSPACE}/repo2
dir('repo2') {
checkout([
$class: 'GitSCM',
branches: [[name: "$OTHER_GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: 'git@github.com/repo2.git',
]]
])
}
}
}
stage('Import and Unzip') {
steps {
dir('repo1') {
bat './gradlew --build-file scripts/Jenkins/build.gradle'
dir('scripts/Jenkins') {
bat '../../../gradlew downloadfile'
}
}
}
}
stage('commit and push') {
steps {
dir('repo2') {
/**
* Copies the downloaded file from repo1 into the
* directory where we cloned repo2 then executing the
* appropriate git commands
**/
bat '''
cp ../repo1/scripts/Jenkins/<filename> .
git add <filename>
git commit -m "commit msg"
git push
'''
}
}
}
}
我稍微重构了您的代码,以便它在自己的阶段检查不同目录中的源存储库和目标存储库。我认为这是组织文件的一种更简洁的方式。
请注意,您显然需要替换为要添加到其他存储库的文件。您还需要将第二个结帐块中的存储库 URL 更改为实际 URL。您还需要添加 $OTHER_GIT_BRANCH 作为要推送到的分支的参数。您可能需要更改相对路径(我在没有实际构建管道的情况下尽了最大努力)
推荐阅读
- javascript - 有人可以在以下示例中解释 ...spread 运算符的使用吗?
- r - 估计 R 中的向量中有多少个连续的真实元素
- c# - 将包含布尔、ushort、hex、int、float 的字符串解析为 ushort 的优雅方法?
- excel - 将2个excel文件中的数据合并到一个主文件中
- php - 将数组循环分成两部分
- html - 如何在加载时获取角度 6 中下拉列表的选定值(不是 onchange)
- botframework - 如何在网络聊天中显示机器人图标?
- c# - 如何在数组中找到N个最高的数字?
- raspberry-pi3 - 树莓派 2 和 3 上的二进制文件相同吗?
- python - 将文本文件解析为表格数据以进行处理