简介
serverless-plugin-scripts 是一个 npm 包,可以帮助开发者在 serverless 构架中方便地运行脚本,包括在 pipeline 中运行 bash 脚本、在 build 中自动化执行命令行命令等。
本文将介绍如何快速安装 serverless-plugin-scripts,并给出一些深度的使用案例。
安装
使用 npm 包管理器,执行以下命令即可安装 serverless-plugin-scripts:
npm install --save-dev serverless-plugin-scripts
使用方法
- 在项目的
serverless.yml文件中,需要添加以下内容:
plugins: - serverless-plugin-scripts
- 接下来,就可以在 serverless 中使用该插件了。
案例
在 pipeline 中运行 bash 脚本
在使用了 serverless-plugin-scripts 后,我们可以很方便地在 pipeline 中运行 bash 脚本。
plugins:
- serverless-plugin-scripts
custom:
scripts:
- script: "sh ./deploy.sh"
lifecycleEvents:
- deploy在上述示例代码中,我们定义了在 deploy 生命周期事件中,运行 deploy.sh 脚本。这样,我们就能方便地在 pipeline 中执行 bash 脚本了。
在 build 中自动化执行命令行命令
serverless-plugin-scripts 也可以用于在 build 过程中自动化执行命令行命令。下面给出一个使用案例。
plugins:
- serverless-plugin-scripts
custom:
scripts:
- script: yarn install
action: exec
cwd: ./app在上述示例代码中,我们定义了在 build 过程中,在 app 目录下执行 yarn install 命令。这样,我们就能快速地自动化构建 serverless 应用了。
总结
使用 serverless-plugin-scripts,可以方便地在 serverless 构架中运行 bash 脚本,自动化执行命令行命令等。本文介绍了如何安装和使用该 npm 包,并给出了一些深度的使用案例。希望此文能帮助到更多前端开发者。
Source: FunTeaLearn,Please indicate the source for reprints https://funteas.com/post/6005514a81e8991b448ce56f