CentOS 7 安裝 Git 與 Gitlab-runner 實例
-
查看系統版本
lsb_release -a
本筆記系統環境:
LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.5.1804 (Core) Release: 7.5.1804 Codename: Core
-
安裝 git 源碼構建
-
檢查 git 當前版本
# 執行 git --version
# 顯示老版本 git version 1.8.1
-
安裝編譯依賴軟件
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc yum install gcc perl-ExtUtils-MakeMaker
-
卸載老版本
yum remove git
-
下載最新版本
cd /usr/local/src/ wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.xz tar -vxf git-2.9.5.tar.xz cd git-2.9.5
-
編譯
# 編譯時發生錯誤,可能未安裝依賴軟件包 make prefix=/usr/local/git all make prefix=/usr/local/git install
-
環境變量設置
-
Root 用戶
# 添加環境變量 echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile # 生效環境變量 source /etc/profile
-
其他用戶,
登錄該用戶,配置該用戶下的環境變量
echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc source ~/.bashrc
-
-
驗證
git --version
git version 2.9.5
-
-
安裝 gitlab-runner
根據官方文檔可快速安裝,本文只修了
安裝
路徑,並手動授權。/gitlab/
爲安裝路徑,gitlab-runner
爲執行文件-
下載執行文件
# sudo wget -O /保存路徑 https://網址 sudo wget -O /gitlab/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
-
允許運行權限
sudo chmod +x /gitlab/gitlab-runner
-
創建 gitlab-runner 用戶
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
-
安裝作爲服務運行
# 由於沒有設置爲全局變量,添加爲服務時需要指定絕對路徑 # 由於該服務指定用戶 gitlab-runner 執行,所以需要給該用戶配置相關文件夾權限 # --working-directory 參數爲配置緩存地址,可自定義,注意需要文件夾權限 sudo /gitlab/gitlab-runner install --user=gitlab-runner --working-directory=/gitlab/work
/gitlab/work
該目錄會生成一些gitlab-runner
的配置文件,與高速緩存倉庫,需要授權權限。如果不需要強調權限問題,可以使用
chmod
分配:chmod 777 /gitlab/work/
ps:該權限方案適合快速使用,web 站點目錄,都需要權限。
如需要強調權限,可以使用
ACL
分配製定用戶:# 給用戶 gitlab-runner 設置 /gitlab/work 文件夾 rwx 權限 setfacl -m u:gitlab-runner:rwx -R /gitlab/work # 設置後期添加的子目錄和文件繼承父目錄權限 setfacl -m d:u:gitlab-runner:rwx -R /gitlab/work
ps:該方案需要一定的權限分配能力
-
用戶權限
本文使用
apache
代理網站。將
apache
用戶添加至gitlab-runner
組,允許apache
有權讀寫gitlab-runner
生成的文件usermod -a -G 'gitlab-runner' apache
實例
[[email protected] /]# usermod -a -G 'gitlab-runner' apache [[email protected] /]# id apache uid=48(apache) gid=48(apache) groups=48(apache),1000(gitlab-runner) [[email protected] /]# id gitlab-runner uid=1000(gitlab-runner) gid=1000(gitlab-runner) groups=1000(gitlab-runner)
-
註冊服務
文檔非常詳細,自行查找
本文使用
shell
常規執行,功能有限,更多高級功能查看官方文檔 -
運行程序
sudo /gitlab/gitlab-runner start
-
更多功能
-
-
gitlab-ci.yml 例子
使用簡單的
shell
與git
代碼,實現拉取倉庫文件至指定位置# 工作類型標籤 stages: # 自定義工作類名稱 - 同步 # 自定義工作名 dev: tags: # 註冊 gitlab-runner 時填寫的機器人標籤 - gitlab stage: 同步 script: # 打開目錄 - cd /Web # 判斷是否存在 .git 文件 true 狀態 - if [ ! -d ".git" ];then # 初始化倉庫 - git init - else # 已經存在,跳過 git 倉庫初始化 - echo 'skip git int' - fi # 執行 git 根據倉庫路徑拉取文件 - git pull $CI_PROJECT_DIR environment: # 環境名稱 name: dev only: # 只有指定分支推送時才執行 - dev