Is it possible to trigger Jenkins from one specific branch only?

后端 未结 4 374
滥情空心
滥情空心 2020-12-16 23:14

My situation is the following: I have three branches in a repo: master, dev and staging. And I have one job for each one of these branches, configured in \'Branches to build

相关标签:
4条回答
  • 2020-12-16 23:58

    Yes!! You can trigger the jenkins build whenever there is a commit or merge into a specific branch of the git repo.

    STEPS:

    1. Configure the webhook for your jenkins instance in the Webhook section of the github Repository, the Payload URl will look similar to,

      http://jenkinsinstance:8080/github-webhook/

    2. In the Jenkins Job configuration just enable, GitHub hook trigger for GITScm polling

    3. Then in the SCM section add the below configuration available in the image, assuming the branch you want to build being the Hotfix branch. The Below image defines the exact configuration of SCM section.

    SCM Configuartion image

    1. Once done, try to commit code changes to the Hotfix branch, which in return should trigger the jenkins job.
    0 讨论(0)
  • 2020-12-17 00:03

    To have different Jenkis projects for different branches I did the following:

    • Install Bitbucket Plugin at your Jenkins
    • Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:

    https://YOUR.JENKINS.SERVER:PORT/bitbucket-hook

    • Configure your Jenkins project as follows:
      • under build trigger enable Build when a change is pushed to BitBucket
      • under Source Code Management select GIT; enter your credentials and define Branches to build (like **feature/*) <- this is where you define different branches for each project

    By this way I have three build projects, one for all features, one for develop and one for release branch.

    And best of it, you don't have to ad new hooks for new Jenkins projects.

    0 讨论(0)
  • 2020-12-17 00:07

    I just discovered that Bitbucket does not allow to choose a specific hook when pushing to branches. It just calls all the hooks, then it starts all Jenkins' jobs.

    My solution was to create a specific file on my machine, on which Jenkins is installed and set a Bitbucket hook to this file. (e.g. http://{jenkins url}:{apache port}/check.php)

    Note that this apache port is not the same of Jenkins', but Apache's. In my case, Jenkins was running at 8080 and Apache at 7777. It did this to run php script, but not in Jenkins' directory.

    Since Bitbucket hook sends a json file, I was able to verify in check.php which branch has been pushed on. Reference: POST hook management

    After the verification using a simple 'if', I just called the right url to start the right job with exec_curl, like:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, http://{jenkins url}:{jenkins port}/job/{job name}/build?token={job token});
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    

    And voilà.

    0 讨论(0)
  • 2020-12-17 00:14

    Did you set up polling?

    https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository

    ... This will scan all the jobs that's configured to check out the specified URL, the optional branches, and if they are also configured with polling, it'll immediately trigger the polling (and if that finds a change worth a build, a build will be triggered in turn.) We require the polling configuration on the job so that we only trigger jobs that are supposed to be kicked from changes in the source tree.

    0 讨论(0)
提交回复
热议问题