gitlab+gitlab-runner搭建自動(dòng)化部署

前言

實(shí)現(xiàn)自動(dòng)部署

  • 開發(fā)人員上傳代碼到gitlab
  • gitlab-runner 檢測(cè)到操作便開始自動(dòng)部署
  • 部署檢測(cè)代碼的是java還是web,檢測(cè)需要部署的端口,實(shí)現(xiàn)根據(jù)代碼和端口部署
  • 部署完成發(fā)送信息到企業(yè)微信

分析實(shí)現(xiàn)步驟

  • 安裝gitlab-runner
  • 在需要部署java程序的機(jī)器上綁定 ,tags為test部署java程序
  • 在需要部署web程序的機(jī)器上綁定 ,tags為test部署web程序
  • 編寫.gitlab-ci.yml放在項(xiàng)目的第一級(jí)目錄
  • 編寫shell腳本實(shí)現(xiàn)部署
  • 實(shí)現(xiàn)企業(yè)微信報(bào)警
  • 檢測(cè)是否自動(dòng)部署

安裝gitlab

Centos7搭建gitlab

部署gitlab-runner

使用gitlab-runner實(shí)現(xiàn)自動(dòng)部署

# 安裝repository
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
# 安裝gitlab-runner
yum install gitlab-runner

在需要部署的機(jī)器上注冊(cè)

url以及token獲取

url以及token位置
[root@test2 SHELL]# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=53435 revision=d0b76032 version=12.0.2
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.0.71/gitlab/   #輸入U(xiǎn)RL
Please enter the gitlab-ci token for this runner:
-rwbBw2y7GmL7smxuoxt  #輸入TOKEN
Please enter the gitlab-ci description for this runner:
[test2.laozios.com]: test    #后面腳本需要用到
Please enter the gitlab-ci tags for this runner (comma separated):
test                                   #后面腳本需要用到
Registering runner... succeeded                     runner=-rwbBw2y
Please enter the executor: parallels, kubernetes, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, shell, ssh:
shell                                 #通過shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
執(zhí)行過程

查看結(jié)果

  • 命令行查看
gitlab-runner list 
查看是否綁定成功
  • gitlab頁面查看


    查看結(jié)果

重復(fù)上面步驟

  • 剛剛綁定的是test應(yīng)該為對(duì)于的【java】【web】

編寫.gitlab-ci.yml文件

stages:
  - deploy
  - notice-success
  - notice-failure


package:Project:
  stage: deploy
  only:
    - /^feature.*$/
    - /^release.*$/
  variables:
    ############必須配置############
    ## 連接API的接口以及NGINX端口地址
    APIPORT: 9992
    NGINXPORT: 803

    #項(xiàng)目名稱即git名稱
    projectName: $CI_PROJECT_NAME
    #git idea 拉代碼的git地址
    gitUrl: $CI_REPOSITORY_URL
    #工程所在目錄
    baseDir: '/home/gitlab-runner/${CI_PROJECT_NAME}'
    ############選配############
    #打包所在目錄
    buildDir: '/home/gitlab-runner/builds'
    #打包環(huán)境
    branch: $CI_COMMIT_REF_NAME
  script:
    - bash /SHELL/installPack $CI_PROJECT_NAME  $CI_REPOSITORY_URL  $CI_COMMIT_REF_NAME $APIPORT  $NGINXPORT
  tags:
    - web

notice_job:Project:
  variables:
    branch: $CI_COMMIT_REF_NAME
    projectName: $CI_PROJECT_NAME
  stage: notice-failure
  only:
    - /^feature.*$/
    - /^release.*$/
  script:
    - if [[ ${branch:0:8} = "feature/" ]];then env="dev" ;elif [[ ${branch:0:8} = "release-" ]];then  env="test";fi
    - python3 /SHELL/buildNotice.py $env $branch 失敗  $projectName
  when: on_failure
  tags:
    - web

notice_job:Project:
  variables:
    branch: $CI_COMMIT_REF_NAME
    projectName: $CI_PROJECT_NAME
  stage: notice-success
  only:
    - /^feature.*$/
    - /^release.*$/
  script:
    - if [[ ${branch:0:8} = "feature/" ]];then env="dev" ;elif [[ ${branch:0:8} = "release-" ]];then  env="test";fi
    - python3 /SHELL/buildNotice.py $env $branch  成功  $projectName
  when: on_success
  tags:
    - web

實(shí)現(xiàn)環(huán)境判斷并實(shí)現(xiàn)部署腳本

#!/bin/bash
## 拉取代碼進(jìn)行安裝

########獲取編譯函數(shù)###########
source /SHELL/buildPack 
############必須配置############
#項(xiàng)目名稱即git名稱
projectName=$1
#git idea 拉代碼的git地址
gitUrl=$2
#打包分支
branch=$3
APIPORT=$4
NGINXPORT=$5

###########初始化參數(shù)###########

###############################
if [ -z $APIPORT ];then
   APIPORT="9991"
fi
if [ -z $NGINXPORT ];then
   NGINXPORT="802"
fi
###根據(jù)分支判斷環(huán)境
if [[ ${branch:0:8} = "feature/" ]];then
    env="dev"
    APIPORT=$APIPORT
    NGINXPORT=$NGINXPORT
elif [[ ${branch:0:8} = "release/" ]];then
    env="test"
    APIPORT="9999"
    NGINXPORT="801"
fi

###編譯目錄
buildDir="/home/gitlab-runner/$env/$projectName/$branch"

###進(jìn)行部署###
###判斷項(xiàng)目編譯目錄是否存在
if [ ! -d ${buildDir} ];then
    mkdir ${buildDir} -p
fi
cd ${buildDir}

###該分支目錄名字已經(jīng)存在則刪除該目錄重新拉起
if [ -e $projectName ];then
    rm -rf ${buildDir}/${projectName}
fi
## 拉取分支到本地
git  clone $gitUrl
## 判斷是否拉取成功,不成功退出
if [ '0' != $? ]; then 
    echo "注意:更新發(fā)生錯(cuò)誤!" 
    exit 1
fi
## 進(jìn)入項(xiàng)目并且切換分支
cd $projectName && git checkout $branch
if [ '0' != $? ]; then 
    echo "注意:切換分支發(fā)生錯(cuò)誤!"
    exit 3
fi
## 進(jìn)行編譯
if [ $projectName = "web" ];then
  ## 修改配置文件
  echo $NGINXPORT
  sed -i "s/\(^axios.defaults.baseURL = \).*/\1 \'http:\/\/192.168.0.xxx:$NGINXPORT\/api\\'/" src/main.js
  ## 進(jìn)行web編譯
  buildweb  
  ## 編譯成功進(jìn)行文件移動(dòng)
  if [ -d /ENV/$env/web/$NGINXPORT ];then
    ## 文件存在:判斷移動(dòng)目錄
    if [ ! -d /ENV/$env/BACKUP/web/$NGINXPORT ];then
        mkdir /ENV/$env/BACKUP/web/$NGINXPORT -p 
    fi
    ## 文件存在:移動(dòng)到上面目錄
    mv /ENV/$env/web/$NGINXPORT /ENV/$env/BACKUP/web/$NGINXPORT/web_`date "+%Y_%m_%d_%H_%M_%S"` && \
    mv ${buildDir}/$projectName/dist /ENV/$env/web/$NGINXPORT
  else
    echo "修改了端口先通知運(yùn)維"
    exit 6
  fi
  sed -i "s/\(proxy_pass.*:\).*/\1$APIPORT;/" /etc/nginx/conf.d/$NGINXPORT.conf 
  nginx -t && nginx -s reload
  
elif [ $projectName = "java" ];then
  ## 進(jìn)行java編譯
  buildjava
  ## 編譯成功進(jìn)行文件移動(dòng)
  if [ ! -d ${buildDir}/${projectName}/$branch/"$projectName"_build ];then
     mkdir ${buildDir}/$projectName"_build" -p
  fi
  cd ${buildDir}/$projectName"_build"
  unzip -oq ${buildDir}/$projectName/lw-admin/target/lw-admin.war -d ./
  sed -i "s/\(active:\).*/\1 $env/" WEB-INF/classes/application.yml  
  ## 備份以前的文件
  if [ -d /ENV/$env/tomcat/$APIPORT/ROOT ];then
    ## 文件存在:判斷移動(dòng)目錄
    if [ ! -d /ENV/$env/BACKUP/tomcat/$APIPORT ];then
        mkdir /ENV/$env/BACKUP/tomcat/$APIPORT -p 
    fi
    ## 文件存在:移動(dòng)到上面目錄
    mv /ENV/$env/tomcat/$APIPORT/ROOT /ENV/$env/BACKUP/tomcat/$APIPORT/ROOT_`date "+%Y_%m_%d_%H_%M_%S"` && \
    echo "`ls ${buildDir}/"$projectName"_build`"
    echo "`pwd`"
    mv ${buildDir}/"$projectName"_build /ENV/$env/tomcat/$APIPORT/ROOT
    docker restart "$APIPORT"_tomcat
  else
    echo "修改了端口先通知運(yùn)維"
    exit 6
  fi
else
  ## 沒有在這次自動(dòng)化規(guī)劃之內(nèi)
  echo "該項(xiàng)目$projectName沒有該這樣自動(dòng)化部署之內(nèi)"
  exit 3
fi

實(shí)現(xiàn)編譯

[root@test2 SHELL]# vim buildPack 

##編譯web
buildweb(){
    cnpm  install && \
    cnpm rebuild node-sass && cnpm run build
    if [ $? = 0 ];then
       echo "編譯成功"
    else
       echo "編譯失敗"
       exit 4
    fi
}

##編譯java
buildjava(){
    mvn clean && \
    mvn install && \
    mvn package
    if [ $? = 0 ];then
       echo "編譯成功"
    else
       echo "編譯失敗"
       exit 4
    fi
}

獲取企業(yè)微信信息

  • 創(chuàng)建應(yīng)用


    創(chuàng)建應(yīng)用
  • 查看綁定信息


    綁定信息

實(shí)現(xiàn)企業(yè)微信通知

[root@test2 SHELL]# cat  buildNotice.py 
#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
import requests
import sys
import time

##參數(shù)
corpid = "wwXXXXX"
secret = "LXXXXXXa64Bc"
agentid = "100XXX2"
#corpid = "wwXXXX2c"
#secret = "_fhXXXXXXXXp-VpWvJc9U78"
#agentid = "100XXXX3"

localtime = time.asctime(time.localtime(time.time()))



class WeChat(object):
    def __init__(self, corpid, secret, agentid):
        self.url = "https://qyapi.weixin.qq.com"
        self.corpid = corpid
        self.secret = secret
        self.agentid = agentid

    # 獲取企業(yè)微信的 access_token
    def access_token(self):
        url_arg = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format(
            id=self.corpid, crt=self.secret)
        url = self.url + url_arg
        response = requests.get(url=url)
        text = response.text
        self.token = json.loads(text)['access_token']

    # 構(gòu)建消息格式
    def messages(self, msg):
        values = {
            "touser": '@all',
            "msgtype": 'text',
            "agentid": self.agentid,
            "text": {'content': msg},
            "safe": 0
        }
        # python 3
        self.msg = (bytes(json.dumps(values), 'utf-8'))
        # python 2
        #self.msg = json.dumps(values)

    # 發(fā)送信息
    def send_message(self, msg):
        self.access_token()
        self.messages(msg)

        send_url = '{url}/cgi-bin/message/send?access_token={token}'.format(
            url=self.url, token=self.token)
        response = requests.post(url=send_url, data=self.msg)
        errcode = json.loads(response.text)['errcode']

        if errcode == 0:
            print('Succesfully')
        else:
            print('Failed')


# 開發(fā)環(huán)境|測(cè)試環(huán)境 WEB|TEST 部署成功|部署失敗
#  python3 /SHELL/buildNotice.py dev web  成功
def send():
    print(sys.argv)
    msg = "@所有小伙伴們:\r\n{_time}\r\n環(huán)境:{_env} \r\n分支:{_pro} \r\n狀態(tài):{_status}\r\nREPO:{_repo}\r\n有問題請(qǐng)@運(yùn)維小伙伴。謝謝".format(_time=localtime,_env=sys.argv[1],_pro=sys.argv[2],_status=sys.argv[3],_repo=sys.argv[4])
    #msg="jamestest"
    wechat = WeChat(corpid, secret, agentid)
    wechat.send_message(msg)
send()

推送代碼實(shí)現(xiàn)部署

查看流程線

查看執(zhí)行步驟

查看部署流程

查看通知
企業(yè)微信通知

進(jìn)階優(yōu)化問題

  • 安裝時(shí)候
/usr/bin/gitlab-ci-multi-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner
  • 如何修改gitlab-runner的工作路徑
--working-directory /home/gitlab-runner
  • 修改用戶執(zhí)行用戶
--user gitlab-runner
  • 配置文件
--config /etc/gitlab-runner/config.toml

如何查詢

ps aux|grep gitlab-runner
root      9217  2.9  0.0  44996 12988 ?        Ssl  Jul31 161:47 /usr/local/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner
root     21162  0.0  0.0 112712   984 pts/4    S+   18:52   0:00 grep --color=auto gitlab-runner

參考

參考文檔

參考文檔
變量

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容