Gitlab部署CI/CD

一、安装docker 

 

1、服务器执行 

yum remove  -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine 

curl -sSL https://get.docker.com/ | sh 

 

二、部署gitlab 

 

1、部署gitlab服务端 

docker run --detach --hostname gitlab.abc.com --publish 81:80 --publish 22:22 --name gitlab --restart always --volume /data/gitlab/config:/etc/gitlab --volume /data/gitlab/logs:/var/log/gitlab --volume /data/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest 

2、修改 socket 或者映射socket目录 

 unicorn['socket'] = '/var/opt/gitlab/socket' 

 puma['socket'] = '/data/gitlab/socket' 

 storage_check['target'] = 'unix:///var/opt/gitlab/socket' 

3、让配置文件生效 

docker exec gitlab gitlab-ctl reconfigure 

或者 

docker restart gitlab 

4、nginx反向代理 

server { 

    listen       80; 

    server_name  gitlab.abc.com; 

    charset utf-8; 

    ssl_session_timeout     10m; 

    ssl_session_cache       shared:SSL:10m; 

    location / { 

        proxy_set_header Host $host; 

        proxy_set_header X-Real-IP $remote_addr; 

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

        proxy_set_header X-Forwarded-Proto https; 

        proxy_redirect off; 

        proxy_connect_timeout   240; 

        proxy_send_timeout      240; 

        proxy_read_timeout      240; 

        proxy_pass              http://127.0.0.1:81; 

    } 

} 

 

三、部署gitlab runner 

 

1、安装gitlab-runner

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh |sudo bash && 

yum install -y gitlab-runner && 

sed -i 's#/home/gitlab-runner#/data/gitlab-runner#' /etc/systemd/system/gitlab-runner.service && 

sed -i 's#"--user" "gitlab-runner"#"--user" "www"#' /etc/systemd/system/gitlab-runner.service && 

systemctl daemon-reload && 

systemctl start gitlab-runner 

2、运行客户端

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 gitlab/gitlab-runner:latest

3、执行注册绑定gitlab的web端gitlab-runner信息

docker exec gitlab-runner gitlab-runner register 

 

四、配置gitlab邮箱服务


1、进入容器

docker exec -it gitlab  bash

2、修改gitlab配置文件

vim  /etc/gitlab/gitlab.rb
#添加以下内容
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "mail_service"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "mail_user"
gitlab_rails['smtp_password'] = "mail_passwd"
gitlab_rails['smtp_domain'] = "mai_service"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false

gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'mail_user'
gitlab_rails['gitlab_email_display_name'] = 'Gitlab'

#如果邮箱服务没有开启ssl则取消ssl验证 添加下条内容
gitlab_rails['smtp_openssl_verify_mode'] = 'none'

3、重载配置

gitlab-ctl reconfigure

4、进入Rails控制台验证邮件是否发送成功

gitlab-rails console production
irb(main):001:0> Notify.test_email('收件人邮箱', 'Message Subject', 'Message Body').deliver_now

 

五、配置gitlab的ci-cd

配置需求,提交不同代码发布不同环境

 

1、安装编译基础环境nvm,npm,maven,jdk等

安装nvm 
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
source .bashrc
安装npm  
nvm install v12.10.0

安装maven jdk等编译环境请查看另一篇部署文档https://blog.csdn.net/jon_stark/article/details/84797257。

2、vue程序模板脚本

vim .gitlab-ci.yml
stages:
  - prod
  - stg

before_script:
#    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
#    - chmod 700 ~/.ssh/id_rsa
    - pwd
    - hostname
    - ip a
    - id

prod:
  stage: prod
  variables:
    HOSTS: 172.24.0.189 172.24.0.214
  script:
    - nvm use v12.10.0
    - npm install
    - npm run build
    - echo "'Date:'`date` 'Branch:'$CI_COMMIT_REF_NAME 'Commit:'$CI_COMMIT_SHORT_SHA 'Env:'$CI_JOB_STAGE" >> dist/version.txt
    - for HOST in $HOSTS;do echo $HOST;
        rsync -a --delete dist/ $HOST:/data/web/platform/;
        rsync -a --delete dist/ $HOST:/data/backup/platform_`date +%Y%m%d%H%S%M/` ;
        done
  only:
    refs:
      - master

stg:
  stage: stg
  variables:
    HOST: 172.20.2.29
  script:
    - nvm use v12.10.0
    - npm install
    - npm run build
    - echo "'Date:'`date` 'Branch:'$CI_COMMIT_REF_NAME 'Commit:'$CI_COMMIT_SHORT_SHA 'Env:'$CI_JOB_STAGE" >> dist/version.txt
    - rsync -a --delete dist/ $HOST:/data/web/platform/
  only:
    refs:
      - stg

3、java程序模板脚本

vim .gitlab-ci.yml
stages:
  - prod
  - stg
  - dev
before_script:
#    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
#    - chmod 700 ~/.ssh/id_rsa
    - hostname
    - ip a
    - id
    - pwd

prod:
  stage: prod
  variables:
    HOSTS: 172.24.1.16 172.24.1.23
    DIR: game-agg
    PACKAGE: game-agg-api-0.0.1-SNAPSHOT.jar
    PORT: 8003
    ENV: prod
  script:
    - mvn clean install
    - for HOST in $HOSTS;do echo $HOST;
        rsync -a -e 'ssh -p 9999' --delete game-agg-api/target/$PACKAGE $HOST:/data/web/$DIR/$PACKAGE;
        ssh -o stricthostkeychecking=no  $HOST -p9999 "/usr/bin/bash /data/web/game-agg/service.sh";
      done
  only:
    refs:
      - master

stg:
  stage: stg 
  variables:
    HOST: 192.168.1.202
    DIR: game-agg-test
    PACKAGE: game-agg-api-0.0.1-SNAPSHOT.jar
    PORT: 8013
    ENV: stg
  script:
    - mvn clean install
    - rsync -a --delete game-agg-api/target/$PACKAGE $HOST:/data/web/$DIR/$PACKAGE
    - ssh -o stricthostkeychecking=no   $HOST "/usr/bin/bash /data/web/game-agg-test/service.sh"
  only:
    refs:
      - stg

dev:
  stage: dev
  variables:
    HOST: 192.168.1.202
    DIR: game-agg-dev
    PACKAGE: game-agg-api-0.0.1-SNAPSHOT.jar
    PORT: 8023
    ENV: dev
  script:
    - mvn clean install
    - rsync -a --delete game-agg-api/target/$PACKAGE $HOST:/data/web/$DIR/$PACKAGE
    - ssh -o stricthostkeychecking=no  $HOST "/usr/bin/bash /data/web/game-agg-dev/service.sh";
  only:
    refs:
      - dev

4、php等无需编译程序模板脚本

vim .gitlab-ci.yml
stages:
  - prod
  - stg

before_script:

    - pwd
    - hostname
    - ip a
    - id

prod:
  stage: prod
  variables:
    HOSTS: 172.24.0.214 172.24.0.189
  script:
    - echo "'Date:'`date` 'Branch:'$CI_COMMIT_REF_NAME 'Commit:'$CI_COMMIT_SHORT_SHA 'Env:'$CI_JOB_STAGE" >> version.txt
    - for HOST in $HOSTS;do echo $HOST;
       ssh  -o stricthostkeychecking=no $HOST -p 9999 "cd /data/web/php_manager_api/; git pull origin master";
        done
  only:
    refs:
      - master

stg:
  stage: stg
  variables:
    HOST: 192.168.1.81
  script:
    - echo "'Date:'`date` 'Branch:'$CI_COMMIT_REF_NAME 'Commit:'$CI_COMMIT_SHORT_SHA 'Env:'$CI_JOB_STAGE" >> version.txt
    - echo $HOST
    - ssh  -o stricthostkeychecking=no $HOST "cd /data/web/php_manager_api/; git pull origin stg"
  only:
    refs:
      - stg

 

你可能感兴趣的:(Gitlab部署CI/CD)