1、環境准備
前提:系統必須安裝jdk ,ubunt使用默認的install_java.sh安裝,此處不贅述jdk安裝步驟。
java -version
2、安裝jenkins
2.1將存儲庫密鑰添加到系統
在Ubuntu上安裝Jenkins相對簡單。我們將啟用Jenkins APT存儲庫,導入存儲庫GPG密鑰,並安裝Jenkins包。
使用以下wget命令導入Jenkins存儲庫的GPG鍵:
apt-get update wget -q -O - http://pkg.jenkins-ci.org/debian-stable binary/ | apt-key add -
2.2將Debian包存儲庫地址附加到服務器的sources.list
sh -c 'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
2.3 apt-get update
apt-get update
2.3.1安裝jenkins報錯
報錯:由於沒有公鑰,無法驗證下列簽名
W: GPG error: https://pkg.jenkins.io/debian-stable binary/ Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY FCEF32E745F2C3D5
解決方法
導入密鑰:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FCEF32E745F2C3D5
最后的輸入keys要和你報錯顯示的一致哈
如下圖
報錯:
apt install jenkins報錯:
Err:1 https://pkg.jenkins.io/debian-stable binary/ jenkins 2.277.4 gnutls_handshake() failed: Error in the pull function. E: Failed to fetch https://pkg.jenkins.io/debian-stable/binary/jenkins_2.277.4_all.deb gnutls_handshake() failed: Error in the pull function.
報錯已經很明顯了,是下載源問題,更改以下即可。
sh -c 'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
2.4 安裝Jenkins
apt-get install jenkins
二、jenkins插件配置
1、更改jenkins工作目錄workpace
jenkins默認安裝的主目錄為:/var/lib/jenkins,最新版本的jenkins在系統頁面上高級按鈕已經不存在更改選項。只能更改配置文件了。
將自定義目錄更改至變量導入
root@jenkinis:vi /etc/profile
export JENKINS_HOME=/data/jenkins
root@jenkinis:source /etc/profile
然后創建目錄指定權限
mkdir -p /data/jenkins chown -R jenkins:jenkins jenkins
重啟jenkins
/etc/init.d/jenkins restart cat /data/jenkins/secrets/initialAdminPassword
重啟之后在全局配置的主目錄顯示還是/data/jenkins,新建一個項目試下,構建之后發現已經生效/data/jenkins/workspace,以下為jenkins創建的項目。正常使用。
/etc/init.d/jenkins start | restart | stop
2、安裝maven
2.1 自定義版本安裝(我使用的是這個)
下載后解壓至指定目錄安裝:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz tar -xvf apache-maven-3.3.9-bin.tar.gz -C /usr/local/
編輯文件變量添加:
vi /etc/profile
最后加載變量:
在jenkins系統配置頁面增加maven配置:
最后配置maven的庫配置:
[root@saas-cdt-jenkinis:/usr/local/apache-maven-3.3.9]#pwd /usr/local/apache-maven-3.3.9 [root@saas-cdt-jenkinis:/usr/local/apache-maven-3.3.9]#mv settings.xml settings.xml.default [root@saas-cdt-jenkinis:/usr/local/apache-maven-3.3.9]#vi conf/settings.xml
將私服庫的登錄等配置信息錄入:
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>/data/jenkins/repo</localRepository> <pluginGroups></pluginGroups> <proxies></proxies> <servers> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers> <mirrors> <mirror> <id>nexus-releases</id> <mirrorOf>*,!saas-local</mirrorOf> <url>http://192.168.224.234:8081/repository/maven-public/</url> </mirror> <mirror> <id>nexus-snapshots</id> <mirrorOf>*,!saas-local</mirrorOf> <url>http://192.168.224.234:8081/repository/maven-snapshots/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus-releases</id> <url>http://nexus-releases</url> <releases><enabled>true</enabled></releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> <repository> <id>nexus-snapshots</id> <url>http://nexus-snapshots</url> <releases><enabled>true</enabled></releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus-releases</id> <url>http://nexus-releases</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> <pluginRepository> <id>nexus-snapshots</id> <url>http://nexus-snapshots</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> <profile> <id>jdk18</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>
最后驗證mvn版本成功:
備注:訪問maven私服權限
maven私服倉庫192.168.224.234設置了安全組,默認的8081端口需要增加jenkins的訪問權限,如下圖:
在jenkins機器上驗證訪問:
2.2使用系統插件安裝
系統管理>插件管理>可選插件>
然后在右邊的過濾輸入框中輸入搜索關鍵字: Maven Integration 或者 Pipeline Maven Integration ,搜索到了以后,點擊直接安裝,
安裝完成后重啟就好了。 我這安裝的是 Maven Integration
3、安裝git參數插件
4、安裝Gradle
這個包的功能是提供打包apk生成apk文件,
下載地址:https://gradle.org/releases/
下載gradle-5.6.4-all.zip包,將包解壓至/usr/local目錄並加入變量文件
變量文件配置:
vi /etc/profile 我是root用戶編輯
最后source /etc/profile加載變量
在Jenkins上加載Gradle配置
Jenkins的dashboard主頁面>>系統管理>>全局工具配置>>Gradle 設置安裝路徑最后保存
驗證是否安裝。隨便新建一個項目>>增加構建步驟>>Invoke Gradle script
至此,自定義版本安裝完成, 也可以在插件里面選擇此插件進行安裝。
5 安裝Node.js
下載安裝的是:https://nodejs.org/download/release/v10.16.3/node-v10.16.3-linux-x64.tar.gz
下載解壓至/usr/local/
tar -xf node-v10.16.3-linux-x64.tar.gz -C /usr/local
添加變量/etc/profile
export NODE_HOME=/usr/local/node-v10.16.3-linux-x64 export NODE_PATH=/usr/local/node-v10.16.3-linux-x64/lib/node_modules export PATH=$NODE_HOME/bin
保存退出加載變量生效:
source /etc/profile
創建一個軟連接將自己的node的執行文件指到/usr/local/bin/node上,於是修改如下:
[root@CZT-PRD-JENKINS:]#ln -s /usr/local/node-v10.16.3-linux-x64/bin/node /usr/local/bin/ [root@CZT-PRD-JENKINS:]#ln -s /usr/local/node-v10.16.3-linux-x64/bin/npm /usr/local/bin/
不然會報錯:
/usr/bin/env: node: No such file or directory
6 安裝dos2unix
[root@CZT-PRD-JENKINS:/root/soft]#apt-get install dos2unix 直接安裝即可 Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libtomcrypt0 libtommath0 timelimit Use 'apt autoremove' to remove them. The following NEW packages will be installed: dos2unix 0 upgraded, 1 newly installed, 0 to remove and 24 not upgraded. Need to get 68.7 kB of archives. After this operation, 306 kB of additional disk space will be used. Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 dos2unix amd64 6.0.4-1 [68.7 kB] Fetched 68.7 kB in 0s (819 kB/s) Selecting previously unselected package dos2unix. (Reading database ... 104048 files and directories currently installed.) Preparing to unpack .../dos2unix_6.0.4-1_amd64.deb ... Unpacking dos2unix (6.0.4-1) ... Processing triggers for man-db (2.7.5-1) ... Setting up dos2unix (6.0.4-1) ...
三、連接gitlab
1、域名解析添加
我司只有一個gitlab倉庫,存放本地,使用公網ip映射,端口是8083,在阿里雲內網jenkins服務器增加本地解析,如下:
appdeploy@saas-cdt-jenkinis:~$ cat /etc/hosts | grep git 61.141.xxx.3 gitlab.smartcharge.com appdeploy@saas-cdt-jenkinis:~$
2、項目添加gitlab地址
說明:gitlab服務器信息實際為本地辦公網服務器10.101.72.226,使用了電信公網ip:61.141.xx.3
http://pkg.jenkins-ci.org/debian-stable binary/