使用.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 --from=publish /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;"
}
}
将代码合并到main上以后,自动触发Actions:
ACI实例运行起来后可以看到:
验证
我们从本地的Insomia上访问API端口,先看看liveness probes是否正常工作:
请求的host可以从这里获取:
查询TodoList后查询TodoItems结果:
一点扩展
微软新推出的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