Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Latest commit

 

History

History

build-github-pull-request

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Build GitHub Pull Request in Hyper.sh Container with Jenkins

1.Summary

1.1 Prerequisite

1.2 GitHub Repo

1.3 Workflow

  • Commit the change to cloned repo.
  • Create PR via cloned repo.
  • Jenkins job(for Main Repo) will be triggered via GitHub Webhook.
  • Jenkins build PR automatically.
  • Jenkins will set build status of PR on GitHub.

2.Run Jenkins Server

//start Jenkins Server container
$ hyper run --name jenkins-server \
  -d -P \
  -v jenkins-data:/var/jenkins_home \
  hyperhq/hykins

//add public ip to container
$ FIP=$(hyper fip allocate 1)
$ hyper fip attach $FIP jenkins-server

3. Access Jenkins Web UI

Open http://$FIP:8080 in Web Browser.

The default admin account is admin/nimda.

If you want to customize the admin account, please see this

4.Config Jenkins

4.1 Config hyper-slaves-plugin

(Menu) Manage Jenkins ->  Configure System -> Hyper.sh Config
  -> Input "Hyper.sh Access Key" and "Hyper.sh Secret Key"
  -> Click "Save Credentials" button
  -> Click "Test connection to Hyper.sh" button

image alt text

4.2 Config GitHub Pull Request Builder Plugin

4.2.1 Create API Token

(Menu) Manage Jenkins ->  Configure System -> GitHub Pull Request Builder
  -> Click "Create API Token" button
  -> Input "Username temp" and "Password temp"  (GitHub account to create token)
  -> Click "Create Token" button

-> Click global "Save" button of "Config System"

image alt text

Goto https://github.com/settings/tokens.

A personal access token named Jenkins GitHub Pull Request Builder will be created in GitHub.

This token is used to create GitHub Webhook and update status of each PR on GitHub

image alt text

4.2.2 Config other options

(Menu) Manage Jenkins ->  Configure System -> GitHub Pull Request Builder
  -> Shared secret  : Secret of GitHub Webhook.
  -> Credentials    : Select GitHub auto generated token credentials.

image alt text

5.Config Job

Create a job of Freestyle project.

5.1 General

image alt text

5.2 Source Code Management

image alt text

Repository URL: https://github.com/Jimmy-Xu/example

Refspec+refs/pull/*:refs/remotes/origin/pr/*

Branch Specifer${sha1}

[Optional] Trigger Build PR manually

  1. add a "String Parameter" like this:
    image alt text
  1. disable "GitHub Pull Request Builder" under "Build Triggers"
    image alt text
  1. remove "Set build status to "pending" on GitHub commit" in "Build"
    image alt text
  1. remove "Set build status on GitHub commit [deprecated]" in "Post-build Actions"
    image alt text

5.3 Build Triggers

image alt text

  • GitHub API credentials
    • Be Configured in Jenkins global config
  • Use GitHub Webhook for build triggering
    • Trigger Build via GitHub Webhook
    • This option will disable cron-based polling
  • Whitelist
    • People who submit pull requests can have their pull requests automatically build.
    • If you don’t add people to the white list you will need to tell Jenkins to build the PR.

5.4 Build Step

Add two build step:

  • Set build status to pending on GitHub commit
  • Execute shell

image alt text

Build script

  set +x

  START_TIME=$(date "+%F %T %z")
  START_TS=$(date "+%s")

  #################################
  echo "------------------------------------------------" > result.txt
  cat /proc/cpuinfo | grep -E '(processor|model name|cpu cores)' >> result.txt
  echo "-------------------------------------" >> result.txt
  cat /proc/meminfo | grep ^Mem >> result.txt
  echo "-------------------------------------" >> result.txt
  cat result.txt
  #################################


  echo "----- prepare -----"
  export GOPATH=$WORKSPACE/../gopath
  mkdir -p $GOPATH $GOPATH/src/github.com/golang
  if [ ! -L $GOPATH/src/github.com/golang/example ];then
    ln -s $WORKSPACE $GOPATH/src/github.com/golang/example
  fi

  echo "----- start unit test -----"
  cd $WORKSPACE/stringutil
  go test -v

  echo "----- start compile -----"
  cd $WORKSPACE/hello
  go build

  RLT=$(./hello)
  if [ "$RLT" == "Hello, Go examples!" ];then #centos use bash
    echo OK
  else
    echo Failed
    exit 1
  fi

  echo "----- compress -----"
  tar czvf hello.tar.gz hello

  #################################
  END_TIME=$(date "+%F %T %z")
  END_TS=$(date "+%s")
  echo "--------------------------------"
  echo "Step Start Time:${START_TIME}"
  echo "Step End Time:${END_TIME}"
  echo "Step duration: $((END_TS-START_TS))"

5.5 Post-build Actions

image alt text

5.6 Save Job

After the above Jenkins Job was saved, a GitHub Webhook will be created automatically.

(https://github.com/Jimmy-Xu/example/settings/hooks)

image alt text

6. Trigger Build by GitHub Webhook

To build PR by GitHub Webhook:

  1. Commit the change to cloned repo https://github.com/cocobjcn/example .
  2. Create Pull Request in cloned repo. Then, Jenkins Job build will be triggered via GitHub Webhook automatically.

Note: For test purpose, you just need to create one Pull Request. You can Close and Re-Open the PR, then a new job build will be triggered again.

7. Build Result

7.1 Jenkins Log

Build History

image alt text

Build Status

image alt text

Console Output

image alt text

7.2 GitHub status

Jenkins will set build status of PR on GitHub.

1) Set build status to "pending" on GitHub commit

image alt text

2) Set build status on GitHub commit

Click the Details link will jump to Jenkins build history.

image alt text

3) Build status on each commit

image alt text

8 Problem

8.1 Doesn’t support build in parallel

image alt text

FAQ

1. Launch container timeout

This will occur when use EIP as Jenkins URL.
To solve this issue, Please use internal ip(default) as "Jenkins URL".
image alt text

And use EIP in "Jenkins URL override" of "GitHub Pull Request Builder" image alt text

2. GitHub show internal ip of Jenkins Master

The Commit Status URL will be JENKINS URL by default.

For Hykins, the JENKINS URL will be private ip by default.

To customize the url in github, please set the Commit Status URL in job configuration:

Build Triggers -> GitHub Pull Request Builder -> Trigger Setup -> Update commit status during build
  -> Commit Status Context
  -> Commit Status URL

Commit Status URL example:

//x.x.x.x could be public ip or a domain.
http://x.x.x.x:8080/job/${JOB_NAME}/${BUILD_NUMBER}

Note: Not the following config item

Build Environment -> Set GitHub commit status with custom context and message (Must configure upstream job using GHPRB trigger)