Gitlab+Jenkins+Nexus 环境搭建

孤雪飘寒 2019-07-01

Gitlab代码仓库、Jenkins持续集成、Nexus仓库私服,可以搭配构建一个较完整的开发环境。
根据需求,在CentOS 6.9 系统上搭建以上服务。

Gitlab安装部署

前面的话

源码安装 GitLab 步骤繁琐:需要安装依赖包,Mysql,Redis,Postfix,Ruby,Nginx……安装完毕还得一个个手动配置这些软件。源码安装容易出错,不顺利的话,一天都搞不定。源码最大的好处是私人定制,如果不做定制化,还是使用官方推荐的 omnibus packages 方式安装,网络好的话,一个小时内搞定。

安装环境

系统:CentOS 6.9 64位
内核:2.6.32-696.el6.x86_64

安装前准备

关闭防火墙、关闭selinux
$ service iptables stop
$ vim /etc/selinux/config
    disabled
$ reboot
添加gitlab yum源
$ cd /etc/yum.repos.d/
$ touch gitlab-ce.repo
$ vim gitlab-ce.repo
    [gitlab-ce]
    name=gitlab-ce
    baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6
    repo_gpgcheck=0
    gpgcheck=0
    enabled=1
    gpgkey=https://packages.gitlab.com/gpg.key
    :wq
$ yum clean all
$ yum makecache

开始安装

安装依赖包
$ yum -y install curl openssh-server openssh-clients postfix cronie
$ service postfix start
$ chkconfig postfix on
$ yum -y install gitlab-ce
It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Thank you for installing GitLab!

    gitlab-ctl reconfigure
Running handlers:
Running handlers complete
Chef Client finished, 423/602 resources updated in 02 minutes 37 seconds
gitlab Reconfigured!

已安装好Gitlab

配置域名访问

添加访问的 host,修改/etc/gitlab/gitlab.rb的external_url
external_url 'http://gitlab.example.com'

vim /etc/hosts
127.0.0.1    gitlab.example.com

gitlab-ctl reconfigure        \\使配置生效
修改本机电脑hosts文件
172.16.16.11 gitlab.example.com

默认账户

账户:root
密码:5iveL!fe

汉化

Gitlab的汉化主要还是需根据安装的版本进行语言包的替换,操作需要灵活性。
$ cat /opt/gitlab/emdedded/service/gitlab-rails/VERSION
10.8.4

$ git clone https://gitlab.com/xhang/gitlab.git
$ ls;cd gitlab
$ git branch -a    #查看当前有哪些版本的汉化包
$ git diff remotes/origin/10-8-stable remotes/origin/10-8-stable-zh > /tmp/10-8.diff
$ gitlab-ctl stop
$ patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < 10-8.diff
$ gitlab-ctl start
$ gitlab-ctl reconfigure

数据备份

Gitlab的数据备份还是非常有必要的,毕竟都是辛辛苦苦写的代码
默认数据备份目录是/var/opt/gitlab/backups,手动创建备份文件:

  • gitlab-rake gitlab:backup:create
  • 日常备份,添加crontab
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

数据恢复

  • 将数据备份文件拷贝至备份目录

cp 1393513186_gitlab_backup.tar /var/opt/gitlab/backups/

  • 停止连接数据库的进程

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

  • 恢复1393513186这个备份文件,将覆盖GitLab数据库!

gitlab-rake gitlab:backup:restore BACKUP=1393513186

  • 启动 GitLab

gitlab-ctl start

  • 检查 GitLab

gitlab-rake gitlab:check SANITIZE=true

Gitlab安装完成,可以先登录试试水了

Jenkins安装部署

安装环境

系统:CentOS 6.9 64位
内核:2.6.32-696.el6.x86_64

安装JDK1.8

Jenkins需要jdk1.8的支持,否则会启动失败
下载地址
$ tar -xvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
$ cd /usr/local
$ mv jdk1.8.0_171/ jdk1.8
$ vim /etc/profile
    export JAVA_HOME=/usr/local/jdk1.8
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
$ source /etc/profile
#之后java -version没有生效,还是java1.7
$ which java 
/usr/bin/java
$ which javac
/usr/bin/javac
$ mv /usr/bin/java /usr/bin/java_original
$ mv /usr/bin/javac /usr/bin/javac_original
$ ln -s $JAVA_HOME/bin/java /usr/bin/java
$ ln -s $JAVA_HOME/bin/javac /usr/bin/javac
$ source /etc/profile
$ java -version
    java version "1.8.0_171"
    Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

安装Maven

$ wget http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
$ tar -xvf apache-maven-3.5.4-bin.tar.gz -C /usr/local
$ mv apache-maven-3.5.4/ maven-3.5.4
$ vim /etc/profile
    export MAVEN_HOME=/usr/local/maven-3.5.4
    export PATH=$MAVEN_HOME/bin:$PATH
$ surce /etc/profile
$ mvn -v 
    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
    Maven home: /usr/local/maven-3.5.4
    Java version: 1.8.0_171, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8/jre
    Default locale: zh_CN, platform encoding: UTF-8
    OS name: "linux", version: "2.6.32-696.el6.x86_64", arch: "amd64", family: "unix"

安装tomcat,配置war包启动

配置tomcat启动Jenkins war包,需要先下载tomcat安装。
下载地址
$ tar -xvf apache-tomcat-8.5.31.tar.gz  -C /usr/local/
        $ mv apache-tomcat-8.5.31/ tomcat-jenkins
默认配置启动Tomcat,访问正常后关闭。下载Jenkins war包
下载地址
$ wget http://mirrors.jenkins-ci.org/war-stable/2.121.1/jenkins.war
$ vim /usr/local/tomcat-jenkins/conf/server.xml
            148       <Host name="localhost"  appBase="/usr/local/tomcat-jenkins/webapps/jenkins"
            149             unpackWARs="true" autoDeploy="true">
            150         <Context path="" docBase="/usr/local/tomcat-jenkins/webapps/jenkins" debug="0" reloadable="true"/>
保存并启动Tomcat,访问地址:http://172.16.16.12:8080/jenkins

Nginx反向代理,实现域名访问

添加域名解析

  • linux
vim /etc/hosts
172.16.16.12    jenkins.example.com
  • windows
C:\Windows\System32\drivers\etc\hosts
172.16.16.12    jenkins.example.com

Nginx配置文件

user  nginx;
worker_processes  2;

error_log    /var/run/nginx/error.log    notice;

pid            /usr/local/nginx/logs/nginx.pid;


events {
    use    epoll;
    worker_connections  10000;
}


http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $server_name $remote_user $time_local  [$request_time] [$upstream_response_time] "$server_name" "$request" "$request_uri"' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$http_cookie"';

    access_log  /var/run/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 3;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    client_header_timeout      180s;
    client_body_timeout     180s;
    client_max_body_size     1024M;

    port_in_redirect    off;

    include     /usr/local/nginx/conf/vhosts/*.conf;

}

jenkins.conf
server {
    listen      80;
    add_header Cache-Control no-cache;
    server_name    jenkins.example.com;

    charset utf-8;
    access_log  /var/log/nginx/jenkins.access.log  main;

    location / {
                proxy_pass      http://localhost:8080;
        proxy_redirect      http://localhost:8080 $scheme://example.com;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size    100m;
                client_body_buffer_size 10240k;
                proxy_connect_timeout   60;
                proxy_send_timeout      90;
                proxy_read_timeout      90;
                proxy_buffers           32 1024k;
                keepalive_timeout  180;
                sendfile    on;
                tcp_nodelay on;
    }
}

访问地址:http://jenkins.example.com

Nexus安装部署

下载、解压、启动就OK了

相关推荐

就是那个胖子 / 0评论 2020-06-15