如何将文件从一个仓库复制到另一个仓库 [Jenkins]

How to copy a file from one repo to another repo [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 仓库:git@github.com/repo2.git

知道怎么做吗?

提前致谢

解决方案

您应该能够执行以下操作

  1. 在您的工作区中创建一个新目录
  2. 签出您希望推送到的存储库(我称之为 repo2.git)
  3. 将文件从工作区复制到您创建的新目录(克隆 repo2 的目录)
  4. 运行 bat
  5. 中适当的 git 命令

Without knowing the name of the file, repository, or repository branch I can't give you exact code, but it should look something like the following

示例代码

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
                '''
           }
        }
    }
}

我稍微重构了您的代码,以便它在自己阶段的不同目录中检出源存储库和目标存储库。我认为这是一种更简洁的文件组织方式。

Note you obviously need to replace with the file you wish to add to the other repository. You also need to change the repository url in the second checkout block to the actual URL. You will also need to add $OTHER_GIT_BRANCH as the parameter for the branch you want to push to. You may need to change the relative paths ( I tried my best without actually building a pipeline )