Automatically deploy applications through Maven plug-ins

Introduction:   In addition to deploying applications on SAE through the console, there is another way to deploy applications automatically through Maven's Toolkit Maven plugin plug-in. This article will explain in detail how to deploy applications automatically through Maven plug-in.


For image download, domain name resolution and time synchronization, please click   Alibaba open source mirror station

1, Automated deployment process

The application deployment process is automated through the toolkit Maven plugin plug-in: first add the plug-in dependency, then configure the plug-in, and finally build the deployment.

1. Add plug-in dependency

Add the following plug-in dependencies in the pom.xml file.

<build>
    <plugins>
        <plugin>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>toolkit-maven-plugin</artifactId>
            <version>1.0.6</version>
        </plugin>
    </plugins>
</build>

explain   The latest version of toolkit Maven plugin is recommended.

2. Configure plug-ins

Configuration plug-ins mainly include account configuration, packaging configuration and deployment configuration. If you need more custom configuration items, see Packaging parameters and Deployment parameters.
  a. Account configuration  
Create a file in the root directory of the package project in the format of   YAML   The account configuration file is named toolkit_profile.yaml and fill in the following information:

regionId:        #The area where the application is located, such as Beijing, Shanghai and Hangzhou.
accessKeyId:     #When accessing the AK of Alibaba cloud resources, it is recommended to use the AK of the sub account to reduce security risks
accessKeySecret: #When accessing SK of Alibaba cloud resources, it is recommended to use SK of sub accounts to reduce security risks

  b. Packaging configuration
Create a file in the root directory of the package project in the format of   YAML   Packaging profile for. If the packaging project is a sub module of Maven, you need to create the file in the directory of the sub module and name it toolkit_package.yaml, fill in the following information:

apiVersion: V1
kind: AppPackage
spec:
 packageType: #The application deployment package type supports three formats: WAR, FatJar and Image.
 packageUrl:  #If the application deployment package type is WAR or FatJar, this field can be filled in. If not, the package built by the current maven will be used for deployment.
 imageUrl:    #If the deployment package type is Image, this field can be filled in; The Image type can also be deployed by building a Docker Image locally. Please refer to the packaging file parameters section to set relevant parameters.

  c. Deployment configuration
Create a file in the root directory of the package project in the format of   YAML   A deployment file named toolkit_deploy.yaml and fill in the following information:

apiVersion: V1
kind: AppDeployment
spec:
 type: serverless
 target:
  appId:        #ID of the deployment application. If appId is configured, namespaceId and appName do not need to be configured
  namespaceId:  #If the appId is unknown, the region and application name can be used for deployment
  appName:      #Application name. If you don't know appId, you can use this application name and namespace for deployment

3. Build and deploy

Enter the directory where pom.xml is located (if Maven sub module is deployed, enter the directory where pom.xml is located), and execute the following command:

mvn clean package toolkit:deploy -Dtoolkit_profile=toolkit_profile.yaml -Dtoolkit_package=toolkit_package.yaml -Dtoolkit_deploy=toolkit_deploy.yaml

Command parameters mean:

  • toolkit:deploy: deploy applications after packaging.
  • -Dtoolkit_profile: Specifies the account profile. If the account file is in the same directory as pom.xml and the name is. toolkit_profile.yaml, which is an optional parameter and will be obtained automatically by the plug-in.
    be careful   The file name is preceded by a decimal point.
  • -Dtoolkit_package: Specifies the package file. If the package file is in the same directory as pom.xml and the name is. toolkit_package.yaml, which is an optional parameter and will be automatically obtained by the plug-in.
    be careful   The file name is preceded by a decimal point.
  • -Dtoolkit_deploy: Specifies the deployment file. If the deployment file is in the same directory as pom.xml and the name is. toolkit_deploy.yaml, which is an optional parameter and will be automatically obtained by the plug-in.
  • -Ddeploy version: Specifies the version number of the deployment, which takes precedence over the version configuration in the deployment configuration file.
    explain   The toolkit Maven plugin version 1.0.6 and later supports this configuration parameter.

After the package command is executed, the system displays the following results. When the echo message displays "bud success", it indicates that the deployment is successful.


2, More configuration items

  • The parameters supported by the package file are as follows:
apiVersion: V1
kind: AppPackage
spec:
  packageType:  #The application deployment package type supports WAR, FatJar and Image.
  imageUrl:     #Image address, image package type, can be filled in.
  packageUrl:   #Package address, which can be filled in by WAR and FatJar applications.
  build:
    docker:
       dockerfile:        #Docker image build file. If you want to build an image deployment locally, you need to fill in this field, for example: Dockerfile.
       imageRepoAddress:  #Alibaba cloud image warehouse address. If you want to build an image deployment locally, you need to fill in this field, for example: registry.cn-beijing.aliyuncs.com/edas_demo/demo. 
       imageTag:          #Mirror Tag. If you want to build an image deployment locally, you need to fill in this field, for example: test.
       imageRepoUser:     #Alicloud image warehouse user name. If you want to build an image deployment locally, you need to fill in this field, for example: * * * @ * * *.
       imageRepoPassword: #Alicloud image warehouse password. If you want to build an image deployment locally, you need to fill in this field, for example: password.
    oss:
       bucket:      #Destination bucket name. If you want to use a custom oss warehouse to store deployment packages, you need to fill in this field, for example: bucket-name01.
       key:         #oss custom path. If you want to use a custom oss repository to store deployment packages, you need to fill in this field, for example: test1/test.jpg.
       accessKeyId: #oss account. If you want to use a custom oss repository to store packages, you need to fill in this field.
       accessKeySecret: #oss password. If you want to use a custom oss repository to store packages, you can fill in this field.
  • The parameters supported by the deployment file are as follows:
apiVersion: V1
kind: AppDeployment
spec:
  type: serverless
  target:
    appName:     #apply name
    namespaceId: #Application namespace
    appId:       #Application ID. The plug-in will use this application for deployment. If it is not filled in, it will use namespaceId and appName to find the application for deployment.
  version: #Deployment version number. The default format is day hour minute second
  jdk:     #The deployed package depends on the JDK version. The supported JDK versions are Open JDK 7 and Open JDK 8. Mirroring is not supported.
  webContainer:  #The deployed package depends on the Tomcat version. The WebContainer supports apache-tomcat-7.0.91. Mirroring is not supported.
  batchWaitTime: #Batch waiting time.
  command:       #Mirror start command. The command must be an executable object that exists in the container. For example: sleep. Setting this command will invalidate the original start command of the mirror.
  commandArgs: #Mirror start command parameters. Parameters required for the above start command.
    - 1d
  envs:    #Container environment variable parameters
    - name: envtmp0
      value: '0'
    - name: envtmp1
      value: '1'
  customHostAlias:  #Custom host mapping in container
    - hostName: 'samplehost1'
      ip: '127.0.0.1'
    - hostName: 'samplehost2'
      ip: '127.0.0.1'
  jarStartOptions:  #JAR package launch application options
  jarStartArgs:     #JAR package startup application parameters
  liveness:   #Container health check. Containers that fail the health check will be killed and recovered.
    exec:
      command:
        - sleep
        - 1s
    initialDelaySeconds: 5
    timeoutSeconds: 11
  readiness:  #When the application starts the status check, the container that fails multiple health checks will be killed and restarted. Containers that do not pass the health check will not have SLB flow.
    exec:
      command:
        - sleep
        - 1s
    initialDelaySeconds: 5
    timeoutSeconds: 11
  minReadyInstances: 1  #Minimum number of surviving instances. In the process of rolling upgrade or rolling upgrade fails, the number of available instances will not be less than this value as far as possible. If it is 0, there is no limit. The range of elastic expansion and contraction capacity should not be less than this value, otherwise an exception will be triggered.

3, Typical scenario example

Scenario 1: build WAR or FatJar package locally and deploy it in SAE
Suppose you have WAR or FatJar type applications in Beijing environment, and expect to build WAR or FatJar locally and deploy it in SAE. The packaging configuration and deployment configuration are shown below.

  • Package file:
apiVersion: V1
kind: AppPackage
spec:
  packageType: War
  • Deployment file:
apiVersion: V1
kind: AppDeployment
spec:
  type: serverless
  target:
    appId:  #Application ID. The plug-in will use this application for deployment. If it is not filled in, it will use namespaceId and appName to find the application for deployment.
    namespaceId:  #[optional] namespace. If you don't know appId, you can use this namespace and application name for deployment.
    appName:      #[optional] application name. If you don't know appId, you can use this namespace and application name for deployment.

Scenario 2: deploy image type applications using existing image addresses
Suppose you have an image type application in the Beijing environment. You expect to deploy the application using the existing image (registry.cn-beijing.aliyuncs.com/test/gateway:latest). The packaging configuration and deployment configuration are shown below.

  • Package file:
apiVersion: V1
kind: AppPackage
spec:
  packageType: Image
  imageUrl: registry.cn-beijing.aliyuncs.com/test/gateway:latest
  • Deployment file:
apiVersion: V1
kind: AppDeployment
spec:
  type: serverless
  target:
    appId:  #Application ID. The plug-in will use this application for deployment. If it is not filled in, it will use namespaceId and appName to find the application for deployment.
    namespaceId:  #[optional] namespace. If you don't know appId, you can use this namespace and application name for deployment.
    appName:      #[optional] application name. If you don't know appId, you can use this namespace and application name for deployment.

Scenario 3: upload the local build image to the warehouse and deploy the application
Suppose you have an image type application in Beijing. You expect to compile and build an image locally and upload it to Alibaba cloud image warehouse for deployment. The packaging configuration and deployment configuration are as follows.

  • Package file:
apiVersion: V1
kind: AppPackage
spec:
  packageType: Image
  build:
    docker:
       dockerfile: Dockerfile #Specify Dockerfile
       imageRepoAddress:      #Mirror warehouse address
       imageTag:              #Mirror Tag
       imageRepoUser:         #Mirror warehouse user name
       imageRepoPassword:     #Mirror warehouse password
  • Deployment file:
apiVersion: V1
kind: AppDeployment
spec:
  type: serverless
  target:
    appId:  #Application ID. The plug-in will use this application for deployment. If it is not filled in, it will use namespaceId and appName to find the application for deployment.
    namespaceId:  #[optional] namespace. If you don't know appId, you can use this namespace and application name for deployment
    appName:      #[optional] application name. If you don't know appId, you can use this namespace and application name for deployment

  This article is transferred from: Automatic application deployment through Maven plug-in - alicloud developer community

 

Tags: Java Maven

Posted on Mon, 06 Dec 2021 19:28:29 -0500 by nigma