As agile development and DevOps get more popular, CI/CD has become an indispensable best practice by almost all developers. It aims to deliver practical software programs more quickly.
This document uses GitHub, Jenkins, and CODING as examples to describe how to use Serverless Framework CLI to quick build CI/CD for your SCF project.
GitHub Actions is an automated software development workflow launched by GitHub. It uses actions to execute any tasks, including CI/CD.
serverless.yml
configuration file used for execution in Serverless Framework CLI.Note:SCF has released Tencent Serverless Action in GitHub.
If you are familiar with the action usage, you can use the following command, which encapsulates the steps of installing Serverless Framework and running the deployment command.
- name: serverless scf deploy
uses: woodyyan/tencent-serverless-action@main
If you are new to actions, you can select one of the following YML code samples based on your programming language (Python, Java, or Node.js):
# When the code is pushed to the `main` branch, the current workflow will be executed
# For more information on configuration, visit https://docs.github.com/cn/actions/getting-started-with-github-actions.
name: deploy serverless scf
on: # Configuration of the event and branch listened 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: # Environment variable
STAGE: dev # Your deployment environment
SERVERLESS_PLATFORM_VENDOR: tencent # The serverless platform is `aws` by default outside the Chinese mainland. Here, it is set to `tencent`
TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }} # `secret ID` of your Tencent Cloud account, which needs to be configured in `Settings-Secrets`
TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }} # `secret key` of your Tencent Cloud account, which needs to be configured in `Settings-Secrets`
TENCENT_SECRET_ID
and TENCENT_SECRET_KEY
are required during the deployment. You need to configure such variables in Secrets
in the GitHub code repository settings as follows:
You can get the Tencent Cloud secret ID and key from CAM.
Jenkinsfile is commonly used on Jenkins and CODING platforms. After configuring the Jenkinsfile, you can complete automated deployment on such platforms.
serverless.yml
configuration file used for execution in Serverless Framework CLI.scf_bootstrap
file in the root directory of your project.This document provides Jenkinsfile code in three programming languages: Python, Java, and Node.js. Carefully read the comments.
pipeline {
agent any
stages {
stage('check out') {
steps {
checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('Package'){ // This stage is only used for a Java project
steps{
container("maven") {
echo 'Package start'
sh "mvn package" // This line is used for a Java Maven project
sh "./gradlew build" // This line is used for a Java Gradle project
sh "mkdir zip" // This line is used to store JAR and `scf_bootstrap` files for HTTP-triggered Java functions. You only need to specify the `Jar` directory in `Serverless.yml` for event-triggered Java functions.
sh "cp ./build/libs/XXX.jar ./scf_bootstrap ./zip" // This line is used to move JAR and `scf_bootstrap` files for HTTP-triggered Java functions. You only need to specify the `Jar` directory in `Serverless.yml` for event-triggered Java functions. Note that if you use Maven for compilation, you need to change the JAR path below to `/target`.
}
}
}
stage('Install dependency') {
steps {
echo 'Installing dependency...'
sh 'npm i -g serverless'
sh 'npm install' // This line is used for a Node.js project
echo 'Dependency installed.'
}
}
stage('deploy') {
steps {
echo 'deploying...'
withCredentials([
cloudApi(
credentialsId: "${env.TENCENT_CLOUD_API_CRED}",
secretIdVariable: 'TENCENT_SECRET_ID',
secretKeyVariable: 'TENCENT_SECRET_KEY'
),
]) {
// Generate the credential file
sh 'echo "TENCENT_SECRET_ID=${TENCENT_SECRET_ID}\nTENCENT_SECRET_KEY=${TENCENT_SECRET_KEY}" > .env'
// Deploy
sh 'sls deploy --debug'
// Remove the credential
sh 'rm .env'
}
echo 'deployment complete'
}
}
}
}
You can use the above Jenkinsfile to quickly configure CI/CD on platforms such as Jenkins and CODING.
Note:You can get Tencent Cloud
TENCENT_SECRET_ID
andTENCENT_SECRET_KEY
required during the deployment from CAM.
Was this page helpful?