Ficus

Ficus

Ficus 不知道记录些什么?随缘
github

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

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

`

@TOC


前言#

为了快速上手 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

image

四、全局配置#

设置位置
Manage Jenkins - > System Configuration -> Global Tool Configuration
1.maven 的 settings 配置:
也可以自动安装

image

2.jdk 配置
也可以自动安装

image

3.maven 配置
也可以自动安装

image

五、配置凭据#

image

此处主要需要添加 git 的登录名密码,远程主机的登录密码

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

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

1. 新建 item 选择 Freestyle project

描述随便写
1.1 jenkins 的配置环境变量
勾选参数配置,jenkins 会将此参数传递给 shell 或 bat 命令.

此处主要配置了容器名称,镜像名称,springboot 运行环境等变量:
容器名称

image

镜像名称

image

springboot 激活的运行环境 (可配置)

image

容器中 springboot 的激活环境

image

1.2 JDK 选择:

image

1.3 源码管理:

image

1.4 构建顶层 maven 目标

image

image

说明:
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 镜像:

image

此处是先删除容器,在运行:

image

重点来了,如何将构建的产生的 jar 包和相应的 Dockerfile 文件推送到远程主机,并让远程主机 docker 构建和运行。

1.6. 构建后的操作:
使用推送到远程的选项.

image

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 运行

image

七、构建,打包,部署 vue(TODO)#

TODO

总结#

使用的是 windows 下 jenkins 构建,远程 linux 注解打包部署。使用的是自由风格的 jenkins 构建方式,且相关的 bat(linux 下是 shell)命令未写道文件中。

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。