Github action持续部署springboot+vue前后端分离项目

因为购买的阿里云服务器性能比较拉垮,安装jenkins做持续集成和持续部署会占用比较大量的服务器资源。因为实习的时候当时负责的一个项目就是把jenkins迁移到github action,所以就优先考虑使用Github action.

GitHub Actions 是一个由 GitHub 提供的持续集成(CI)和持续部署(CD)平台,它允许开发者在代码仓库中设置自动化的工作流程。这些工作流程可以在代码的不同事件触发时自动执行,例如推送代码、创建 Pull Request、发布 Release 等。

Secret配置

需要提前配置一些github action secret,打开对应项目setting

image-20230815103929921

配置后即可在yaml文件中使用$使用对应值

vue项目

在项目里编写.github/workflows/node.js.yml

在github action里实现了

1、checkout代码

2、将源代码进行打包、压缩和优化,生成适合部署的生产环境代码

3、压缩并scp发送到服务器指定地址并解压

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
37
38
39
40
41
42
43
44
45
46
47
name: Node.js CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [ 16.x ]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: zip -r ui.zip ui/

# 部署到服务器
- name: Deploy 🚀
uses: cross-the-world/ssh-scp-ssh-pipelines@latest
env:
WELCOME: "ssh scp ssh pipelines"
LASTSSH: "Doing something after copying"
with:
host: ${{ secrets.HOST }}
user: ${{ secrets.USERNAME }}
pass: ${{ secrets.PASSWORD }}
port: 22
first_ssh: |
ls -a
scp: |
'./ui.zip' => /www/wwwroot/default/x-springboot/
last_ssh: |
cd /www/wwwroot/default/x-springboot/
unzip ui.zip

Springboot项目

在项目中编写.github/workflows/maven.yml

1、checkout代码

2、使用maven打包项目成jarbao

3、scp发送到服务器指定地址

4、宝塔重新启动项目,当然你也可以在yaml中编写last_ssh: 执行java -jar启动项目

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
name: Java CI with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

- name: copy jar
uses: cross-the-world/ssh-scp-ssh-pipelines@latest
with:
host: ${{ secrets.HOST }}
user: ${{ secrets.USERNAME }}
pass: ${{ secrets.PASSWORD }}
port: 22
scp: |
./target/x-springboot.jar => /www/wwwroot/default/x-springboot/
# 参考以下在scp上传包以后执行
# last_ssh: |
# nohup java -jar /www/wwwroot/default/x-springboot/x-springboot.jar >temp.txt &

Nginx配置

当然这里每个人的项目不一样nginx配置都不一样,我只是用来记录一下方便以后恢复环境

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
37
38
server {
listen 80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name <域名>;
client_max_body_size 100m;

ssl_certificate <path>_bundle.crt;
ssl_certificate_key <path>.key;
ssl_trusted_certificate <path>_bundle.pem;

# SSL配置
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;

# SSL加密方式,使用现代化的加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDSA-AES256-SHA384:RSA-AES256-SHA384:ECDSA-AES256-SHA:RSA-AES256-SHA:ECDSA-AES128-SHA256:RSA-AES128-SHA256:ECDSA-AES128-SHA:RSA-AES128-SHA:!DSS;


location / {
# UI目录
root /www/wwwroot/default/x-springboot/ui/;
#动态页面
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
if ( !-e $request_filename ) {
proxy_pass http://127.0.0.1:8080;
}
}

location ^~// {
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8080;
}
}

Github action持续部署springboot+vue前后端分离项目
https://cason.work/2023/08/15/Github-action持续部署springboot-vue前后端分离项目/
作者
Cason Mo
发布于
2023年8月15日
许可协议