preface

The usual solution is gitlab+ Jenkins +centos, but such a solution is not suitable for lazy people like me. I have been looking for a simple solution. In the process of seeking solutions, I found that the third party in China is not friendly to c#. Did not find easy to use, so I think of Azure, have to say that MS is still quite to force.

Must-have list

  • GitHub ==>GitHub tutorial
  • Ali Cloud Container image service or hub.docker.com
  • Azure DevOps Registration = azure.microsoft.com/zh-cn/servi…
  • A server ps: collects the ali cloud ECS of 2993

1. Get GitHub

  1. Open VS and create a new oneASP.NET Core Web applications, calledWebNotebook
  2. Click Next, remember to enable Docker support, and click Create
  3. If you did not click Enable Docker support in the previous step, you can add it by creating a new one
  4. Push the code to GitHub

Here is my Docker file configuration


FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src

RUN ls -al

COPY ["WebNotebook/WebNotebook.csproj"."WebNotebook/"]
RUN dotnet restore "WebNotebook/WebNotebook.csproj"
COPY.WORKDIR "/src/WebNotebook"
RUN dotnet build "WebNotebook.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "WebNotebook.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet"."WebNotebook.dll"]
Copy the code

Below is a Demo of my project

Github.com/zhaozhengya…

2. Configure Ali Cloud container image service

  • Go to Aliyun and find container image service = cr.console.aliyun.com/
  • Create a namespace, give it a name, I’ll call itzohnz(PS: private public depends on personal needs)
  • Create a mirror warehouse, give it a name, I’ll call itwebnotebook(Note: Lowercase only)
  • Go to view the basic information of the mirror warehouse, marked red copy down, for a moment to configure Azure Pipeline to use


Azure CI/CD configuration

  • To register an Azure account, go to dev.azure.com/
  • Create an organization, I’ll call itzohnz
  • Create a project, I’ll call itNexter
  • Enter thePipelinesAdd a new Build (PS: this is called continuous integration)
  • The editorazure-pipelines.yml, copy the following code and replace the following parameters with yours

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'


variables:
  dockerId: [email protected]                  # aliyun login username
  namespace: zohnz                              # AliCloud namespace
  imageName: webnotebook                        # Aliyun warehouse name
  registry: registry.cn-hangzhou.aliyuncs.com   # Aliyun public network address
  dockerfilepath:  WebNotebook/Dockerfile       # Github Dockerfile relative path


steps:
- script: | docker build -f $(dockerfilepath) -t $(imageName) . docker login -u $(dockerId) -p $(pwd) $(registry) docker tag $(imageName) $(registry)/$(namespace)/$(imageName) docker push $(registry)/$(namespace)/$(imageName)  displayName: push to aliyun

Copy the code

Here’s a brief explanation of what yamL script parameters mean

instruction annotated
$(xxx) This is the placeholder for Azure, which I declared above with variables
-f Specify the Dockerfile path to use; This is very important, if you do not add it, you will not find the folder error
-t The name and label of the image, usually in the name:tag or name format. Multiple labels can be set for a single image in a single build.
$(pwd) This PWD can be written directly, but is configured in azure variables(parameter environment variables) for security

Readers can set it in the following ways

  • Let’s add a new oneReleaseIs used to Pull and Run images
  • The name of the configuration Agent is called herePush and Run, as well as some other configurations, see the figure below
  • Click on thatPush and RunThe right of the+Number, add Task
  • searchSSH, click on theADDThis means executing your own script on a remote host
  • Click on theManageConfigure your login account and password, and add an SSH Service Connection
  • Select the name you just added asssh_aliyunThe SSH service connection
  • Next fill in the script to be configured and selectInline ScriptI have four tasks here
  • Here are the scripts for my four tasks
  1. Remove Container

    
    #Determine if a WebNotebook container exists
    docker ps | grep webnotebook &> /dev/null
    #If not, Remove
    if [ $? -ne 0 ]
    then
        echo "webnotebook container not exist continue.. "
    else
        echo "remove webnotebook container"
        docker rm webnotebook -f
    fi
    
    Copy the code
  2. Remove old Image

    
    #Determine whether there is any registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook image
    docker images | grep registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook &> /dev/null
    #If no, no operation is performed
    if [ $? -ne 0 ]
    then
        echo "image does not exist , continue..."
    else
        echo "image exists !!! remove it"
        docker rmi --force registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
    fi
    
    Copy the code
  3. Pull Image

    #Pull the image of the push from Ali Cloud
    docker pull registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
    Copy the code
  4. Run Image

    #Run the image on the host and expose port 5003
    docker run --restart unless-stopped -p 5003:80 --name webnotebook -d registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
    Copy the code
  • After configuring the Task, we need to connect the Build and Release in series, select the previous Build pipeline, and remember to Save after adding

  • Here is my configured build pipeline

4. Configure Aliyun

  • Log in your Ali cloud server, use the CMD of Win, I am Ubuntu
C: \ Users \ zhaozy > SSH [email protected] [email protected] 's password: Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-52-generic x86_64)Copy the code
  • Install the Docker
Sudo apt-get update # sudo apt-get -y install docker. IO # Install dockerCopy the code
  • The inputdocker -vCheck whether docker is installed successfully
root@iZs9kgd0x5xmhaZ:~# docker -v docker version 18.09.7, build 2d0083DCopy the code

Now that we’ve completed all the configuration phases, let’s commit the code once to test the release process

  • Github submits code

  • I went into Azure to check the Build progress. I was building very quickly and had already built the Build when I went in

  • Click on the Job log and check the Job log

  • Go to Ali Cloud container image service to check whether Push comes up

  • Go to Azure to check whether Release is successfully executed

  • Go to Ali Cloud security group to add5003Extranet access to the port

  • Open our server address + port number

conclusion

At this point, all four of our Linux releases are complete and you can configure them flexibly according to your needs