In Exercise 2.1 we are going to start by forking an existing Github project that has multiple branches and Jenkinsfiles in each branch.
But first let's create a Github organization to fork the repo into:
- On Github navigate to Organizations: https://github.com/settings/organizations (after logging in)
- Click on New Organization
- Fill in the Organization Name, Billing Email, and click on Create Organization
Now lets fork the repo into the new organization:
- Navigate to the rest server application we are going to work with: https://github.com/PipelineHandsOn/sample-rest-server
- Click on Fork
- Select the Organization you want to fork into
In this exercise we are going to create a Github Organization project from our newly forked repository.
Note: You need to have a Github personal access token (Github-Personal-Access-Token.md) before proceeding.
First let's add your Github credentials to the Jenkins' Credentials manager:
- Navigate back to your personal folder in Jenkins
- Click on Credentials
- Click on [YourFolderName] under Stores Scoped to [YourFolderName]
- Click on Global Credentials (Unrestricted)
- Click on Add Credentials
- Fill out the form (Username with password)
- Username: The Github organization name
- Password: Your Github personal access token
- ID: Create an ID for your credentials (something like yourorg-id)
- Description: Can be left blank if you want
- Click on OK
Now let's create the Github Organization project:
- Click on New Item
- Enter your organization name as the Item Name
- Select GitHub Organization
- Click Ok
- Select the credentials you created above from the Credentials drop down
- Select All from the Strategy drop down under Discover Branches
- Click Save
Once you click on save Jenkins will search your organization for any projects with Jenkinsfiles in them, import those projects as Multibranch projects, and begin building each branch with a Jenkinsfile in it.
When the project was created it also should have created webhooks in Github. Verify that the webhooks were created in Github by checking Webhooks within your organization's Github Settings.
In this exercise we are going to edit the Jenkinsfile file in the development branch of our project to add a branch specific stage.
- Within your sample-rest-server project select the development branch from the Branch drop down menu
- Click on the Jenkinsfile in the file list
- Click on the Edit this file button (pencil)
- Insert the following stage after the existing build stage:
stage('Development Tests') {
when {
branch 'development'
}
steps {
echo "Run the development tests!"
}
}
- Fill out the commit information, select 'Commit directly to the development branch.', and click on Commit Changes
Notice how after you commit your changes the Github web hooks trigger a build of the development branch in Jenkins.
In this exercise we are going to edit the development branch's Jenkinsfile again but make our commit against a feature branch and user a pull request to merge the edits into our development branch.
- Click on the Edit this file button (pencil)
- Insert the following stage after the existing build stage:
stage('Masters Tests') {
when {
branch 'master'
}
steps {
echo "Run the master tests!"
}
}
- Fill out the commit information, select 'Create a new branch for this commit and start a pull request.' and click on Propose file change
- Flip back to your Jenkins job and notice that the new feature branch appears in your projects
- Return back to the Github Open a pull request page
- Click on the Create pull request button
- Go to your Jenkins job and notice that that the PR has been added to the Pull Requests tab
- In Github click on Merge pull request and then Confirm to close the PR and merge the results into the development branch
- Optionally you can also delete the feature branch you created
Finally, we should merge our work into our master branch to verify that our changes work there:
- Return back to your repository's main page where you will be on the master branch by default
- Click on New pull request
- Select your base fork (not the project we forked from)
- Compare master to development
- Click View pull request
- Click Merge pull request
- Click Confirm merge
Notice how after you merge your changes into master the Github web hooks trigger a build of the master branch in Jenkins.