1. 进入Jenkins首页,点击新建任务。

2. 进入新建任务页面,输入任务名称,选择构建maven项目(如果是maven项目的话),确定。

3. 配置
点击任务名称

点击配置,进入配置页面

3.1 配置 - general

填写项目名称。
勾选(丢弃旧的构建)。

保持构建的最大个数:因为每次构建会有记录,次数设置为保留最新10次构建的记录,其他的删除,防止记录过多占用空间。
3.2 配置 - 源码管理
选择源码管理工具(此处为git),配置好源码地址,分支等。

3.3 配置 - 构建触发器
当某个事件被触发时,自动重新构建项目。例如 ,如果勾选了(Build when a change ...) ,则当git上的源码有更新时,会自动重新构建项目。此处不设置任何触发器。

3.3 配置 - 构建环境

此处只勾选(Delete workspace before build starts),即每次重新构建之前删除工作空间(工作区)中的代码。

因为每次重新构建时都会从配置的源码地址取到最新的项目代码放到工作区中,但是Jenkins只会将代码放到工作区中,而不执行删除操作,同样的文件会覆盖掉,但如果新的代码中有些文件已经删除了,则在工作区中还会保留。所此处要构造这个选项。
3.4 配置 - Pre Steps/Build/Post Steps

Pre Steps(构建前操作)、Post Steps(构建后操作),此处不需要配置。
在Build中,因为构建的是Maven项目,所以在这里需要告诉Jenkins项目的pom.xml以及Maven的配置文件在哪里,点击“高级...”展开如下。

这些配置可以在系统设置中设置好,然后每次新建任务就不需要设置了。
3.5 配置 - 构建设置
勾选后会有邮件通知,此处不设置。
3.6 配置 - 构建后操作

选择 Send build artifacts over SSH(使用SSH发送artifacts到服务器)
为什么选这个?
因为此处Jenkins和项目没有部署在同一个Linux机器上。这个插件可以通过ssh连接其他Linux机器,如果没有这个选项,则可以安装插件:系统管理→管理插件→可选插件→Artifact Uploaders→Publish Over SSH
设置如下

Name: 选择一个服务器(配置好的服务器)
Source files:打包好的项目jar文件路径(在工作区)
Remove prifix:去掉Source files 中的前缀
Remote directory:jar包存储路径
Exec command:shell脚本
Jenkins会在工作区中项目根目录下去寻找 Source files 中指定的jar文件,去掉 Remove prifix 指定的前缀后,将文件存放在 Remote directory 指定的位置下(如果Remove prifix中没有设置,则存放的位置会变成/data/appuser/hello-world-web/target),然后执行 Exec command 中的脚本命令,构建项目(当然,只有当点击了“构建”才会执行这些命令)。
|