代码改变世界

天行健,君子以自强不息

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

概述

本文讲述的是代码签入Gitlab后,Gitlab Runner自动运行

  • dotnet build,自动生成.NET项目;
  • dotnet pack,自动制作nupkg的包;
  • dotnet push,自动将nupkg包推送到NuGet服务器。

准备工作

需要两个步骤

  • 安装Gitlab服务器;
  • 安装Gitlab Runner;
  • 将Gitlab Runner注册到Gitlab,并且注册时使用装有DOTNET 8.0的映像。
  • 用Visual Studio 2022创建一个空白的基于DOTNET 8.0的类库项目

当前Gitlab Runner的配置

Gitlab Runner当前的配置环境是注册时自动生成的,贴在这里存档。我们要详细讲解修改默认配置文件的步骤。

config/config.toml配置文件内容如下:

concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "东方大力士甲"
  url = "https://gitlab.amihome.cn"
  id = 3
  token = "glrt-hY7-gcCwwsp4Rsa-8NbR"
  token_obtained_at = 2024-03-01T10:34:14Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    tls_verify = false
    image = "docker.amihome.cn/amihome/gitlab/docker-linux-dotnet8.0:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    network_mtu = 0

用Visual Studio 2022创建一个空白的基于DOTNET 8.0的类库项目

img

img

img

img

在Gitlab中创建空白项目,用于托管本文的示例代码

img

img

img

在上图中,我们复制到的网址是

https://gitlab.amihome.cn/brain/auto-pack-sample.git

在Visual Studio 2022中签入代码到Gitlab

img

img

img

img

在解决方案根目录下,创建“.gitlab-ci.yml”

img

内容如下:

# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages

stages:          # List of stages for jobs, and their order of execution
  - build

variables:
    VERSION_ID : 2022.12.$CI_PIPELINE_IID
    NUPKG_OUTPUT_DIR : $CI_PROJECT_DIR/nupkg
    NEXUS_REPO: "https://nuget.amihome.cn/repository/yee-change/"
    NUGET_API_KEY: "5f7aebee-534b-32ff-9c4b-e9cfb04882f6"
    # Use no compression for caches
    CACHE_COMPRESSION_LEVEL: "fastest"

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  before_script:
    - bash $DOWNLOAD_ALL_DIRECTORY_BUILD_PROPS_FILES_SH $DIRECTORY_BUILD_DIST_SERVER_URL
  script:
    - echo "Compiling the code..."
    - cd .
    - dotnet build "AutoPackProjectSample/AutoPackProjectSample.csproj" -c Release -p:Version=$VERSION_ID
    - dotnet pack "AutoPackProjectSample/AutoPackProjectSample.csproj" -c Release --no-build -o $NUPKG_OUTPUT_DIR -p:PackageVersion=$VERSION_ID
    - echo "即将推送nupkg包到NuGet仓库..."
    - cd $NUPKG_OUTPUT_DIR
    - dotnet nuget push *.$VERSION_ID.nupkg -k $NUGET_API_KEY -s $NEXUS_REPO
    - echo "构建、打包和推送,成功执行。."

签入文件

img

img

接下来就是开启自动继承的步伐了:

逐步修改Gitlab Runner的config.toml

待我们在Visual Studio中,解决方案根目录下新建“.gitlab-ci.yml”文件,内容如下:

然后我们就签入代码到Gitlab。

很快我们就看到第一个错误:

Gitlab Runner自身缺少docker.sock的映射

img

img

解决办法

在Gitlab Runner的docker-compose.yml里加一个磁盘目录映射

version: '3'
services:
  cap:
    image: 'docker.amihome.cn/amihome/gitlab/runner:2024'
    scale: 2
    restart: always
    privileged: true
    volumes:
     - ./config:/etc/gitlab-runner
     - /var/run/docker.sock:/var/run/docker.sock #增加这一行

然后重新启动Gitlab Runner

root@dev-server:/data/gitlab.amihome.cn/gitlab-runners# docker-compose restart
Restarting gitlab-runners_home_1 ... done
Restarting gitlab-runners_home_2 ... done

第二个错误,获取docker-linux-dotnet8.0的映像时出错

解决了第一个错误,在Gitlab里面重新执行刚才失败的作业。

img

img

“作业”依然失败:

img

img

上边的错误信息,就是说在docker pull之前需要先docker login。那么用户名和密码我们放置在哪里呢?

这就是第一次修改Gitlab Runner的config.toml:

img

然后重新启动Gitlab Runner

root@dev-server:/data/gitlab.amihome.cn/gitlab-runners# docker-compose restart
Restarting gitlab-runners_home_1 ... done
Restarting gitlab-runners_home_2 ... done

成功执行如下:

img
img

至此,自动构建就成功了。上述日志里面能看到包的版本时2022.12.2,最后一个2是Gitlab CI提供的变量,每次作业都是不同的,也就是版本号会自动增长。

我们去NuGet仓库查看一下我们刚才的包

img

上图高亮的地方就是Gitlab Runner自动构建并推送的2022.12.2的AutoPackSample项目的包。

posted on 2024-03-01 18:48  终南山人  阅读(94)  评论(0编辑  收藏  举报