首页 > 解决方案 > 如何将文件从一个仓库复制到另一个仓库 [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

任何想法如何做到这一点?

提前致谢

标签: gitjenkinsgradlegroovyjenkins-groovy

解决方案


解决方案

您应该能够执行以下操作

  1. 在您的工作区中创建一个新目录
  2. 签出您希望推送到的存储库(我称之为 repo2.git )
  3. 将文件从工作区复制到您创建的新目录(克隆 repo2 的目录)
  4. 在 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 作为要推送到的分支的参数。您可能需要更改相对路径(我在没有实际构建管道的情况下尽了最大努力)


推荐阅读