【linux部署运维】linux系统使用脚本自动拉取git项目并部署

发布时间 2023-09-09 00:12:15作者: csjoz11

linux系统使用脚本自动拉取git项目并部署
前言
一、主要目的
二、使用步骤
1.idea创建简单的可运行springboot项目
2.linux(centos 7)系统上部署项目拉取脚本
3.解决需手动输入git账号密码问题
总结
前言
含泪记录一下linux系统上自动化部署项目的过程,有错欢迎指出

一、主要目的
linux系统上自动拉取git项目并一键部署

二、使用步骤
1.idea创建简单的可运行springboot项目
主要代码如下:

package com.yuan.helloworld.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("")
    public String hello(){
        System.out.println("hello world...");
        return "okok";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
然后推送至个人码云账户:


2.linux(centos 7)系统上部署项目拉取脚本
前置条件:已部署好jdk、tomcat、maven等插件,参考相关博客https://blog.csdn.net/qq_41328735/article/details/125986505
在已部署的linux(centos 7)系统上写一下项目拉取脚本
脚本如下:

#!/bin/sh
echo =================================
echo  自动化部署脚本启动
echo =================================

echo 停止原来运行中的工程
APP_NAME=helloworld

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Stop Process...'
    kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Kill Process!'
    kill -9 $tpid
else
    echo 'Stop Success!'
fi

echo 准备从Git仓库拉取最新代码
cd /usr/local/helloworld

echo 开始从Git仓库拉取最新代码
git pull
echo 代码拉取完成

echo 开始打包
output=`mvn clean package -Dmaven.test.skip=true`
echo "$output"
cd target

echo 启动项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
3.解决需手动输入git账号密码问题
此时可部署项目,但每次都得手动输入账号密码很麻烦,

那么我们使用yum下载expect插件:

[root@localhost sh]# yum install expect
1
写pull.exp脚本如下:

#!/usr/bin/expect
spawn ./bootStart.sh
expect "Username for 'https://gitee.com'"
send -- "yuanliangyou\n"
expect "Password for 'https://yuanliangyou@gitee.com'"
send -- "************\n"
interact

1
2
3
4
5
6
7
8
直接运行即可将前面的bootStart.sh脚本通过自动交互的方式自动完成

总结
以此为参照,后面的项目都可以通过此脚本实现自动化部署
————————————————
版权声明:本文为CSDN博主「桂圆粮油」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/good_hi/article/details/130209588