Jenkins/GitHub Integration
This page explains how to integrate Jenkins builds into a GitHub workflow. In particular:
a Jenkins build gets triggered by any PR or any update to any PR or when any PR are merged
the outcome of the Jenkins build outcome is displayed on GitHub
a Jenkins build failure disables the GitHub merge button
Instructions
start:
an existing repo
a Jenkins machine
end:
one more job on Jenkins
GitHub repo linked
Create token on GitHub
Log into you GitHub account, that must be an account with admin privileges to the repo
Go to “Settings” of your GitHub account, then go to “Developer settings->Personal access tokens“
Click “Generate new token“ and Select the following scopes:
repo
repo:status
repos_deployment
public_repo
admin:repo_hook
write:repo_hook
read:repo_hook
Give a name of the token in the “Note“ field and Click “Generate token“
The token will show up under the “Personal access tokens“ page, click “copy” button to copy the token to somewhere temporal. When it’s set in the target application, remove delete the token from anywhere else.
Create webhook on GitHub
Log into your GitHub account, that must be an account with admin privileges to the repo
In the “Settings” tab of the repo, click on “Webhooks”
Click “Add webhook”, under “Webhooks / Manage webhook”
Put <https://openwsn-builder.paris.inria.fr/github-webhook/> (“github-webhook“ is a magic string identifying the GitHub plugin) in the Payload URL
Select “Let me select individual events“ and check the “Pull Request”, “Push” and “Repositories” as the event to trigger the build
Click “Update webhook” to create the webhook
You should see the payload URL is the “Webhooks” list, with a green check mark.
Create Job on Jenkins
install plugin
Log into builder.openwsn.org
Go to “Manage Jenkins → Manage Plugins”, under Available tab, search for ”GitHub Branch Source”, mark the checkbox of the plugin
search for ”SCM Filter Branch PR” and mark the checkbox of the plugin
click “Install without restart” to install
Go to builder.openwsn.org/restart and click “Yes” to restart Jenkins
create new job
Click “New Item” to create a job, give a name of the job, select “GitHub Organization” and then click “OK” to get in the job configure page
In the “Projects” Tab, in the “GitHub Organization“ section, Click “Add” next to “Credentials“
Select the option with the job name
In “Kind“ text box, select “Username with password“
In “Username“ text box, write down the GitHub account name
In “Password“ text box, write down the token created in Create-token-on-GitHub
Click “Add“ to return to the “configure“ page
In the drop box of “Credentials“, select “[accoutname]/**********“
In the “Owner“ text box, type down the GitHub Account/Organization name. This allows the Job to read the related branches/PR associated with the account/organization
In the “Behaviors“ part, click the “Add“ button at bottom to add the following configurations:
Repositories
“Filter by name (with wildcards)”, using “Include“/”Exclude” to filter the repository you want the job to hook
Within repository
“Discover branches“, select “Strategy“ as “Exclude branches that are also filed as PRs“
“Discover pull requests from forks“, select “Strategy“ as “Merging the pull request with current target“, select “Trust“ as “Everyone“
“Filter by name including PRs destined for this branch (with wildcards)“, type down “master“ in “ Branch Include” text box
In the “Project Recongnizers“ part, write down the Script path to the “Jenkinsfile“, which contains the Pipeline script that the Job should run
In “Scan organization triggers“, uncheck the “Periodically if not otherwise run“
In “Child Scan Triggers“, uncheck the “Periodically if not otherwise run“
Click “Apply“ then “Save” to complete the settings.Create-token-on-GitHub
Create Jenkinsfile in Repository
The Github Organization job uses Pipeline as the build action to run.
The Pipeline script lives in the repository with name “jenkinsfile”. Following is an example what the Pipeline script looks like.
pipeline {
agent {
label 'windows && a102'
}
stages {
stage('Test') {
steps {
bat 'pytest --junitxml=report.xml'
}
}
}
post {
always {
junit 'report.xml'
}
}
}
Demo
repo: https://github.com/changtengfei/demo_github_jenkins_repo
Jenkins job: https://openwsn-builder.paris.inria.fr/job/testdemo/
GitHub Status images:
When Jenkins build passes, GitHub shows
When Jenkins build fails, GitHub shows
Related
https://wiki.jenkins.io/display/JENKINS/GitHub+Branch+Source+Plugin
https://wiki.jenkins.io/display/JENKINS/SCM+Filter+Branch+PR+Plugin
https://crystalfree.atlassian.net/browse/ADM-2