jenkins部署java項目到遠程linux上,腳本文件和項目一起上傳到gogs上,直接執行gogs上的腳本文件來執行項目
(1)新建maven項目
pom.xml的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId> <artifactId>jenkins_jar</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jenkins_jar</name> <url>http://maven.apache.org</url> <build> <finalName>jenkins_jar</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 --> <!-- <index>true</index> --> <manifest> <mainClass>cn.demo.jenkins_jar.demo.Demo</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <id>${project.version}</id><!--名字任意 --> <phase>package</phase> <!-- 綁定到package生命周期階段上 --> <goals> <goal>single</goal> <!-- 只運行一次 --> </goals> <configuration> <descriptors> <!--描述文件路徑--> <descriptor>script.xml</descriptor> </descriptors> <!--這樣配置后,mvn deploy不會把assembly打的zip包上傳到nexus--> <attach>false</attach> </configuration> </execution> </executions> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
>maven-assembly-plugin插件指定的 script.xml文件 (文件存放的位置:直接在項目下 (非src))
script.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>script</id> <formats><!--打包的文件格式 --> <format>zip</format> </formats> <fileSets> <fileSet> <directory>script</directory><!--需要打包的目錄 --> <outputDirectory>/</outputDirectory> <!-- 打包后輸出的路徑 輸出子啊target目錄下 --> </fileSet> </fileSets> </assembly>
在該項目下直接新建文件夾 script ,在文件夾下新建腳本文件 start.sh 內容如下
start.sh
#!/bin/sh cd /root/home/program/pro_java/ #得到進程ID pid,kill該進程 pid=`cat /root/home/program/pro_java/pid` if [ -n "$pid" ] then echo "kill -9 的pid:" $pid kill -9 $pid fi #執行jar,並將進程掛起,保存進程ID到 pid文件 echo "Execute shell Finish" BUILD_ID=dontKillMe nohup java -jar /root/home/program/pro_java/jenkins_jar.jar & echo "$!" > pid
(2)新建jenkins項目,選擇構建maven項目,開始配置
項目名稱:新建項目時,填的名稱
描述:這里是對項目的描述,解釋 自己定義就行了 不影響

源碼管理: Git
Repository URL:這里填gogs的倉庫地址
Credential:這里選擇gogs的登錄賬號和密碼(如果沒有就點擊右側的add按鈕,添加 只需要填寫 用戶名和密碼就可以了)

構建觸發器(這里的配置其實都沒什么影響,隨便寫個令牌,只要選中了觸發遠程構建就行了,在gogs上對應的倉庫里配置web鈎子就行)
使用web鈎子的作用:只有遠程代碼有push ,jenkins就會自動構建 (推薦使用)
也可以不選擇觸發遠程構建,選中Poll SCM 配置 H/5 * * * * 表示每5分鍾檢查一次代碼,發現有變動就觸發構建 ,但是這種效率不好,不推薦

Build 執行構建pom.xml
執行 maven命令clean package,先clean項目,再重新打包編譯
Post Steps:這個勾中第一個就行了,表示構建成功時才運行

點擊 add-post build step
選擇 send files or execute commands over SSH

進入如下配置
SSH Server ,
Name: root@172.150.12.32 //安裝publish over SSH插件,進行系統設置時的配置 Name名
Source files: target/jenkins_jar.jar,target/jenkins_jar-script.zip 將項目的jar包傳送到服務器,將打包shell腳本的zip壓縮包傳給服務器 //表示需要傳送的文件,多個文件用逗號分開
remove prefix:遠程linux需要移除的目錄名
Remote Directory:遠程linux接收這些文件的目錄
Exec Command:執行的腳本命令,命令如下
#!/bin/sh
cd /root/home/program/pro_java/ #進入遠程目錄
unzip -o -d /root/home/program/pro_java/ jenkins_jar-script.zip #將存放腳本文件的壓縮包解壓
cd jenkins_jar #打開解壓后的文件夾
sh start.sh #執行該腳本 該腳本文件內容在文章開頭 (1)中

配置到此結束,可以執行構建了
