Above answers are correct but i am addressing to them who are newbie here for their simplicity
especially for setting build trigger for pipeline:
Consider you have two Github branches: 1.master, 2.dev,
and Jenkinsfile (where pipeline script is written) and other files are available on each branch
Configure new Pipeline project (for dev branch)
##1.Code integration with git-plugin and cron based approach
Prerequisite git plugin should be installed and configure it with your name and email
- General section.Check checkbox - 'This project is parameterized’ and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
- Build triggers section" Check checkbox - 'Poll SCM' and schedule as per need for checking commits e.g. '*/1 * * * *' to check every minute
- Pipeline definition section.Select - Pipeline script from SCM—> select git—> addRepository URL—>add git credentials—> choose advanced—> add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev'(dev is github branch )—> Branches to build - ${SBRANCH} (Parameter name from ref 1st point)—> Script Path—> Jenkinsfile —> Uncheck Lightweightcheckout
- Apply—> save
##2.Code integration: github-plugin and webhook approach
Prerequisite Github plugin should be installed and Github server should be configured, connection should be tested if not consider following configuration
Configure Github plugin with account on Jenkins
GitHub section Add Github server if not present API URL: https://api.github.com Credentials: Add secret text (Click add button: select type secret text) with value Personal Access Token (Generate it from your Github accounts—> settings—> developer setting—> personal access token—> add token—> check scopes—> copy the token) Test Connection—> Check whether it is connected to your Github account or not Check checkbox with Manage Hooks In advance sub-section just select previous credential for 'shared secret'
Add webhook if not added to your repository by
- Go to Github Repository setting —> add webhook—> add URL
http://Public_IP:Jenkins_PORT/github-webhook/
- Or if you don't have Public_IP use ngrok. Install, authenticate, get public IP from command ./ngrok http 80(use your jenkins_port) then add webhook —> add URL http://Ngrok_IP/github-webhook/
- Test it by delivering payload from webhook page and check whether you get 200 status or not.
If you have Github Pull requests plugin configure it also with published Jenkins URL.
- General section.Check checkbox - 'Github project' add project URL
-(github link ending with '.git/')
- General section.Check checkbox - 'This project is parameterized' and add Name-SBRANCH Default Value-'refs/remotes/origin/dev'
- Build triggers.section.Check checkbox - 'GitHub hook trigger for GITScm polling'
- Pipeline def'n section: Select - Pipeline script from SCM—> select git—> addRepository URL—> add git credentials—>choose advanced —>add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev' (dev is github branch ) —> Branches to build - ${SBRANCH} (Parameter name from ref 1.st point)—> Script Path—> Jenkinsfile—> Uncheck Lightweightcheckout
- Apply—> save