.Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

  • 2019 年 10 月 3 日
  • 笔记

??

linux ???????????????? core ??,??????? gitlab+jenkins+centos???????????????????????????????????????????????????c#???????????????????Azure?????MS???????

????

?? ??GitHub

  1. ??VS??? ASP.NET Core Web ???? ???? WebNotebook
  2. ?????????? Docker ????? ??
  3. ???????????Docker???????????
  4. ???Push?GitHub

?????Docker????

  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"]

????????Demo

https://github.com/zhaozhengyan/WebNotebook

?????????????

  • ?????,????????=?https://cr.console.aliyun.com/
  • ???????????????? zohnz (PS:?????????)
  • ???????????????? webnotebook (??:????)
  • ?????????????????????????? Azure Pipeline ??

add container

20190731172346.png

??Azure CI/CD ??

  • ??????Azure ????? https://dev.azure.com/
  • ??????? ???? zohnz
  • ????project,???? Nexter
  • ?? Pipelines,?????? Build?PS:??????????

  • ?? azure-pipelines.yml???????????????????
  # 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]                  # ??? login username    namespace: zohnz                              # ??? ????    imageName: webnotebook                        # ??? ????    registry: registry.cn-hangzhou.aliyuncs.com   # ??? ????    dockerfilepath:  WebNotebook/Dockerfile       # Github Dockerfile????      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  

??????? yaml ??????????

?? ????
$(xxx) ???azure ?????????variables ???
-f ??????Dockerfile??????????????????????
-t ??????????? name:tag ?? name ???????????????????????
$(pwd) ??pwd ??????????????????azure?variables(????????)?

????????????

??pwd??

  • ????????? Release,?????Pull???Run??
    new release pipeline
    add empty stage
    click task
  • ??Agent???????Push and Run ??????????????
    config agent
  • ?????Push and Run??? + ????Task
    add Task
  • ??SSH???ADD ????????????????????
    config task
  • ??Manage ????????????,???? SSH Service connection
    add service connection
  • ??????????ssh_aliyun ? SSH service connection
    chose ssh service connection
  • ???????????????Inline Script????????task
    preview task list
  • ???????task???
  1. Remove Container

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

      #??????registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook ??  docker images | grep registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook &> /dev/null  #??????????  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  
  3. Pull Image

    #????????push???  docker pull registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
  4. Run Image

    # ??????????5003??  docker run --restart unless-stopped -p 5003:80 --name webnotebook -d registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
  • ???Task????????Build?Release????,????Build pipeline,????????Save
    add artifacts

  • ???????? build pipeline

20190801095115.png

???????

  • ?????????????win???cmd????????ubuntu
C:Userszhaozy>ssh [email protected]  [email protected]'s password:  Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-52-generic x86_64)
  • ??Docker
sudo apt-get update #???  sudo apt-get -y install docker.io #??docker
  • ??docker -v ??docker??????
root@iZs9kgd0x5xmhaZ:~# docker -v  Docker version 18.09.7, build 2d0083d

?????????????????????????????????

  • github ????
    github commit code
  • ??Azure??Build??????????????????????Build??
    Azure build
  • ?????Job????????????
    build job detail
  • ??????????????Push??
    aliyun image detail
  • ?Azure ?????Release ??????
    preview release
    pipeline
    release detail

  • ?????????5003???????
    add port
  • ??????????+???
    preview webnotebook

??

????????Linux?????????????????????????