You may want to store your OpenShift application’s code on GitHub. We can trigger Jenkins builds from GitHub when a new commit is made using post-receive hooks and the Jenkins GitHub Plugin.
Installing the plugin
- On the main page of Jenkins click “Manage Jenkins”.
- Visit the “Manage Plugins” section.
-
Click the “Advanced” tab, scroll to the bottom and click the “Check now” button. This will fetch the latest version information for all your plugins.
- Click the “Updates” tab, select all the updates and click to download and install them after a restart.
-
Click the “Available” tab and search for the plugin named “GitHub plugin”. Install this with the button at the bottom.
Adding GitHub credentials to Jenkins
This is a bit fiddly, but we need to create both a GitHub username/password credential item and a “secret text” credential item using a GitHub Personal Access Token.
Adding your username/password
- On the main Jenkins page click “Credentials” in the sidebar.
- On the main Credentials page click the child item “System” in the sidebar.
-
Click “Global credentials (unrestricted)” to access this credentials domain.
-
Click “Add Credentials in the sidebar to add a credentials item to this domain.
-
Choose “Username with password” as the “Kind”, leave the “Scope” as “Global” and supply your GitHub username and password.
- Press the “Save” button to make these username/password credentials accessible for use elsewhere in Jenkins.
Adding your Personal Access Token
-
Navigate to Credentials > System and this time click the “Add domain” link in the sidebar.
-
Add the domain name “api.github.com” and click the “OK” button to create this domain.
-
Navigate to Credentials > System and click this new “api.github.com” domain.
-
Click “Add Credentials” in the sidebar to add a credentials item to this domain.
-
Open GitHub in a new tab and generate a new Personal Access Token with the access level
repo
andadmin:repo_hook
. -
Back in Jenkins select “Secret text” for “Kind” in the dropdown, leave “Scope” as “Global” and paste your Personal Access Token in “Secret”. Press the “OK” button to make these credentials accessible elsewhere in Jenkins.
Creating the Webhook in GitHub
We want GitHub to notify your Jenkins instance whenever you push commits to the repo. We’ll use Webhooks for this.
In the “Webhooks & services” section of your GitHub repo’s settings, add a new Webhook with the Payload URL of https://jenkins-yoursitename.rhcloud.com/github-webhook/
and choose the “Content type” application/x-www-form-urlencoded
.
Configuring the plugin in Jenkins
- On the main Jenkins page click “Manage Jenkins”.
- On the Manage Jenkins page that appears click “Configure System”.
-
Scroll down to the “GitHub” section click the “Add GitHub Server” button then “GitHub Server” which appears in the dropdown
-
Choose the personal access token credentials you made earlier from the dropdown, click the “Test Connection” button and ensure Jenkins is able to communicate with your GitHub account.
- Finally click the “Save” button and your Jenkins instance will now be capable of pulling code from your repositories and deploying it to your OpenShift gears.
Configuring Jenkins to build from your repo
- Now your Jenkins instance has access to your GitHub account, navigate to your Jenkins build project’s configuration page.
-
Check the “GitHub project” checkbox and provide the project URL e.g.
https://github.com/yourusername/yoursitename/
-
In the “Source Code Management” section replace the OpenShift repo SSH URL with your GitHub repo URL e.g.
https://github.com/yourusername/yoursitename
and choose the username/password credentials you made earlier from the dropdown. - Choose
master
as the branch to build (as screenshot above) unless you have any specific requirements. - Finally, scroll down to the “Build Triggers” section and click the new “Build when a change is pushed to GitHub” checkbox, then save your changes.
Now when you push to your GitHub repo Jenkins will be notified via the Webhook and a build started. It will use the GitHub repo URL you specified in Step 3 ($GIT_SSH
) in the deployment script which is visible in the “Build” section at the bottom of the configuration page.
Also worth noting:
- Make sure hot deploy is not enabled in your application, or the deployment script will stop your application but not start it again.
- You can set up more fun stuff in Jenkins like setting the status of the GitHub commit based on the build and creating git “tags” for each of your releases.
Excellent post. This helped me when so many others could not. Many thanks !