Jenkins是一个可扩展的持续集成引擎,是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。
官网
场景二
#!/bin/bash
# 关闭防火墙
echo "正在关闭防火墙..."
sudo systemctl stop firewalld
sudo systemctl disable firewalld
echo "防火墙已关闭并禁用。"
# 检查 SELinux 状态
sestatus=$(sestatus | grep "SELinux status" | awk '{print $3}')
if [ "$sestatus" == "enabled" ]; then
# 关闭 SELinux
echo "正在关闭 SELinux..."
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SELinux 已关闭。"
else
echo "SELinux 已经处于禁用状态,无需操作。"
fi
echo "脚本执行完毕。"
yum install java-1.8.0-openjdk* -y
[root@workstation ~]# java -version
openjdk version "1.8.0_372"
OpenJDK Runtime Environment (build 1.8.0_372-b07)
OpenJDK 64-Bit Server VM (build 25.372-b07, mixed mode)
[root@workstation ~]# rpm -ivh jenkins-2.150.3-1.1.noarch.rpm
warning: jenkins-2.150.3-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jenkins-2.150.3-1.1 ################################# [100%]
[root@workstation ~]# systemctl start jenkins
[root@workstation ~]# systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@workstation ~]# ps -aux | grep 8080
jenkins 7687 80.3 25.7 2359788 256212 ? Ssl 09:49 0:32 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root 7760 0.0 0.0 112708 972 pts/0 R+ 09:50 0:00 grep --color=auto 8080
[root@workstation ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
7c1491b0219849f981255179e9981bba
退出后,再次登录使用admin用户, 密码为cat /var/lib/jenkins/secrets/initialAdminPassword
得到的字符串
yum install git -y
[root@servera ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -C "root@servera" -N ""
[root@servera ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDOHXkXsoYB1JtMJtXBeZrtcqsRRKBJEAzxGCoUyGM0B7SQYBQXaes4xTtQjOjJkL5HWQdaSelGGA8ySRjKuseIdYsPPcG5kxARkJ1+C6jJp3MamzuxJCjo2n3hmmopfB4f2eE3Kn5kbAUcmJRJfjUCgufsFjuY836Np9HNLqqYOuZCAKJEQm/IMTJgZ13aq/ss6VcA/+IikX4d/+PDsgYdNYSL8IxKiThfNlv3NIdOqNxAz2q4mFg2Vj9o1hvfnqGDIxuXyDGjVLZAW6bvJliJvJs3MfD7f4tn8gyFlArxPT0tMS7ePbxGBCBj2XC8qW9lcmrXD2163pbP/N6Io+CT root@servera
[email protected]:rkun0068/jenkins.git
[root@servera ~]# git config --global user.name "servera"
[root@servera ~]# git config --global user.email "[email protected]"
[root@servera ~]# git clone [email protected]:rkun0068/jenkins.git
[root@servera ~]# cd jenkins/
[root@servera jenkins]# echo "Jenkins Test " >> README.md
[root@servera jenkins]# git add README.md
[root@servera jenkins]# git commit -m "add README.md"
[master (root-commit) eb74eb9] add README.md
1 file changed, 1 insertion(+)
create mode 100644 README.md
[root@servera jenkins]# git branch
* master
[root@servera jenkins]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 228 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: To [email protected]:rkun0068/jenkins.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
在Nginx服务器上安装Nginx,并启动服务
yum install epel-release
yum install nginx -y
systemctl start nginx
systemctl enable nginx
Jenkins图形确认安装git plugin和publish over ssh插件
[root@workstation ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -C "root@workstation" -N ""
[root@workstation ~]# cat ~/.ssh/id_rsa
#填上Nginx服务器IP地址
[root@workstation ~]# ssh-copy-id -i 192.168.182.132
凭据使Jenkins给字节构建项目授权使用的。
此文章中Jenkins通过SSH免密去找GitHub的项目仓库clone或pull项目代码。
但Jenkins服务本身不允许使用自己的私钥访问GitHub公钥,给凭据,让它可以实现功能就可以。
凭据是一种和第三方程序通讯的权限授权。本案例的凭据就是允许使用SSH免密。
可以使凭据在Jenkins里授权给任何需要SSH免密通讯的构建项目。
[root@serverb ~]# ls /usr/share/nginx/html/
404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png README.md
#可以看到README.md被传过来了
[root@servera ~]# git clone [email protected]:root/auto_build_web.git
[root@servera ~]# cd auto_build_web/
[root@servera auto_build_web]#
[root@workstation ~]# git clone [email protected]:root/auto_build_web.git
#!/bin/bash
#源目录为jenkins存放任务文件的目录
SOURCE_DIR=/var/lib/jenkins/workspace/$JOB_NAME/
#目标目录为nginx服务器的家目录
DEST_DIR=/usr/share/nginx/html
#使用rsync同步源到nginx服务器家目录(需要免密登录),IP为nginx服务器IP
/usr/bin/rsync -av --delete $SOURCE_DIR [email protected]:$DEST_DIR
[root@workstation ~]# grep jenkins /etc/passwd
jenkins:x:997:995:Jenkins Automation Server:/var/lib/jenkins:/bin/false
[root@workstation ~]# usermod -s /bin/bash jenkins
[root@workstation ~]# grep jenkins /etc/passwd
jenkins:x:997:995:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
[root@workstation ~]# su - jenkins
-bash-4.2$ ssh-keygen -t rsa -C "jenkins user" -N ""
-bash-4.2$ ssh-copy-id -i [email protected]
[root@servera auto_build_web]# echo "auto_build_web" >> index.html
[root@servera auto_build_web]# git add index.html
[root@servera auto_build_web]# git commit -m "add index.html"
[master (root-commit) fddcaba] add index.html
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@servera auto_build_web]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:root/auto_build_web.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
[root@serverb ~]# cat /usr/share/nginx/html/index.html
auto_build_web
如果出现构建失败的情况很可能是某个主机rsync没有安装