This topic describes how to use GitHub Actions and Serverless Devs to implement continuous integration and continuous delivery (CI/CD).

Prerequisites

The following operations are complete:

Procedure

  1. Initialize a CI/CD template.
    Run the following command in the project directory to generate a GitHub Actions template:
    s cli cicd github
    Command output:
    √ File decompression completed
    [2021-08-09T10:45:03.137] [INFO ] [CICD] -
    
         Github Action CI/CD template created successfully.
           1️ Edit the file [./.github/workflow/serverless-devs.yml] to complete the CI/CD function configuration
           2️ Configure user key information through GitHub Secrets
    
        Related documents:
           Use GitHub Actions and Serverless Devs to implement CI/CD: http://short.devsapp.cn/cicd/github/action/usage
        Best practices:
           The official website of Serverless Devs is deployed by using Serverless Devs: http://short.devsapp.cn/cicd/github/action/practice
           Combine SAE with GitHub Actions to implement CD with ease: http://www.serverless-devs.com/blog/aliyun-sae-github-action-cicd
           Best practices of CI/CD for Alibaba Cloud custom containers: http://www.serverless-devs.com/blog/aliyun-custom-container-ci-cd
           Update only the code and then release the version: Atomic operations in Function Compute: http://www.serverless-devs.com/blog/serverless-devs-update-fc-code
    
    End of method: github                        
  2. Configure the serverless-devs.yml file.
    After you initialize the CI/CD template, the system generates a simple case that contains the serverless-devs.yml file. The file contains the following content:
    name: Serverless Devs Project CI/CD
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      serverless-devs-cd:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v2
            with:
              node-version: 12
              registry-url: https://registry.npmjs.org/
          - run: npm install
          - run: npm install -g @serverless-devs/s
          # The default key configuration command is an Alibaba Cloud key configuration command. For more information, see the following topics:
          # Use GitHub Actions and Serverless Devs to implement CI/CD: http://short.devsapp.cn/cicd/github/action/usage
          # The official website of Serverless Devs is deployed by using Serverless Devs: http://short.devsapp.cn/cicd/github/action/practice
          - run: s config add --AccountID ${{secrets.AccountID}} --AccessKeyID ${{secrets.AccessKeyID}} --AccessKeySecret ${{secrets.AccessKeySecret}} -a default
          - run: s deploy                                
    1. Configure keys.
      1. Log on to GitHub.
      2. Go to the details page of the code repository that you use and click the Settings tab.
      3. In the left-side navigation pane of the Settings tab, click Secrets.
      4. In the upper-right corner of the Actions secrets page, click New repository secret.
      5. Set the secret parameters and click Add secret. Github
        You must set the following parameters:
        • ALIYUN_ACCESS_KEY_ID: the AccessKey ID of the Alibaba Cloud account or RAM user that you use.
        • ALIYUN_ACCESS_KEY_SECRET: the AccessKey secret of the Alibaba Cloud account or RAM user that you use.
        • ALIYUN_ACCOUNT_ID: the ID of the Alibaba Cloud account that you use. If you use a RAM user, specify the ID of the Alibaba Cloud account to which the RAM user belongs.
        Note If your s file contains two services that use different components and different aliases, you must configure multiple keys to configure CI/CD.
      6. After the keys are configured in GitHub, specify the information about the keys in the serverless-devs.yml file.
        • If you configure one key, specify the following information about the key in the serverless-devs.yml file:
          s config add --AccountID ${{secrets.ALIYUN_ACCOUNT_ID}} --AccessKeyID ${{secrets.ALIYUN_ACCESS_KEY_ID}} --AccessKeySecret ${{secrets.ALIYUN_ACCESS_KEY_SECRET}} -a website_access
        • If you configure multiple keys, specify the following information about the keys in the serverless-devs.yml file:
          s config add -kl tempToken1,tempToken2 -il tempValue1,tempValue2 -a website_access
          s config add -kl tempToken3,tempToken4 -il tempValue3,tempValue4 -a fc_access
        Note If you have developed a component, you can modify the values of the Key-Value parameters. For example, you want to configure a key pair whose alias is demo, including a key with the key parameter set to tempToken1 and the Value parameter set to tempToken1, and a key with the key parameter set to tempToken2 and the Value parameter set to tempToken2. Specify the following content in the serverless-devs.yml file:
        s config add -kl tempToken1,tempToken2 -il tempValue1,tempValue2 -a demo
    2. Configure features.
      You can customize features in the serverless-devs.yml file by visiting GitHub Actions. For example, after you configure the keys, you must complete the following operations:
      1. Create an application.
      2. Delete the temporarily generated folder ./abc.
      The following example shows the content of the serverless-devs.yml file:
      name: Serverless Devs Project CI/CD
      
      on:
        push:
          branches: [ master ]
      
      jobs:
        serverless-devs-cd:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                node-version: 12
                registry-url: https://registry.npmjs.org/
            - run: npm install
            - run: npm install -g @serverless-devs/s
            # The default key configuration command is an Alibaba Cloud key configuration command. For more information, see the following topics:
            # Use GitHub Actions and Serverless Devs to implement CI/CD: http://short.devsapp.cn/cicd/github/action/usage
            # The official website of Serverless Devs is deployed by using Serverless Devs: http://short.devsapp.cn/cicd/github/action/practice
            - run: s config add --AccountID ${{secrets.AccountID}} --AccessKeyID ${{secrets.AccessKeyID}} --AccessKeySecret ${{secrets.AccessKeySecret}} -a default
            - run: s build
            - run: rm -rf ./abc
            - run: s deploy