.Net Core in Docker - use and Ali Ali cloud cloud Codepipeline container mirroring services to achieve sustained delivery / deployment (CD)

The last time demonstrates how to use Ali cloud Codepipeline, Ali cloud mirrored container service implementation CI , talking about this after we push the code to automatically compile, run automatic unit testing, automated build mirror, mirror automatically pushed to the private warehouse. So from our original goal set so're just a small step, and that is automatically deployed to test / production environment, this step is the continuous delivery / deployment (CD).

CD is actually two meanings

(1)Continuous delivery (持续交付)  
    指的是,频繁地将软件的新版本,交付给质量团队或者用户,以供评审。如果评审通过,代码就进入生产阶段。
(2)continuous deployment(持续部署)   
    指的是代码通过评审以后,自动部署到生产环境。

Great God taken from Ruan Yifeng blog

Before I always thought that CD just continuous deployment mean, recently made carefully check the information before actually has two meanings. Although the two meanings, but in fact similar, are deployed into a run up the environment, the program up and running. Continuous Delivery usually deployed to a test environment, test team for review; the continued deployment of means testing after review by the deployment to the production environment. Since almost here I do not broken down, because they were deployed, but the deployment is not the same position.

Process


The last process to the image pushed to the private warehouse (Ali cloud container mirror service) after the end of the back of the manual processes required to run a shell script to complete. We should flow back to string together, have the shell script automatically up and running, you need a trigger mechanism, such as webhook. Fortunately, Ali cloud container mirroring services have such a feature that allows us to string together the processes that trigger function. This trigger function with webhook almost the same, when the container mirroring service will receive a new image sends an HTTP POST request. Then we only need to deploy a web service on the server when receiving a POST request to the server running the shell script, pull mirroring, run the container, so that the program deployed up.

New PublishHook Service

Has been said above POST request sent to the receiving vessel image of the service, it is necessary to receive a web service request process. The service is very simple to use ASP.NET MVC is overkill, just a request to monitor it. Here I use another wheel the Aserver https://github.com/kklldog/AServer .

1. Create a console program, named PublishHook

2. Use nuget installation AServer

3. Modify the Program's main function

using Agile.FrameworkNetCore.Log;
using System;
using System.Diagnostics;

namespace PublishHook
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("PublishHook is running now !");

            var server = new Agile.AServer.Server();
            server.AddHandler(new Agile.AServer.HttpHandler()
            {
                Method = "POST",
                Path = "/api/hook",
                Handler = (req, resp) =>
                {
                    string shell_name = req.Query.shell;
                    if (!string.IsNullOrEmpty(shell_name))
                    {
                        RunShell(shell_name);
                    }
                    return resp.Write("ok");
                }
            });

            server
                .SetIP("0.0.0.0")
                .SetPort(9000)
                .Run();

            Console.Read();
        }

        static void RunShell(string fileName)
        {
            var processStartInfo = new ProcessStartInfo(fileName) { RedirectStandardOutput = true };
            var process = Process.Start(processStartInfo);
            if (process == null)
            {
                Console.WriteLine("Can not run shell .");
            }
            else
            {
                using (var sr = process.StandardOutput)
                {
                    while (!sr.EndOfStream)
                    {
                        var str = sr.ReadLine();
                        Console.WriteLine(str);
                        Logger.Info(str);
                    }

                    if (!process.HasExited)
                    {
                        process.Kill();
                    }
                }
            }
        }
    }
}

Start a http Server listening port 9000, add a http handler, receives the request, parse QueryString Gets the name of the script, and then run the script

Run publish_hook

sudo dotnet restore
sudo dotnet publish

Dotnet publish this release using the command program, and then copied to the server.

sudo dotnet PublishHook.dll


Dotnet use this command to run the service on the server. Note: This service can not be used docker run, because it needs to run the shell script to the host operating docker. If this service is running in the container, then shell it performs relative to its container, it can not operate the host of docker environment.

Copy the last new publish_cicd_test.sh script file to the program directory and give permission PublishHook

Copy the last new publish_cicd_test.sh script file to PublishHook program directory, the program is able to read again from the root directory.

chmod +x publish_cicd_test.sh

Use chmod + x to the shell script executable permissions assignment

In New Trigger container mirror service


Click to create a trigger

to fill in the name of the trigger in the new interface, trigger url. This is the url address to listen PublishHook

have a test

After configuring the trigger container good image of the service, our configuration work basically completed. Let's change the CoreCICDTest project, then push to the Gitee, after watching push automatic deployment can not succeed.

@{
    ViewData["Title"] = "Home Page";
}


<h3>
    .NET CORE CICD TEST -- V 3.0
</h3>

修改home/index首页,从V2.0改为V3.0,然后使用git push命令推送代码。等待一会后,访问一下CoreCICDTest的网址。

Wow!可以看到我们的网址已经自动部署成功了,终于完成了我们一开始设定的目标。

总结

回顾整个过程,我们可以发现各个服务之间虽然是彼此独立,但是我们可以通过WebHook功能串联起来。甚至最后我们自己定义了一个WebHook的监听程序来替我们执行对应的脚步。其实通过这种思想我们可以把更多的流程串联起来,实现更多自动化流程。
这次我们顺利的使用阿里云的Codepipeline、容器镜像服务,实现了最基本的CICD。现在各大云服务厂商基本都提供了很多基础功能,而且大部分是免费的,有效的利用这些服务可以节省宝贵的时间,开发者可以更专注在核心业务上面。

Guess you like

Origin www.cnblogs.com/kklldog/p/core_in_docker_cd.html