1、jenkins介紹php
咱們都知道在公司裏,一個軟件的開發到發佈,須要經歷一系列的流程,並且目前也已經造成了一套標準流程。一個大的功能模塊的開發,每每是幾我的一塊兒進行的,而每一個人寫完一些小功能的代碼後,通常都須要將代碼集成到主幹,或者說將代碼都合併在一塊兒。這種將代碼集成到主幹的操做,可能一天須要進行不少次,並且是多人進行。這樣的一個環節就是持續集成,在這個開發到發佈的流程中最重要的組成部分就是持續集成。 開發到發佈的流程大體以下: 產品設計成型 -> 開發人員開發代碼 -> 測試人員測試功能 -> 運維人員發佈上線 其中的幾個概念: 持續集成 (Continuous integration,簡稱CI),頻繁地(一天屢次)將代碼集成到主幹。 持續交付(Continuous delivery),頻繁地(一天屢次)將集成的代碼交付給測試人員測試。 持續部署(continuous deployment),頻繁地(一天屢次)將測試好的代碼交給運維人員部署。
示意圖:java
關於持續集成的詳細可參考如下文章: 因爲這種頻繁地、一天屢次的操做,重複性很高,因此咱們但願能夠自動化的去完成這些重複的環節,提升整個流程的效率。這時咱們就須要藉助一些工具來完成自動化了,對於小公司來講收費的工具成本過高划不來,因此像Jenkins這樣開源的、優秀的工具就流行了起來。 Jenkins是一個用Java編寫的開源的持續集成工具,用於監控持續重複的工做,旨在提供一個開放易用的軟件平臺,使軟件的持續集成變成可能。在與Oracle發生爭執後,項目從Hudson項目復刻。 Jenkins提供了軟件開發的持續集成服務。它運行在Servlet容器中(例如Apache Tomcat)。它支持軟件配置管理(SCM)工具(包括AccuRev SCM、CVS、Subversion、Git、Perforce、Clearcase和RTC),能夠執行基於Apache Ant和Apache Maven的項目,以及任意的Shell腳本和Windows批處理命令。Jenkins的主要開發者是川口耕介。Jenkins是在MIT許可證下發布的自由軟件。 能夠經過各類手段觸發構建。例如提交給版本控制系統時被觸發,也能夠經過相似Cron的機制調度,也能夠在其餘的構建已經完成時,還能夠經過一個特定的URL進行請求。 Jenkins概要: Jenkins是一個開源的、可擴展的持續集成、交付、部署(軟件/代碼的編譯、打包、部署)基於web界面的平臺。 Jenkins是一個工具集,提供了各類各樣的插件 好比獲取git上最新的代碼 好比能夠幫你編譯源代碼 好比能夠調用自定義的shell腳本遠程執行命令 Jenkins官網地址以下: https://jenkins.io
備註:以上內容來源於網絡。
node
2、jenkins安裝linux
本例是用jenkins直接git clone代碼到jenkins服務器,而後ssh遠程登錄到php網站服務器進行部署。另外還有一種狀況,就是開發人員直接提供包,好比java項目的war包,而後進行替換。老是想使用jenkins,開發和運維的發佈流程都要標準。
git
關閉防火牆、selinux:web
[root@localhost yum.repos.d]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 (Maipo) [root@localhost yum.repos.d]# systemctl stop firewalld [root@localhost yum.repos.d]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. [root@localhost yum.repos.d]# setenforce 0 [root@localhost yum.repos.d]# vim /etc/sysconfig/selinux …… SELINUX=disabled ……
安裝依賴包和其它包:shell
[root@localhost ~]# yum install -y java-1.8.0-openjdk wget lrzsz vim net-tools
配置jenkins的yum源:apache
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo [root@localhost yum.repos.d]# rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key [root@localhost yum.repos.d]# ll 總用量 160 -rw-r--r--. 1 root root 71 11月 30 2016 jenkins.repo -rw-r--r--. 1 root root 156348 1月 31 10:05 redhat.repo
安裝檢查jenkins:vim
[root@localhost yum.repos.d]# yum install -y jenkins [root@localhost yum.repos.d]# systemctl start jenkins [root@localhost yum.repos.d]# systemctl status jenkins [root@localhost yum.repos.d]# ps aux | grep jenkins | grep -v grep jenkins 2949 52.3 8.3 2579428 156668 ? Ssl 10:04 0:15 /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@localhost yum.repos.d]# netstat -luntp | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 2949/java
#若是啓動不了,/var/log/jenkins/jenkins.log報錯以下bash
Caused by: java.io.IOException: Failed to bind to 0.0.0.0/0.0.0.0:8080 Caused by: java.net.BindException: Address already in use
#是8080端口被佔用了,修改/etc/sysconfig/jenkins配置文件便可。
web訪問jenkins:
http://192.168.146.156:8080/
[root@localhost yum.repos.d]# cat /var/lib/jenkins/secrets/initialAdminPassword cec1f28aedff4d1eac758a14128669a4
3、jenkins瞭解
[root@localhost yum.repos.d]# 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@localhost yum.repos.d]# ll /var/lib/jenkins/ 總用量 64 -rw-r--r--. 1 jenkins jenkins 1642 2月 11 10:18 config.xml -rw-r--r--. 1 jenkins jenkins 156 2月 11 10:04 hudson.model.UpdateCenter.xml -rw-r--r--. 1 jenkins jenkins 370 2月 11 10:14 hudson.plugins.git.GitTool.xml -rw-------. 1 jenkins jenkins 1712 2月 11 10:04 identity.key.enc -rw-r--r--. 1 jenkins jenkins 94 2月 11 10:04 jenkins.CLI.xml -rw-r--r--. 1 jenkins jenkins 5 2月 11 10:18 jenkins.install.InstallUtil.lastExecVersion -rw-r--r--. 1 jenkins jenkins 5 2月 11 10:18 jenkins.install.UpgradeWizard.state -rw-r--r--. 1 jenkins jenkins 185 2月 11 10:18 jenkins.model.JenkinsLocationConfiguration.xml -rw-r--r--. 1 jenkins jenkins 171 2月 11 10:04 jenkins.telemetry.Correlator.xml drwxr-xr-x. 2 jenkins jenkins 6 2月 11 10:04 jobs drwxr-xr-x. 3 jenkins jenkins 18 2月 11 10:04 logs -rw-r--r--. 1 jenkins jenkins 907 2月 11 10:04 nodeMonitors.xml drwxr-xr-x. 2 jenkins jenkins 6 2月 11 10:04 nodes drwxr-xr-x. 76 jenkins jenkins 8192 2月 11 10:15 plugins -rw-r--r--. 1 jenkins jenkins 64 2月 11 10:04 secret.key -rw-r--r--. 1 jenkins jenkins 0 2月 11 10:04 secret.key.not-so-secret drwx------. 4 jenkins jenkins 4096 2月 11 10:16 secrets drwxr-xr-x. 2 jenkins jenkins 4096 2月 11 10:14 updates drwxr-xr-x. 2 jenkins jenkins 23 2月 11 10:04 userContent drwxr-xr-x. 3 jenkins jenkins 52 2月 11 10:16 users drwxr-xr-x. 2 jenkins jenkins 6 2月 11 10:14 workflow-libs
主要目錄介紹:
jobs:jenkins建立的任務 logs:日誌 nodes:用於多節點環境 plugins:jenkins插件 secrets:密碼和密鑰 updates:更新相關 users:用戶 workflow-libs:工做庫
jenkins的配置文件:
[root@localhost yum.repos.d]# vim /etc/sysconfig/jenkins ## Path: Development/Jenkins ## Description: Jenkins Automation Server ## Type: string ## Default: "/var/lib/jenkins" ## ServiceRestart: jenkins # # Directory where Jenkins store its configuration and working # files (checkouts, build reports, artifacts, ...). # JENKINS_HOME="/var/lib/jenkins" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Java executable to run Jenkins # When left empty, we'll try to find the suitable Java. # JENKINS_JAVA_CMD="" ## Type: string ## Default: "jenkins" ## ServiceRestart: jenkins # # Unix user account that runs the Jenkins daemon # Be careful when you change this, as you need to update # permissions of $JENKINS_HOME and /var/log/jenkins. # JENKINS_USER="jenkins" ## Type: string ## Default: "false" ## ServiceRestart: jenkins # # Whether to skip potentially long-running chown at the # $JENKINS_HOME location. Do not enable this, "true", unless # you know what you're doing. See JENKINS-23273. # #JENKINS_INSTALL_SKIP_CHOWN="false" ## Type: string ## Default: "-Djava.awt.headless=true" ## ServiceRestart: jenkins # # Options to pass to java when running Jenkins. # JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true" ## Type: integer(0:65535) ## Default: 8080 ## ServiceRestart: jenkins # # Port Jenkins is listening on. # Set to -1 to disable # JENKINS_PORT="8080" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # IP address Jenkins listens on for HTTP requests. # Default is all interfaces (0.0.0.0). # JENKINS_LISTEN_ADDRESS="" ## Type: integer(0:65535) ## Default: "" ## ServiceRestart: jenkins # # HTTPS port Jenkins is listening on. # Default is disabled. # JENKINS_HTTPS_PORT="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Path to the keystore in JKS format (as created by the JDK 'keytool'). # Default is disabled. # JENKINS_HTTPS_KEYSTORE="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Password to access the keystore defined in JENKINS_HTTPS_KEYSTORE. # Default is disabled. # JENKINS_HTTPS_KEYSTORE_PASSWORD="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # IP address Jenkins listens on for HTTPS requests. # Default is disabled. # JENKINS_HTTPS_LISTEN_ADDRESS="" ## Type: integer(1:9) ## Default: 5 ## ServiceRestart: jenkins # # Debug level for logs -- the higher the value, the more verbose. # 5 is INFO. # JENKINS_DEBUG_LEVEL="5" ## Type: yesno ## Default: no ## ServiceRestart: jenkins # # Whether to enable access logging or not. # JENKINS_ENABLE_ACCESS_LOG="no" ## Type: integer ## Default: 100 ## ServiceRestart: jenkins # # Maximum number of HTTP worker threads. # JENKINS_HANDLER_MAX="100" ## Type: integer ## Default: 20 ## ServiceRestart: jenkins # # Maximum number of idle HTTP worker threads. # JENKINS_HANDLER_IDLE="20" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Pass arbitrary arguments to Jenkins. # Full option list: java -jar jenkins.war --help # JENKINS_ARGS=""
簡單介紹一下jenkins配置文件中一些參數的含義: JENKINS_HOME # 定義你的jenkins安裝目錄的路徑 JENKINS_JAVA_CMD # 定義java的可執行程序的路徑 JENKINS_USER # 定義啓動jenkins服務的用戶 JENKINS_JAVA_OPTIONS # java虛擬機的內存設置 JENKINS_PORT # 定義jenkins服務監聽的端口(http) JENKINS_LISTEN_ADDRESS # 定義jenkins服務監聽的ip地址 JENKINS_HTTPS_PORT # 定義https協議的監聽端口 JENKINS_HTTPS_KEYSTORE # 定義ssl密鑰文件的路徑 JENKINS_HTTPS_KEYSTORE_PASSWORD # 定義ssl密鑰文件的密碼 JENKINS_HTTPS_LISTEN_ADDRESS # 定義https協議的監聽ip JENKINS_DEBUG_LEVEL # 定義日誌的調試級別——值越高,日誌記錄越詳細 JENKINS_ENABLE_ACCESS_LOG # 定義是否啓用訪問日誌記錄 JENKINS_HANDLER_MAX # 定義jenkins服務的最大工做線程數量 JENKINS_HANDLER_IDLE # 定義空閒工做線程的最大數量 JENKINS_ARGS # 定義jenkins的參數選項
jenkins的日誌文件所在的目錄:
[root@localhost yum.repos.d]# ll /var/log/jenkins 總用量 180 -rw-r--r--. 1 jenkins jenkins 180382 2月 11 10:15 jenkins.log
4、jenkins發佈php網站
環境說明:
jenkins服務器:192.168.146.156 gitlab服務器:192.168.146.128 網站服務器:192.168.146.134
[root@localhost yum.repos.d]# systemctl restart jenkins [root@localhost yum.repos.d]# systemctl status jenkins
有如下兩個,表示插件安裝成功。
jenkins服務器免密鑰登錄網站服務器,用於部署網站:
[root@localhost ~]# ssh-keygen -f /root/.ssh/jenkins Generating public/private rsa key pair. Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/jenkins. Your public key has been saved in /root/.ssh/jenkins.pub. The key fingerprint is: 8d:55:44:34:2c:3d:4d:17:49:e9:f4:8f:9d:2f:aa:a0 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | **ooo=| | ..+..= | | .. .o .| | + ..| | S . oo| | ..o| | . .| | . . . .| | E .... . | +-----------------+ [root@localhost ~]# scp -P 22 /root/.ssh/jenkins.pub root@192.168.146.134:~/.ssh/authorized_keys root@192.168.146.134's password: jenkins.pub 100% 408 0.4KB/s 00:00
驗證是否需求密碼訪問,若是不須要密碼登錄就表示成功:
[root@localhost ~]# ssh -i /root/.ssh/jenkins 192.168.146.134
查看公鑰:
[root@localhost ~]# cat /root/.ssh/jenkins.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCYCLM0o/TFI/dU5BesD1jrAUKcJl/3MsMwe+VS7hCleSC0tFjgrL0CJeC22tcjMoBJn1Eq1mGO3S4bLmpe6bV0+847bsTNB2++JsPSERY1MRO0V8QyQiO9ux5qaf0/neD4RpsjncLPpOBi3tU99aB2m7v68SUWPR3OlBO6gw+VnLf3BAnoMwb34IqdtiNXcdLTqLpsganeqCOVq36jipxlzd/XiCo7NdMv/GsFsYkYPocalYNSYymaSEqkBowkQhhD6z6XYIB5S0OImBo4LfOyqIjsLkMQmMFgALm03C2Csn6sj+6PDUuEVDJbPnil5ZAFjYiBvx12UPO522qMduDf root@localhost.localdomain
查看私鑰(jenkins服務器是用這個私鑰去匹配網站服務器的公鑰):
[root@localhost ~]# cat /root/.ssh/jenkins -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAmAizNKP0xSP3VOQXrA9Y6wFCnCZf9zLDMHvlUu4QpXkgtLRY 4Ky9AiXgttrXIzKASZ9RKtZhjt0uGy5qXum1dPvOO27EzQdvvibD0hEWNTETtFfE MkIjvbseamn9P53g+EabI53Cz6TgYt7VPfWgdpu7+vElFj0dzpQTuoMPlZy39wQJ 6DMG9+CKnbYjV3HS06i6bIGp3qgjlat+o4qcZc3f14gqOzXTL/xrBbGJGD6HGpWD UmMpmkhKpAaMJEIYQ+s+l2CAeUtDiJgaOC3zsqiI7C5DEJjBYAC5tNwtgrJ+rI/u jw1LhFQyWz54peWQBY2Igb8ddlDzudtqjHbg3wIDAQABAoIBAG9AZoqNs66TDAkV w/DHZkTJrkl/l1fX0JdpAj22MX407ebt1d/K56BrwU1tRGLgMJovOAEF0WvulC+V vobntD3/iUFaZC64U1+tFDxi/oI8AHRGiS5R0b+j7rTmDEiCKj7jw+DDL+wsdXpE H3zwgdRpT5xo27+zR12EZA9mmn/ahsQqShh5gkMkNlFON4PKhNCWFJppAfy+XL2o k3LeHVwOKnt3vurMgfDuFimrU+Vwe2+HV3AuazMUWnGzLrtQRG1Y/R3L84mClS5Q bwfkDMj2mFZU1zBOOIgPZRZWvHhhrg1QEA79ntMT7nZ17Bh46JsS43SCxNJwH/Dj iG/71vECgYEAyYsBZU545RsnKDeM4FlxOJXvxru9vl8xYD8Sk0jIGuHzUl5mdzX+ gmteFol7sFoJjkFaAwTJ2iWP60KwC2vefAUr1xnsx3T5maTj/kfCKck01TcirtWN vF9jw6g98fqvp1eyj8RNBNn+F5uZfJ9ul9FjwN1jAsEf6naPZs7veQkCgYEAwR0X YVH1ToMW3ktBFNFEzes48r+UphZ0u2Jl8fM3/GckfO1Nx4bJL6y68nkuVpcehUc/ MKO/Au5v3Larb21uaSU39TED5Iv3F+4PweTijCkYpeyxsspioSreuzQNO/1fxSOl jY8Ws3wGL0bjj2tP+AcXQlTFyrnG+1ipsmEejKcCgYEAmzuT0aHFgEGPwWw3970s dbcWve1w0iaj80vhVMwkRIBwV6saJQvTgkSMbbNrL4PKBGXVnKmdh4aS+VOeIM3u yXt6Lu1q8J3qeisE6KAArwqoxOj4r80QpFycjOETBURe8vebMxyxbeEx0qEocVKj bg/7U1uxnTd/C6pR/Y+553ECgYBqDKXIRg/mm8q1LLKC0iFsy94sQNVLGE3mkbTW V/DI5YN+L3tlNTnKupq3iaDO4JJcMrIa7fF5lKNNm+std/UIPO4r8kzm944sUAnL OvzGZ9MnW4KCYu4i/73tJDqYVR6AzoCd6pRW4Kcs00soeZAi7xHw1UkBeWAyq5AV a4wxRQKBgQC0Ykrx2xRSJc/DfnSwmN8rP4caCp3zX4aGIz6nD7r73R1p6VHmAA+b KwtDuv/FxqEfXwh/2zJ7UO00+mzeQX00NRSQTmV9JuJEBqhybeqUN2lWoVNFfSDA kcOqWslotg0G+h39emMGN5DGYPb3JWiB3G941xu5dfUZYyTA6eWpww== -----END RSA PRIVATE KEY-----
測試成功後點擊「應用」。
建立一個新任務
備註:這裏使用的是帳號密碼方式,可使用免密鑰登錄,即將jenkins的公鑰設置到gitlab項目中。
備註:jenkins能夠免密鑰登錄到全部服務器,即把jenkins產生的公鑰發佈給全部服務器上。
備註:幾臺服務器這裏就添加幾個ssh(Add Server)。
備註:生成環境Exec command通常是這樣的,或者用腳本。
mkdir -p /tmp/gxm/ssss/ddddd chown -R test.tesst /tmp/gxm systemctl stop httpd systemctl stop php-fpm ps -ef | grep httpd | grep -v grep | awk '{print $2}' | xargs -r kill -9 ps -ef | grep php-fpm | grep -v grep | awk '{print $2}' | xargs -r kill -9 mkdir -p /opt/backup/$(date +%Y%m%d) cp -a /opt/XYZ/gxm/ssss/ddddd/files /opt/backup/$(date +%Y%m%d)/ cp -a /opt/XYZ/gxm/ssss/ddddd/db.php /opt/backup/$(date +%Y%m%d)/ tar -czvf /opt/backup/$(date +%Y%m%d)/gxm.tar.gz /opt/XYZ/gxm echo "gxm backuptime:$(date +%Y%m%d) $(date +%H:%M:%S)" >> /opt/backup/backup.list rm -rf /opt/XYZ/gxm/* cp -a /tmp/gxm/* /opt/XYZ/gxm/ cp -a /opt/backup/$(date +%Y%m%d)/files /opt/XYZ/gxm/ssss/ddddd/ cp -a /opt/backup/$(date +%Y%m%d)/settings.php /opt/XYZ/gxm/ssss/ddddd/ systemctl start httpd sleep 5s systemctl start php-fpm sleep 5s ps -ef | grep httpd | grep -v grep ps -ef | grep php-fpm | grep -v grep tail -n 10 /usr/local/apache/logs/error_log tail -n 10 /var/log/php-fpm/error.log rm -rf /tmp/gxm
當即構建
最後到網站服務器上檢查目錄和權限、訪問網站驗證。
備註:也能夠不用git拉去代碼,而是直接ssh免密鑰登錄網站服務器,而後git克隆,替換網站目錄、還原個別文件。