头图

foreword

1.png

With the popularity of agile and DevOps, CI/CD has become an indispensable best practice for all developers in the development process. The main goal is to deliver effective software to users at a faster speed and in a shorter cycle. .

It can bring us the following benefits:

  • Shorten release cycles
  • reduce risk
  • Improve code quality
  • More efficient feedback loops
  • visualization process

Therefore, with Serverless becoming more and more popular today, how to quickly build CI/CD for Serverless projects is the focus of this article.

Users who are accustomed to CI/CD may expect a tutorial to quickly build automated deployments, so this article will explain how to build automated deployments on the following popular platforms, so that you can push the code to complete the deployment automatically.

  • Github
  • Jenkins
  • Coding

GitHub-based automated deployment

GitHub Actions is an automated software development workflow launched by Github. Actions can perform any task, including CI/CD.

2.png

Precondition

  • Your serverless project code has been hosted on Github.
  • The project must contain serverless.yml required for Serverless framework deployment. For the usage of serverless.yml , please refer to official website .
  • If it is a web function, you need to ensure that the root directory has the scf_bootstrap file. For details, please refer to official website .

steps

In order to make this deployment process easier, I released an Action for Tencent Cloud Serverless deployment in the GitHub market to help you quickly complete automated deployment.

Search for tencent serverless in GitHub's Marketplace. As shown below. There are detailed Action codes in it.

3.png

First, select Set up a workflow yourself in Actions, as shown below.

4.png

If you know how to use Action, you can use the following sentence directly, which encapsulates the steps of installing the Serverless framework and executing the deployment command.

    - name: serverless scf deploy
      uses: woodyyan/tencent-serverless-action@main

If you don't know how to use Action, you can choose the following different yml writing methods according to different languages. Below I list the writing methods of Python, Java, and NodeJS.

for Python item

# 当代码推动到 main 分支时,执行当前工作流程
# 更多配置信息: https://docs.github.com/cn/actions/getting-started-with-github-actions
name: deploy serverless scf
on: #监听的事件和分支配置
  push:
    branches:
      - main
jobs:
  deploy:
    name: deploy serverless scf
    runs-on: ubuntu-latest
    steps:
      - name: clone local repository
        uses: actions/checkout@v2
      - name: deploy serverless
        uses: woodyyan/tencent-serverless-action@main
        env: # 环境变量
          STAGE: dev #您的部署环境
          SERVERLESS_PLATFORM_VENDOR: tencent #serverless 境外默认为 aws,配置为腾讯
          TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }} #您的腾讯云账号 sercret ID,请在Settings-Secrets中配置
          TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }} #您的腾讯云账号 sercret key,请在Settings-Secrets中配置

is suitable for the Java project, please read the remarks in the code carefully

name: deploy serverless scf
on: #监听的事件和分支配置
  push:
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'temurin'
          server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
          settings-path: ${{ github.workspace }} # location for the settings.xml file
      - name: Build with Gradle # Gradle项目用这个
        uses: gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7
        with:
          arguments: build
      - name: Build with Maven # Maven项目用这个
        run: mvn -B package --file pom.xml
      - name: create zip folder # 此步骤仅用于Java Web函数,用于存放jar和scf_bootstrap文件。Java事件函数只需要在Serverless.yml中指定Jar目录就好。
        run: mkdir zip
      - name: move jar and scf_bootstrap to zip folder # 此步骤仅用于Java Web函数,用于移动jar和scf_bootstrap文件。Java事件函数只需要在Serverless.yml中指定Jar目录就好。注意如果是Maven编译请修改下面的jar路径为/target。
        run: cp ./build/libs/XXX.jar ./scf_bootstrap ./zip
      - name: deploy serverless
        uses: woodyyan/tencent-serverless-action@main
        env: # 环境变量
          STAGE: dev #您的部署环境
          SERVERLESS_PLATFORM_VENDOR: tencent #serverless 境外默认为 aws,配置为腾讯
          TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }} #您的腾讯云账号 sercret ID
          TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }} #您的腾讯云账号 sercret key

for NodeJS item

# 当代码推动到 main 分支时,执行当前工作流程
# 更多配置信息: https://docs.github.com/cn/actions/getting-started-with-github-actions
name: deploy serverless scf
on: #监听的事件和分支配置
  push:
    branches:
      - main
jobs:
  deploy:
    name: deploy serverless scf
    runs-on: ubuntu-latest
    steps:
      - name: clone local repository
        uses: actions/checkout@v2
      - name: install dependency
        run: npm install
      - name: build
        run: npm build
      - name: deploy serverless
        uses: woodyyan/tencent-serverless-action@main
        env: # 环境变量
          STAGE: dev #您的部署环境
          SERVERLESS_PLATFORM_VENDOR: tencent #serverless 境外默认为 aws,配置为腾讯
          TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }} #您的腾讯云账号 sercret ID,请在Settings-Secrets中配置
          TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }} #您的腾讯云账号 sercret key,请在Settings-Secrets中配置

Finally, since Tencent Cloud's TENCENT_SECRET_ID and TENCENT_SECRET_KEY need to be used during deployment, these two variables need to be configured in Secrets in the Github code repository settings. As shown below. ID and KEY can be obtained in Tencent Cloud's access control.

5.png

After the configuration is complete, every time you push code, the deployment process will be automatically triggered, and you can see the execution results and error logs in Actions in real time. As shown below.

You can also add steps such as testing, security check, and release to the process according to the needs of the project.

6.png

Automated deployment based on Jenkinsfile

Jenkinsfile is common to Jenkins, Coding and other platforms, so you only need to configure Jenkinsfile to complete automated deployment on these platforms.

Prerequisites

  • Your serverless project has been hosted on platforms such as Coding/Github/Gitlab/Code Cloud.
  • The project must contain serverless.yml , which is required for Serverless framework deployment. Please refer to official website serverless.yml for the usage of .
  • If it is a web function, make sure that the root directory has the scf_bootstrap file. For details, please refer to official website .

Operation steps

According to the needs of different languages, I have written the syntax required for all languages in the following Jenkinsfile, which is suitable for Python, Java, NodeJS, please read the comments carefully.

pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
            userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
      }
    }
        stage('Package'){ // 此stage仅用于Java项目
       steps{
        container("maven") {
          echo 'Package start'
          sh "mvn package" // 此行用于Java Maven项目
                    sh "./gradlew build" // 此行用于Java Gradle项目
                    sh "mkdir zip" // 此行仅用于Java Web函数,用于存放jar和scf_bootstrap文件。Java事件函数只需要在Serverless.yml中指定Jar目录就好。
          sh "cp ./build/libs/XXX.jar ./scf_bootstrap ./zip" // 此行仅用于Java Web函数,用于移动jar和scf_bootstrap文件。Java事件函数只需要在Serverless.yml中指定Jar目录就好。注意如果是Maven编译请修改下面的jar路径为/target。
        }           
           }
    }
    stage('安装依赖') {
      steps {
        echo '安装依赖中...'
        sh 'npm i -g serverless'
        sh 'npm install' // 此行用于NodeJS项目
        echo '安装依赖完成.'
      }
    }
    stage('部署') {
      steps {
        echo '部署中...'
        withCredentials([
          cloudApi(
            credentialsId: "${env.TENCENT_CLOUD_API_CRED}",
            secretIdVariable: 'TENCENT_SECRET_ID',
            secretKeyVariable: 'TENCENT_SECRET_KEY'
          ),
        ]) {
             // 生成凭据文件
             sh 'echo "TENCENT_SECRET_ID=${TENCENT_SECRET_ID}\nTENCENT_SECRET_KEY=${TENCENT_SECRET_KEY}" > .env'
             // 部署
             sh 'sls deploy --debug'   
             // 移除凭据
             sh 'rm .env' 
        }
        echo '部署完成'
      }
    }
  }
}

Using the above Jenkinsfile, you can complete the CI/CD configuration with one click on Jenkins, coding and other platforms.

Note that you need to configure the two variables TENCENT_SECRET_ID and TENCENT_SECRET_KEY that Tencent Cloud needs to use in the platform.

Summarize

As a developer, you always hope that all code work is done automatically, which can improve efficiency. Therefore, mastering how to quickly configure an automated CI/CD process is one of the skills every developer must master.

Sharing these out-of-the-box configurations here also hopes to greatly reduce everyone's learning costs and start core business development quickly.

In the future, I will continue to explore more DevOps practices for Serverless and share them with you.

If you have any questions or encounter any difficulties in the operation, you can leave a message below the article, and I will reply to you.


Woody
843 声望62 粉丝

ThoughtWorks|高级咨询师