作者:鄧超 Serverless Devs 開源貢獻者
背景
我們在上文 [Aliyun] [FC] 如何使用 @serverless-devs/s 部署靜態網站到函式計算 中,詳細的介紹了如何通過 @serverless-devs/s 將已經開發好了的靜態網站部署到阿里云函式計算(FC)上, 但是近期函式計算和 @serverless-devs/s 都更新了一系列的功能, 目前部署靜態網站的步驟可以更為簡潔了!
使用 website-fc 插件部署靜態網站到 Custom Runtime 函式
假設我們現在有如下結構的前端工程:
/
├ dist/ 待部署的構建產物
│ └ index.html
├ src/
└ package.json
step 3.安裝 @serverless-devs/s 并撰寫 s.yaml
你問我步驟 1 和 2 去哪兒了? 當然是省掉了!
添加 @serverless-devs/s 命令列工具到工程:

然后在根目錄下創建一個基礎的 s.yaml 組態檔:
# https://github.com/devsapp/fc/blob/main/docs/zh/yaml/
edition: 1.0.0
name: my-awesome-website-project
services:
my-service: # 任意的名稱
actions:
pre-deploy:
- plugin: website-fc # 在 pre-deploy 插槽中安裝 website-fc 插件
component: devsapp/fc # 使用 fc 組件
props:
region: cn-shenzhen # 部署到任意的可用區, 例如深圳.
service:
name: my-awesome-websites # 深圳可用區的 my-awesome-websites 服務
function:
name: website-fc-plugin # my-awesome-websites 服務下的一個函式
runtime: custom # 使用 custom 運行環境
handler: dummy-handler # 由于使用了 custom 運行環境, 所以這里可以隨便填
codeUri: ./dist # 部署 dist 檔案夾下的全部內容
triggers:
- name: http
type: http # 創建一個 HTTP 型別的觸發器, 以便客戶端可以通過 HTTP 協議進行訪問
config:
authType: anonymous # 允許匿名訪問
methods: [ HEAD, GET ] # 靜態網站只需要處理 HEAD 和 GET 請求就夠了
與上文中不同的地方在于:
actions:
pre-deploy:
- plugin: website-fc # 在 pre-deploy 插槽中安裝 website-fc 插件
在 pre-deploy 插槽中安裝的 website-fc 插件能代替上文中的步驟 1 和步驟 2;
以及:

現在不必將整個工程部署到函式中, 只需要部署構建好的靜態檔案了,
step 4.部署到函式計算
配置好 AccessKey 和 AccessSecret 后(opens new window), 詳情參考:
https://www.serverless-devs.com/serverless-devs/command/config,執行命令:

你的網站就部署上去啦,
接下來就是配置自定義域名了, 配置好以后就可以通過你自己的域名訪問到這個網站了,
step 5. 配置自定義域名
以自定義域名 deploy-static-website-with-website-fc-plugin.example.dengchao.fun 為例,
首先添加 CNAME 記錄, 決議值填寫 ${UID}.${REGION}.fc.aliyuncs.com,因為我們的 s.yaml 中設定的 region 是 cn-shenzhen, 所以對應的值就是 xxxxxx.cn-shenzhen.fc.aliyuncs.com ,

接下來設定函式計算控制臺上的自定義域名:

訪問一下試試看: http://deploy-static-website-with-website-fc-plugin.example.dengchao.fun(opens new window)
樣本工程
本文中的樣本工程已經上傳到 GitHub:
https://github.com/DevDengChao/deploy-static-website-with-website-fc-plugin-example(opens new window)
參考
[1] 阿里云函式計算-產品簡介(opens new window)
https://help.aliyun.com/document_detail/52895.html
[2] 資源使用限制(opens new window)
https://help.aliyun.com/document_detail/51907.html
[3] 自定義運行環境(opens new window)
https://help.aliyun.com/document_detail/132044.html
[4] 配置自定義域名(opens new window)
https://help.aliyun.com/document_detail/90763.html
[5] Serverless devs 官網(opens new window)
https://www.serverless-devs.com/
[6] 配置 AccessKey 和 AccessSecret(opens new window)
https://www.serverless-devs.com/serverless-devs/command/config
[7] website-fc 插件
https://github.com/devsapp/website-fc
點擊此處,了解 Serverless Devs 官網更多資訊!
發布云原生技術最新資訊、匯集云原生技術最全內容,定期舉辦云原生活動、直播,阿里產品及用戶最佳實踐發布,與你并肩探索云原生技術點滴,分享你需要的云原生內容,
關注【阿里巴巴云原生】公眾號,獲取更多云原生實時資訊!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/458126.html
標籤:其他
上一篇:重學邏輯回歸【學習筆記】
下一篇:機械外骨骼中的恒力懸浮背包研究
