In this example we are going to use SSH to integrate Jenkins with GitHub. As an example we will create a Pipeline project and run a dummy Jenkinsfile that is stored in the GitHub repository.


Prerequisites


First of all you need to make sure that your Jenkins server can talk to GitHub over SSH. Follow steps defined in Connecting to GitHub with SSH page.


Repository


This is our dummy GitHub project and it is located at https://github.com/inanzzz/game.


.
├── ci
│   └── pipeline
│   └── branch
│   ├── develop
│   │   └── Jenkinsfile
│   ├── feature
│   │   └── Jenkinsfile
│   └── master
│   └── Jenkinsfile
├── composer.json
├── composer.lock
├── src
│   └── Football.php
├── tests
│   └── FootballTest.php
└── vendor

Jenkins


Login to Jenkins UI and do the following.


Credentials


  1. Click "Credentials" link on the left.

  2. Click "Jenkins/Global" and then "Add credentials" link.

  3. Select "SSH Username and private key" from "Kind" option.

  4. Set a username (e.g. GitHub-inanzzz) for "Username" option.

  5. Tick "Enter directly" radio button and paste content of the ~/.ssh/id_rsa.pub file into the textarea.

  6. Save and exit.

Pipeline


In Jenkins's main page, click "New Item" link to create a new item called "Test" and then select "Pipeline" option in the following page.


  1. Select "Pipeline script from SCM" option under "Pipeline" section.

  2. Select "Git" as SCM.

  3. Use https://github.com/inanzzz/game for "Repository URL".

  4. Select GitHub-inanzzz from "Credentials".

  5. Optionaly change */master to */develop.

  6. Add ci/pipeline/branch/develop/Jenkinsfile to "Scripth path".

  7. Save and exit.

If you click "Build Now" link, it should successfuly run the first build and the console output should look like below.


Started by user admin
Obtained ci/pipeline/branch/develop/Jenkinsfile from git https://github.com/inanzzz/game
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Game
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 7a41495c
Cloning the remote Git repository
Cloning repository https://github.com/inanzzz/game
> git init /var/lib/jenkins/workspace/Game # timeout=10
Fetching upstream changes from https://github.com/inanzzz/game
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress https://github.com/inanzzz/game +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/inanzzz/game # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/inanzzz/game # timeout=10
Fetching upstream changes from https://github.com/inanzzz/game
using GIT_SSH to set credentials
> git fetch --tags --progress https://github.com/inanzzz/game +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/develop^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/develop^{commit} # timeout=10
Checking out Revision f63bf36b (refs/remotes/origin/develop)
> git config core.sparsecheckout # timeout=10
> git checkout -f f63bf36b
Commit message: "Very first commit"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 1 min 0 sec
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Code Pull)
[Pipeline] echo
Pulling...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Testing...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Image Build)
[Pipeline] echo
Building...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Image Push)
[Pipeline] echo
Pushing...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Deploying...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS