How to Build and Deploy a Maven Project

Prerequisites

  • You need to enable KubeSphere DevOps System.
  • You need to create DockerHub account.
  • You need to create a workspace, a DevOps project, and a user account, and this account needs to be invited into the DevOps project as the role of maintainer.

Workflow for Maven Project

As is shown in the graph, there is the workflow for a Maven project in KubeSphere DevOps, which uses the pipeline of Jenkins to build and deploy the Maven project. All steps are defined in the pipeline.

When running, Jenkins Master creates a Pod to run the pipeline. Kubernetes creates the Pod as the agent of Jenkins Master, and the Pod will be destoryed after pipeline finished. The main process is to clone code, build & push image, and deploy the workload.

workflow

Default Configurations in Jenkins

Maven Version

Execute the following command in the Maven builder container to get version info.

  1. mvn --version
  2. Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T19:49:05Z)
  3. Maven home: /opt/apache-maven-3.5.3
  4. Java version: 1.8.0_232, vendor: Oracle Corporation
  5. Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.i386/jre
  6. Default locale: en_US, platform encoding: UTF-8

Maven Cache

Jenkins Agent mounts the directories by Docker Volume on the node. So the pipeline can cache some spicial directory such as /root/.m2, which is used for the Maven building and the default cache directory for Maven tools in KubeSphere DevOps so that the dependency packages are downloaded and cached on the node.

Global Maven Settings in Jenkins Agent

The default Maven settings file path is maven and the configuration file path is /opt/apache-maven-3.5.3/conf/settings.xml. Execute the following command to get the content of Maven settings.

  1. kubectl get cm -n kubesphere-devops-system ks-devops-agent -o yaml

Network of Maven Pod

The Pod labeled maven uses the docker-in-docker network to run the pipeline. That is, the /var/run/docker.sock in the node is mounted into the Maven container.

A Maven Pipeline Example

Prepare for the Maven Project

Create the Credentials

  • dockerhub-id. A Account Credentials for registry, e.g DockerHub.
  • demo-kuebconfig. A Kubeconfig Credential for deploying workloads.

For details, please refer to the Credentials Management.

view credential list

Create the Project for Workloads

In this demo, all of workloads are deployed under kubesphere-sample-dev. So you need to create the project kubesphere-sample-dev in advance.

view namespace

Create the Pipeline for the Maven Project

At first, create a DevOps Project and a Pipeline refer to Create a Pipeline - using Graphical Editing Panel.

Secondly, click Edit Jenkinsfile button under your pipeline.

edit jenkinsfile

Paste the following text into the pop-up window and save it.

  1. pipeline {
  2. agent {
  3. node {
  4. label 'maven'
  5. }
  6. }
  7. parameters {
  8. string(name:'TAG_NAME',defaultValue: '',description:'')
  9. }
  10. environment {
  11. DOCKER_CREDENTIAL_ID = 'dockerhub-id'
  12. KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
  13. REGISTRY = 'docker.io'
  14. // need to replace by yourself dockerhub namespace
  15. DOCKERHUB_NAMESPACE = 'shaowenchen'
  16. APP_NAME = 'devops-java-sample'
  17. BRANCH_NAME = 'dev'
  18. }
  19. stages {
  20. stage ('checkout scm') {
  21. steps {
  22. git branch: 'master', url: "https://github.com/kubesphere/devops-java-sample.git"
  23. }
  24. }
  25. stage ('unit test') {
  26. steps {
  27. container ('maven') {
  28. sh 'mvn clean -o -gs `pwd`/configuration/settings.xml test'
  29. }
  30. }
  31. }
  32. stage ('build & push') {
  33. steps {
  34. container ('maven') {
  35. sh 'mvn -o -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
  36. sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
  37. withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
  38. sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
  39. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
  40. }
  41. }
  42. }
  43. }
  44. stage('deploy to dev') {
  45. steps {
  46. kubernetesDeploy(configs: 'deploy/dev-ol/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
  47. }
  48. }
  49. }
  50. }

After saving, you will get this.

view jenkinsfile

Run and test

Click run and type TAG_NAME to run the pipeline.

run maven pipeling

After the run is complete, you can see the following figure.

view result

Under the project of kubesphere-sample-dev, there are new workloads created.

maven workload

You can view the access address of the service through service.

maven service

Summary

This document is not a getting-started document. It introduces some configurations for building Maven projects on the KubeSphere DevOps Platform. At the same time, a example flow of the Maven project is provided. In your case, you are free to add new steps to improve the pipeline.