jenkins自动拉取git分支构建项目

时间:2023-03-09 00:25:36
jenkins自动拉取git分支构建项目

一,创建jenkins项目

new item -》freestyle project, 自定义一个项目名称

二,配置项目

  1,Source Code Management 选择 git,输入git项目地址

  2,添加一个账户:即将登录git的账号添加到jenkins,添加完成后选择这个账号

  3,Branches to build, 填写需要被构建的分支名称,这里通常填 */develop

  4,Post-build Actions 选择 “send build artifacts over SSH” (如没有这项,则需要先安装插件 Publish Over SSH)

  5,添加服务器后,选择新添加的服务器

  6,Transfers 中输入对应参数

    。Source files: **/**

    。Remove prefix:留空

    。Remote directory:/www/wwwroot/ (根据实际情况填写)

    。Exec command:   

#复制git中忽略的配置文件到相应位置
cp /www/files/config_release.php /www/wwwroot/tp_web_release/application/config.php
cp /www/files/database_release.php /www/wwwroot/tp_web_release/application/database.php
#如果存在这样的不能删除的文件,先修改一下属性
chattr -i /www/wwwroot/tp_web_release/public/.user.ini
rm -f /www/wwwroot/tp_web_release/public/.user.ini
#创建需要的目录
mkdir /www/wwwroot/tp_web_release/runtime
mkdir /www/wwwroot/tp_web_release/runtime/cache
mkdir /www/wwwroot/tp_web_release/runtime/log
mkdir /www/wwwroot/tp_web_release/runtime/tlogs
mkdir /www/wwwroot/tp_web_release/runtime/temp
#修改权限
chown -R www:www /www/wwwroot/tp_web_release

三,如何添加新服务器

  1,Manage Jenkins -》 Configure System, 找到 Publish over SSH

  2,点击“Add”,可以通过密码,也可以通过密钥文件的方式来添加。 密码就是ssh用户密码,添加非常方便,密钥的话需要将公钥添加到服务器上,可以自行百度。

  3,点击“Test Configure”,显示 success 就是连接成功, save。

四,自动构建

  1,jenkins对应项目的配置中,Build Triggers,勾选 Build when a change is pushed to GitLab. GitLab webhook URL:xxx

  2,将这里的url地址填到gitlab对应的hook中

  3,在Allowed branches中输入要允许自动构建的分支,如选择Filter branches by regex,填入  .*release.*