Skip to content

Create a Classic Pipeline with Jenkins on OpenShift 4.5

Requirements

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:

  1. Go to https://github.com/remkohdev/spring-client,
  2. 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
  3. Review the Jenkinsfile that is included in the Spring Client repository,
  4. Edit the Jenkinsfile,
  5. Copy the login command to your OpenShift cluster,

    OpenShift Copy Login Command

    oc login https://c100-e.us-south.containers.cloud.ibm.com:30645 --token=CgwpwTu12sJV3u45iFFWd-6V7JsD8b90JBoJk1zGR2I
    
    1. In the environment section of the Jenkinsfile, change the LOGIN_URL and the LOGIN_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.

  6. Commit changes to the Jenkinsfile to your Github fork. The Jenkins pipeline will use your Jenkinsfile to deploy your forked spring-client to your own OpenShift cluster.

Create a Github Personal Access Token

  1. Login to your Github account at https://github.com/,
  2. Go to https://github.com/settings/tokens,
  3. Click Generate new token,
  4. Under Note add github-access-token-for-jenkins-on-openshift,
  5. Select the scopes for repo, read:repo_hook, and user,
  6. Click Generate token,
  7. Copy the token and save it, you need it to create the Jenkins pipeline from the Github source,

    github personal access token 1

    github personal access token 2

  8. E.g. create an environment variable GITHUB_TOKEN,

    export GITHUB_TOKEN=<your token>
    

Configure Jenkins Access to OpenShift

  1. Go to the OpenShift web console again or use the Copy Login Command from earlier again,
  2. 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>
    
  3. Copy the OpenShift API token value, e.g. aaHYcMwUyyusfNaS45aAiQer_Kas1YUa45YTA2AxsNI,

  4. Go to the Jenkins dashboard,

  5. Click Credentials, or
  6. Go to Jenkins > Manage Jenkins > Configure Credentials

    OpenShift Jenkins credentials

  7. Go to Credentials > System,

    OpenShift Jenkins credentials

  8. In the System view, select the dropdown for Global credentials (unrestricted),

    OpenShift Jenkins credentials

  9. From the drowdown, click Add credentials,

  10. The Jenkinsfile expects an OpenShift API token credential to be available named openshift-login-api-token,
  11. For Kind select Username with password,
  12. For Scope select Global,
  13. For Username enter token,
  14. For Password paste the OpenShift API token from the OpenShift web console login command,
  15. For ID enter openshift-login-api-token, which is the ID that the Jenkinsfile will look for,
  16. For Description enter openshift-login-api-token,

    Jenkins credentials

  17. Click OK,

    Jenkins new credentials

Create a Jenkins Pipeline

  1. Make sure a project springclient-ns exists in OpenShift,
  2. if no springclient-ns project exists, create it from the cloud shell,

    oc new-project springclient-ns
    
  3. Or via the UI, open the OpenShift web console,

  4. From the top navigation dropdown, go to the Cluster Console,
  5. Go to Administration > Projects,
  6. Filter projects by springclient-ns,
  7. If there is no such project, click Create Project to create it,

    OpenShift Jenkins credentials

  8. 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,

  9. Go back to the Jenkins dashboard. If you closed Jenkins,

  10. Go to the Application Console, and go to the project jenkins,
  11. Click the Route for External Traffic to open the Jenkins instance,
  12. Click Log in with OpenShift,
  13. In the Jenkins Dashboard, click Open Blue Ocean to open the Blue Ocean editor.

    Jenkins Open Blue Ocean

  14. If the Welcome to Jenkins popup window shows, click the Create a new Pipeline button,

    Jenkins Create new pipeline

  15. Otherwise, click the New Pipeline button in the Pipelines window. This will create a new Multibranch Pipeline,

    Jenkins New pipeline

  16. Select the GitHub option,

    Jenkins Select SCM

  17. In the Connect to GitHub section, paste the personal access token you created in your Github account,

    Jenkins Select Organization

  18. Click Connect,

  19. Select the organization to where you forked the spring-client repository,
  20. Search for and select the spring-client repo,

    Jenkins Choose Repo

  21. Click Create Pipeline,

  22. When the pipeline creation is completed, a build is triggered automatically,

    Jenkins Run Pipeline

  23. You should see a successful build of the pipeline,

  24. 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,
  25. Unfold the step in the stage, to see the log output,

    Jenkins Successful Build

    Jenkins Successful Build

  26. 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,

    Jenkins Successful Build

  27. If you're interested, review the pipeline settings:

    1. Click the Configure option,
    2. Review the settings,
  28. From the cloud shell, make sure you're logged in to the OpenShift console,
  29. 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".
    
  30. 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" }