【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image

问题描述

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

 

操作步骤

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

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

func init --worker-runtime python --docker


# --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 容器注册表)。

命令示例如下:

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

参考文档 -- 向 Azure 容器注册表推送映像 : https://docs.azure.cn/zh-cn/app-service/tutorial-custom-container?pivots=container-linux#push-the-image-to-azure-container-registry

 

第三步:配置用户标识

启用用户标识,主要就是为了能够让它有权限去访问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的设置

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

第二段,配置Image 路径

- name: 'Run Azure Functions Container Action'
      uses: Azure/functions-container-action@v1
      id: fa
      with:
        app-name: fun-name
        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内容:

# Action Requires
# 1. Setup the AZURE_CREDENTIALS secrets in your GitHub Repository
# 2. Setup the REGISTRY_USERNAME secrets in your GitHub Repository
# 3. Setup the REGISTRY_PASSWORD secrets in your GitHub Repository
# 4. Replace REGISTRY, NAMESPACE, IMAGE, TAG in the following template with proper values
# 5. Add this yaml file to your project's .github/workflows/
# 6. Push your local project to your GitHub Repository
name: Linux_Container_Workflow
on: [push]
#on:
#  push:
#    branches:
#    - master
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    environment: dev
    steps:
    - name: 'Checkout GitHub Action'
      uses: actions/checkout@v3
    #- name: 'Login via Azure CLI'
    #  uses: Azure/login@v1.4.6
    #  with:
    #    creds: ${{ secrets.AZURE_CREDENTIALS }}
    #    environment: AzureChinaCloud
    #    #allow-no-subscriptions: true
    
    - name: 'set subscriptions'
      run: |
          az cloud set --name AzureChinaCloud
          az login -u your azure user name -p "password" 
          az account set --subscription "your subscription id"
    - name: 'Docker Login'
      uses: azure/docker-login@v1
      with:
        login-server: youracrname.azurecr.cn
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}
      
    # - name: 'Compose Customized Docker Image'
    #   shell: bash
    #   run: |
    #     # If your function app project is not located in your repository's root
    #     # Please change the path to your directory for docker build
    #     docker build . -t REGISTRY/NAMESPACE/IMAGE:TAG
    #     docker push REGISTRY/NAMESPACE/IMAGE:TAG
    - name: Azure App Service Settings
      uses: Azure/appservice-settings@v1
      with:
        # Name of the Azure Web App
        app-name: functionappname
        general-settings-json: '{"acrUseManagedIdentityCreds": "true", "acrUserManagedIdentityID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}'
    - name: 'Run Azure Functions Container Action'
      uses: Azure/functions-container-action@v1
      id: fa
      with:
        app-name: functionappname
    #image: REGISTRY/NAMESPACE/IMAGE:TAG
        image: youracrname.azurecr.cn/azurefunctionimage:v1
    #- name: 'use the published functionapp url in upcoming steps'
    #  run: |
    #    echo "${{ steps.fa.outputs.app-url }}"
    - name: Azure logout
      run: |
        az logout
# For more information on GitHub Actions:
#   https://help.github.com/en/categories/automating-your-workflow-with-github-actions

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

 

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

 

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

2023-01-13T03:09:36.682Z INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-01-13T03:09:45.209Z INFO  - Initiating warmup request to container funtest01_1_b4054967_msiProxy for site funtest01
2023-01-13T03:09:45.261Z INFO  - Container funtest01_1_b4054967_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T03:09:45.268Z INFO  - Initiating warmup request to container funtest01_1_b4054967 for site funtest01
2023-01-13T03:10:01.707Z INFO  - Waiting for response to warmup request for container funtest01_1_b4054967. Elapsed time = 16.4981389 sec
2023-01-13T03:10:13.069Z INFO  - Container funtest01_1_b4054967 for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T03:10:13.089Z INFO  - Initiating warmup request to container funtest01_1_b4054967_middleware for site funtest01
2023-01-13T03:10:17.032Z INFO  - Container funtest01_1_b4054967_middleware for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T03:22:40.065Z INFO  - Recycling container because of AppSettingsChange and isMainSite = True
2023-01-13T03:22:55.207Z INFO  - Pulling image: mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart
2023-01-13T03:22:56.079Z INFO  - 3.0-appservice-quickstart Pulling from azure-functions/dotnet
2023-01-13T03:22:56.080Z INFO  -  Digest: sha256:99f2de1ba2d097fe7fca8098351bd7d9d2e1cabbc32e3c3506321f7f1811bd1b
2023-01-13T03:22:56.081Z INFO  -  Status: Image is up to date for mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart
2023-01-13T03:22:56.084Z INFO  - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2023-01-13T03:22:56.157Z INFO  - Starting container for site
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  
2023-01-13T03:22:56.166Z INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-01-13T03:23:10.342Z INFO  - Initiating warmup request to container funtest01_2_24a23a85_msiProxy for site funtest01
2023-01-13T03:23:10.745Z INFO  - Container funtest01_2_24a23a85_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T03:23:10.753Z INFO  - Initiating warmup request to container funtest01_2_24a23a85 for site funtest01
2023-01-13T03:23:27.483Z INFO  - Waiting for response to warmup request for container funtest01_2_24a23a85. Elapsed time = 17.1407378 sec
2023-01-13T03:23:38.014Z INFO  - Container funtest01_2_24a23a85 for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T03:23:38.023Z INFO  - Initiating warmup request to container funtest01_2_24a23a85_middleware for site funtest01
2023-01-13T03:23:54.109Z INFO  - Container funtest01_2_24a23a85_middleware for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T06:14:26.600Z INFO  - Recycling container because of AppFrameworkVersionChange and appFrameworkVersion = <youracrname>.azurecr.cn/azurefunctionimage:v1
2023-01-13T06:14:50.804Z INFO  - Pulling image: <youracrname>.azurecr.cn/azurefunctionimage:v1
2023-01-13T06:14:51.203Z INFO  - v1 Pulling from azurefunctionimage
2023-01-13T06:14:51.228Z INFO  - 3f4ca61aafcd Pulling fs layer
2023-01-13T06:14:51.233Z INFO  - 3f487a3359db Pulling fs layer
2023-01-13T06:14:51.233Z INFO  - cf20d7997674 Pulling fs layer
2023-01-13T06:14:51.234Z INFO  - 8fa944797ac7 Pulling fs layer
2023-01-13T06:14:51.234Z INFO  - 268581bec5af Pulling fs layer
2023-01-13T06:14:51.235Z INFO  - 320a9b97d2ed Pulling fs layer
2023-01-13T06:14:51.235Z INFO  - 14bf15bf0e2a Pulling fs layer
2023-01-13T06:14:51.235Z INFO  - 888c871585b1 Pulling fs layer
2023-01-13T06:14:51.243Z INFO  - dc54e8c78a21 Pulling fs layer
2023-01-13T06:14:51.244Z INFO  - 0b8d318d756a Pulling fs layer
2023-01-13T06:14:51.244Z INFO  - 686f382362d7 Pulling fs layer
2023-01-13T06:14:51.252Z INFO  - a108b4c555c7 Pulling fs layer
2023-01-13T06:14:51.253Z INFO  - 07a70c22a7c4 Pulling fs layer
2023-01-13T06:14:52.512Z INFO  - 3f487a3359db Downloading 799KB / 1MB
...
2023-01-13T06:17:09.734Z INFO  - 07a70c22a7c4 Extracting 9MB / 9MB
2023-01-13T06:17:09.938Z INFO  - 07a70c22a7c4 Pull complete
2023-01-13T06:17:09.955Z INFO  -  Digest: sha256:26a409b16044e27bdd97627a14118e33e84f840052d9fe4711f1ca471b09d22b
2023-01-13T06:17:09.957Z INFO  -  Status: Downloaded newer image for <youracrname>.azurecr.cn/azurefunctionimage:v1
2023-01-13T06:17:10.056Z INFO  - Pull Image successful, Time taken: 2 Minutes and 19 Seconds
2023-01-13T06:17:10.688Z INFO  - Starting container for site
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  
2023-01-13T06:17:10.707Z INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-01-13T06:17:20.451Z INFO  - Initiating warmup request to container funtest01_3_e9514d82_msiProxy for site funtest01
2023-01-13T06:17:20.721Z INFO  - Container funtest01_3_e9514d82_msiProxy for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T06:17:20.722Z INFO  - Initiating warmup request to container funtest01_3_e9514d82 for site funtest01
2023-01-13T06:17:36.951Z INFO  - Waiting for response to warmup request for container funtest01_3_e9514d82. Elapsed time = 16.4996091 sec
2023-01-13T06:17:44.426Z INFO  - Container funtest01_3_e9514d82 for site funtest01 initialized successfully and is ready to serve requests.
2023-01-13T06:17:44.427Z INFO  - Initiating warmup request to container funtest01_3_e9514d82_middleware for site funtest01
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

 

相关实践学习
通过容器镜像仓库与容器服务快速部署spring-hello应用
本教程主要讲述如何将本地Java代码程序上传并在云端以容器化的构建、传输和运行。
Kubernetes极速入门
Kubernetes(K8S)是Google在2014年发布的一个开源项目,用于自动化容器化应用程序的部署、扩展和管理。Kubernetes通常结合docker容器工作,并且整合多个运行着docker容器的主机集群。 本课程从Kubernetes的简介、功能、架构,集群的概念、工具及部署等各个方面进行了详细的讲解及展示,通过对本课程的学习,可以对Kubernetes有一个较为全面的认识,并初步掌握Kubernetes相关的安装部署及使用技巧。本课程由黑马程序员提供。 &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
相关文章
|
2月前
|
存储 安全 数据安全/隐私保护
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
107 20
|
2月前
|
人工智能 JSON 小程序
【一步步开发AI运动APP】七、自定义姿态动作识别检测——之规则配置检测
本文介绍了如何通过【一步步开发AI运动APP】系列博文,利用自定义姿态识别检测技术开发高性能的AI运动应用。核心内容包括:1) 自定义姿态识别检测,满足人像入镜、动作开始/停止等需求;2) Pose-Calc引擎详解,支持角度匹配、逻辑运算等多种人体分析规则;3) 姿态检测规则编写与执行方法;4) 完整示例展示左右手平举姿态检测。通过这些技术,开发者可轻松实现定制化运动分析功能。
|
10天前
|
C++ Windows
【Function App】本地通过VS Code调试Function时候遇见无法加载文件错误Microsoft.Extensions.Diagnostics.Abstractions
在使用 VS Code 调试 Azure Functions 时,执行 `func host start` 可能因版本冲突报错。错误信息显示 Rpc Initialization Service 启动失败,可能是由于缺少文件或组件导致。解决方法包括:1) 使用 npm 卸载并重新安装 Azure Functions Core Tools;2) 若问题未解决,重新下载安装包(如 func-cli-x64.msi)修复旧版本工具;3) 退出并重启 VS Code,重新加载项目即可恢复正常运行。参考资料链接提供了更多背景信息。
|
18天前
|
安全 Linux 开发工具
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
|
19天前
|
存储 数据可视化 开发工具
【Application Insights】Application Insights存储的Function App的日志存在"Operation Link" 为空的情况
在将 Azure Functions 升级到 .NET 8 和 Isolated Worker 模式后,Application Insights 的请求日志中 `operation_Link` 字段为空,导致分布式追踪无法正常关联。解决方法包括:确保引用正确的 SDK 包(如 `Microsoft.Azure.Functions.Worker.ApplicationInsights`),正确配置 Application Insights 服务,移除默认日志过滤规则,并使用最新依赖包以支持分布式追踪。通过这些步骤,可恢复端到端事务视图的可视化效果。
46 10
|
1月前
【Function App】在PowerShell Function中指定特殊的Microsoft.Graph.Users版本
在Azure Function App中运行PowerShell Function时,通过Requirements.psd1文件管理模块版本。若需将“Microsoft.Graph.Users”从最新版2.26.0改回2.23.0以避免冲突,可通过以下步骤解决:1) 在requirements.psd1中明确指定版本为2.23.0 2) 在profile.ps1中添加`Import-Module Microsoft.Graph.Users -RequiredVersion 2.23.0`语句。此方法确保加载特定版本模块
56 18
|
11天前
|
Linux 网络安全 开发工具
在Linux下配置gitee与Github的远程仓库
注意,git push后,是输入你的账号与密码。这个步骤可以通过特殊设置省去,但是一开始还是不要太省。
|
1月前
|
JSON JavaScript API
MCP 实战:用配置与真实代码玩转 GitHub 集成
MCP 实战:用配置与真实代码玩转 GitHub 集成
733 4
|
2月前
|
监控 C#
【Function App】如果一个拥有多个Function App的Plan遇见了High CPU问题? 如何方便定位是哪一个Function App引发的呢?
在Azure Function App测试中,若多个Function App共用同一App Service Plan资源,当出现High CPU问题时,由于Function App公开指标无法直接观测CPU状态,可通过启用Application Insights解决。其Live Metrics功能可过滤并查看每个Function App的CPU使用情况。具体步骤为:将所有Function App连接至同一Application Insights资源,进入Live Metrics页面按Role筛选监控数据。附有三段C#代码示例,分别展示占用CPU、Memory及普通功能的实现方法。
119 36
|
3月前
|
Swift iOS开发 开发者
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
215 13
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡

热门文章

最新文章