Spring Boot tutorial

This chapter shows you how to set up the Spring Boot project for your CloudBees Core for Kubernetes CD instance using the Jenkins X command line interface (CLI) tool.

Prerequisites

For this tutorial, the following applications must be installed locally on your computer:

Create the Spring Boot project

The Spring Boot project is a simple project that’s designed to help you understand how to work with CloudBees Core for Kubernetes CD.

This process prompts you several times for input, with each prompt explained in the relevant steps below.

  1. Open a terminal or command prompt window, or the GCP Cloud Shell in your web browser and cd to your local working directory for Git.

  2. Begin creating the Spring Boot project project with this command:

    jx create spring
  3. Perform the following actions:

    When prompted with Do the following

    Language:

    Choose the default value java.

    Group:

    Choose the default value com.example.

    Artifact:

    Choose the default value demo.

    Dependencies:

    Use Space to choose the values actuator and web from this scrolling list.

    Tip: Since this is a long list, scrolling up may be quicker when choosing web.

  4. When prompted with:

    • Created Spring Boot project at local/git/working/directory/path/demo
      No username defined for the current Git server!

    • Do you wish to use github-user as the Git user name: (where github-user is likely to be your GitHub bot account’s username), choose the value n for "No". Do not choose the default value Y for "Yes".

  5. Then, when prompted with:

    1. Git user name:, specify your own/usual GitHub account username.

    2. GitHub user name:, choose the default value, which should be your own GitHub account username that you specified in the previous step.

  6. If prompted with API Token:, either click or copy and paste the URL immediately above this command line prompt into your browser window. This URL should be:

    https://github.com/settings/tokens/new?scopes=repo,read:user,read:org,user:email,write:repo_hook,delete_repo
    1. If prompted with GitHub’s Username or email address and Password fields, specify your own/usual GitHub account username and password, respectively.

      Important
      Ensure you are signed in to GitHub with your own GitHub account.
      If not, sign out of GitHub, click Sign in at the top-right of the GitHub home page and repeat this substep to sign into GitHub with your own GitHub account credentials.
    2. In the Token description field, enter a name for the personal access token/API token.

    3. Scroll down to the end of the page and click the Generate token button.

    4. On the resulting GitHub page, copy the token code and paste it into your terminal/command prompt window or GCP Cloud Shell browser window at the API Token: prompt. You can now close this GitHub page.

  7. Perform the following actions:

    When prompted with Choose

    The directory local/git/working/directory/path/demo is not yet using git
    Would you like to initialise git now?

    The default value Y for "Yes".

    Commit message:

    The default value Initial import or customize it appropriately.

    The Jenkins X CLI tool sets up its demo repository locally on your computer.

    Which organisation do you want to use?

    The CloudBees Core for Kubernetes CD GitHub organization you configured previously in the "Install CloudBees Core for Kubernetes CD guide".

    About to create repository demo on server https://github.com with user your-github-account-username
    Enter the new repository name:

    The default value demo.

    The Jenkins X CLI tool now sets up the demo repository in your CloudBees Core for Kubernetes CD GitHub organization.

  8. Do the following to trigger a build of your demo repository’s Pipeline (Jenkinsfile) in CloudBees Core for Kubernetes CD:

    1. cd into the demo directory:

      cd demo
    2. Submit an empty commit to trigger the build:

      git commit -a -m 'Trigger initial release' --allow-empty
    3. Push your changes to the master branch of your demo repository on GitHub:

      git push origin master
  9. Check the CloudBees Core for Kubernetes CD UI to see that your application is now being deployed to the staging environment.

    CloudBees for Kubernetes CD’s main UI showing staging environment

    To access this area of the UI:

    1. Ensure you are logged into CloudBees Core for Kubernetes CD.

      Tip

      If you forget your CloudBees Core for Kubernetes CD URL, enter the following command:

      kubectl get ing -n jx

      and check the entry for core. Copy the URL for this entry (under HOSTS) and paste it into your browser window.

    2. Click your Team name (Jx).

    3. Ensure the Environments option is selected on the left.

    Now verify that continuous integration (CI) has been set up correctly.

  10. Create a GitHub issue with this command:

    jx create issue -t 'add a welcome page'

    You can check the creation of this issue in GitHub:

    GitHub - new issue
  11. When prompted with user name to use for authenticating with git issues, choose your personal GitHub account.

  12. Create and check out a new "work in progress" branch of your local demo repository:

    git checkout -b wip
  13. Add a simple HTML page file to the src/main/resources/static directory of your local demo repository:

    curl -o src/main/resources/static/index.html https://gist.githubusercontent.com/rawlingsj/ae624d06fd411e321ade0a180c973a13/raw/3b608e7e5c5dda4f76f33e834f2a199877fbe1bd/index.html
    Note
    Ensure you copy this entire line.
  14. Stage all changes in your local demo repository:

    git add --all
  15. Commit these changes locally, making a reference to the GitHub issue you created above (whose number should be #1):

    git commit -a -m 'add welcome page fixes #1'
  16. Push your changes in the wip branch to your demo repository on GitHub:

    git push origin wip
  17. Access your demo repository in GitHub and create a pull request (PR) of the wip branch into the master branch:

    GitHub - open a pull request
  18. Click Create pull request, and upon doing so, CloudBees Core for Kubernetes CD analyses the proposed change and adds a size label that can be used to help reviewers.

    GitHub - 'bot' information on PR and label

    After a few minutes, the serverless Jenkins X engine starts and eventually, a preview environment of your change/s is created, which you can access from a comment in the PR:

    GitHub - comment with preview
  19. Check the same Environments area of the CloudBees Core for Kubernetes CD UI (which you accessed above) to see that your application is now being deployed to the temporary pull requests area.

    CloudBees for Kubernetes CD’s main UI showing environments with pull request

    Clicking View App opens the same view of your application (as does clicking the here link to the preview environment in the GitHub comment above).

  20. Validate that the change looks correct from the preview, and if so, approve the PR by submitting a comment in the GitHub PR with the Prow GitHub event handler command /approve.

    GitHub - comment with approve command

    This triggers a new release and deploys it to the staging environment.

  21. Validate that the change looks correct from the preview of the staging environment (via its View App option from the ellipsis drop-down menu).

    CloudBees for Kubernetes CD’s main UI showing View App from staging environment
  22. If the change looks correct in the staging environment, click the Promote button to promote your change to the production environment (and verify this action in the resulting message box).

    CloudBees for Kubernetes CD’s main UI showing Promote message box

    You can validate the change in production via the production environment’s View App option (from the ellipsis drop-down menu).

    CloudBees for Kubernetes CD’s main UI showing production environment