经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
来源:cnblogs  作者:路边两盏灯  时间:2023/1/18 8:43:03  对本文有异议

问题描述

使用Github Action,通过 Azure/functions-container-action@v1 插件来完成 yaml 文件的配置,并成功部署Function Image 的过程记录。

 

操作步骤

第一步: 准备Function的镜像文件

如在VS Code中,通过Terminal(命令行窗口),根据所使用的语言,创建或初始化DockerFile

  1. func init --worker-runtime python --docker
  2. # --docker 选项生成该项目的 Dockerfile,其中定义了适合用于 Azure Functions 和所选运行时的自定义容器 Python

执行后的效果为在Function 项目中添加Dockerfile文件。

参考文档 -- 在 Linux 上使用自定义容器创建函数:https://docs.azure.cn/zh-cn/azure-functions/functions-create-function-linux-custom-image?tabs=in-process%2Cbash%2Cazure-cli&pivots=programming-language-python

第二步:上传镜像到ACR

首先,在本地启动Docker Desktop后,使用Docker build 生产镜像文件。

然后,登录ACR(Azure Container Registry :Azure 容器注册表)。 

命令示例如下:

  1. ## 本地生产Image文件
  2. docker build --tag azurefunctionsimage:v1 .
  3. ## 登录Azure镜像库
  4. docker login <your-registry-name>.azurecr.cn --username <your-registry-username>
  5. ## 设置tag,推送到ACR
  6. docker tag azurefunctionsimage <your-registry-name>.azurecr.cn/azurefunctionsimage:v1
  7. docker push <your-registry-name>.azurecr.cn/azurefunctionsimage:v1

第三步:配置用户标识

启用用户标识,主要就是为了能够让它有权限去访问ACR并且拉取镜像文件

1:创建用户标识:Create User Assigned Managed Identity - Microsoft Azure 由世纪互联运营

2:在ACR中为用户标识赋予权限(Contributor or Reader):分配 Azure 角色的步骤 https://docs.azure.cn/zh-cn/role-based-access-control/role-assignments-steps

 

第四步:配置Github Action的 workflow yaml文件

Action的workflow文件中,有两段内容需要配置,一是设置 用户标识,二是设置镜像路径。

第一段,修改Function App的设置

  1. - name: Azure App Service Settings
  2. uses: Azure/appservice-settings@v1
  3. with:
  4. # Name of the Azure Web App
  5. app-name: fun-name
  6. general-settings-json: '{"acrUseManagedIdentityCreds": "true", "acrUserManagedIdentityID": "user managed identity id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}'

第二段,配置Image 路径

  1. - name: 'Run Azure Functions Container Action'
  2. uses: Azure/functions-container-action@v1
  3. id: fa
  4. with:
  5. app-name: fun-name
  6. image: youracrname.azurecr.cn/imagename:version

以上配置与 Azure Funciton 门户上 Development Center 的设置对比关系如下:

参考的github上yaml文件内容:https://github.com/Azure/actions-workflow-samples/tree/master/FunctionApphttps://github.com/Azure/actions-workflow-samples/blob/master/FunctionApp/linux-container-functionapp-on-azure.yml

修改后的yaml内容:

  1. # Action Requires
  2. # 1. Setup the AZURE_CREDENTIALS secrets in your GitHub Repository
  3. # 2. Setup the REGISTRY_USERNAME secrets in your GitHub Repository
  4. # 3. Setup the REGISTRY_PASSWORD secrets in your GitHub Repository
  5. # 4. Replace REGISTRY, NAMESPACE, IMAGE, TAG in the following template with proper values
  6. # 5. Add this yaml file to your project's .github/workflows/
  7. # 6. Push your local project to your GitHub Repository
  8.  
  9. name: Linux_Container_Workflow
  10.  
  11. on: [push]
  12.  
  13. #on:
  14. # push:
  15. # branches:
  16. # - master
  17.  
  18. jobs:
  19. build-and-deploy:
  20. runs-on: ubuntu-latest
  21. environment: dev
  22. steps:
  23. - name: 'Checkout GitHub Action'
  24. uses: actions/checkout@v3
  25.  
  26. #- name: 'Login via Azure CLI'
  27. # uses: Azure/login@v1.4.6
  28. # with:
  29. # creds: ${{ secrets.AZURE_CREDENTIALS }}
  30. # environment: AzureChinaCloud
  31. # #allow-no-subscriptions: true
  32. - name: 'set subscriptions'
  33. run: |
  34. az cloud set --name AzureChinaCloud
  35. az login -u your azure user name -p "password"
  36. az account set --subscription "your subscription id"
  37.  
  38. - name: 'Docker Login'
  39. uses: azure/docker-login@v1
  40. with:
  41. login-server: youracrname.azurecr.cn
  42. username: ${{ secrets.REGISTRY_USERNAME }}
  43. password: ${{ secrets.REGISTRY_PASSWORD }}
  44.  
  45. # - name: 'Compose Customized Docker Image'
  46. # shell: bash
  47. # run: |
  48. # # If your function app project is not located in your repository's root
  49. # # Please change the path to your directory for docker build
  50. # docker build . -t REGISTRY/NAMESPACE/IMAGE:TAG
  51. # docker push REGISTRY/NAMESPACE/IMAGE:TAG
  52.  
  53.  
  54. - name: Azure App Service Settings
  55. uses: Azure/appservice-settings@v1
  56. with:
  57. # Name of the Azure Web App
  58. app-name: functionappname
  59. general-settings-json: '{"acrUseManagedIdentityCreds": "true", "acrUserManagedIdentityID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}'
  60.  
  61. - name: 'Run Azure Functions Container Action'
  62. uses: Azure/functions-container-action@v1
  63. id: fa
  64. with:
  65. app-name: functionappname
  66. #image: REGISTRY/NAMESPACE/IMAGE:TAG
  67. image: youracrname.azurecr.cn/azurefunctionimage:v1
  68.  
  69. #- name: 'use the published functionapp url in upcoming steps'
  70. # run: |
  71. # echo "${{ steps.fa.outputs.app-url }}"
  72.  
  73. - name: Azure logout
  74. run: |
  75. az logout
  76. # For more information on GitHub Actions:
  77. # https://help.github.com/en/categories/automating-your-workflow-with-github-actions

以上操作完成后,即可上传workflow yaml文件到 .github/workflows/ 目录下。因为条件设置为 on: [push],所以任何对代码库的push操作就会触发该workflow。

 

成功的效果图如本文最开始“问题描述”中的图片一致。

 

在Azure Function的log中,也能发现类似的Container启动日志:

  1. 2023-01-13T03:09:36.682Z INFO - Logging is not enabled for this container.
  2. Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
  3. 2023-01-13T03:09:45.209Z INFO - Initiating warmup request to container funtest01_1_b4054967_msiProxy for site funtest01
  4. 2023-01-13T03:09:45.261Z INFO - Container funtest01_1_b4054967_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
  5. 2023-01-13T03:09:45.268Z INFO - Initiating warmup request to container funtest01_1_b4054967 for site funtest01
  6. 2023-01-13T03:10:01.707Z INFO - Waiting for response to warmup request for container funtest01_1_b4054967. Elapsed time = 16.4981389 sec
  7. 2023-01-13T03:10:13.069Z INFO - Container funtest01_1_b4054967 for site funtest01 initialized successfully and is ready to serve requests.
  8. 2023-01-13T03:10:13.089Z INFO - Initiating warmup request to container funtest01_1_b4054967_middleware for site funtest01
  9. 2023-01-13T03:10:17.032Z INFO - Container funtest01_1_b4054967_middleware for site funtest01 initialized successfully and is ready to serve requests.
  10. 2023-01-13T03:22:40.065Z INFO - Recycling container because of AppSettingsChange and isMainSite = True
  11. 2023-01-13T03:22:55.207Z INFO - Pulling image: mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart
  12. 2023-01-13T03:22:56.079Z INFO - 3.0-appservice-quickstart Pulling from azure-functions/dotnet
  13. 2023-01-13T03:22:56.080Z INFO - Digest: sha256:99f2de1ba2d097fe7fca8098351bd7d9d2e1cabbc32e3c3506321f7f1811bd1b
  14. 2023-01-13T03:22:56.081Z INFO - Status: Image is up to date for mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart
  15. 2023-01-13T03:22:56.084Z INFO - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
  16. 2023-01-13T03:22:56.157Z INFO - Starting container for site
  17. 2023-01-13T03:22:56.165Z INFO - docker run -d --expose=80 --name funtest01_2_24a23a85 -e WEBSITE_CORS_ALLOWED_ORIGINS=https://portal.azure.cn -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=funtest01 -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=funtest01.chinacloudsites.cn -e WEBSITE_INSTANCE_ID=50a285a49ae3758d44951d408c7ec6cb3077821b90868ed2bf52d6c32be391fa -e WEBSITE_USE_DIAGNOSTIC_SERVER=False mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart
  18.  
  19. 2023-01-13T03:22:56.166Z INFO - Logging is not enabled for this container.
  20. Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
  21. 2023-01-13T03:23:10.342Z INFO - Initiating warmup request to container funtest01_2_24a23a85_msiProxy for site funtest01
  22. 2023-01-13T03:23:10.745Z INFO - Container funtest01_2_24a23a85_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
  23. 2023-01-13T03:23:10.753Z INFO - Initiating warmup request to container funtest01_2_24a23a85 for site funtest01
  24. 2023-01-13T03:23:27.483Z INFO - Waiting for response to warmup request for container funtest01_2_24a23a85. Elapsed time = 17.1407378 sec
  25. 2023-01-13T03:23:38.014Z INFO - Container funtest01_2_24a23a85 for site funtest01 initialized successfully and is ready to serve requests.
  26. 2023-01-13T03:23:38.023Z INFO - Initiating warmup request to container funtest01_2_24a23a85_middleware for site funtest01
  27. 2023-01-13T03:23:54.109Z INFO - Container funtest01_2_24a23a85_middleware for site funtest01 initialized successfully and is ready to serve requests.
  28. 2023-01-13T06:14:26.600Z INFO - Recycling container because of AppFrameworkVersionChange and appFrameworkVersion = <youracrname>.azurecr.cn/azurefunctionimage:v1
  29. 2023-01-13T06:14:50.804Z INFO - Pulling image: <youracrname>.azurecr.cn/azurefunctionimage:v1
  30. 2023-01-13T06:14:51.203Z INFO - v1 Pulling from azurefunctionimage
  31. 2023-01-13T06:14:51.228Z INFO - 3f4ca61aafcd Pulling fs layer
  32. 2023-01-13T06:14:51.233Z INFO - 3f487a3359db Pulling fs layer
  33. 2023-01-13T06:14:51.233Z INFO - cf20d7997674 Pulling fs layer
  34. 2023-01-13T06:14:51.234Z INFO - 8fa944797ac7 Pulling fs layer
  35. 2023-01-13T06:14:51.234Z INFO - 268581bec5af Pulling fs layer
  36. 2023-01-13T06:14:51.235Z INFO - 320a9b97d2ed Pulling fs layer
  37. 2023-01-13T06:14:51.235Z INFO - 14bf15bf0e2a Pulling fs layer
  38. 2023-01-13T06:14:51.235Z INFO - 888c871585b1 Pulling fs layer
  39. 2023-01-13T06:14:51.243Z INFO - dc54e8c78a21 Pulling fs layer
  40. 2023-01-13T06:14:51.244Z INFO - 0b8d318d756a Pulling fs layer
  41. 2023-01-13T06:14:51.244Z INFO - 686f382362d7 Pulling fs layer
  42. 2023-01-13T06:14:51.252Z INFO - a108b4c555c7 Pulling fs layer
  43. 2023-01-13T06:14:51.253Z INFO - 07a70c22a7c4 Pulling fs layer
  44. 2023-01-13T06:14:52.512Z INFO - 3f487a3359db Downloading 799KB / 1MB
  45. ...
  46. 2023-01-13T06:17:09.734Z INFO - 07a70c22a7c4 Extracting 9MB / 9MB
  47. 2023-01-13T06:17:09.938Z INFO - 07a70c22a7c4 Pull complete
  48. 2023-01-13T06:17:09.955Z INFO - Digest: sha256:26a409b16044e27bdd97627a14118e33e84f840052d9fe4711f1ca471b09d22b
  49. 2023-01-13T06:17:09.957Z INFO - Status: Downloaded newer image for <youracrname>.azurecr.cn/azurefunctionimage:v1
  50. 2023-01-13T06:17:10.056Z INFO - Pull Image successful, Time taken: 2 Minutes and 19 Seconds
  51. 2023-01-13T06:17:10.688Z INFO - Starting container for site
  52. 2023-01-13T06:17:10.699Z INFO - docker run -d --expose=80 --name funtest01_3_e9514d82 -e WEBSITE_CORS_ALLOWED_ORIGINS=https://portal.azure.cn -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=funtest01 -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=funtest01.chinacloudsites.cn -e WEBSITE_INSTANCE_ID=50a285a49ae3758d44951d408c7ec6cb3077821b90868ed2bf52d6c32be391fa -e WEBSITE_USE_DIAGNOSTIC_SERVER=False <youracrname>.azurecr.cn/azurefunctionimage:v1
  53.  
  54. 2023-01-13T06:17:10.707Z INFO - Logging is not enabled for this container.
  55. Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
  56. 2023-01-13T06:17:20.451Z INFO - Initiating warmup request to container funtest01_3_e9514d82_msiProxy for site funtest01
  57. 2023-01-13T06:17:20.721Z INFO - Container funtest01_3_e9514d82_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
  58. 2023-01-13T06:17:20.722Z INFO - Initiating warmup request to container funtest01_3_e9514d82 for site funtest01
  59. 2023-01-13T06:17:36.951Z INFO - Waiting for response to warmup request for container funtest01_3_e9514d82. Elapsed time = 16.4996091 sec
  60. 2023-01-13T06:17:44.426Z INFO - Container funtest01_3_e9514d82 for site funtest01 initialized successfully and is ready to serve requests.
  61. 2023-01-13T06:17:44.427Z INFO - Initiating warmup request to container funtest01_3_e9514d82_middleware for site funtest01
  62. 2023-01-13T06:17:45.431Z INFO - Container funtest01_3_e9514d82_middleware for site funtest01 initialized successfully and is ready to serve requests.

 

 

参考资料

Action Samples for deploying to Azure Functions :https://github.com/Azure/actions-workflow-samples/tree/master/FunctionApp

 
 
【END】

原文链接:https://www.cnblogs.com/lulight/p/17057797.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号