.Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)
- 2019 年 10 月 3 日
- 笔记
??
linux ???????????????? core ??,??????? gitlab+jenkins+centos???????????????????????????????????????????????????c#???????????????????Azure?????MS???????
????
- GitHub ==>GitHub ????
- ????????? ? https://hub.docker.com
- Azure DevOps ??=?https://azure.microsoft.com/zh-cn/services/devops/
- ????? ps : ??299??????ECS
?? ??GitHub
- ??VS???
ASP.NET Core Web ????
????WebNotebook
- ?????????? Docker ????? ??
- ???????????Docker???????????
- ???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 ??
??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(????????)? |
????????????
- ?????????
Release
,?????Pull???Run??
- ??Agent???????
Push and Run
??????????????
- ?????
Push and Run
???+
????Task
- ??
SSH
???ADD
????????????????????
- ??
Manage
????????????,???? SSH Service connection
- ??????????
ssh_aliyun
? SSH service connection
- ???????????????
Inline Script
????????task
- ???????task???
-
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
-
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
-
Pull Image
#????????push??? docker pull registry.cn-hangzhou.aliyuncs.com/zohnz/webnotebook
-
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
-
???????? build pipeline
???????
- ?????????????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 ????
- ??Azure??Build??????????????????????Build??
- ?????Job????????????
- ??????????????Push??
-
?Azure ?????Release ??????
- ?????????
5003
???????
-
??????????+???
??
????????Linux?????????????????????????