Create a Classic Pipeline with Jenkins on OpenShift 4.5¶
Requirements¶
- An IBM Cloud account
- An OpenShift 4.x cluster with a minimum of 2 worker nodes
- Access to a terminal with the
oc
cli andibmcloud
cli, use Skills Network or IBM Cloud shell. For instructions go here. - A Jenkins instance on OpenShift 4.x, see setup below,
- A Github fork of https://github.com/remkohdev/spring-client,
- Github personal access token
Setup Jenkins on OpenShift 4.x¶
Go to Setup Jenkins on OpenShift 4.x to complete the Jenkins setup and configuration on OpenShift 4.x using the Jenkins Operator.
Fork the spring-client
App in Github¶
To create a fork of the spring-client repository:
- Go to https://github.com/remkohdev/spring-client,
- Cick the
Fork
button in the to right to create a fork in your own GitHub organization, e.g.https://github.com/<username>/spring-client
- Review the Jenkinsfile that is included in the Spring Client repository,
- Edit the Jenkinsfile,
-
Copy the login command to your OpenShift cluster,
oc login https://c100-e.us-south.containers.cloud.ibm.com:30645 --token=CgwpwTu12sJV3u45iFFWd-6V7JsD8b90JBoJk1zGR2I
- In the
environment
section of theJenkinsfile
, change theLOGIN_URL
and theLOGIN_PORT
to match
pipeline { agent any tools { maven 'maven' } environment { LOGIN_URL = 'https://c100-e.us-south.containers.cloud.ibm.com' LOGIN_PORT = '30645' }
Note: it is not proper to leave configuration details in your repository, let alone on public Github, but for simplicity I defined the URL and NodePort here.
- In the
-
Commit changes to the
Jenkinsfile
to your Github fork. The Jenkins pipeline will use yourJenkinsfile
to deploy your forkedspring-client
to your own OpenShift cluster.
Create a Github Personal Access Token¶
- Login to your Github account at https://github.com/,
- Go to https://github.com/settings/tokens,
- Click
Generate new token
, - Under
Note
addgithub-access-token-for-jenkins-on-openshift
, - Select the scopes for
repo
,read:repo_hook
, anduser
, - Click
Generate token
, -
Copy the token and save it, you need it to create the Jenkins pipeline from the Github source,
-
E.g. create an environment variable GITHUB_TOKEN,
export GITHUB_TOKEN=<your token>
Configure Jenkins Access to OpenShift¶
- Go to the OpenShift web console again or use the
Copy Login Command
from earlier again, -
From the logged in user profile dropdown, click the
Copy Login Command
. The command should look like,oc login https://<your-openshift-url>:<your-openshift-port> --token=<your-openshift-api-token>
-
Copy the OpenShift API token value, e.g. aaHYcMwUyyusfNaS45aAiQer_Kas1YUa45YTA2AxsNI,
-
Go to the Jenkins dashboard,
- Click Credentials, or
-
Go to Jenkins > Manage Jenkins > Configure Credentials
-
Go to Credentials > System,
-
In the System view, select the dropdown for Global credentials (unrestricted),
-
From the drowdown, click
Add credentials
, - The Jenkinsfile expects an OpenShift API token credential to be available named
openshift-login-api-token
, - For Kind select
Username with password
, - For Scope select
Global
, - For Username enter
token
, - For Password paste the OpenShift API token from the OpenShift web console login command,
- For ID enter
openshift-login-api-token
, which is the ID that the Jenkinsfile will look for, -
For Description enter
openshift-login-api-token
, -
Click OK,
Create a Jenkins Pipeline¶
- Make sure a project
springclient-ns
exists in OpenShift, -
if no
springclient-ns
project exists, create it from the cloud shell,oc new-project springclient-ns
-
Or via the UI, open the OpenShift web console,
- From the top navigation dropdown, go to the
Cluster Console
, - Go to Administration > Projects,
- Filter projects by
springclient-ns
, -
If there is no such project, click
Create Project
to create it, -
The Jenkinsfile of the spring-client application defines a stage to delete and create the
springclient-ns
project. The delete step causes an error when the project it tries to delete is missing, -
Go back to the Jenkins dashboard. If you closed Jenkins,
- Go to the
Application Console
, and go to the projectjenkins
, - Click the Route for External Traffic to open the Jenkins instance,
- Click
Log in with OpenShift
, -
In the Jenkins Dashboard, click
Open Blue Ocean
to open the Blue Ocean editor. -
If the Welcome to Jenkins popup window shows, click the
Create a new Pipeline
button, -
Otherwise, click the
New Pipeline
button in the Pipelines window. This will create a new Multibranch Pipeline, -
Select the GitHub option,
-
In the Connect to GitHub section, paste the personal access token you created in your Github account,
-
Click Connect,
- Select the organization to where you forked the
spring-client
repository, -
Search for and select the
spring-client
repo, -
Click Create Pipeline,
-
When the pipeline creation is completed, a build is triggered automatically,
-
You should see a successful build of the pipeline,
- If an error occurs, you can debug the pipeline by unfolding the red cross indicating on the stage, which indicates the pipeline failed in that stage,
-
Unfold the step in the stage, to see the log output,
-
Any update to the Github repository, e.g. a push to update the Jenkinsfile, source code of the Spring Boot application, or the README.md file, will automatically trigger a new build of the pipeline,
-
If you're interested, review the pipeline settings:
- Click the Configure option,
- Review the settings,
- From the cloud shell, make sure you're logged in to the OpenShift console,
-
Use the project
oc project springclient-ns
,oc project springclient-ns
outputs,
$ oc project springclient-ns Now using project "springclient-ns" on server "https://c100-e.us-south.containers.cloud.ibm.com:30645".
-
Get the route and test the deployment,
ROUTE="$(oc get route springclient -o json | jq -r '.spec .host')" curl -X GET http://$ROUTE/api/hello?name=you
outputs,
ROUTE="$(oc get route springclient -o json | jq -r '.spec .host')" curl -X GET http://$ROUTE/api/hello?name=you { "message" : "Hello you" }