Jenkins 流水线 获取git 分支列表_Jenkins发布PHP项目之一自动化部署

这个说白了就是拉取到上线文件之后,直接通过执行rsync命令,来实现发布到对应的服务器

客户端macOS系统提交代码到===GitServe 10.211.55.3 /home/git/repos/app.git

关于这个的配置可以参考这里

Centos7下搭建简单的git服务器https://www.toutiao.com/i6882653528150508044/

开始构建项目

新建—输入项目名称php-deploy,选择构建自由风格的软件项目—下一步

Gernal配置,丢弃旧的构建,防止jenkins构建较多之后变臃肿

8fe86d5ba8a714f51d412c31604d6f49.png

配置好源码管理之后可以直接先保存构建一次

下面这个能跑通的前提是用户已经打通Credentials

手动在Jenkins主机上切换到Jenkins用户,执行git命令拉取代码

sudo su -s /bin/bash jenkins

git clone git@10.211.55.3:/home/git/repos/app.git

我这里通过rpm安装的Jenkins 所以将

/var/lib/jenkins/.ssh/id_rsa.pub追加到/home/git/.ssh/authorized_keys里面

一般就OK了

关于凭据这里配置了Jenkins用户

dbd76c1544ec41cc4f2096e8be795835.png

关于凭据说明参考此图

只有上面条件OK之后,下面源码管理这里才不会出错

73fbf998cc444dc3f5a34e91b3ba4ae7.png

然后直接构建

a09d80eeeaaabf4bb55d0523a85e1490.png

然后点击最左侧的图标:

e9b966b8fa381d9a1fd7077942739612.png

查看控制台输出:

Started by user adminRunning as SYSTEMBuilding in workspace /var/lib/jenkins/workspace/php-deployThe recommended git tool is: NONEusing credential 73d0c28a-8320-4393-86eb-2b60472899a1Cloning the remote Git repositoryCloning repository git@10.211.55.3:/home/git/repos/app.git > /usr/bin/git init /var/lib/jenkins/workspace/php-deploy # timeout=10Fetching upstream changes from git@10.211.55.3:/home/git/repos/app.git > /usr/bin/git --version # timeout=10 > git --version # 'git version 1.8.3.1'using GIT_SSH to set credentials  > /usr/bin/git fetch --tags --progress git@10.211.55.3:/home/git/repos/app.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > /usr/bin/git config remote.origin.url git@10.211.55.3:/home/git/repos/app.git # timeout=10 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10Avoid second fetch > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10Checking out Revision 8d2821c2b9e4ac77d758ba2ca6ad87bc9a7e709a (refs/remotes/origin/master) > /usr/bin/git config core.sparsecheckout # timeout=10 > /usr/bin/git checkout -f 8d2821c2b9e4ac77d758ba2ca6ad87bc9a7e709a # timeout=10Commit message: "add php file"First time build. Skipping changelog.[php-deploy] $ /bin/sh -xe /tmp/jenkins6495258260213274036.shFinished: SUCCESS

到现在为止,说明数据的拉取是正常的。都可以到对应的目录里边来查看。

# pwd

/var/lib/jenkins/workspace/php-deploy

# ls

index2.html index3.html index.html phpinfo.php

下一步的操作就是将拉渠道的代码推送到对应的PHP服务器的对应目录/home/www

命令简单说明:因为这个是在root下执行的,所以需要配置root用户的免密登陆

将Jenkins 的root用户的id_rsa.pub放到

cat /root/.ssh/id_rsa.pub >> /home/www/.ssh/authorized_keys

源目录删除的文件,目标文件也要删除,加--delete参数 --delete                delete extraneous files from destination dirs--progress              show progress during transferrsync -raz --delete --progress * www@10.211.55.3:/home/www

配置中如下填写

1473fb1d4cbabaa08878237d576cb863.png

再次构建,控制台输出

Started by user adminRunning as SYSTEMBuilding in workspace /var/lib/jenkins/workspace/php-deployThe recommended git tool is: NONEusing credential 73d0c28a-8320-4393-86eb-2b60472899a1 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > /usr/bin/git config remote.origin.url git@10.211.55.3:/home/git/repos/app.git # timeout=10Fetching upstream changes from git@10.211.55.3:/home/git/repos/app.git > /usr/bin/git --version # timeout=10 > git --version # 'git version 1.8.3.1'using GIT_SSH to set credentials  > /usr/bin/git fetch --tags --progress git@10.211.55.3:/home/git/repos/app.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10Checking out Revision 8d2821c2b9e4ac77d758ba2ca6ad87bc9a7e709a (refs/remotes/origin/master) > /usr/bin/git config core.sparsecheckout # timeout=10 > /usr/bin/git checkout -f 8d2821c2b9e4ac77d758ba2ca6ad87bc9a7e709a # timeout=10Commit message: "add php file" > /usr/bin/git rev-list --no-walk 8d2821c2b9e4ac77d758ba2ca6ad87bc9a7e709a # timeout=10[php-deploy] $ /bin/sh -xe /tmp/jenkins5178794915886016847.sh+ rsync -raz --delete --progress index2.html index3.html index.html phpinfo.php www@10.211.55.3:/home/www/  ___                 ___   (o o)               (o o) (  V  ) ALex CentOS (  V  )--m-m-----------------m-m--sending incremental file listFinished: SUCCESS

登录到PHP服务器,可以看到文件已经存在了

$ pwd/home/www$ lsindex2.html  index3.html  index.html  phpinfo.php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值