.Net Core Automated Deployment Series (three): Use GitLab CI / CD automatically deploy Api to Docker

Jenkins wrote before use to automate deployment, recently studied the automated deployment just fine under GitLab, the way to record it.

Use GitLab we need to prepare to deploy two things, first you have to have at least GitLab, build their own or use the official can ha, I've used the official, want to build their own students can refer to this, the use of Docker build gitLab:

https://www.imooc.com/article/23168

Once you have GitLab we also need to install their own deployment GitLab Runner, GitLabRunner is used to pull GitLab warehouse code, to compile and deploy the code you have to .gitlab-ci.yml according to script, usually in order to spread the pressure and risks and GitLab GitLabRunner not on the same server, I here use the local virtual machine to install gitLab Runner.

 

Because I was to be deployed in Docker years, so before installing GitLab Runner we had better Docker installed, not installed students can refer to the following script:

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
systemctl enable docker
systemctl start docker

The new installation, then also the accelerator mirrored configuration, or download the image particularly slow.

 

Installation GitLabRunner

1. Add repository

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash

2. Install Package

https://blog.csdn.net/weiguang1017/article/details/77720778

3. Registration runner

sudo gitlab-runner register

Then we will be prompted to enter the appropriate information GitLab warehouse, we first went to GitLab the project you want to deploy, select Settings - CI / CD:

 

The first requirement input URL, the second asks for Token.

There step is to enter the label "Please enter the gitlab-ci tags for this runner (comma separated):", this step can be entered nothing, skip.

Then you will be asked whether to lock the current project "Whether to lock Runner to current project [true / false]:", enter False.

The last option implementation Runner, because I am not here to Docker deployment, so the input Shell.

After completing the refresh what GitLab just that page you will see the Runner of the information we have registered:

Then you edit it corresponds Runner, modify one place:

把上面标记的那个选项勾选上,表示无标签的任务也可以运行。

 

到这里还会有问题,因为Git Runner运行的时候默认会使用gitlab-runner用户去运行脚本,但是这个用户默认没有Docker的操作权限,所以如果直接测试会报错:“couldn't connect to Docker daemon at http+docker.......”,因此我们需要给这个用户开通下权限,在Git Runner服务器上执行如下脚本:

sudo groupadd docker

sudo gpasswd -a gitlab-runner docker

sudo service docker restart(或者systemctl start docker)

newgrp - docker

到这里Git Runner安装就算完成了,下面我们来测试。

 

 

自动部署测试

新建.net core Api项目 WebTest,项目里添加Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
Copy . .

RUN dotnet restore
RUN dotnet build -c Release -o /app

FROM build as publish
RUN dotnet publish -c Releease -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebTest.dll"]

然后在项目文件根目录下,注意是根目录哦,就是和解决方案同一级目录下添加.gitlab-ci.yml脚本文件,这个就是专门用来执行部署命令的,脚本规则参考官方:

https://docs.gitlab.com/ee/ci/yaml/README.html#only-and-except-simplified

stages:
  - deploy_dev
deploy_dev_job:
  stage: deploy_dev
  environment:
    name: development
  only:
    - master
  script:
    # 发布程序并部署运行
    - cd WebTest
    - docker stop webTest
    - docker rm webTest
    - docker rmi webtest
    - docker build -t webtest .
    - docker run -d --name webTest -p 8010:80 webtest

因为是测试,我这里很简单,标准是三个流程,我这里只用到了部署流程,script哪里就是我要执行的脚本命令,可以看到这里就是每次执行先把上次的容器和镜像删除,然后重新build一个镜像,部署到8010端口。

写好这些之后,我们就可以提交代码了,提交之后去GitLab中查看部署状态:

 

可以看到你提交的代码到底有没有部署成功,我们可以点击状态按钮查看详细信息:

 

 

如果失败了,可以通过这里查看具体的原因。可以看到我最新的提交已经通过来,OK,我们现在可以通过浏览器访问了,服务器IP:8010:

可以看到正常访问,看下Docker容器信息:

 

 OK,大功告成,本篇只是个小的Demo,下一篇会使用GitLab CI/CD实现商城项目 k8s的自动化编译、测试、发布流程。

Guess you like

Origin www.cnblogs.com/weiBlog/p/11257444.html