{"id":17337352,"url":"https://github.com/anycodes/fc-update-functioncode-demo","last_synced_at":"2025-03-27T08:11:52.510Z","repository":{"id":144049663,"uuid":"383360180","full_name":"anycodes/FC-Update-FunctionCode-Demo","owner":"anycodes","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-17T06:45:15.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T13:11:13.359Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anycodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-06T06:14:47.000Z","updated_at":"2021-07-17T06:45:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"8da3d80d-8785-4f19-a9b7-de5a7f37d02f","html_url":"https://github.com/anycodes/FC-Update-FunctionCode-Demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2FFC-Update-FunctionCode-Demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2FFC-Update-FunctionCode-Demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2FFC-Update-FunctionCode-Demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2FFC-Update-FunctionCode-Demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anycodes","download_url":"https://codeload.github.com/anycodes/FC-Update-FunctionCode-Demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245806458,"owners_count":20675298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T15:34:44.132Z","updated_at":"2025-03-27T08:11:52.490Z","avatar_url":"https://github.com/anycodes.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 基于Serverless Devs原子化操作阿里云函数计算\n\n众所周知，随着时间的发展，Serverless命令行工具也逐渐的玩出了更多的花样，就目前来看，常见的形态有两种，一种是通过Yaml来进行资源的描述，另外一种是纯粹的命令行操作，而不依赖这些内容。\n\n第一种通过Yaml来进行资源描述，其好处不言而喻，目前主流的Serverless开发者工具均是类似的模式，例如阿里云的Funcraft，著名的开源项目Serverless Framework等，通过Yaml，使用者可以通过简单的命令，进行复杂的操作，例如开发者在Yaml中描述好服务、函数等配置，描述好代码位置，只需要deploy就可以将本地项目部署到线上，非常方便。但是这里有一个非常明显的劣势，在很多时候我们的企业管理者，给每个人分配的权限是固定的，例如运维人员只能更新某些内容，开发人员只能更新某些代码，某些负责可以发布版本等，那么这个时候\"一把梭\"的行为就显得非常尴尬，想为开发者做更多，但是有些开发者不需要你做更多，那么\"高阶能力\"和\"原子能力\"的平衡就显得至关重要的。\n\n第二种模式，虽然是不需要依赖Yaml，在很多时候使用起来可能会稍微复杂一些，例如我们创建一个函数可能涉及到很多流程：创建服务，创建函数，创建触发器...，相对比上面所说的一条指令而言，确实复杂很多，但是这种无Yaml的模式，更适合做原子操作，可以最大程度解决上述问题，同时这种做法也可以在一定程度上进行更多的拓展，例如某些本不需要依赖Yaml的行为：查询服务列表，查询函数列表......\n\n所以这两种模式各有优缺点，我们在使用的时候完全可以组合来使用，达到最大的一个生产效能。那么一个新问题来了，以阿里云函数计算为例，如何同时拥有这两种模式的使用方法呢？\n\n其实Serverless Devs天然支持Yaml描述和非Yaml描述的能力，例如阿里云函数计算的FC组件就是一个可以依靠Yaml描述进行资源操作的组件，而FC-API组件则是API相关的原子性操作。\n\n本文将会以这样一个案例/场景为例，为读者介绍这两者的使用方法：\n\n1. 通过Serverless Devs快速创建一个服务/函数/触发器\n2. 通过无Yaml的模式对其中的代码部分进行单独的更新\n3. 更新之后发布一个版本\n4. 通过Git+Github Action实现一个代码自动化发布和版本自动化发布的能力\n\n## 快速创建函数\n\n我们只需要通过`s init`并且选择阿里云函数计算的Python3 Http函数即可：\n\n\n![image](https://user-images.githubusercontent.com/21079031/124547503-d2365400-de5e-11eb-9044-bf9c9a5ccd01.png)\n\n创建完成之后，我们只需要进入到对应的文件夹，并且执行`s deploy`，即可将项目快速部署到线上。在进入到项目后，我们可以在项目下看到一个`s.yaml`的文件，这个文件就是资源描述文件：\n\n\n![image](https://user-images.githubusercontent.com/21079031/124547911-7fa96780-de5f-11eb-91d1-bf1a2607c8f3.png)\n\n其完整的描述：https://github.com/devsapp/fc/blob/main/docs/Others/yaml.md\n\n此时我们可以通过`s deploy`进行项目的部署：\n\n\n![image](https://user-images.githubusercontent.com/21079031/124548730-b16efe00-de60-11eb-8b0f-c58cefb70053.png)\n\n\n\n部署完成，我们可以打开系统分配给我们的域名，我们可以看到内容：\n\n![image](https://user-images.githubusercontent.com/21079031/124548769-c0ee4700-de60-11eb-8be2-273b079abf72.png)\n\n\n## 通过无Yaml模式更新函数\n\n此时，我们可以编辑`index.py`，将`Hello world!`变为`Hello world Serverless Devs!`\n\n![image](https://user-images.githubusercontent.com/21079031/124549589-f2b3dd80-de61-11eb-9156-a18d1bf09863.png)\n\n然后我们就要接触一个新的组件FC-API：https://github.com/devsapp/fc-api\n\n我们可以执行帮助文档：`s cli fc-api -h`：\n\n![image](https://user-images.githubusercontent.com/21079031/124549126-4d990500-de61-11eb-94fb-cf94bb365135.png)\n\n此时我们需要明确的是，当我们执行`s cli`的时候，系统就不去读Yaml，而直接进行相关方法的调用。\n\n如果我们对这个方法还是不清楚，我们可以：`s cli fc-api updateFunction -h`\n\n![image](https://user-images.githubusercontent.com/21079031/124549293-8d5fec80-de61-11eb-9f75-31c66070b8fa.png)\n\n此时我们只需要按照规范，填写好地区，服务名，函数名，以及要更新的字段即可：\n\n```\ns cli fc-api updateFunction --region cn-hangzhou --serviceName fc-deploy-service --functionName http-trigger-function --code '{\"zipFile\": \"./\"}'\n```\n\n完成之后，我们可以再去看一下之前的页面是否同步更新了：\n\n![image](https://user-images.githubusercontent.com/21079031/124550008-9ac9a680-de62-11eb-9701-54c4305123e9.png)\n\n\u003e 此处可能有疑问，你的帮助文档写的是：`--code string                    [JSON String] The code of the function. The code must be packaged into a ZIP file. `\n\u003e 你是怎么知道传递`--code '{\"zipFile\": \"./\"}'`的？\n\u003e\n\u003e 因为在我们看帮助文档的时候，题已经提醒了我们这是一个JSON String，同时在帮助文档最上面是有链接地址：\n\n\n```\n    Usage\n    \n      s cli fc-api updateFunction                                                   \n      API Document: https://help.aliyun.com/document_detail/189986.html             \n    \n    Options\n    \n      --region string                  The region of fc endpoint.                                                    \n      --access string                  Specify the key name.                                                         \n      --props string                   The json string of props.                                                     \n      --serviceName string             The name of the service.                                                      \n      --functionName string            The description of the function.                                              \n      --code string                    [JSON String] The code of the function. The code must be packaged into a ZIP file.                                        \n```\n\n\u003e 此时，我们可以打开https://help.aliyun.com/document_detail/189986.html：\n\n![image](https://user-images.githubusercontent.com/21079031/124550239-f5fb9900-de62-11eb-819b-9e662cb80fe6.png)\n\n![image](https://user-images.githubusercontent.com/21079031/124550302-0ca1f000-de63-11eb-974e-9453449e525b.png)\n\n\u003e 此时为了方便，Serverless devs支持本地路径，会帮助你进行打包等操作。\n\n当然，我们还可以更刺激一些，修改其他内容，例如单纯修改一些timeout:\n\n```\n s cli fc-api updateFunction --region cn-hangzhou --serviceName fc-deploy-service --functionName http-trigger-function --timeout 70 \n```\n\n![image](https://user-images.githubusercontent.com/21079031/124550447-3bb86180-de63-11eb-836a-01d102a6eab9.png)\n\n## 通过无Yaml模式发布版本\n\n和上面一样，我们可以用`s cli fc-api -h `查看一下版本发布的方法：`s cli fc-api publishVersion -h`\n\n![image](https://user-images.githubusercontent.com/21079031/124550575-715d4a80-de63-11eb-8182-bd154507e19d.png)\n\n尝试拼接参数：\n\n```\ns cli fc-api publishVersion --region cn-hangzhou --serviceName fc-deploy-service --description \"This is a test version\"\n```\n\n得到结果：\n\n![image](https://user-images.githubusercontent.com/21079031/124550685-9ce03500-de63-11eb-95bf-ed59c494fd7d.png)\n\n\n## CI/CD组件的使用\n\n当我们想要把上面只更新代码，发布版本的能力集成到CI/CD，或者某些自动化流程中，如何操作呢？\n\n以Github Action为例，我们可以直接执行`s cli cicd`:\n\n![image](https://user-images.githubusercontent.com/21079031/124550942-07917080-de64-11eb-8b57-d59eba0cdc47.png)\n\n接下来，我们对`./.github/workflow/serverless-devs.yml`进行自定义编辑：\n\n```yaml\nname: Serverless Devs Project CI/CD\n\non:\n  push:\n    branches: [ master ]\n\njobs:\n  serverless-devs-cd:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v2\n      - uses: actions/setup-node@v2\n        with:\n          node-version: 12\n          registry-url: https://registry.npmjs.org/\n      - run: npm install\n      - run: npm install -g @serverless-devs/s\n      # 默认密钥配置指令是阿里云密钥配置指令，更多可以参考：\n      # 如何通过Github Action使用Serverless Devs做CI/CD：http://short.devsapp.cn/cicd/github/action/usage\n      # Serverless Devs的官网是通过Serverless Devs部署的: http://short.devsapp.cn/cicd/github/action/practice\n      - run: s config add --AccountID ${{secrets.AccountID}} --AccessKeyID ${{secrets.AccessKeyID}} --AccessKeySecret ${{secrets.AccessKeySecret}} -a default\n      - run: s cli fc-api updateFunction --region cn-hangzhou --serviceName fc-deploy-service --functionName http-trigger-function --code '{\"zipFile\":\"./\"}'\n      - run: s cli fc-api publishVersion --region cn-hangzhou --serviceName fc-deploy-service\n```\n\n其实，我们只是在最后加了两个人run，一个是发布代码，一个是发布版本，此时我们可以创建一个Github仓库，尝试一下：\n\n![image](https://user-images.githubusercontent.com/21079031/124551282-7c64aa80-de64-11eb-91e4-bcb97156cfde.png)\n\n创建完成之后，我们可以按照案例提醒，进行密钥的配置：\n\n```\n# 默认密钥配置指令是阿里云密钥配置指令，更多可以参考：\n# 如何通过Github Action使用Serverless Devs做CI/CD：http://short.devsapp.cn/cicd/github/action/usage\n# Serverless Devs的官网是通过Serverless Devs部署的: http://short.devsapp.cn/cicd/github/action/practice\n```\n\n![image](https://user-images.githubusercontent.com/21079031/124551345-8d152080-de64-11eb-82e5-b2c26194501a.png)\n\n\n![image](https://user-images.githubusercontent.com/21079031/124551702-20e6ec80-de65-11eb-9ed9-9cf20b2ec112.png)\n\n\n接下来， 我们通过git init等一系列指令，完成代码推到仓库：\n\n![image](https://user-images.githubusercontent.com/21079031/124551849-51c72180-de65-11eb-89da-8c6608718b95.png)\n\n此时，我们再次修改代码：\n\n![image](https://user-images.githubusercontent.com/21079031/124552138-b6827c00-de65-11eb-9f8a-8ab8aff63437.png)\n\n修改完成之后，我们将代码push到测试仓库，可以看到，我们在Action中可以看到一个workflow在执行：：\n\n![image](https://user-images.githubusercontent.com/21079031/124552227-d44fe100-de65-11eb-9acc-8eb9a66312a4.png)\n\n稍等片刻，当这个流程完成：\n\n![image](https://user-images.githubusercontent.com/21079031/124552662-6c4dca80-de66-11eb-9943-256ea78eabab.png)\n\n我们打开之前的页面，可以看到，网页内容已经顺利被更新：\n\n![image](https://user-images.githubusercontent.com/21079031/124552637-648e2600-de66-11eb-9df2-ee23e41d693d.png)\n\n# 总结\n\n本文以阿里云为例，通过在Github上使用Servelress devs单纯对代码进行更新，并进行版本发布，该流程是比较常见的，也是比较通用的，希望读者可以发挥想象力，将这个流程应用到自己的项目中。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanycodes%2Ffc-update-functioncode-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanycodes%2Ffc-update-functioncode-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanycodes%2Ffc-update-functioncode-demo/lists"}