头图

[Detailed] CI/CD with ASP.NET Core as an example

微软技术栈
中文

In software development, project deployment is a very important part. Especially in agile development, how to quickly, efficiently and smoothly transform the modified code into actual results has always been a topic of interest. The common continuous integration and continuous deployment (hereinafter referred to as CI/CD) implementation scheme is Jenkins, through Jenkins + host server, rapid project iteration, this will indeed bring us great convenience, and it is also a popular scheme. . However, in reality, it is not that simple. We are now more based on containerization to implement K8s cluster development. Jenkins will always seem a little weak, let alone implement a complete set of multi-environment-based CI/CD operations in K8s. So how can we perfectly implement CI/CD based on K8s?
Through this experiment, we take Microsoft Azure Kubernetes Service as an example (hereinafter referred to as AKS) and use the ASP.NET Core project to analyze CI/CD operations.

CI

Preliminary preparation of account

First, you need to register an Azure account.

Secondly, you need a source code management warehouse, you can create a new and use Azure DevOps code base, there are very good Kanban and operation tips, it is recommended to use.
6db341149974e9b3f9947aaaf6d4c7d4.png
You can also use your own GitHub account. Since Microsoft acquired Github, the compatibility of Azure is particularly friendly! If you already have a project on GitHub, you can directly build a CI Pipeline. After the completion, the effect is shown in the figure. The specific operation and deployment will be shown below.
99638066cb12cd6e5b7d353d7d3c163e.png
There are two main purposes for using Azure to create a Pipeline:
1. Ensure the accuracy of the code. For example, if you temporarily modify a code without a compiler, you will usually modify it directly, then submit it, and let CI check the accuracy of the code initially;
2. You can build an image and push it to the warehouse, or even deploy it directly as mentioned in the next article.
Really realize the modification code == preview effect .
Github's Action also has such a function, and the realization idea is roughly the same, except that it is not easy to operate on CD (continuous deployment). For details, please refer to the code of my Blog.Core project. I will not go into details here.
65670e985167836684cbcd2cc8486945.png

Connect to the Dockers Registry service

To create a new CI pipeline, it is recommended to configure a Docker Registry service, so that the image after the build can be pushed to the mirror warehouse to facilitate subsequent CD operations.

Step 1-project settings

Click the Project settings operation link under the project:
c9bc139aae422d7601ebf2a0b99bf06e.png

In the new page that pops up, select the Service connections operation in the left navigation bar:
01eea0e1dc87ee35189fc27294c823e9.png
Click the New Service Connection button:

0dbc8226b267e8a0d8aa5b63ebce2068.png

Step 2-Configure Docker Registry options

Search for docker keywords and select the Docker Registry option:

cd7031844868f69c2dc119771e6617c3.png
Select the Registry type, enter the DockerID and password, give this connection a name, and click the Save button.
38e8000c453baf6b51340e9e30d350c2.png
A service connection is created, and this connection will be used in future CI operations.
65653e479c6fdd3a9ac5da67de4afad0.png
The user name and password must be correct, otherwise an error will be prompted, you can click Verify to verify:
c077666ffa2ad3284f37949678cc397f.png

Create a new pipeline

Step 1-Configure Code source repository

Select New Pipeline, check Github, if the source code management is in Azure DevOps, you can go directly to the first option. Because I am using Github, I directly check GitHub. Basically, I use YAML.

e04788775e81953eecd90b6c1fbf4234.png
Choose a project of your own (PS: Github secondary password confirmation may be required at this time):
cf0a0b18b4f9c799639ac75cf1843b78.png

Step 2-configure your pipeline

Using containerized deployment, click on the Docker option and pay attention to the difference with the second one:

dfab0145aa4924f6395bf2964eac81b1.png
Configure the YAML file, the system will create one by default, only the build operation, you can remove the default task, select a docker Assistant template on the back:
a9d835bf63c85614be1f8caa4cc3762c.png
You can also directly click the settings link in the code on the left to automatically call up the editing window:
6e91057469bfe08db3552fa429648a0f.png
Others are the default, just check the service connection just now, and then enter your own container warehouse name, Please note that you need to add a prefix to the image name, which is the respective Docker ID , click the Add button, on the left The synchronization has changed.
5534fad103e25bc87097f14079b0d066.png

Step 3-Click to run Pipeline

Then click the Save and run button in the upper right corner, and a complete CI operation is completed.

35344655d3c07aeb446d4a326bc4024a.png
At the same time, the YAML file code just generated to create the Pipeline will be synchronized to GitHub:
558d3a28ba99a57b158c4dc371ab95b2.png
After the build is completed, we can see the pushed image in the Docker hub:

fca0a0d37b43711d6a43a75a9bad4422.png

Above, we have completed the continuous integration (hereinafter referred to as CI) part, successfully packaged and compiled the ASP.NET Core project, and pushed it to the designated Docker mirror warehouse.

Now we continue to complete the continuous deployment (hereinafter referred to as CD) process of the finished product.

CD

Add Release pipeline

Step 1-Create a new pipeline

In the left navigation bar, click the Release option under Pipelines to create a new Pipeline. If you have not created a Release Pipeline before, the default display effect of the page is as follows:

30bf948838df02e2caea28884009a2d5.png

In the template on the right, select an empty template:

9b3beb9e0d472b819e17d9d87665f2f5.png

Step 2-Configure Artifact

Move the mouse to the Artifacts module on the left, and click to add an Artifact. At this time, the editing window will be called up on the right, and click the build option:

f0a31dbca4f230e614b6314c8af10a93.png

Choose the pipeline of the previous build and use the latest version of Latest, At this moment, our CD pipeline is officially associated with the CI pipeline .
33f8297393ecbe1351435604aca47759.png

Step 3-automatic build

Click the build icon in the upper right corner of the product to turn on the automatic build, so that as long as the code is submitted, the CI Build operation will be triggered, and then the CD Release operation will be triggered immediately, and the whole process will be completed in one go.
616d8919a37fb19f0e8fc94fc690b31f.png

After configuring the Artifact, you can configure the task task.

Configure Agent

Place the mouse on the Stage option on the right, you can see that there are three function options, namely:

①. Rename the Stage;

②. Add a new Stage;

③, edit task (task);

d63a6ec505910d78b63ee239681e4ef8.png

Click on the task link to configure Agent Job. Here are two points to note:

1. Proxy pool refers to the place of deployment, currently the default is sufficient, and you can use your own server in the future;

2. Agent specification (agent specification), which is the server specification configuration;

Please pay attention to The default here is the vs2019 specification, which is a windows environment. If it is not changed, there will be a platform problem that Docker cannot run. So just choose Linux.

368e99c780bf934f790d71f3f7bf35d9.png

Configure Task

Step 1-delete the old container

Click the plus sign on the right side of AgentJob to filter the task template of docker
1892e4a171eb7a7a5e1cb2ee91300827.png

Edit the command in the newly invoked edit page, delete the old container, and use the run command directly, so the Task version uses 0.*:

fb3d0bfa15a10785b88412f7c29eec3a.png
Select the container Registry address, configure an action (behavior), and add a command to delete the image

rm -f xxxx

Step 2-run the new container

Similar to the first step of deleting the old container, build a stage to run the container. The overall process is the same. The configuration diagram is as follows:

8a94d2d0df3aecab71e5c6bad1b030a2.png

Task version is 1.* Docker container configuration, use a custom DockerRegistry, configure the image name, support customization, for example, I added a prefix, and finally specify the port.

Click Save, and a simple continuous integration pipeline is built
aae16ad9da76a3f89694a35d143a6e91.png

You can manually trigger, create release, and you can see the detailed process:

1dc06a448919fc0406af8eaaf009c5ad.png

After waiting for a period of time, the new container has been successfully run, but it has not yet configured its own proxy pool, so it is impossible to view the specific display effect.

How to check the effect

There are currently two common ways to deploy projects in Azure:

1. Configure a custom proxy pool, use your own server to provide the proxy pool, generate images and run containers will run in your own proxy pool server.

2. Directly use Azure's k8s service, create a new Deploy Task, specify the corresponding service connection and Yml file, so that the image will be pushed to the Azure mirror warehouse, and the image will be run to build a Pod instance.

The editor has used both, using the second example, roughly like this:

first build and push the image to the mirror warehouse
11a45ee160a1a017f0e7daaf30771924.png

Then deploy the image
be962be0e091b6121a7db8748c320115.png

Summarize

This article first uses ASP.NET Core as an example to explain how to implement continuous integration operations in Azure. The overall process is simple and convenient, and the documentation is particularly clear. Once again, cheers for the Microsoft Docs documentation.

Afterwards, we take ASP.NET Core as an example to explain how to implement continuous deployment operations in Azure. The overall process is based on continuous integration, and our Code is run step by step, which in a real sense realizes automated operations.

Source Link:
https://docs.microsoft.com/zh-cn/learn/paths/az-900-describe-cloud-concepts/
https://devblogs.microsoft.com/dotnet/category/net-core/?WT.mc_id=DOP-MVP-5003704
Github:
https://github.com/anjoy8/Blog.Core/
Source Link:
https://docs.microsoft.com/zh-cn/azure/devops/pipelines/tasks/build/docker?WT.mc_id=AZ-MVP-5003704
https://devblogs.microsoft.com/dotnet/category/net-core/?WT.mc_id=DOP-MVP-5003704
Github:
https://github.com/anjoy8/Blog.Core/

Microsoft's most valuable expert

bc93fde364ea9dd3d9106b58e805b770.png

Microsoft's Most Valuable Expert is a global award granted by Microsoft to third-party technology professionals. For 28 years, technology community leaders around the world have won this award for sharing their expertise and experience in online and offline technology communities.

MVP is a rigorously selected team of experts. They represent the most skilled and intelligent people. They are experts who are passionate and helpful to the community. MVP is committed to helping others through speeches, forum questions and answers, creating websites, writing blogs, sharing videos, open source projects, organizing conferences, etc., and to help users in the Microsoft technology community use Microsoft technology to the greatest extent.
For more details, please visit the official website:
https://mvp.microsoft.com/zh-cn


Here are more official Microsoft learning materials and technical documents, scan the code to get the free version!
The content will be updated from time to time!
208f6785e4bc3f899ded709a80dff426.jpg

阅读 655

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。

205 声望
894 粉丝
0 条评论

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。

205 声望
894 粉丝
文章目录
宣传栏