Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

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

...

  • one more job on Jenkins

  • GitHub repo linked

Create token on GitHub

  1. Log into you GitHub account, that must be an account with admin privileges to the repo

  2. Go to “Settings” of your GitHub account, then go to “Developer settings->Personal access tokens“

  3. Click “Generate new token“ and Select the following scopes:

    1. repo

      1. repo:status

      2. repos_deployment

      3. public_repo

    2. admin:repo_hook

      1. write:repo_hook

      2. read:repo_hook

  4. Give a name of the token in the “Note“ field and Click “Generate token“

  5. 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

  1.  Log into your GitHub account, that must be an account with admin privileges to the repo

  2. In the “Settings” tab of the repo, click on “Webhooks”

  3. Click “Add webhook”, under “Webhooks / Manage webhook”

    1. Put <https://openwsn-builder.paris.inria.fr/github-webhook/> (“github-webhook“ is a magic string identifying the GitHub plugin) in the Payload URL

    2. Select “Let me select individual events“ and check the “Pull Request”, “Push” and “Repositories” as the event to trigger the build

    3. Click “Update webhook” to create the webhook

  4. You should see the payload URL is the “Webhooks” list, with a green check mark.

Create Job on Jenkins

  1. install plugin

    1. Log into builder.openwsn.org

    2. Go to Manage “Manage Jenkins → Manage PluginsPlugins”, under Available tab, search for ”GitHub pull request builder”Mark the checkbox and click Install without restart Branch Source”, mark the checkbox of the plugin

    3. search for ”SCM Filter Branch PR” and mark the checkbox of the plugin

    4. click “Install without restart” to install

    5. Go to builder.openwsn.org/restart and click Yes “Yes” to restart Jenkins

    Configure plugin

    1. Go to Manage Jenkins → Configure System, in GitHub Pull Request Builder setting, add the GitHub account into the credentials area. The GitHub account needs to have admin rights on the repository (pull/push is NOT enough).

    2. Click Test Credentials... button, verify you read Test basic connection to GitHub

    3. Click Connect to API button, verify you read “Connected to https://api.github.com…”

    4. Check Auto-manage webhooks checkbox

    5. in the “admin list“, add the GitHub account

    6. Leave the rest as default and click Save to save the settings

  2. create new job

  3.  Log into your GitHub account, that must be an account with admin privileges to the repo

  4. In the Settings tab of the repo, click on Webhooks

  5. Click Add webhook, under Webhooks / Manage webhook

    1. Put <https://openwsn-builder.paris.inria.fr/ghprbhook/> (“ghprbhook“ is a magic string identifying the GitHub PR build book plug) in the Payload URL

    2. Select Let me select individual events and check the Pull Request and Push as the event to trigger the build

    3. Click Update webhook to create the webhook

  6. You should see the payload URL is the Webhooks list, with a green check mark.
    1. Click New Item “New Item” to create a job, give a name of the job, select Freestyle project “GitHub Organization” and then click OK “OK” to get in the job configure page

    2. In the General “Projects” Tab, check GitHub project and put the repository url there, e.g. “https://github.com/changtengfei/dummy_test

    3. In the Source Code Management Tab, select Git, put the same URL and leave the credentials as “none”

    4. Click the Advanced… button and put “+refs/pull/:refs/remotes/origin/pr/”in theRefspec field. This tells the Jenkins job, when it runs, to retrieve the list of branches from the forked repo.
    5. In the branches to build section, enter “${sha1}” in the branch Specifier textfield. This tells Jenkins to merge in the branch specified in the PR.

    6. In the Build Triggers Tab

      1. check GitHub Pull Request Builder

      2. put the GitHub admin account in the Admin list field

      3. check the use GitHub hooks for build triggering

      4. check the Build every pull request automatically without asking (Dangerous!)

      5. leave the rest settings as default.

    7. In the Build Tab

      1. put the test script you want the job to do after getting the code from the Pull Request.

    8. In Post-build Actions, add any post actions you want after the build process is done.

    9. Click Save to save the settings.

Create webhook on GitHub

    1. in the “GitHub Organization“ section, Click “Add” next to “Credentials“

      1. Select the option with the job name

      2. In “Kind“ text box, select “Username with password“

      3. In “Username“ text box, write down the GitHub account name

      4. In “Password“ text box, write down the token created in section above

      5. Click “Add“ to return to the “configure“ page

      6. In the drop box of “Credentials“, select “[accoutname]/**********“

  1. 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

  2. In the “Behaviors“ part, click the “Add“ button at bottom to add the following configurations:

    1. Repositories

      1. “Filter by name (with wildcards)”, using “Include“/”Exclude” to filter the repository you want the job to hook

    2. Within repository

      1. “Discover branches“, select “Strategy“ as “Exclude branches that are also filed as PRs“

      2. “Discover pull requests from forks“, select “Strategy“ as “Merging the pull request with current target“, select “Trust“ as “Everyone“

      3. “Filter by name including PRs destined for this branch (with wildcards)“, type down “master“ in “ Branch Include” text box

  3. In the “Project Recongnizers“ part, write down the Script path to the “Jenkinsfile“, which contains the Pipeline script that the Job should run

  4. In “Scan organization triggers“, uncheck the “Periodically if not otherwise run“

  5. In “Child Scan Triggers“, uncheck the “Periodically if not otherwise run“

  6. Click “Apply“ then “Save” to complete the settings.

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.

Code Block
pipeline {
    agent {
        label 'windows && a102'
    }
    stages {
        stage('Test') {
            steps {
                bat 'pytest --junitxml=report.xml'
            }
        }
    }
    post {
        always {
            junit 'report.xml'
        }
    }
}

Demo

Related

...