需求说明

根据领导要求,要把python 项目移到Gitlab 进行管理,并利用Gitlab CI/CD 进行自动化测试,打包,部署。(听起来很简单吧)

比较头大,完全没有经验,python 也是刚上手两个月,什么Gitlab, Gitlab runner 完全没听说过,powershell 也不常用,可以说是零起步。疯狂的查询资料,国内网站,国外网站,七八十篇博客文档,总算是出了点成果。

坏境:(关于环境的安装部署,大家另行查询资料吧)

1.Linux 服务器: 安装Gitlab https://about.gitlab.com/

2.Window 服务器:安装Gitlab runner(https://docs.gitlab.com/runner/), powershell(系统自带), pyinstaller(https://www.pyinstaller.org/打包python使用),Git(https://git-scm.com/与Git lab交互拉取,推送代码).

 

Gitlab runner for windows 10

我们这里重点说一下Gitlab runner 的安装,注册,使用.

install

1. 把下载好的exe 文件放到路径 C:\GitLab-Runner 文件夹下,命名为gitlab-runner.exe

 

 

2. 以管理员身份运行CMD

输入以下命令

gitlab-runner.exe install

 

 REGISTER

输入以下命令 进行注册

gitlab-runner.exe register

 

 输入GitLab instance URL:(gitlab 部署的服务器网址)

在gitlab ->Settings->CI/CI:   找到Runner 展开后可以找到 URL 和 token。

 

 

输入URL, Token, description for the runner, tags for the runner

tags 很重要,因为当你在脚本里使用他的时候,可以根据tags 指定要使用的runner. 因为每个runner可以有不同的系统环境,你要用的可能是特殊的。

最后选择excutor:我这里选的是shell.

可选项如下:Enter an executor: virtualbox, docker-ssh+machine, kubernetes, custom, docker-windows, docker-ssh, shell, docker, parallels, ssh, docker+machine

 

 注册成功!这时候服务gitlab runner 应该已经启动了

可以去gitlab 服务器查看结果。

 

代码管理

Gitlab 新建一个test项目, 用于我们本次的自动化测试。

 

 

在自己的PC 上使用Git bash 拉取刚刚建的test项目(网址写你自己的, git clone 命令)

git clone https://code.com/GAS_GC_IT/test.git

项目结构如下:

重点关注一下 .gitlab-ci.yml、build_binaries.ps1、test.py 这三个文件

.gitlab-ci.yml:这个文件是CI/CD 执行时的配置文件,网上很多教程,可以看看如何配置。https://docs.gitlab.com/ee/ci/quick_start/

build_binaries.ps1:这个文件是powershell 要执行的脚本文件

test.py:是我们的python 脚本

 

 

 这里我要进行的操作是:客户端PC push 代码到gitlab 的时候,利用gitrunner 执行job 对python 脚本进行打包

.gitlab-ci.yml 内容如下:

build:
  tags:
    - wz94
  script:
    - "powershell.exe -File  C:\\GitLab-Runner\\builds\\7zyx1xwC\\0\\GAS_GC_IT\\test\\build_binaries.ps1"

build_binaries.ps1 内容如下, 利用pyinstaller 打包 test.py脚本

pyinstaller.exe -F "test.py"

test.py 内容如下

if __name__ == "__main__":
    print('TEST......')

 

划重点

一号坑

在安装gitlab-runner.exe的时候会生产一个配置文件:config.toml(for window. 其他系统可能名字不同)

 

 

 看一下它的内容(它的内容是可以修改的!!!!)

shell = 'pwsh'要改成shell = 'powershell',否则在gitlab 运行的时候,会报错stackoverflow

exec: “pwsh”: executable file not found in %PATH%

另外有关配置还可以查看文档https://docs.gitlab.com/runner/configuration/advanced-configuration.html

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "wz94"
  url = "https://code.com/"
  token = "id7_cuyyyA-xWzNhtdsB"
  executor = "shell"
  ***shell = "pwsh"***
  shell = "powershell"

[runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

二号坑

 这个路径要搞清楚,你可能不知道它应该是哪个,这是因为你还没有理解gitlab runner的运行原理。gitlab runner 和gitlab 在不同的服务器,他们之间是怎么通信的?

答案是SSH(一种网络协议),SSH用于计算机之间的加密登录,还记得我们注册gitlab runner 时输入的 URL 和token么。

实际上在我们push 代码到gitlab 后,会触发CI/CD 的pipline job ,这时候,gitlab runner 会从gitlab 拉取(clone )代码到 gitlab runner 服务器,没错,我们已经在gitlab runner 服务器上配置好所需要的系统环境,我们可以在这台服务器上对代码进行打包(利用pyinstaller),那它把代码拉到哪里了?请看:

 

 Reinitialized existing Git repository in C:/GitLab-Runner/builds/7zyx1xwC/0/GAS_GC_IT/test/.git/

起码C:/GitLab-Runner 你是知道的,后面的路径是根据项目自动生成的。当然你会问了,能自己设置路径么?答案是肯定的。还记得config.toml么?

[runners.custom_build_dir] 改成自己的路径(未经尝试,可以自己实验)

 

 

"powershell.exe -File  C:\\GitLab-Runner\\builds\\7zyx1xwC\\0\\GAS_GC_IT\\test\\build_binaries.ps1"

 

结果

自己尝试吧兄嘚!祝你成功!

 

posted on 2021-08-26 14:46  姬如千泷  阅读(2093)  评论(0编辑  收藏  举报