使用Gitlab-CI 实现NetCore项目Docker化并部署到阿里云K8S

2022-12-07,,,,

使用Gitlab-CI 实现NetCore项目Docker化并部署阿里云K8S

先行条件:

1.了解NetCore项目基础命令,如dotnet publish   等几个常用命令。

2.了解Docker基础命令

3.了解centos基础命令

部署步骤:

大致会分为如下几个步骤,后面会详细解析

1.安装 Runner

2.注册 Runner

3.安装Docker环境

4.编写Dockerfile 脚本

5.编写 .gitlab-ci.yml 脚本

6.Push 代码

步骤分解:

1.安装 Runner (文章末尾有专门的命令解析参考地址)

Runner简介:Runner是配合 gitlab-ci 一起使用的,它可以拉取gitlab的代码并且执行一些命令,例如编译代码  发布代码等。并且最后将结果通知给Gitlab-CI。

我们Runner可以安装在任何系统的任何位置,我这里把他安装在centos系统中。

安装步骤如下:

1.1.为您的系统下载其中一个二进制文件:

  sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

1.2.赋予它执行权限:

  sudo chmod +x /usr/local/bin/gitlab-runner

1.3.创建GitLab CI用户:

  sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

1.4.安装并作为服务运行:

  sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

  sudo gitlab-runner start

2.注册 Runner

 注册ranner是指让你刚刚安装的 runner与你的gitlab关联起来,使他有权限来访问你的代码并且发送通知给gitlab-ci。

2.1.执行注册命令

  先开打如下图的页面找到 URL 和 Token

  

 

然后执行如下命令:(文章末尾有专门的命令解析参考地址)

sudo gitlab-runner register

会提示您输入 URL和Token 先把上图的URL 复制  粘贴 回车。然后在复制Token 粘贴 回车。

下面就会提示输入 Please enter the gitlab-ci description for this runner (输入描述)可直接跳过或者随便输入或稍后再GitLabUI中输入都可以。

接着提示输入 Please enter the gitlab-ci tags for this runner (comma separated)  (输入标签信息 )可直接跳过或稍后再GitLabUI中输入都可以。

接着提示输入 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh (输入Runner执行程序)我们这里输入 : shell  然后回车。

最后到这里就说明runner已经安装成功并且跟项目关联成功了,我们可以在 GitLab中查看 如下图:

3.安装Docker环境

安装Docker环境是为了我们把应用打包成 DockerImage 后上传到 阿里云的镜像仓库中 用于k8s的应用部署。

安装Docker大家自行 google 这里不再介绍如何安装。

4.编写Dockerfile 脚本

 如果是NetCore 项目Dockerfile 脚本应该在项目的根目录下面(官方推荐)如下图是我的项目。

Dockerfile 内容:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
COPY . /root/publish/api.config.internal
WORKDIR /root/publish/api.config.internal
ENV ASPNETCORE_URLS http://*:7012
ENV ConnectionStrings_ConfigDbContext server=10.10.10.228;port=5566;user id=uat_ApiConfigCenterManage_Web;password=4g4TesrWg4;database=ApiConfigCenterDB;persistsecurityinfo=True;SslMode=None
ENV LoggerSettings_ClientName configcenter.internalapi_aliyun_log
ENV LoggerSettings_ServerUrl http://ulog.colipu.com:8080/v1/logs
ENV LoggerSettings_Level error
ENTRYPOINT ["dotnet", "Api.ConfigCenter.InternalApi.dll"]

以上为的Docfile脚本,大家可以参考一下,脚本中必须的命令是  FROM COPY  WORKDIR ENTRYPOINT 这几个。至于为什么这样写大家可参考Docker官方文档。

5.编写 .gitlab-ci.yml 脚本

如果你的是netCore项目并且有解决方案,那么这个脚本应该创建在解决方案的根目录下。

如下图:

.gitlab-ci.yml 脚本内容:

stages:
- build-image-internalapi
- build-image-manageapi
job1:
stage: build-image-internalapi
only:
refs:
- tags
variables:
- $CI_COMMIT_TAG =~ /^internalapi-.*/
script:
# The output path is relative to the position of the csproj-file
- dotnet publish -c Release -o ../publishinternalapi ./Api.ConfigCenter.InternalApi/Api.ConfigCenter.InternalApi.csproj --configfile Nuget.config
- docker login -u sunqiang@aliyun -p Aa123456 registry-vpc.cn-shanghai.aliyuncs.com
- cd publishinternalapi/
- docker build -t registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-internalapi:$CI_COMMIT_TAG .
- docker push registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-internalapi:$CI_COMMIT_TAG job2:
stage: build-image-manageapi
only:
refs:
- tags
variables:
- $CI_COMMIT_TAG =~ /^manageapi-.*/
script:
# The output path is relative to the position of the csproj-file
- dotnet publish -c Release -o ../publishmanageapi ./Api.ConfigCenter.ManageApi/Api.ConfigCenter.ManageApi.csproj --configfile Nuget.config
- docker login -u sunqiang@aliyun -p Aa123456 registry-vpc.cn-shanghai.aliyuncs.com
- cd publishmanageapi/
- pwd
- docker build -t registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-manageapi:$CI_COMMIT_TAG .
- docker push registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-manageapi:$CI_COMMIT_TAG

注意:yml脚本对格式要求非常严格,所以不能有一点儿错。

有了这个脚本之后,在你每次Push代码的时候GitLab会检测到项目的根目录是否有.gitlab-ci.yml 文件 如果有就会执行里面的内容。

脚本解释:(文章末尾有专门的命令解析参考地址)

stages:表示 定义构建的阶段,我这里定义了2个阶段,因为我这个解决方案下面有两个项目。也可以定义一个那就是把脚本全部写在一块了。
job1:表示第一个阶段的名字,这个是自定义的。
stage:定义job stage (默认:test) 表示要执行哪个阶段
only:定义一列git的分支。 refs tags 表示只通过标签的形式触发job任务来构建应用(如果不这样写,那你每次推送代码的时候都会执行job)
variables:表示在job中是可以使用关键字variables来定义job变量。
$CI_COMMIT_TAG =~ /^manageapi-.*/ 表示我以标签 manageapi-v1 为例 要推送的项目 如下截图


script:必须存在的。在这里面的可以写任何脚本命令。其实就是命令行。
当最后执行完 docker push 命令的时候我的阿里云镜像仓库就可以看到了


最后就是通过当前镜像部署K8S应用了。
参考文档:
Gitlab CI yaml官方配置文件翻译:https://segmentfault.com/a/1190000010442764
Gitlab Runner:https://docs.gitlab.com/runner/install/linux-manually.html

使用Gitlab-CI 实现NetCore项目Docker化并部署到阿里云K8S的相关教程结束。

《使用Gitlab-CI 实现NetCore项目Docker化并部署到阿里云K8S.doc》

下载本文的Word格式文档,以方便收藏与打印。