快速开始
通过 Serverless Website 组件快速构建一个 Serverless Hexo 站点
1. 安装
安装前提:
如您未安装上述应用程序,可以参考排Hexo 安装说明。
安装 Serverless Framework
$ npm install -g serverless
安装 Hexo
$ npm install -g hexo-cli
安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件。
$ hexo init hexo # 生成hexo目录
$ cd hexo
$ npm install
新建完成后,指定文件夹的目录如下:
.
├── _config.yml
├── package.json
├── scaffolds
├── source
| ├── _drafts
| └── _posts
└── themes
安装完成后,可以通过hexo g
命令生成静态页面
$ hexo g # generate
注:如果希望在本地查看效果,也可以运行下列命令,通过浏览器访问
localhost:4000
查看页面效果。
$ hexo s # server
2. 配置
在hexo
目录下,创建serverless.yml
文件,在其中进行如下配置
$ touch serverless.yml
# serverless.yml
myWebsite:
component: '@serverless/tencent-website'
inputs:
code:
src: ./localhexo/public # Upload static files generated by HEXO
index: index.html
error: index.html
region: ap-guangzhou
bucketName: my-bucket
配置完成后,文件目录如下:
.
├── .serverless
├── hexo
| ├── public
| ├── ...
| ├── _config.yml
| ├── ...
| └── source
└── serverless.yml
3. 部署
通过sls
命令进行部署,并可以添加—debug
参数查看部署过程中的信息
如您的账号未登陆或注册腾讯云,您可以直接通过微信
扫描命令行中的二维码进行授权登陆和注册。
$ serverless --debug
DEBUG ─ Resolving the template's static variables.
DEBUG ─ Collecting components from the template.
DEBUG ─ Downloading any NPM components found in the template.
DEBUG ─ Analyzing the template's components dependencies.
DEBUG ─ Creating the template's components graph.
DEBUG ─ Syncing template state.
DEBUG ─ Executing the template's components graph.
DEBUG ─ Starting Website Component.
Please scan QR code login from wechat
Wait login...
Login successful for TencentCloud
DEBUG ─ Preparing website Tencent COS bucket my-bucket-1250000000.
DEBUG ─ Deploying "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
DEBUG ─ "my-bucket-1250000000" bucket was successfully deployed to the "ap-guangzhou" region.
DEBUG ─ Setting ACL for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
DEBUG ─ Ensuring no CORS are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
DEBUG ─ Ensuring no Tags are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
DEBUG ─ Configuring bucket my-bucket-1250000000 for website hosting.
DEBUG ─ Uploading website files from D:\hexotina\localhexo\public to bucket my-bucket-1250000000.
DEBUG ─ Starting upload to bucket my-bucket-1250000000 in region ap-guangzhou
DEBUG ─ Uploading directory D:\hexotina\localhexo\public to bucket my-bucket-1250000000
DEBUG ─ Website deployed successfully to URL: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com.
myWebsite:
url: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com
env:
13s » myWebsite » done
访问命令行输出的 website url,即可查看您的 Serverless Hexo 站点
注:如果希望更新 hexo 站点中的文章,需要在本地重新运行
hexo g
进行生成静态页面,再运行serverless
更新到页面
4. 移除
可以通过以下命令移除 hexo 网站
$ sls remove --debug
DEBUG ─ Flushing template state and removing all components.
DEBUG ─ Starting Website Removal.
DEBUG ─ Removing Website bucket.
DEBUG ─ Removing files from the "my-bucket-1250000000" bucket.
DEBUG ─ Removing "my-bucket-1250000000" bucket from the "ap-guangzhou" region.
DEBUG ─ "my-bucket-1250000000" bucket was successfully removed from the "ap-guangzhou" region.
DEBUG ─ Finished Website Removal.
6s » myWebsite » done
账号配置(可选)
当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 .env
文件
$ touch .env # 腾讯云的配置信息
在 .env
文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存
如果没有腾讯云账号,可以在此注册新账号。
如果已有腾讯云账号,可以在API 密钥管理中获取 SecretId
和SecretKey
.
# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123
当前内容版权归 Serverless官网 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Serverless官网 .