jenkins的應用與搭建

實驗條件2個服務器,

git服務器  ip地址:192.168.200.151java

Jenkins服務器 ip地址:192.168.200.132linux

操做系統:centos7.5git

[root@git ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@git ~]# uname -r
3.10.0-862.el7.x86_64
[root@git ~]# systemctl stop firewalld
[root@git ~]# systemctl disable firewalld
[root@git ~]# systemctl stop NetworkManager
[root@git ~]# systemctl disable NetworkManager
[root@git ~]# 

在git服務器上操做

[root@git ~]# yum -y install gitweb

[root@git ~]# useradd git
[root@git ~]# echo "12341234" | passwd --stdin git
Changing password for user git.
passwd: all authentication tokens updated successfully.

用git帳號建立項目倉庫

[root@git ~]# su - git
[git@git ~]$ mkdir repos
[git@git ~]$ cd repos/
[git@git repos]$ mkdir app.git
[git@git repos]$ cd app.git/
[git@git app.git]$ pwd
/home/git/repos/app.git
[git@git app.git]$ git --bare init
Initialized empty Git repository in /home/git/repos/app.git/
[git@git app.git]$ ls 
branches  config  description  HEAD  hooks  info  objects  refs
[git@git app.git]$ 

在Jenkins服務器上測試推送代碼

[root@jenkins ~]# yum -y install git
[root@jenkins ~]# mkdir -p /test
[root@jenkins ~]# cd /test

[root@jenkins test]# git clone git@192.168.200.151:/home/git/repos/app.git
Cloning into 'app'...
The authenticity of host '192.168.200.151 (192.168.200.151)' can't be established.
ECDSA key fingerprint is SHA256:gm/RhqGrfDo5Rgcr/LmBAaqPv6tmni7cRpXjGEWZQpg.
ECDSA key fingerprint is MD5:ae:f6:0b:6e:80:96:67:cf:bd:e8:f5:b5:c4:e0:da:11.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.151' (ECDSA) to the list of known hosts.
git@192.168.200.151's password:
warning: You appear to have cloned an empty repository.
[root@jenkins test]# ls
appapache

[root@jenkins test]# cd app
[root@jenkins app]# touch test.sh
[root@jenkins app]# echo "hello" >> test.sh
[root@jenkins app]# git add *
[root@jenkins app]# git commit -m "first"vim

*** Please tell me who you are.centos

Run服務器

git config --global user.email "you@example.com"
git config --global user.name "Your Name"app

to set your account's default identity.
Omit --global to set the identity only in this repository.運維

fatal: unable to auto-detect email address (got 'root@jenkins.(none)')
[root@jenkins app]# git config --global user.email "493115250@qq.com"
[root@jenkins app]# git config --global user.name "cash_su"

[root@jenkins app]# git commit -m "first"
[master (root-commit) 731ab6e] first
1 file changed, 1 insertion(+)
create mode 100644 test.sh
[root@jenkins app]# git push -u origin master
git@192.168.200.151's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.200.151:/home/git/repos/app.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

給git作一個免祕鑰

[root@jenkins app]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:toacG+JZEW1VNmQKQcp9slNyCHCaq55K9Ym82EZcboU root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
|    ..o.+..o*    |
|     = = + + .   |
|    o = B =      |
|     E + B       |
|  ..+ o S        |
|  o+o+.= o       |
| .oooo* o        |
|..o+.+ +         |
|.o+oo .          |
+----[SHA256]-----+
[root@jenkins app]# 
[root@jenkins app]# 
[root@jenkins app]# ssh-copy-id -i ~/.ssh/id_rsa.pub git@192.168.200.151
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.200.151's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@192.168.200.151'"
and check to make sure that only the key(s) you wanted were added.

[root@jenkins app]# ssh git@192.168.200.151
Last login: Sun Dec 23 13:28:56 2018
[git@git ~]$ exit

準備安裝Jenkins

Jenkins官網:https://jenkins.io/

redhat版Jenkins官網下載頁面:https://pkg.jenkins.io/redhat-stable/

下載了rpm包之後經過localinstall直接安裝

yum -y localinstall 安裝包名

利用yum直接安裝Jenkins

[root@jenkins app]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
#導入證書祕鑰

[root@jenkins app]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

[root@jenkins app]# yum -y install jenkins

#用命令查看安裝路徑

 
 

[root@jenkins app]# rpm -ql jenkins
/etc/init.d/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins
/usr/lib/jenkins
/usr/lib/jenkins/jenkins.war
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins
[root@jenkins app]#

 

安裝java

[root@jenkins ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@jenkins ~]# cd /usr/local/
[root@jenkins local]# mv jdk1.8.0_171/ jdk
[root@jenkins local]# vim /etc/profile
[root@jenkins local]# tail -3 /etc/profile
export    JAVA_HOME=/usr/local/jdk/
export    PATH=$PATH:$JAVA_HOME/bin
export    CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

[root@jenkins local]# source /etc/profile
[root@jenkins local]# 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)
[root@jenkins local]#

安裝maven

[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/
[root@jenkins ~]# cd /usr/local/
[root@jenkins local]# mv apache-maven-3.5.0/ maven
[root@jenkins local]# vim /etc/profile
[root@jenkins local]# tail -2 /etc/profile
MAVEN_HOME=/usr/local/maven
export    PATH=${MAVEN_HOME}/bin:$PATH

[root@jenkins local]# source /etc/profile
[root@jenkins local]# mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/local/jdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
[root@jenkins local]#

啓動jenkins

[root@jenkins ~]# ln -s /usr/local/jdk/bin/java /usr/bin/
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# ss -antup | grep 8080
tcp    LISTEN     0      50       :::8080                 :::*                   users:(("java",pid=5642,fd=162))
[root@jenkins ~]# 

添加到開機自啓

[root@jenkins ~]# systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@jenkins ~]# echo $?
0

開始使用Jenkins

查看解鎖碼
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword 6c2b251fde9348e1ba5f3ecd968f0917 [root@jenkins ~]#

 

使用默認的插件便可

 

 

權限管理

開發人員用的   生產環境項目代碼版本發佈(A/B測試等)

測試人員用的        測試環境自動部署

運維人員用的            生產環境項目代碼版本回滾

安裝控制角色的插件

 

 

 

 測試建立2個用戶,一個開發,一個測試的

一樣主從user2測試的帳號密碼

最終的權限選項

建立2個項目來實驗權限,空項目便可

超戶視角

測試視角

開發視角

在超戶上構建兩個項目的視圖

超戶視角

開發視角

測試視角

測試開發的構建功能

超戶改變配置

 

 

構建成功

帶參數的構建(在超戶裏修改配置)

 

在開發視角開始構建

構建成功

安裝插件Extended能夠讓用戶本身選分支

 

 在超戶修改配置,進行多項選擇實驗

咱們能夠將參數寫到文件裏,讓其引用這個文件,就能夠有參數了

[root@jenkins ~]# vim /opt/jenkins.property
[root@jenkins ~]# cat /opt/jenkins.property
ok=can1.can2,can3

用git參數化構建

安裝插件

添加私鑰到全局憑據

[root@jenkins ~]# cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVAT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x4/9AoGBAMqnZbjPhWUxJ+4q/4VD
jjRUa9YIvXRAaICf5c1Onnx/9mzF71sziXWn4TkEH4Uca/GfU479AMu4hKDNawfh
rai1URdvnBLCviXXvP6LBAoJBp8EM6kAEaL9EAkC2UDeNG5+F4h1HDT7Zpq1DTUx
hs3mGzMwmgB0NWUXTIuEa0adAoGANJzb8ta2oKm8NBfha4ucJ9MvIXtuMDE2+3ht
Az8sseOkKDJorN6gwRuqVxHsoNZ1ZrsUhPhQsI0ezK3lEsse//oWjsNkv/9mkOjJ
Ycr1Dun5/DHJO25s/BAmiqB3QLBuOYVHYw7LyxR7PkdTUpAbIGxTxfF1DrLtaaur
hI101q0CgYEA8IQKMINCF4SuJ+TSAtzav0WKBqayOWi1mdVoOZl7MoqxjUywFXw5
V4oKxxty3tWvTa5MR6R0jfY0Om92LWXKmicZWnq6pZC/26H3ZuuMwO1WlGE5OBmo
ZpuYlD1okBWUhVSzKmzLp6MzCpUgUbu2ffuSjeaSv6bVEYdS0l/uX9Q=
-----END RSA PRIVATE KEY-----
[root@jenkins ~]# 

修改配置文件

節點主從構建,

建立全局憑證,

測試用新的節點構建

在git服務器上查看Jenkins工做的目錄有沒有拉取下來

[root@git A-web]# pwd
/var/lib/jenkins/workspace/A-web
[root@git A-web]# ls 
test.sh
[root@git A-web]# cat test.sh 
hello
[root@git A-web]# 
相關文章
相關標籤/搜索