当前位置:主页 > 查看内容

只更新代码,然后发布版本:基于 Serverless Devs 原子化操作阿

发布时间:2021-07-10 00:00| 位朋友查看

简介:作者 | Anycodes 众所周知 随着时间的发展 Serverless 命令行工具也逐渐的玩出了更多的花样 就目前来看 常见的形态有两种 一种是通过 Yaml 来进行资源的描述 另外一种是纯粹的命令行操作 而不依赖这些内容。 第一种通过 Yaml 来进行资源描述 其好处不言而喻……


image

作者 | Anycodes

众所周知 随着时间的发展 Serverless 命令行工具也逐渐的玩出了更多的花样 就目前来看 常见的形态有两种 一种是通过 Yaml 来进行资源的描述 另外一种是纯粹的命令行操作 而不依赖这些内容。

第一种通过 Yaml 来进行资源描述 其好处不言而喻 目前主流的 Serverless 开发者工具均是类似的模式 例如阿里云的 Funcraft 著名的开源项目 Serverless Framework 等 通过 Yaml 使用者可以通过简单的命令 进行复杂的操作 例如开发者在 Yaml 中描述好服务、函数等配置 描述好代码位置 只需要 deploy 就可以将本地项目部署到线上 非常方便。但是这里有一个非常明显的劣势 在很多时候我们的企业管理者 给每个人分配的权限是固定的 例如运维人员只能更新某些内容 开发人员只能更新某些代码 某些负责可以发布版本等 那么这个时候”一把梭”的行为就显得非常尴尬 想为开发者做更多 但是有些开发者不需要你做更多 那么”高阶能力”和”原子能力”的平衡就显得至关重要的。

第二种模式 虽然是不需要依赖 Yaml 在很多时候使用起来可能会稍微复杂一些 例如我们创建一个函数可能涉及到很多流程 创建服务 创建函数 创建触发器… 相对比上面所说的一条指令而言 确实复杂很多 但是这种无 Yaml 的模式 更适合做原子操作 可以最大程度解决上述问题 同时这种做法也可以在一定程度上进行更多的拓展 例如某些本不需要依赖 Yaml 的行为 查询服务列表 查询函数列表……

所以这两种模式各有优缺点 我们在使用的时候完全可以组合来使用 达到最大的一个生产效能。那么一个新问题来了 以阿里云函数计算为例 如何同时拥有这两种模式的使用方法呢

其实 Serverless Devs 天然支持 Yaml 描述和非 Yaml 描述的能力 例如阿里云函数计算的 FC 组件就是一个可以依靠 Yaml 描述进行资源操作的组件 而 FC-API 组件则是 API 相关的原子性操作。

本文将会以这样一个案例/场景为例 为读者介绍这两者的使用方法

1.通过 Serverless Devs 快速创建一个服务/函数/触发器

2.通过无 Yaml 的模式对其中的代码部分进行单独的更新

3.更新之后发布一个版本

4.通过 Git Github Action 实现一个代码自动化发布和版本自动化发布的能力

快速创建函数

我们只需要通过s init并且选择阿里云函数计算的 Python3 Http 函数即可

image

创建完成之后 我们只需要进入到对应的文件夹 并且执行s deploy 即可将项目快速部署到线上。在进入到项目后 我们可以在项目下看到一个s.yaml的文件 这个文件就是资源描述文件
image

其完整的描述

https://github.com/devsapp/fc/blob/main/docs/Others/yaml.md

此时我们可以通过s deploy进行项目的部署
image

部署完成 我们可以打开系统分配给我们的域名 我们可以看到内容

image

通过无 Yaml 模式更新函数

我们可以编辑index.py 将Hello world!变为Hello world Serverless Devs!

image

然后我们就要接触一个新的组件 FC-API

https://github.com/devsapp/fc-api

我们可以执行帮助文档 s cli fc-api -h
image

此时我们需要明确的是 当我们执行s cli的时候 系统就不去读 Yaml 而直接进行相关方法的调用。

如果我们对这个方法还是不清楚 我们可以 s cli fc-api updateFunction -h

image

此时我们只需要按照规范 填写好地区 服务名 函数名 以及要更新的字段即可

s cli fc-api updateFunction --region cn-hangzhou --serviceName fc-deploy-service --functionName http-trigger-function--code { zipFile : ./ }

完成之后 我们可以再去看一下之前的页面是否同步更新了
image

此处可能有疑问 你的帮助文档写的是 --code string [JSON String] The code of the function. The code must be packaged into a ZIP file.
你是怎么知道传递--code { zipFile : ./ } 的

因为在我们看帮助文档的时候 题已经提醒了我们这是一个 JSON String 同时在帮助文档最上面是有链接地址


Usage

?s cli fc-api updateFunction

? API Document: https://help.aliyun.com/document_detail/189986.html


Options


--region stringThe region of fc endpoint. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

--access stringSpecify the key name. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

--props stringThe json string of props. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

--serviceName stringThe name of the service. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

--functionName stringThe description of the function. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

--code string[JSON String]The code of the function.The code must be packaged into a ZIP file.

此时 我们可以打开 https://help.aliyun.com/document_detail/189986.html


![image](https://user-images.githubusercontent.com/21079031/124550239-f5fb9900-de62-11eb-819b-9e662cb80fe6.png)


![image](https://user-images.githubusercontent.com/21079031/124550302-0ca1f000-de63-11eb-974e-9453449e525b.png)


此时为了方便 Serverless devs 支持本地路径 会帮助你进行打包等操作。


当然 我们还可以更刺激一些 修改其他内容 如单纯修改一些 timeout:

s cli fc-api updateFunction —region cn-hangzhou —serviceName fc-deploy-service —functionName http-trigger-function —timeout 70

![image](https://user-images.githubusercontent.com/21079031/124550447-3bb86180-de63-11eb-836a-01d102a6eab9.png)


## 通过无 Yaml 模式发布版本


和上面一样 我们可以用 s cli fc-api -h 查看一下版本发布的方法 s cli fc-api publishVersion -h


![image](https://user-images.githubusercontent.com/21079031/124550575-715d4a80-de63-11eb-8182-bd154507e19d.png)


尝试拼接参数

s cli fc-api publishVersion —region cn-hangzhou —serviceName fc-deploy-service —description “This is a test version”

得到结果


![image](https://user-images.githubusercontent.com/21079031/124550685-9ce03500-de63-11eb-95bf-ed59c494fd7d.png)



## CI/CD 组件的使用


当我们想要把上面只更新代码 发布版本的能力集成到 CI/CD 或者某些自动化流程中 如何操作呢


以 GithubAction 为例 我们可以直接执行 s cli cicd :


![image](https://user-images.githubusercontent.com/21079031/124550942-07917080-de64-11eb-8b57-d59eba0cdc47.png)


接下来 我们对 ./.github/workflow/serverless-devs.yml 进行自定义编辑


yaml

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

? ? ? # 默认密钥配置指令是阿里云密钥配置指令 更多可以参考

? ? ? # 如何通过 Github Action使用Serverless Devs 做 CI/CD http://short.devsapp.cn/cicd/github/action/usage

? ? ? # Serverless Devs 的官网是通过 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 cli fc-api updateFunction --region cn-hangzhou --serviceName fc-deploy-service --functionName http-trigger-function --code { zipFile : ./ }

? ? ? - run: s cli fc-api publishVersion --region cn-hangzhou --serviceName fc-deploy-service


我们只是在最后加了两个人 run 一个是发布代码 一个是发布版本 此时我们可以创建一个 Github 仓库 尝试一下
image

创建完成之后 我们可以按照案例提醒 进行密钥的配置

# 默认密钥配置指令是阿里云密钥配置指令 更多可以参考

# 如何通过 Github Action 使用 Serverless Devs 做 CI/CD http://short.devsapp.cn/cicd/github/action/usage

# Serverless Devs 的官网是通过 Serverless Devs 部署的: http://short.devsapp.cn/cicd/github/action/practice

image

image

接下来 我们通过 git init 等一系列指令 完成代码推到仓库

image

此时 我们再次修改代码

image

修改完成之后 我们将代码 push 到测试仓库 可以看到 我们在 Action 中可以看到一个 workflow 在执行

image

稍等片刻 当这个流程完成

image

我们打开之前的页面 可以看到 网页内容已经顺利被更新

image

总结

本文以阿里云为例 通过在 Github 上使用 Servelress Devs 单纯对代码进行更新 并进行版本发布 该流程是比较常见的 也是比较通用的 希望读者可以发挥想象力 将这个流程应用到自己的项目中。

(文章转载自 Go Serverless

Serverless Devs 参与的贡献

Serverless Devs 的开源为国内外开发者提供了 Serverless 工具的新选择 让开发者可以以更短的路径体验到多云 Serverless 产品 以更快的速度创建和部署 Serverless 应用 以更简单和更自动化的方法进行项目管理/运维 未来期待更多开发者参与共建。

目前代码已经在 Gitee(码云 和 GitHub 上正式开放

Github 地址 https://github.com/serverless-devsGitee 地址 https://gitee.com/organizations/serverless-devs/projectsServerless Devs 官网 https://www.serverless-devs.com




本文转自网络,原文链接:https://developer.aliyun.com/article/785162
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:闲鱼同城第三方对接架构设计 下一篇:没有了

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐