windows下使用jenkins进行构建,打包,部署(freestyle方式)

windows下使用jenkins进行构建,打包,部署(freestyle方式)

`


前言

为了快速上手jenkins,云服务器没那么多…所以用windows作为构建服务器,另一台腾讯云作为部署服务器。


一、安装

说明:之前其实采用的是在docker-desktop中安装的jenkins,但是遇到了问题,就是:docker内的jenkins怎么使用windows下的docker命令。(Windows下的调用docker的文件和linux中的不一样)

1.官网下载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

1.配置 SSH Servers
在这里插入图片描述

四、全局配置

设置位置
Manage Jenkins - > System Configuration -> Global Tool Configuration
1.maven的settings配置:
也可以自动安装
在这里插入图片描述

2.jdk配置
也可以自动安装
在这里插入图片描述

3.maven配置
也可以自动安装
在这里插入图片描述

五、配置凭据

在这里插入图片描述
此处主要需要添加 git的登录名密码,远程主机的登录密码

六、构建,打包,部署微服务

基本思路:windows下jenkins拉去仓库的项目并构建,产生jar包;然后将jar包和Dockerfile等打包所需的文件推送到远程部署服务器,由远程服务器来进行docker打包和部署。
此处没有使用pipeline,以及没有将命令写道bat(linux下是shell)脚本文件中
项目结构:
在这里插入图片描述

1.新建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)命令未写道文件中。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值