京东-优惠雷达
箱包品类日
嗨翻暑期档
随心所动,自在安睡
自营热卖

.net core 自动部署 gitlab-runner CI CD入门

翩若惊鸿 1年前   阅读数 186 0

1.准备工作

1.准备2台linux CentOS-7虚拟机
2.本文演示虚拟机IP 192.168.33.100和192.168.33.101
3.33.100机器上安装gitlab-runner和.net core SDK用来做CI CD
4.33.101机器安装.net core SDK和Supervisor守护进程模拟测试服务器

2.配置gitlab-ruuner

1.下载gitlab-ruuner

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

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

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

2.赋予权限

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

3.创建GitLab CI用户

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

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

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

5.注册gitlab-ruuner

sudo gitlab-runner register

6.输入gitlab URL

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com 

7.输入token

Please enter the gitlab-ci token for this runner
xxx

8.输入描述(可空)

Please enter the gitlab-ci description for this runner
[hostname] my-runner

8.输入tags(可空)

Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag

8.选择runner执行程序(本文用的是shell)

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell

在这里插入图片描述

3.安装.net core SDK

1.切换账号(最好用gitlab-runner账号去安装SDK要不后期权限问题很麻烦)33.101机器不用切换账号直接安装就可以

su gitlab-runner

2.安装.net core (下载地址:https://dotnet.microsoft.com/download/dotnet-core/2.2#sdk-2.2.402)

//我是用的本地安装包
mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-2.2.402-linux-x64.tar.gz -C $HOME/dotnet 
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

4.配置服务器之间的ssh登录

1.设置公钥

//我是用的本地安装包
ssh-keygen -t rsa
//接着一路回车,然后可以键入以下命令查看生成的内容

2.生成文件回生成2个文件id_rsa.pub 和id_rsa

cd /home/gitlab-runner/.ssh
ls -a
.  ..  authorized_keys  id_rsa  id_rsa.pub  known_hosts

3.在机器33.100上输入以下命令,将机器33.100的公钥发送给机器33.101

ssh-copy-id root@192.168.33.101 

4.接着按提示输入yes或回车,最后需要输入机器33.101的密码,成功的话将会看到

Number of key(s) added: 1

5.尝试登录机器33.101

ssh root@192.168.33.101

5.安装Supervisor守护进程

1.安装Python包管理工具

yum install python-setuptools

2.安装Supervisor

easy_install supervisor

3.配置Supervisor应用守护

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

4.修改supervisord.conf配置

vi /etc/supervisor/supervisord.conf

[unix_http_server] 节点 把file目录修改下supervisor.sock 在tmp目录会丢失
;file=/tmp/supervisor.sock 
file=/var/run/supervisor.sock  
[supervisorctl] 节点 修改serverurl
;serverurl=unix:///tmp/supervisor.sock
serverurl=unix:///var/run/supervisor.sock 

最后一行添加
[include]
files = conf.d/*.conf 

5.创建应用守护配置

mkdir /etc/supervisor/conf.d
vi  /etc/supervisor/conf.d/webapi.conf //weiapi是我项目的名称
[program:webapi] ;程序名称,终端控制时需要的标识
command=/home/lvtao/dotnet/dotnet /home/www/webapi.dll ; 运行程序的命令
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/var/log/webapi.err.log ; 错误日志文件
stdout_logfile=/var/log/webapi.out.log ; 输出日志文件
environment=ASPNETCORE_ENVIRONMENT=Production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=INT

6.运行supervisord,查看是否生效

supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep webapi

7.详细资料请看https://www.jianshu.com/p/39b476e808d8

6.配置gitlab-ci.yml

1.创建一个.net core项目
2.创建一个gitlab-ci.yml文件
在这里插入图片描述
gitlab-ci.yml配置

image: microsoft/dotnet:latest

stages:
    - build
    - deploy

build:
    stage: build
    variables:
        PATH_publish: '/home/gitlab-runner/publish$CI_PIPELINE_ID'
        PATH_dotnet: '/home/gitlab-runner/dotnet/dotnet'
    only:
       - /^issue-.*$/i  #issue-开头的分支也会自动构建
       - master
    script:
        - echo "安装构建依赖阶段"
        - echo "$PATH_publish"
        - echo "$PATH_dotnet"
        - "ls -l"
        - echo ".net core 编译"
        - "cd webapi" 
        - "$PATH_dotnet build"
        - echo ".net core 发布"
        - "$PATH_dotnet publish"
        - "cd bin/Debug/netcoreapp2.1"
        - "rm -rf $PATH_publish"
        - "mkdir $PATH_publish"
        - "cp -R publish/* $PATH_publish"

        
deploy_fat:
  stage: deploy
  variables:
    PATH_publish: '/home/gitlab-runner/publish$CI_PIPELINE_ID'
  only:
    - /^issue-.*$/i #issue-开头的分支也会自动构建
    - master
  when: manual
  script:
    - echo "$PATH_publish"
    - echo "登录项目部署服务器,移除旧版本项目文件,最后将打包好的文件拷贝过去"
    - "ls -l"
    - "cd $PATH_publish" 
    - pwd
    - whoami # gitlab-runner
    - "ssh root@192.168.33.101 supervisorctl stop webapi"
    - "ssh root@192.168.33.101 rm -rf /home/www/*"
    - "scp -r -P 22 ./* root@192.168.33.101:/home/www"
    - "ssh root@192.168.33.101 supervisorctl start webapi"
  environment:
    name: staging

这样就全部搞定了以后没次提交代码都会自动编译,但是发布需要手动点击(这个是我在脚本中加了when: manual 设置)
在这里插入图片描述

7.参考资料

https://blog.csdn.net/u011215669/article/details/80446624
https://docs.gitlab.com/ee/ci/environments.html
https://blog.csdn.net/weixin_34353714/article/details/93165210
https://www.jianshu.com/p/39b476e808d8


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: