使用.NET 6开发TodoList应用(31)——实现基于Github Actions和ACI的CI/CD
系列导航及源代码#
需求和目标#
在这个系列的最后一节中,我们将使用GitHub Actions将TodoList应用部署到Azure Container Instance上。
实现#
为了确保部署的应用能够正确运行,我们需要做以下几件事:
创建Azure SQL Server实例#
选择最便宜的数据库规格就可以了,新建一个ResourceGroup名为todolist
,并新建数据库实例,获得连接字符串:
创建Azure Container Registry#
用于构建好的镜像上传和部署
设置GitHub Secrets#
首先创建Service Principle认证:
groupId=$(az group show \
--name todolist \
--query id --output tsv)
az ad sp create-for-rbac \
--scope $groupId \
--role Contributor \
--sdk-auth
# 会得到类似下面的输出
{
"clientId": "xxxx6ddc-xxxx-xxxx-xxx-ef78a99dxxxx",
"clientSecret": "xxxx79dc-xxxx-xxxx-xxxx-aaaaaec5xxxx",
"subscriptionId": "xxxx251c-xxxx-xxxx-xxxx-bf99a306xxxx",
"tenantId": "xxxx88bf-xxxx-xxxx-xxxx-2d7cd011xxxx",
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
"galleryEndpointUrl": "https://gallery.azure.com/",
"managementEndpointUrl": "https://management.core.windows.net/"
}
接下来更新Registry的认证
registryId=$(az acr show \
--name code4nothing \
--query id --output tsv)
az role assignment create \
--assignee <ClientId> \
--scope $registryId \
--role AcrPush
最后将Secrets配置到Github Repo的设置里:
具体的参数说明请参考:Save credentials to GitHub repo
创建Actions配置#
在Github的Repo里选择Actions,选择set up a workflow yourself
定义以下构建步骤:
on: [push]
name: Linux_Container_Workflow
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# checkout the repo
- name: 'Checkout GitHub Action'
uses: actions/checkout@main
- name: 'Login via Azure CLI'
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: 'Build and push image'
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- run: |
docker build -f src/TodoList.Api/Dockerfile.prod . -t ${{ secrets.REGISTRY_LOGIN_SERVER }}/todolist:${{ github.sha }}
docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/todolist:${{ github.sha }}
- name: 'Deploy to Azure Container Instances'
uses: 'azure/aci-deploy@v1'
with:
resource-group: ${{ secrets.RESOURCE_GROUP }}
dns-name-label: ${{ secrets.RESOURCE_GROUP }}${{ github.run_number }}
image: ${{ secrets.REGISTRY_LOGIN_SERVER }}/todolist:${{ github.sha }}
registry-login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
registry-username: ${{ secrets.REGISTRY_USERNAME }}
registry-password: ${{ secrets.REGISTRY_PASSWORD }}
name: aci-todolist
location: 'east asia'
修改项目代码以进行Production部署#
Dockerfile.prod
ARG NET_IMAGE=6.0-bullseye-slim
FROM mcr.microsoft.com/dotnet/aspnet:${NET_IMAGE} AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_ENVIRONMENT=Production
FROM mcr.microsoft.com/dotnet/sdk:${NET_IMAGE} AS build
WORKDIR /src
COPY ["src/TodoList.Api/TodoList.Api.csproj", "TodoList.Api/"]
COPY ["src/TodoList.Application/TodoList.Application.csproj", "TodoList.Application/"]
COPY ["src/TodoList.Domain/TodoList.Domain.csproj", "TodoList.Domain/"]
COPY ["src/TodoList.Infrastructure/TodoList.Infrastructure.csproj", "TodoList.Infrastructure/"]
RUN dotnet restore "TodoList.Api/TodoList.Api.csproj"
COPY ./src .
WORKDIR "/src/TodoList.Api"
RUN dotnet build "TodoList.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish --no-restore "TodoList.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY /app/publish .
ENTRYPOINT ["dotnet", "TodoList.Api.dll"]
appsettings.Production.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"UseFileToLog": true,
"ConnectionStrings": {
"SqlServerConnection": "Server=tcp:code4nothing.database.windows.net,1433;Initial Catalog=TodoListDb;Persist Security Info=False;User ID=code4nothing;Password=TestTodo@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
}
}
验证#
我们从本地的Insomia上访问API端口,先看看liveness probes是否正常工作:
一点扩展#
微软新推出的Azure服务Azure Container Apps(preview)
是作为容器化服务部署的另一个选择,简单的说明:
Deploy containerized apps without managing complex infrastructure. Write code using your preferred programming language or framework, and build microservices with full support for Distributed Application Runtime (Dapr). Scale dynamically based on HTTP traffic or events powered by Kubernetes Event-Driven Autoscaling (KEDA).
来自官方文档Azure Container Apps
总结#
如果在部署ACI的过程中失败了,尤其是容器在ACI中没有成功运行,可以参考:Troubleshoot common issues in Azure Container Instances来查看和解决问题。
在本文中我们实现了如何将应用通过Github Actions部署到Azure Container Instance服务中。那么到本节为止,使用.NET 6开发TodoList应用文章索引这个用于串联.NET Web API开发基础系列的文章就结束了,感谢各位朋友在此过程中的支持。
在这个系列的写作和发表中,我们还漏掉了几篇文章暂时没有完成,包括有评论指出的几个没有填完的坑,我会找时间尽量都补齐。但是更新速度应该不会像写这个系列的时候那么密集了。
后面的计划是,在正式开始微服务系列实践文章开始之前,会写几个小的系列来预先熟悉一下后面也许会用到的知识点,例如GraphQL,RabbitMQ,gRPC,Envoy以及Dapr等内容。
感谢各位对文章中错误的指正,希望我们能继续一起努力,一步一个脚印地掌握更多的技能。
参考资料#
作者:CODE4NOTHING
出处:https://www.cnblogs.com/code4nothing/p/build-todolist-31.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
欢迎转载,转载请注明出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!