【问题标题】:Jenkins multibranch pipeline with multiple repo to build for the changed repo onlyJenkins 多分支管道,具有多个 repo,仅用于为更改后的 repo 构建
【发布时间】:2018-12-24 05:17:32
【问题描述】:

所以,我确实有一个复杂的存储库结构(至少我认为是这样),我需要在那里实现 CI-CD。我所有的 webhook 和设置都已完成,现在我被部署部分困住了。

所以我有 4 个不同的存储库,它们最终为整个生产构建了一个 tar 文件。

Repo1: https://gitlab.example.com/repo1
Repo2: https://gitlab.example.com/repo2
Repo3: https://gitlab.example.com/repo3
Repo4: https://gitlab.example.com/repo4

我在每个 repo 的根目录下都有 JenkinsFile(每个项目下都有相同的 Jenkinsfile)。

这是我的 git checkout 管道脚本的片段:

    properties ([
    [$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab'],
    pipelineTriggers([[
        $class: "GitLabPushTrigger",
        triggerOnPush: true,
        triggerOnMergeRequest: true,
        // triggerOpenMergeRequestOnPush:"both",
        triggerOnNoteRequest: true,
        noteRegex: "CI BUILD THIS AGAIN",
        // skipWorkInProgressMergeRequest: true,
        // ciSkip:false ,
        //setBuildDescription: true,
        addNoteOnMergeRequest:true,
        addCiMessage: true,
        addVoteOnMergeRequest: true,
        acceptMergeRequestOnSuccess: true,
        //branchFilterType: "NameBasedFilter",
        //targetBranchRegex: "feature-decc-changes",
        //includeBranchesSpec: "feature-decc-changes,master,stage,prod",
        excludeBranchesSpec: "dev"
    ]])
])

stage('CHECKOUT: Checkout the Repos '){
        steps {
            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/node']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo1.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/angular']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo2.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/common']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo3.git']]])

            checkout([$class: 'GitSCM', branches: [[name: "**"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '$BUILD_NUMBER/extra']],
                        submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${GIT_SSH_KEY_ID}",
                        url: 'git@gitlab.example.com:repo4.git']]])
        }

}

对于任何分支中的任何更改,它正在创建构建并从 repo 中随机检查任何分支并创建构建。

但我希望 jenkins 检查刚刚更改的分支。例如。如果我在 repo2 下将更改推送到“feature-abc”分支,它应该从 repo2 和所有其他 repo 中签出分支“feature-abc”。此外,如果 feature-abc 分支不存在,请从自定义 repo(例如默认 master)中签出。

如果我不清楚,请告诉我。我会尽力提供更多信息。

【问题讨论】:

    标签: git jenkins github continuous-integration jenkins-pipeline


    【解决方案1】:

    根据我的理解...在 repo 中的一个分支上推送,您将构建所有 所有其他回购和这个回购中的相同分支

    试试env.BRANCH_NAME,它会给你正在签出的分支(当推送发生时)......使用它你可以签出其他仓库......

    检查其他分支时,检查分支是否存在git ls-remote --heads git@github.com:user/repo.git existingbranch,如果存在,它将返回一个 hashCode,如果不从您的自定义 repo 中获取它

    希望对你有帮助:)

    【讨论】:

    • 这似乎是个好主意...但是当我尝试运行 git 命令时出现权限被拒绝错误...任何解决方案?
    • def branch_exists_in_angular = sh(script: 'git ls-remote --heads $ANGULAR_REPO $EXTRA_BRANCH} | wc -l', returnStdout: true) 给出权限被拒绝错误
    • 您必须传递用户名/密码或密钥
    • 最后一个问题.. 任何指针或示例已完成?我找不到它
    • stackoverflow.com/questions/8223906/… 检查分支是否存在
    猜你喜欢
    • 2021-03-22
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多