[技術(shù)探索]jenkins自動化部署配置

Ubuntu18.04安裝Jenkins

通過進行系統(tǒng)更新和升級來開始安裝:

sudo  apt-get update
sudo apt-get upgrade

buntu 18.04上提供的Jenkins版本比上游版本舊,我們將使用項目維護的軟件包來安裝Jenkins。使用以下命令添加存儲庫密鑰:

wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

一旦添加了密鑰,我們需要添加Jenkins穩(wěn)定的Debian軟件包存儲庫:

echo "deb https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list

然后更新apt包索引:

sudo apt-get update

完成后,安裝openjdk-8-jdk,因為Jenkins與java 9或其它版本可能會不兼容:

sudo apt-get install openjdk-8-jdk

如果安裝了多個版本的Java JDK,則可以使用以下命令選擇版本8作為默認值:

sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Nothing to configure.

然后安裝Jenkins,運行以下命令:

sudo apt-get install jenkins

安裝完成后如果8080端口是可以正常訪問的,那么在瀏覽器中輸入
http://IP_ADDRESS:8080就會自動跳轉(zhuǎn)到j(luò)enkins登錄界面

image.png

將/var/libjenkins/secrets/initialAdminPassword文件中的內(nèi)容復(fù)制黏貼進去就會出現(xiàn)以下內(nèi)容,新手選第一個即可
image.png

安裝完之后(如果失敗,登陸進去之后可以去插件管理里面重新添加插件)設(shè)置一個登錄用戶
image.png

以下界面就是jenkins主界面


image.png

jenkins郵箱配置

點擊系統(tǒng)配置-》jenkins Location換成自己的URL和郵箱


image.png

往下找到Extended E-mail Notification,設(shè)置如下


image.png

上面如果沒有看到Use SMTP Authentication可以查看右側(cè)是否有“高級選項”,其中的Password不是User Name的登錄密碼或者前面登錄jenkins時候設(shè)置的密碼,而是該郵箱的授權(quán)碼,用于第三方登錄,以qq為例。進入qq郵箱設(shè)置-》賬戶,即可找到


image.png

郵箱的端口:25、465、587 是qq郵箱的端口,郵箱發(fā)送成但沒有收到郵件,可以嘗試換郵箱端口試試。


image.png

項目構(gòu)建

  • 系統(tǒng)管理-》節(jié)點管理-》新建節(jié)點,設(shè)置一個節(jié)點名稱,并勾選固定節(jié)點。
  • 新建任務(wù)-》新建一個自由風格的任務(wù)-》
    • 添加描述
    • 任務(wù)綁定特定節(jié)點


      image.png
    • 丟棄舊的構(gòu)建任務(wù)


      image.png
    • 參數(shù)化構(gòu)建過程


      image.png

      有選項參數(shù),字符串參數(shù)...


      image.png

      按照上面可以添加多個參數(shù),最后你會看到如下的內(nèi)容
      image.png
    • 源碼管理
      添加git的項目地址,以及可以拉取該項目的用戶憑據(jù)


      image.png
    • 新增構(gòu)建腳本,寫入構(gòu)建的腳本內(nèi)容


      image.png
    • 開始構(gòu)建


      image.png
    • 構(gòu)建列表


      image.png
  • 構(gòu)建日志終端顯示(點擊上面的任一構(gòu)建項)


    image.png

WebHook配置

配置webhooks的作用就是當開發(fā)人員將代碼提交到github或者gitlab之后,jenkins監(jiān)聽到有代碼提交就會觸發(fā)上面的項目構(gòu)建任務(wù),具體項目倉庫下點擊settings->webhooks->addwebhooks

image.png

jenkins->系統(tǒng)設(shè)置-》GithubServer
image.png

在項目構(gòu)建時勾選

image.png

觸發(fā)成功


image.png

使用中出現(xiàn)的問題

主界面出現(xiàn):反向代理設(shè)置有誤

問題原因:系統(tǒng)管理-->系統(tǒng)設(shè)置里的Jenkins URL沒有寫對

HTTP ERROR 403
Problem accessing /job/deployWebTask/descriptorByName/hudson.plugins.git.UserRemoteConfig/checkUrl. Reason:
No valid crumb was included in the request

解決方案:位置: Jenkins > 全局安全配置 > CSRF Protection,取消勾選

Command "git ls-remote -h -- git@github.com:yeqianxun/myweb.git HEAD" returned status code 128:stdout:stderr: git@github.com: Permission denied (publickey).

fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解決方案:源碼管理位置點擊添加


image.png

注意下圖所示三個箭頭,這里要添加的是私鑰(id_rsa)的內(nèi)容,而相應(yīng)的公鑰在github上,之前我添加的是id_rsa.pub文件的內(nèi)容,一直報上述錯誤

image.png

Sending email for trigger: Success An attempt to send an e-mail to empty list

首先我確認我的Recipient List是添加了的。不是empty狀態(tài)


image.png

image.png
image.png

測試郵件可以接收通知,但是編譯構(gòu)建之后無論成功失敗都沒有收到郵件通知

image.png
image.png

MessagingException message: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1

image.png

嘗試將465換成587,之后正常收到郵件,可能qq郵件服務(wù)器的問題。

Jenkins全局變量

https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容