使用.NET 6开发TodoList应用(31)——实现基于Github Actions和ACI的CI/CD


系列导航及源代码

需求和目标

在这个系列的最后一节中,我们将使用GitHub Actions将TodoList应用部署到Azure Container Instance上。

实现

为了确保部署的应用能够正确运行,我们需要做以下几件事:

创建Azure SQL Server实例

选择最便宜的数据库规格就可以了,新建一个ResourceGroup名为todolist,并新建数据库实例,获得连接字符串:

image

创建Azure Container Registry

用于构建好的镜像上传和部署

image

设置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的设置里:

image

具体的参数说明请参考:Save credentials to GitHub repo

创建Actions配置

在Github的Repo里选择Actions,选择set up a workflow yourself
image

定义以下构建步骤:

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:
image

ACI实例运行起来后可以看到:
image

验证

我们从本地的Insomia上访问API端口,先看看liveness probes是否正常工作:
image

请求的host可以从这里获取:
image

查询TodoList后查询TodoItems结果:
image

一点扩展

微软新推出的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等内容。

感谢各位对文章中错误的指正,希望我们能继续一起努力,一步一个脚印地掌握更多的技能。

参考资料

  1. Configure a GitHub action to create a container instance
  2. Troubleshoot common issues in Azure Container Instances
  3. Azure Container Apps
文章出处:https://www.cnblogs.com/code4nothing/p/build-todolist-31.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:.NET中大型项目开发必备(7)--ORM数据库访问技术
下一篇:使用.NET 6开发TodoList应用(30)——实现Docker打包和部署
评论列表

发表评论

评论内容
昵称:
关联文章

使用.NET 6开发TodoList应用(31)——实现基于Github ActionsACICI/CD
使用.NET 6开发TodoList应用(24)——实现基于JWTIdentity功能
使用.NET 6开发TodoList应用(30)——实现Docker打包部署
使用.NET 6开发TodoList应用(25)——实现RefreshToken
使用.NET 6开发TodoList应用(12)——实现ActionFilter
使用.NET 6开发TodoList应用(26)——实现ConfigurationOption强类型绑定
使用.NET 6开发TodoList应用(22)——实现缓存
使用.NET 6开发TodoList应用(14)——实现查询过滤
使用.NET 6开发TodoList应用(15)——实现查询搜索
使用.NET 6开发TodoList应用(9)——实现PUT请求
使用.NET 6开发TodoList应用(11)——使用FluentValidationMediatR实现接口请求验证
使用.NET 6开发TodoList应用(16)——实现查询排序
使用.NET 6开发TodoList应用(10)——实现DELETE请求以及HTTP请求幂等性
使用.NET 6开发TodoList应用(27)——实现APISwagger文档化
使用.NET 6开发TodoList应用(6)——使用MediatR实现POST请求
使用.NET 6开发TodoList应用(23)——实现请求限流
使用.NET 6开发TodoList应用(8)——实现全局异常处理
使用.NET 6开发TodoList应用(19)——处理OPTIONHEAD请求
使用.NET 6开发TodoList应用(7)——使用AutoMapper实现GET请求
使用.NET 6开发TodoList应用(28)——实现应用程序健康检查

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码