Setup Jenkins Project Integrated with Github using Jenkins Declarative Pipeline
The following tutorial is about how to set up Jenkins to build automatically on a GitHub push event.
Prerequisite
- Jenkins Plugin: GitHub Integration Plugin
Setup for Jenkins
- Create a multibranch project
- Integrate with you Github Project
Branch Source > Add source > GitHub
- Tell Jenkins about your repository at GitHub Sources
API endpoint: GitHub
Credential: depending on whether you are accessing an private repository
Owner: your GitHub account or organization
Repository: wait a couple seconds and Jenkins will fetch all of the repositories with respect to the account you entered, choose the one you’d like to build
- GitHub Enterprise (optional)
Tell Jenkins where your GitHub Enterprise is before having your project set
Manage Jenkins > Configure System > GitHub Enterprise Servers
API endpoint: https://{YOUR_GITHUB_ENTERPRISE}/api/v3
— — — — —
Go back your project setting of GitHub source with following information
API endpoint: Select your GitHub Enterprise
Credentials: credentials for accessing your GitHub Enterprise
Owner: your organization or account
Repository: wait a couple seconds and Jenkins will fetch all of the repositories with respect to the account you entered, choose the one you’d like to build - Fetch tags (optional)
Jenkins will not checkout tags by default. If you need the tags along with the build (ex. tagging the build or image using git describe), you have to it manually by
Branch Sources > GitHub > Behaviours > Add > Advanced clone behavious
Check Fetch tags
- Setup Jenkinsfile
Jenkins supposes that you will put your Jenkinsfile at the root of your project by default, remember to overwrite the Script Path topath/to/Jenskinsfile
if you’d like to put it somewhere else
Now it’s all set! you shall see the following message at Scan Repository Log
Examining account/repository
Checking branches...
Getting remote branches...
Checking branch master
Getting remote pull requests...
‘Jenkinsfile’ found
Met criteria
Scheduled build for branch: master
Branch master will now be built according to your Jenkinsfile
Setup for GitHub
The final step for the automation pipeline is to tell GitHub that you have to notify Jenkins whenever there is an update (ex. a push event) and this is where the webhook is introduced.
{Repository} > Settings > Hooks and services > Add webhook
Payload URL: http://{JENKINS_UR}/github-webhook/
Content type: Application/json
Check Send me everything.
Your code will be fetched, tested, built and deployed (depends) once it is pushed to GitHub automatically. Life should be wasted on wonderful things, everything shall be automated. Happy coding 🐤