jenkins 在Bitbucket中生成git合并提交引用

swvgeqrz  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(174)

在我们的一个Jenkins作业中,签入到拉取请求中,我们依赖于Bitbucket的实现细节,即拉取请求合并提交,可以使用pull-requests/*/merge访问它。
Bitbucket已删除对3路合并的支持,因此我们无法运行:

if (isPR(config.BRANCH)) {
            echo 'In PR type commit'
            PR_ID=parsePRId(config.BRANCH)
            TARGET_COMMIT="pull-requests/$PR_ID/merge"
            FROM_COMMIT="pull-requests/$PR_ID/from"
        } else {
            echo 'Non PR type commit'
            TARGET_COMMIT=config.BRANCH
            FROM_COMMIT=config.BRANCH
        }

        // Fetch hash of from commit for marking build status
        sh "git fetch origin $FROM_COMMIT"
        result.branch = config.BRANCH
        result.fromCommitId = sh(returnStdout: true, script: "git rev-parse FETCH_HEAD").trim()
        result.fromCommitIdShort = sh(returnStdout: true, script: "git rev-parse --short FETCH_HEAD").trim()

        try {
            // Checkout target commit
            sh """
                git fetch origin $TARGET_COMMIT && \
                git checkout -f FETCH_HEAD
            """
            result.targetCommitId = sh(returnStdout: true, script: "git rev-parse FETCH_HEAD").trim();
            result.targetCommitIdShort = sh(returnStdout: true, script: "git rev-parse --short FETCH_HEAD").trim();
        } catch (Exception err) {
            result.targetCommitId = null
            result.targetCommitIdShort = null
        }

最终目标是 checkout pull请求,以便在monorepo中构建/部署微服务。

iih3973s

iih3973s1#

我通过创建一个重 checkout 来解决这个问题。这仍然需要更好地测试。

if (isPR(config.BRANCH)) {
                echo 'In PR type commit'
                PR_ID=parsePRId(config.BRANCH)
                FROM_COMMIT="pull-requests/$PR_ID/from"
                /* this is needed because bitbucket dropped support for PR merge refs 
                    more info in https://jira.dna.fi/browse/CXA-1509
                */
                sh """
                    git checkout origin/master && \
                    git checkout -b interim && \
                    git merge "origin/${env.CHANGE_BRANCH}"
                """
                def interimCommit = sh(returnStdout: true, script: "git rev-parse HEAD").trim();

                TARGET_COMMIT = interimCommit
            }

相关问题