How to trigger the Jenkins Job by a GitHub push

How to trigger the Jenkins Job by a GitHub push

Jenkins

  1. Create Jenkins job (freestyle project)
  2. In the section “Source-Code-Management” select the “Git” option. If you don’t see it, check your GitHub Plugin installation. Enter the repository clone-URL in the appearing text field. When you are using a public GitHub repository, you don’t have to specify any credentials otherwise enter them.
  3. Finally, check the “GitHub hook trigger for GITScm polling” option in the “Build Triggers” section. Hit the “Save” button and you’re done. You have successfully set-up the Jenkins project. In the next step, we will trigger it by pushing to the GitHub repository.

GitHub

  1. Go to your GitHub project settings and click on “Integrations & Services”. Press the “Add service”button and choose “Jenkins (GitHub plugin)” from the service list.
  2. Enter your webhook URL in the “Jenkins hook url” field and press “Add service”. http://<JENKINS_HOST>/github-webhook/
  3. To verify that the hook is working, click on the created service and press the “Test service” button in the upper right corner. If everything is correctly configured, the Jenkins service has got a green checkmark icon in the service overview and the build process has started.

How to send the jenkins job status to github repo

Github

  1. Go to GitHub, account "Settings->Developer Settings", "Personal access tokens", click on "Generate new token".
  2. Check "repo:status", "repo", "user", "gist", "public_repo"
  3. Generate the token, copy it.
  4. Make sure the GitHub user you're going to use is a repository collaborator (for private repos) or is a member of a team with push and pull access (for organization repos) to the repositories you want to build.

Jenkins (case #1)

  1. "Manage Jenkins → Configure System"
  2. Under "GitHub Web Hook" select "Let Jenkins auto-manage hook URLs", then specify your GitHub username and the OAuth token you got in step 3.
  3. Verify that it works with the "Test Credential" button. "Save" the settings.
  4. Find the Jenkins job and add "Set build status on GitHub commit" to the post-build steps
  5. That's it. Now do a test build and go to GitHub repository to see if it worked. Click on Branches in the main repository page to see build statuses.

Jenkins (case #2)

  1. Install the Hudson Post Task Plugin
  2. Add a Post Task Plugin that always put success. Just specify the line in the field "Log" what you are waiting for in the console job logs to send the success status
  3. curl -XPOST -H "Authorization: token GITHUB_TOKEN" https://api.github.com/repos/ORG_NAME/REPO_NAME/statuses/$(git rev-parse HEAD) -d "{
      \"state\": \"success\",
      \"target_url\": \"${BUILD_URL}\",
      \"description\": \"The build has succeeded!\"
    }"
  4. Add a Post Task Plugin that will put failure if "marked build as failure". Just specify the line in the field "Log" what you are waiting for in the console job logs to send the failure status
  5. curl -XPOST -H "Authorization: token GITHUB_TOKEN" https://api.github.com/repos/ORG_NAME/REPO_NAME/statuses/$(git rev-parse HEAD) -d "{
      \"state\": \"failure\",
      \"target_url\": \"${BUILD_URL}\",
      \"description\": \"The build has failed!\"
    }"