在 Windows 下使用 Jenkins 進行構建、打包、部署(Freestyle 方式)#
`
@TOC
前言#
為了快速上手 Jenkins,雲伺服器沒那麼多.. 所以用 Windows 作為構建伺服器,另一台騰訊雲作為部署伺服器。
一、安裝#
說明:之前其實採用的是在 docker-desktop 中安裝的 Jenkins,但是遇到了問題,就是:docker 內的 Jenkins 怎麼使用 Windows 下的 docker 命令。(Windows 下的調用 docker 的文件和 Linux 中的不一樣)
- 下載 Jenkins
https://www.jenkins.io/
一路默認安裝即可,在選擇安裝路徑時可安裝在其他盤。
P.S.
這裡需要注意的是,Jenkins 的默認工作路徑實在 C 盤下:C:\Users\ 用戶名 \AppData\Local
如果改到其他盤,需找到安裝時所選的路徑下的 jenkins.xml 文件
修改如下: 注意那三個 D 盤的修改:
<env name="JENKINS_HOME" value="D:\java\jenkins-win\home\Jenkins\.jenkins"/>
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "D:\java\jenkins-win\jenkins.war" --httpPort=9091 --webroot="D:\java\jenkins-win\home\Jenkins\war"</arguments>
<pidfile>D:\java\jenkins-win\home\Jenkins\jenkins.pid</pidfile>
完整配置
<!--
The MIT License
Copyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Oleg Nenashev, and other Jenkins contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!--
Windows service definition for Jenkins.
To uninstall, run "jenkins.exe stop" to stop the service, then "jenkins.exe uninstall" to uninstall the service.
Both commands don't produce any output if the execution is successful.
-->
<service>
<id>jenkins</id>
<name>Jenkins</name>
<description>This service runs Jenkins automation server.</description>
<env name="JENKINS_HOME" value="D:\java\jenkins-win\home\Jenkins\.jenkins"/>
<!--
if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe.
The following value assumes that you have java in your PATH.
-->
<executable>D:\java\jdk\jdk-11.0.4\bin\java.exe</executable>
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "D:\java\jenkins-win\jenkins.war" --httpPort=9091 --webroot="D:\java\jenkins-win\home\Jenkins\war"</arguments>
<!--
interactive flag causes the empty black Java window to be displayed.
I'm still debugging this.
<interactive />
-->
<logmode>rotate</logmode>
<onfailure action="restart"/>
<!--
In the case WinSW gets terminated and leaks the process, we want to abort
these runaway JAR processes on startup to prevent corruption of JENKINS_HOME.
So this extension is enabled by default.
-->
<extensions>
<!-- This is a sample configuration for the RunawayProcessKiller extension. -->
<extension enabled="true" className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension" id="killOnStartup">
<pidfile>D:\java\jenkins-win\home\Jenkins\jenkins.pid</pidfile>
<stopTimeout>10000</stopTimeout>
<stopParentFirst>false</stopParentFirst>
</extension>
</extensions>
<!-- See the referenced examples for more options -->
</service>
最後重啟 Jenkins 服務
二、安裝常用插件#
1.Publish Over SSH (遠程推送)#
三、系統設置#
設置位置如下
Manage Jenkins - > System Configuration -> Configure System
- 配置 SSH Servers
四、全局配置#
設置位置
Manage Jenkins - > System Configuration -> Global Tool Configuration
- maven 的 settings 配置:
也可以自動安裝
- jdk 配置
也可以自動安裝
- maven 配置
也可以自動安裝
五、配置憑據#
此處主要需要添加 git 的登錄名密碼,遠程主機的登錄密碼
六、構建、打包、部署微服務#
基本思路:Windows 下 Jenkins 拉取倉庫的項目並構建,產生 jar 包;然後將 jar 包和 Dockerfile 等打包所需的文件推送到遠程部署伺服器,由遠程伺服器來進行 docker 打包和部署。
此處沒有使用 pipeline,以及沒有將命令寫到 bat(Linux 下是 shell)腳本文件中
項目結構:
- 新建 item 選擇 Freestyle project
描述隨便寫
1.1 Jenkins 的配置環境變量
勾選參數配置,Jenkins 會將此參數傳遞給 shell 或 bat 命令.
此處主要配置了容器名稱,鏡像名稱,springboot 運行環境等變量:
容器名稱
鏡像名稱
springboot 激活的運行環境 (可配置)
容器中 springboot 的激活環境
1.2 JDK 選擇:
1.3 源碼管理:
1.4 構建頂層 maven 目標
說明:
clean install :清理和安裝到本地倉庫
-pl 指定打包的模塊
-am 將 - pl 所依賴到的模塊也進行編譯(需要加上,否則會找不到所有來的模塊)
pom: 指定 執行頂層 pom 所在的路徑(maven 執行命令所在的路徑);${WORKSPACE}/pom.xml git 項目的最外層的路徑
例如項目的結構:
A-cloud:
B-gateway
pom.xml
C-common
pom.xml
此時 ${WORKSPACE}=A-cloud 所在的路徑
-pl B-gateway : 指定 A-cloud 下的 B-gateway 為打包模塊
-am 將所依賴的模塊也進行構建打包。(此時就是從 A-cloud 下的 pom 進行查找的)
1.5 增加構建步驟,執行本地主機的 bat 命令:
此處是先清理鏡像,在打包 docker 鏡像:
此處是先刪除容器,在運行:
重點來了,如何將構建的產生的 jar 包和相應的 Dockerfile 文件推送到遠程主機,並讓遠程主機 docker 構建和運行。
1.6. 構建後的操作:
使用推送到遠程的選項.
dockerFIle:
FROM openjdk:11.0.14.1-slim-buster
MAINTAINER FICUS
WORKDIR myapp
VOLUME /tmp
ARG JAR_FILE=./target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 7070
#ENTRYPOINT ["sh","-c","java -jar -Dspring.profiles.active=dev2 app.jar "]
ENTRYPOINT ["sh","-c","java -jar app.jar --spring.profiles.active=dev2"]
#CMD在ENTRY POINT之後執行
#CMD [ "redis-server" ]
P.S.
/ficus-cloud 也可以用 ${WORKSPACE} 替換
source files: 本地的需要推送的文件
remote prefix: 去除路徑前綴。(此處沒有填寫,即遠程會創建 ficus-gateway/target 目錄,ficus-gateway 目錄)
remote directory: 推送所在的基本路徑,改路徑是追加在之前系統設置的工作路徑後的
即:/root/mytarget/ficus-cloud
cd mytarget/ficus-cloud :切換到項目目錄下(命令默認的位置是用戶所在的主目錄)。
1.7 運行
七、構建、打包、部署 Vue(TODO)#
TODO
總結#
使用的是 Windows 下 Jenkins 構建,遠程 Linux 進行打包部署。使用的是自由風格的 Jenkins 構建方式,且相關的 bat(Linux 下是 shell)命令未寫到文件中。