This document uses deploying an Express website with the tencent-express
component as an example to describe how to use Serverless Framework to develop, manage, deploy, and publish a project. You can see the demo here.
Project development may involve the following branches:
Branch Type | Description |
---|---|
master | It is used to deploy a production environment |
testing | It is used for testing in a testing environment |
dev | It is used for daily development |
feature-xxx | It is used to add a new feature; for example, different developers can pull different feature branches from dev for development |
hotfix-xxx | It is used to fix an urgent bug |
#serverless.yml
app: expressDemoApp # Application name, which is the component instance name by default
stage: ${env:STAGE} # Parameter used to isolate the development environment, which is `dev` by default
component: express # Name of the imported component, which is required. The `express-tencent` component is used in this example
name: expressDemo # Name of the instance created by the component, which is required
inputs:
src:
src: ./
exclude:
- .env
region: ap-guangzhou
runtime: Nodejs10.15
functionName: ${name}-${stage}-${app} # Function name
apigatewayConf:
protocols:
- http
- https
environment: release
TENCENT_SECRET_ID=xxxxxxxxxx # `SecretId` of your account
TENCENT_SECRET_KEY=xxxxxxxx # `SecretKey` of your account
STAGE=prod # `STAGE` is the `prod` environment. You can also run `sls deploy --stage prod` to pass in the parameter
After the deployment by running sls deploy
succeeds, access the generated URL as shown below:
Create a remote repository (sample), submit the project code to the remote master
branch, and create testing
and dev
branches. In this way, the code of the three branches is on the same version (suppose it is v0).
In this stage, a feature module needs to be developed. Suppose two developers are needed: Tom and Jorge, who create feature1
and feature2
feature branches from dev
(v0) for development, respectively.
Tom starts developing feature1
. In this example, a feature.html
file is added, and "This is a new feature 1." is written in it.
sls.js
file:// Routes
app.get(`/feature`, (req, res) => {
res.sendFile(path.join(__dirname, 'feature.html'))
})
feature.html
:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Serverless Component - Express.js</title>
</head>
<body>
<h1>
This is a new feature 1.
</h1>
</body>
</html>
serverless.yml
as follows:TENCENT_SECRET_ID=xxxxxxxxxx
TENCENT_SECRET_KEY=xxxxxxxx
STAGE=feature1
sls deploy
succeeds, the following content will be returned:region: ap-guangzhou
apigw:
serviceId: service-xxxxxx
subDomain: service-xxxxxx-123456789.gz.apigw.tencentcs.com
environment: release
url: https://service-xxxxxx-123456789.gz.apigw.tencentcs.com/release/
scf:
functionName: express-demo-feature1
runtime: Nodejs10.15
namespace: default
lastVersion: $LATEST
traffic: 1
Full details: https://serverless.cloud.tencent.com/instances/expressDemoApp%3Afeature1%3AexpressDemo
10s » expressDemo » Success
At this point, Tom has completed feature development and successfully tested the feature.
Suppose Jorge has also completed feature development and successfully tested the feature. In this example, a feature.html
file is added, and "This is a new feature 2." is written in it.
dev
branch (conflicts may occur and need to be solved manually).dev
. The .env file is configured as follows in the joint testing environment:TENCENT_SECRET_ID=xxxxxxxxxx
TENCENT_SECRET_KEY=xxxxxxxx
STAGE=dev
sls deploy
to deploy the joint testing environment, access the URL (https://service-xxxxxx-123456789.gz.apigw.tencentcs.com/release/feature) as shown below:dev
branch into testing
code to enter the testing stage.TENCENT_SECRET_ID=xxxxxxxxxx
TENCENT_SECRET_KEY=xxxxxxxx
STAGE=testing
sls deploy
succeeds, the testing personnel will carry out relevant tests until the features are stable in the testing.After the test is passed, merge the testing code into the master
branch and prepare for release and launch.
Set the .env file in the production environment as follows:
TENCENT_SECRET_ID=xxxxxxxxxx
TENCENT_SECRET_KEY=xxxxxxxx
STAGE=prod
Run the deployment command:
sls deploy
At this point, a serverless-express
project has been developed and published.
Was this page helpful?