Build and Deploy a Maven Project

Prerequisites

Workflow for a Maven Project

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

maven-project-jenkins

At first, the Jenkins Master creates a Pod to run the pipeline. Kubernetes creates the Pod as the agent of Jenkins Master, and the Pod will be destroyed after the pipeline finished. The main process includes cloning code, building and pushing an image, and deploying the workload.

Default Configurations in Jenkins

Maven version

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

  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

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

Global Maven settings in the Jenkins Agent

The default file path of Maven settings 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-worker ks-devops-agent -o yaml

Network of Maven Pod

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

A Maven Pipeline Example

Prepare for the Maven project

Create credentials

Credential IDTypeWhere to Use
dockerhub-idUsername and passwordRegistry, such as Docker Hub
demo-kubeconfigkubeconfigWorkload deployment

For details, refer to the Credential Management.

Create a project for workloads

In this example, all workloads are deployed in kubesphere-sample-dev. You must create the project kubesphere-sample-dev in advance.

Create a pipeline for the Maven project

  1. Go to Pipelines of your DevOps project and click Create to create a pipeline named maven. For more information, see Create a Pipeline - using Graphical Editing Panel.

  2. Go to the details page of the pipeline and click Edit Jenkinsfile.

  3. Copy and paste the following content into the displayed dialog box. You must replace the value of DOCKERHUB_NAMESPACE with yours. When you finish editing, click OK to save the Jenkinsfile.

    1. pipeline {
    2. agent {
    3. label 'maven'
    4. }
    5. parameters {
    6. string(name:'TAG_NAME',defaultValue: '',description:'')
    7. }
    8. environment {
    9. DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    10. KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    11. REGISTRY = 'docker.io'
    12. // need to replace by yourself dockerhub namespace
    13. DOCKERHUB_NAMESPACE = 'Docker Hub Namespace'
    14. APP_NAME = 'devops-maven-sample'
    15. BRANCH_NAME = 'dev'
    16. PROJECT_NAME = 'kubesphere-sample-dev'
    17. }
    18. stages {
    19. stage ('checkout scm') {
    20. steps {
    21. // Please avoid committing your test changes to this repository
    22. git branch: 'master', url: "https://github.com/kubesphere/devops-maven-sample.git"
    23. }
    24. }
    25. stage ('unit test') {
    26. steps {
    27. container ('maven') {
    28. sh 'mvn clean test'
    29. }
    30. }
    31. }
    32. stage ('build & push') {
    33. steps {
    34. container ('maven') {
    35. sh 'mvn -Dmaven.test.skip=true 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. container ('maven') {
    47. withCredentials([
    48. kubeconfigFile(
    49. credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
    50. variable: 'KUBECONFIG')
    51. ]) {
    52. sh 'envsubst < deploy/all-in-one/devops-sample.yaml | kubectl apply -f -'
    53. }
    54. }
    55. }
    56. }
    57. }
    58. }
  4. You can see stages and steps are automatically created on graphical editing panels.

Run and test

  1. Click Run, enter v1 for TAG_NAME in the displayed dialog box, and then click OK to run the pipeline.

  2. When the pipeline runs successfully, you can go to the Run Records tab to view its details.

  3. In the project of kubesphere-sample-dev, new workloads were created.

  4. On the Services page, view the external access information about the Service created.