运维&部署Gitlab程序员

Jenkins+Maven+GitLab搭建持续集成和自动化部署

2017-03-23  本文已影响2244人  Luomeng

1.背景

下面本文就介绍了在一台阿里云服务器上搭建一套自动化部署的方案,本人实践,仅供参考,欢迎讨论。

2.开始

操作系统版本:CentOS Linux release 7.3.1611 (Core)

本次需要安装的软件列表:

2.1 Maven

直接使用yum安装Maven

获取安装源:

wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo

然后使用命令:yum install apache-maven,输入y,等待安装结束。

使用mvn -v验证安装是否成功

2.2 Git

使用命令安装:yum install git

使用git --version验证安装是否成功

2.3 Jenkins

准备

CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。

  1. 关闭firewall:
    systemctl stop firewalld.service #停止firewall
    systemctl disable firewalld.service #禁止firewall开机启动

  2. 安装iptables防火墙
    yum install iptables-services
    vi /etc/sysconfig/iptables #编辑防火墙配置文件
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8282 -j ACCEPT #添加策略(开放8282端口)

  3. 添加防火墙策略后,启动iptables
    service iptables start

安装

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo

sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

sudo yum install jenkins

配置
  1. 修改Jenkins监听端口:
    vim /etc/sysconfig/jenkins #JENKINS_PORT="8282"

  2. 启动Jenkins
    service jenkins restart

  3. 浏览器访问:
    http://IP:8282/

  4. Unlock Jenkins
    Jenkins初次登陆需要验证安装,查看/var/lib/jenkins/secrets/initialAdminPassword文件中密码,输入到Administrator password中。
    按照指引安装Jenkins

  5. 配置Jenkins构建信息。

恢复备份
  1. 用户配置文件路径/var/lib/jenkins/users/jenkins
最后
  1. 设置Maven
  2. 设置JDK
  3. 安装插件:Publish Over SSH

2.4 GitLab

准备

安装和配置依赖程序:
sudo yum install curl policycoreutils openssh-server openssh-clients
sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

执行最后的命令时可能会发生错误:

/usr/sbin/postconf: fatal: parameter inet_interfaces: no local interface found for ::1

如此的话需要修改一下postfix的配置文件:

vi  /etc/postfix/main.cf

inet_interfaces = localhost

修改为:

inet_interfaces = all

再次执行,应该就可以了。

安装
  1. 下载安装文件:
    curl -LJO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.9.4-ce.1.el7.x86_64.rpm

  2. 安装:
    rpm -i gitlab-ce-8.9.4-ce.1.el7.x86_64.rpm

  3. 初始化:
    sudo gitlab-ctl reconfigure

恢复备份
  1. 拷贝旧gitlab服务器/var/opt/gitlab/backups下需要恢复的备份到新服务器相同目录下。
    执行gitlab-rake gitlab:backup:restore BACKUP=1489687253,数字为要恢复的备份文件前缀名称。

  2. 拷贝旧服务器/etc/gitlab/gitlab-secrets.json文件到新服务器相同目录下。

  3. 执行命令:sudo gitlab-rails runner "Project.where.not(import_url: nil).each { |p| p.import_data.destroy if p.import_data }"

  4. 修改:vim /var/opt/gitlab/gitlab-rails/etc/gitlab.yml,gitlab节点host修改为实际IP地址,并重启。

大功告成

维护

附录

#2017.5.2更新

通过yum安装最新Jenkins后,访问失败,查看jenkins.log,显示一下错误信息:

Caused by: java.lang.UnsupportedClassVersionError: jenkins/util/SystemProperties : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:442)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:64)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:354)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
        at org.eclipse.jetty.webapp.WebAppClassLoader.findClass(WebAppClassLoader.java:510)
        at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:441)
        at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:403)
        at org.eclipse.jetty.server.handler.ContextHandler.loadClass(ContextHandler.java:1583)
        at org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitListener(StandardDescriptorProcessor.java:1956)
        ... 25 more

在网上找了下资料:

version 52.0 of the java class file means, that the java source code was compiled for jre8 and has to be executed with jre8.
Here the class file versions and theire equivalent java versions:
Java SE 9 = 53 (0x35 hex)
Java SE 8 = 52 (0x34 hex)
Java SE 7 = 51 (0x33 hex)
Java SE 6.0 = 50 (0x32 hex)
Java SE 5.0 = 49 (0x31 hex)
JDK 1.4 = 48 (0x30 hex)

This means you have either to upgrade the jvm or you have to downgrade the jenkins version. You can see the jenkins version right down in the corner of your jenkins installation, when you open it in the webbrowser.
Here is the official blogpost for the upgrade: [https://jenkins.io/blog/2017/01/17/Jenkins-is-upgrading-to-Java-8/](https://jenkins.io/blog/2017/01/17/Jenkins-is-upgrading-to-Java-8/)
Since java 7 doesn't get any public updates since April 2015, the best way is to upgrade your jvm to 8.

猜测应该是最新的Jenkins基于JDK8编译的,所以试着更新服务器的JDK版本。

在官网下载的JDK8安装后,可以启动并访问Jenkins了。

上一篇下一篇

猜你喜欢

热点阅读