【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
第三步:配置用户标识
启用用户标识,主要就是为了能够让它有权限去访问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/FunctionApp 和 https://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
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image的更多相关文章
- 【Azure Developer】Github Action部署资源(ARM模板)到Azure中国区时,遇见登录问题的解决办法
问题描述 在参考文档"使用 GitHub Actions 部署 ARM 模板"一文中,由于是在中国区Azure上操作,所以生产的部署凭证为中国区凭证.当创建工作流时,在登录到Azu ...
- 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...
- 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式
问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...
- 【Azure Developer】使用Java代码启动Azure VM(虚拟机)
问题描述 在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code ...
- struts2对action中的方法进行输入校验---xml配置方式(3)
上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...
- Action中动态方法的调用 Action中通配符的使用 Result的配置
Action中动态方法的调用 动态方法调用(Dynamic Method Invocation,DMI) 标识符:! 一.通过以下选中的文件来查看是否禁止调用动态方法
- 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...
- 理解Struts2的Action中的setter方法是怎么工作的
接触过webwork和Struts2的同行都应该知道, 提交表单的时候,只要Action中的属性有setter 方法,这些表单数据就可以正确赋值到Action中属性里:另外对于Spring配置文件中声 ...
- struts2学习笔记之八:Action中方法的动态调用
方法一:action名称+!+方法名称+后缀 Action类中增加addUser()和delUser()方法, package com.djoker.struts2; import org.apach ...
- 如何使用同一个Action中的不同方法
如何使用同一个Action中的不同方法 1.使用Action的DMI(Dynamic Method Invocation--动态方法调用) (1)动态方法调用: 表单元素的action不是直接为某个A ...
随机推荐
- 齐博x1标签之异步加载标签数据
为什么要异步加载标签?他有什么好处 如果一个页面的标签太多,又或者是页面中某一个标签调用数据太慢的话,就会拖慢整个页面的打开,非常影响用户体验.这个时候,用异步加载的话,就可以一块一块的显示,用户体验 ...
- 如何实现一个SQL解析器
作者:vivo 互联网搜索团队- Deng Jie 一.背景 随着技术的不断的发展,在大数据领域出现了越来越多的技术框架.而为了降低大数据的学习成本和难度,越来越多的大数据技术和应用开始支持SQL进 ...
- CentOS 7.9 Related Software Directory
一.CentOS 7.9 Related Software Directory Installing VMware Workstation Pro on Windows Installing Cent ...
- Arctic 基于 Hive 的流批一体实践
背景 随着大数据业务的发展,基于 Hive 的数仓体系逐渐难以满足日益增长的业务需求,一方面已有很大体量的用户,但是在实时性,功能性上严重缺失:另一方面 Hudi,Iceberg 这类系统在事务性,快 ...
- 2022NISACTF--WEB
easyssrf 打开题目,显示的是 尝试输入, 发现输入flag有东西 读取文件 访问下一个网站 读取文件 不能以file开头 直接伪协议,base64解码 checkIn 奇怪的unicode编码 ...
- 图机器学习(GML)&图神经网络(GNN)原理和代码实现(前置学习系列二)
项目链接:https://aistudio.baidu.com/aistudio/projectdetail/4990947?contributionType=1 欢迎fork欢迎三连!文章篇幅有限, ...
- 第2-3-2章 环境搭建-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss
目录 5. 文件服务开发 5.1 环境搭建 5.1.1 数据库环境搭建 5.1.2 Nacos环境搭建 5.1.3 Nginx环境搭建 5.1.4 maven工程环境搭建 5. 文件服务开发 全套代码 ...
- Go语言核心36讲26
你好,我是郝林.今天我分享的主题是测试的基本规则和流程的(下)篇. Go语言是一门很重视程序测试的编程语言,所以在上一篇中,我与你再三强调了程序测试的重要性,同时,也介绍了关于go test命令的基本 ...
- 深度学习之Tensorflow入门
声明 本文参考[中文][吴恩达课后编程作业]Course 2 - 改善深层神经网络 - 第三周作业_何宽的博客-CSDN博客我对这篇博客加上自己的理解,力求看懂 本文所使用的资料已上传到百度网盘[点击 ...
- 广州2022CCPC补题
I Infection 知识点: 树上背包 第一次写树上背包的题目,没想到就是在区域赛中 神奇的是树上背包的复杂度,看起来是\(O(n^3)\),但是实际计算只有\(O(n^2)\) 学会树上背包后可 ...