LNMP環境,Gitlab,Runner 持續化自動部署整理(詳解踩過的坑)

本文將以LNMP+Docker自動化部署項目

1.安裝Docker

安裝原文參照:https://blog.csdn.net/qq_36892341/article/details/73918672

2.基於Docker搭建LNMP環境

安裝原文參照:https://blog.csdn.net/xy752068432/article/details/75975065

照着上面安裝基本沒問題

注意下nginx.conf配置文件支持php的完整代碼

    server {
        listen       80; #指定端口
        server_name  127.0.0.1; #指定服務名

    	root           /var/www/html;  #指定訪問的路徑(放代碼位置)
        location / {
            index  index.html index.htm index.php l.php;
           autoindex  off;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
		location ~ \.php$ {
		    fastcgi_index  index.php;
		    fastcgi_pass   phpfpm:9000;
		    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
		    include        fastcgi_params;                                                                                                                                                                         
		}
    }

3.安裝gitlab

安裝原文參照:https://blog.csdn.net/qq_21592575/article/details/88777368

4.在Docker安裝並配置GitLab Runner

gitlab runner作用:提交合並代碼時,指定分支在runner安裝的服務器進行某些操作(執行sh文件);比如提交更新代碼時,把代碼更新到服務器,並設置某些文件過濾不更新。

(1)使用命令在Docker中安裝Gitlab Runner

docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/nginx/www/html:/var/www/html \
gitlab/gitlab-runner:latest

-v 添加目錄映射,即主機上的/var/nginx/www/html和容器中/var/www/html目錄是同步的

注意1:加上 -v /var/nginx/www/html:/var/www/html 這個目錄映射,是爲了同步服務器運行的代碼到對應的目錄,/var/nginx/www/html和上面講安裝nginx的路徑是一樣的。
(2)註冊並設置Gitlan Runner

[1]訪問Gitlab獲取http://你的gitlab地址/admin/runners (爲了獲取下步註冊用的token)

[2]運行Gitlab Runner註冊設置

命令行:

docker exec -it gitlab-runner gitlab-runner register
#請輸入gitlab-ci協調員URL(例如https://gitlab.com/),沒有公網域名的輸入ip,因爲上面的gitlab教程是用端口8081的,別忘記加上。例:http://ip:8081/
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
#請輸入此runner的gitlab-ci令牌,上一步獲得的token
Please enter the gitlab-ci token for this runner:
33gfh5i****2R54
#請輸入該runner的gitlab-ci描述,隨便填
Please enter the gitlab-ci description for this runner:
[6d1bc8938869]:
#請輸入這個runner的gitlab-ci標籤(逗號分隔),這個標籤作爲標示,爲了後面yml文件指定runner
Please enter the gitlab-ci tags for this runner (comma separated):
demo
#是否運行未標記的構建[true/false]:
Whether to run untagged builds [true/false]:
[false]: true
#是否將Runner鎖定到當前項目[true/false]:
Whether to lock the Runner to current project [true/false]:
[true]: true
Registering runner... succeeded                    
runner=eF8wyziy
#請輸入執行器(這裏選擇shell就可以了,因爲我們的lnmp環境不在這個容器搭建,只需執行shell操作,把代碼更新到映射的目錄就可以了)
Please enter the executor: parallels, shell, ssh, virtualbox, docker+machine, docker, docker-ssh, docker-ssh+machine, kubernetes:
shell

註冊好之後就能在Runners看見了

安裝好Runner後要配置 .gitlab-ci.yml 文件讓runner執行shell,.gitlab-ci.yml 文件放在根目錄下

例子:

stages:
  - deploy 

variables:
  PREVIEW_PROJECT_DIR: "/data/project/$CI_PROJECT_DIR"    #定義更新代碼到服務器的路徑
  RSYNC_SOURCE_DIR: "$CI_PROJECT_DIR"                     #項目目錄

# 可自定義任務名稱
job1:
  stage: deploy        
  tags:
    - news      #標籤,上面配置runner的標題,指定runner執行
  only:
    - master    #指定合併更新的分支
  script:       #runner執行的代碼
    - "set -xe"
    - "mkdir -p $PREVIEW_PROJECT_DIR"                         #創建目錄文件夾
    - > 
    - "rsync -auv  $RSYNC_SOURCE_DIR/ $PREVIEW_PROJECT_DIR/"  #拉取代碼到指定目錄下

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章