Gitlab + Gitlab runner + Window powershell


需求說明

根據領導要求,要把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"

 

結果

自己嘗試吧兄嘚!祝你成功!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM