1.背景
- 在當(dāng)今互聯(lián)網(wǎng)形勢下,軟件開發(fā)過程中,開發(fā)部署的頻率會非常高,傳統(tǒng)的本地打包-上傳覆蓋-重啟的方式會導(dǎo)致開發(fā)人員的時間浪費(fèi)。
- 自動化部署可以加快部署速度,并可以有效減少人為操作的失誤
- 使用Jenkins+GitLab+Maven+Git搭建持續(xù)集成系統(tǒng)。
下面本文就介紹了在一臺阿里云服務(wù)器上搭建一套自動化部署的方案,本人實(shí)踐,僅供參考,歡迎討論。
2.開始
操作系統(tǒng)版本:CentOS Linux release 7.3.1611 (Core)
本次需要安裝的軟件列表:
- Maven
- Git
- Jenkins
- GitLab
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,等待安裝結(jié)束。
使用mvn -v驗(yàn)證安裝是否成功
2.2 Git
使用命令安裝:yum install git
使用git --version驗(yàn)證安裝是否成功
2.3 Jenkins
準(zhǔn)備
CentOS 7.0默認(rèn)使用的是firewall作為防火墻,這里改為iptables防火墻。
關(guān)閉firewall:
systemctl stop firewalld.service#停止firewall
systemctl disable firewalld.service#禁止firewall開機(jī)啟動安裝iptables防火墻
yum install iptables-services
vi /etc/sysconfig/iptables#編輯防火墻配置文件
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8282 -j ACCEPT#添加策略(開放8282端口)添加防火墻策略后,啟動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
配置
修改Jenkins監(jiān)聽端口:
vim /etc/sysconfig/jenkins#JENKINS_PORT="8282"啟動Jenkins
service jenkins restart瀏覽器訪問:
http://IP:8282/Unlock Jenkins
Jenkins初次登陸需要驗(yàn)證安裝,查看/var/lib/jenkins/secrets/initialAdminPassword文件中密碼,輸入到Administrator password中。
按照指引安裝Jenkins配置Jenkins構(gòu)建信息。
恢復(fù)備份
- 用戶配置文件路徑
/var/lib/jenkins/users/jenkins
最后
- 設(shè)置Maven
- 設(shè)置JDK
- 安裝插件:
Publish Over SSH、
2.4 GitLab
準(zhǔn)備
安裝和配置依賴程序:
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
執(zhí)行最后的命令時可能會發(fā)生錯誤:
/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
再次執(zhí)行,應(yīng)該就可以了。
安裝
下載安裝文件:
curl -LJO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.9.4-ce.1.el7.x86_64.rpm安裝:
rpm -i gitlab-ce-8.9.4-ce.1.el7.x86_64.rpm初始化:
sudo gitlab-ctl reconfigure
恢復(fù)備份
拷貝舊gitlab服務(wù)器
/var/opt/gitlab/backups下需要恢復(fù)的備份到新服務(wù)器相同目錄下。
執(zhí)行gitlab-rake gitlab:backup:restore BACKUP=1489687253,數(shù)字為要恢復(fù)的備份文件前綴名稱。拷貝舊服務(wù)器
/etc/gitlab/gitlab-secrets.json文件到新服務(wù)器相同目錄下。執(zhí)行命令:
sudo gitlab-rails runner "Project.where.not(import_url: nil).each { |p| p.import_data.destroy if p.import_data }"修改:
vim /var/opt/gitlab/gitlab-rails/etc/gitlab.yml,gitlab節(jié)點(diǎn)host修改為實(shí)際IP地址,并重啟。
大功告成
維護(hù)
- 重啟:
sudo gitlab-ctl restart
附錄
#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
在網(wǎng)上找了下資料:
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.
猜測應(yīng)該是最新的Jenkins基于JDK8編譯的,所以試著更新服務(wù)器的JDK版本。
在官網(wǎng)下載的JDK8安裝后,可以啟動并訪問Jenkins了。