參考 https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+on+Ubuntu#app-switcher
安裝
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
升級
//Once installed like this, you can update to the later version of Jenkins (when it comes out) by running the following commands:
sudo apt-get update
sudo apt-get install jenkins
安裝包做了如下事情
- Jenkins will be launched as a daemon up on start. See /etc/init.d/jenkins for more details.
- The 'jenkins' user is created to run this service.
Log file will be placed in /var/log/jenkins/jenkins.log. Check this file if you are troubleshooting Jenkins. - /etc/default/jenkins will capture configuration parameters for the launch like e.g JENKINS_HOME
- By default, Jenkins listen on port 8080. Access this port with your browser to start configuration.
If your /etc/init.d/jenkins file fails to start jenkins, edit the /etc/default/jenkins to replace the line
HTTP_PORT=8080byHTTP_PORT=8081
Here, 8081 was chosen but you can put another port available.
完成之后,打開ip:8081即可看到j(luò)enkins

第一次打開會(huì)要求輸入密碼
可用
cat /var/lib/jenkins/secrets/initialAdminPassword查看并復(fù)制密碼
一般選擇推薦即可。
開啟jenkins之旅吧。
遇到的問題總結(jié):
-
按照windows的配置方式配置好之后,想把代碼拉取到自定義的workspace目錄下,在windows上正常運(yùn)行,但是在ubuntu上報(bào)錯(cuò)了,
自定義workspace
后來分析原因是安裝時(shí)jenkins用的是自己新建的用戶叫jenkins,jenkins沒有足夠權(quán)限,不能創(chuàng)建文件夾導(dǎo)致,后取消自定義工作空間,后正常構(gòu)建。jenkins默認(rèn)工作目錄/var/lib/jenkins/workspace/ -
構(gòu)建node應(yīng)用程序時(shí)需要用到npm命令
npm install,但是會(huì)碰到
npm WARN checkPermissions Missing write access to /var/lib/jenkins/workspace/meeting/node_modules的權(quán)限錯(cuò)誤問題,這是需要提升權(quán)限,顧用sudo npm install,但是此時(shí)還會(huì)出現(xiàn)# [sudo: no tty present and no askpass program specified”的錯(cuò)誤,由于帳號并沒有開啟免密碼導(dǎo)致的 ,解決辦法就是切換到root用戶并添加jenkins的權(quán)限vim /etc/sudoers添加免密碼jenkins ALL = NOPASSWD: ALL,也可以將jenkins用戶添加可以用sudo執(zhí)行命令
jenkins權(quán)限及免密
構(gòu)建 -
jenkins權(quán)限問題終極解決方案
在Ubuntu下,當(dāng)執(zhí)行apt-get install方式安裝Jenkins的時(shí)候,會(huì)自動(dòng)創(chuàng)建jenkins用戶,這是一個(gè)沒有用戶目錄的賬號。
用root運(yùn)行jenkins
更換jenkins name
修改/etc/default/jenkins文件,

將jenkins賬號分別加入到root組中
gpasswd -a root jenkins
把jenkins用戶加入sudo用戶組
adduser jenkins sudo
設(shè)置密碼(也可以選擇在visudo設(shè)置NOPASSWD讓用戶請求sudo權(quán)限時(shí)不需要輸入密碼)
passwd jenkins
然后重啟服務(wù)
service jenkins restart
- 將jenkins 8080端口映射到80端口 參考jenkins官網(wǎng)
在/etc/nginx/sites-available/ 下新建名為jenkins文件,將以下內(nèi)容拷貝進(jìn)去
server {
# listen 80;
server_name jenkins.example.com;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:8080;
proxy_read_timeout 90;
proxy_redirect http://127.0.0.1:8080 https://jenkins.example.com;
# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_request_buffering off;
# workaround for https://issues.jenkins-ci.org/browse/JENKINS-45651
add_header 'X-SSH-Endpoint' 'jenkins.domain.tld:50022' always;
}
}
然后輸入一下命令,在sites-enabled文件夾下建立jenkins文件的鏈接
ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/
最后再重啟nginx即可
service nginx restart
nginx -s reload
//如果是用supervisor管理的nginx進(jìn)程可以重新加載
supervisorctl restart nginx
<未完.>



