One minute to start the continuous integration journey series: WeChat official account server-side application (based on Java back-end)

Posted Jun 29, 20205 min read

Author:CODING - Zhuzeng Hui

Foreword

This article is a tutorial of CODING Continuous Integration Custom Build Node Function , through a Demo that enables development configuration for WeChat public account Demonstration, explain how to access the custom build node, how to use the custom build node to build, test, and deploy the server.

Ready to work

surroundings

The following tools will be used in this article, please confirm that they are installed, or install them according to the linked documents.

To develop WeChat public account, you need to prepare the following two resources in advance.

WeChat public account

The WeChat public account can be applied on WeChat public platform official website , the platform also provides detailed [development help documents]( https://developers.weixin.qq.com /doc/offiaccount/Getting_Started/Overview.html).

server

The server here refers to a computer that can be accessed by the WeChat server and is used to run the server program in this article. Many companies provide cloud server rental, which is still very easy to obtain. The CVM of Tencent Cloud Platform is recommended here. If it is used temporarily, the auction instance is really cost-effective.

Code

This article focuses on introducing CODING platform continuous integration custom build node function , the specific business logic is not important, here has been advanced I have prepared a demo code , which can be combined with the following steps for actual operation.

The code itself is relatively simple, and it should be noted that in order to be able to repeat the deployment, the code introduces [Spring Boot Actuator]( https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready -features.html) Support calling API to exit the server process.

Step 1 Prepare the construction plan

Select Build Plan in the left menu bar, and click New Build Plan Configuration in the opened page.

ci-empty

In order to simplify the construction plan configuration, CODING provides a wealth of templates to choose from, here we choose the Java compilation and deployment jar package template.

ci-module

The template supports to transfer the built jar package to the remote server through ssh, and execute the command to start. This article uses a custom build node method, which can be built and deployed directly on the server without transmission, so here the configuration information of the remote server can be left blank.

ci-module

The adjusted build script(that is, the content of the Jenkinsfile file in the code) is as follows, and you can adjust it according to your needs on this basis.

def VERSION = ""

pipeline {
  agent any
  stages {
    stage('checked out) {
      steps {
        checkout([$class:'GitSCM',
        branches:[[name:env.GIT_BUILD_REF]],
        userRemoteConfigs:[[
          url:env.GIT_REPO_URL,
          credentialsId:env.CREDENTIALS_ID
       ]]])
        script {
             def pom = readMavenPom()
             VERSION = pom.getVersion()
             println "current version ${VERSION}"
        }
      }
    }
    stage('unit test') {
      steps {
        sh'mvn clean test'
      }
    }
    stage('compile') {
      steps {
        sh'mvn clean package -Dmaven.test.skip=true'
      }
    }
    stage('Deployment') {
      steps {
        sh "curl -X POST localhost/actuator/shutdown || true"
        sh "JENKINS_NODE_COOKIE=dontKillMe nohup java -Dserver.port=80 -jar ${env.WORKSPACE}/target/demo-${VERSION}.jar> log &"
      }
    }
  }
}

There are two points that need special explanation:
(1) To facilitate repeated deployment, the build script will call the actuator/shutdown API to exit the running service process before deployment.
(2) The bottom layer of CODING CI is implemented based on Jenkins , and Jenkins will stop all processes created by this task after the completion of the construction task, so it is added during deployment The environment variable JENKINS_NODE_COOKIE=dontKillMe was added to prevent Jenkins from stopping the deployed server process.

Step 2:Access custom build node

Open the Build Node page under Continuous Integration from the left menu bar, select the node pool you want to add nodes, CODING creates a default node pool for each project, here we select the default node pool, open In the right-hand panel, click Access New Node, and then click the corresponding column according to the operating system of the node to be accessed.

agent-sh

Click Generate access configuration and copy to get access command. This article uses the Tencent Cloud CVM machine pre-installed with Linux as the node, so select the Linux section to generate the access command and execute it in the Shell, which can be automatically completed The process of initialization, registration, and startup.

agent-start

In fact, the access command is a shell script, mainly used to download and execute the cci-agent program. cci-agent is a client program developed by CODING specifically for custom build node functions. You can execute the ./cci-agent help command to view more detailed instructions.

After executing the access command, you can see the newly connected node on the node pool page, and the node status will change from in preparation to idle. At this time, you can use this node to build.

agent-idle

Step 3:deploy with custom node

After accessing the custom build node, you also need to adjust the build plan configuration and specify that the build plan uses the custom node to build. Open the build plan settings page, find the Node Pool Configuration option under the Basic Information page, select the node pool to be used, and select the default node pool here.

ci-job-agent

After the configuration is modified, click Save Changes, and then click Build Now on the Build Plan page. After the build is complete, the server is deployed.

ci-build-success

Step 4 Configure WeChat public account

First find the public network IP of the building node. If you are also using the Tencent Cloud CVM machine, you can easily find it in Tencent Cloud Cloud Server Console Found.

tencent-cvm

Open the development configuration page of WeChat official account and click the enable button.

wxoa-enable

Fill in the pop-up page with information about the server program deployed in the previous step.

wxoa-config

Click the Submit button. If there is no error message on the page, it means that the configuration is successful. You can continue to develop and implement more functions.

to sum up

This article takes the WeChat public account development as the background and highlights the use of CODING platform continuous integration of custom build node function . By accessing the continuous integration function of the CODING platform, we have realized the automatic construction, testing and deployment of the WeChat public account server program, which saves time and improves efficiency while avoiding human errors, and hopes to help you.