'How to deal with the error "Couldn't find any revision to build" when build Jenkins server with Pipeline?

I'm trying to build a Jenkins server to run unit tests for the pull request of the repository in GitHub. The following is my pipeline code:

pipeline {
    agent any
    
    stages {
        // Clone code from github in local workspace
        stage('Build') {
            steps {
                // Set the label of pr in GitHub as started
                gitHubPRStatus githubPRMessage('${GITHUB_PR_COND_REF} started')
                // Pull code from the specific pr in GitHub
                // TODO: modify
                git branch: "${sha1}", credentialsId: 'xxxxxxxxxxxxxxxxxxx', url: 'https://github.com/DavdGao/JenkinsTest.git'
            }
        }
        //
        stage('Test') {
            steps {
                // Run unit tests by docker in Jenkins Serverst
                sh 'pwd'
            }
        }
    }
}

The Jenkins receive the pull request message from GitHub successfully, and report the following error

14:53:14  GitHub pull request #10 of commit 35f836cd202cf761f5ef3d85f6efb740390d6745, no merge conflicts.
14:53:14  Setting status of 35f836cd202cf761f5ef3d85f6efb740390d6745 to PENDING with url http://x.xxx.xx.xx:8080/job/JenkinsTest_Pipeline/18/ and message: 'Build started for merge commit.'
14:53:15  [Pipeline] Start of Pipeline
14:53:15  [Pipeline] node
14:53:15  Running on Jenkins in /var/lib/jenkins/workspace/JenkinsTest_Pipeline
14:53:15  [Pipeline] {
14:53:15  [Pipeline] stage
14:53:15  [Pipeline] { (Build)
14:53:15  [Pipeline] gitHubPRStatus
14:53:15  [Pipeline] git
14:53:15  The recommended git tool is: NONE
14:53:15  using credential xxxxxxxxxxxxxxxxxxxxx
14:53:15   > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/JenkinsTest_Pipeline/.git # timeout=10
14:53:15  Fetching changes from the remote Git repository
14:53:15   > git config remote.origin.url https://github.com/DavdGao/JenkinsTest.git # timeout=10
14:53:15  Fetching upstream changes from https://github.com/DavdGao/JenkinsTest.git
14:53:15   > git --version # timeout=10
14:53:15   > git --version # 'git version 2.17.1'
14:53:15  using GIT_ASKPASS to set credentials 
14:53:15   > git fetch --tags --progress -- https://github.com/DavdGao/JenkinsTest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
14:53:16   > git rev-parse refs/remotes/origin/origin/pr/10/merge^{commit} # timeout=10
14:53:16   > git rev-parse origin/origin/pr/10/merge^{commit} # timeout=10
14:53:16  [Pipeline] }
14:53:16  [Pipeline] // stage
14:53:16  [Pipeline] stage
14:53:16  [Pipeline] { (Test)
14:53:16  Stage "Test" skipped due to earlier failure(s)
14:53:16  [Pipeline] }
14:53:16  [Pipeline] // stage
14:53:16  [Pipeline] }
14:53:16  [Pipeline] // node
14:53:16  [Pipeline] End of Pipeline
14:53:16  ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
14:53:16  Setting status of 35f836cd202cf761f5ef3d85f6efb740390d6745 to FAILURE with url http://x.xxx.xx.xx:8080/job/JenkinsTest_Pipeline/18/ and message: 'Build finished. '
14:53:16  Finished: FAILURE

And I find the workspace /var/lib/jenkins/workspace/JenkinsTest_Pipeline is empty. It seems like Jenkins doesn't pull the code from Github actually.

So how to deal with the error "ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job."?

Note

When I modify git branch: "${sha1}" ... into git branch: "main" ..., the pipeline run successfully. However, when I check the git status in workspace, the branch is main rather than the commit ${sha1} of the pull request in GitHub.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source