Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Use Github Action CI/CD Deploy SpringBoot Project On Private Server

Notifications You must be signed in to change notification settings

DiiD1999/Example-GithubAction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example-GithubAction

一个小Demo,通过使用Github的自动化CICD服务,部署运行SpringBoot项目在私人服务器上

环境准备

项目设置

Github Setting

进入Github仓库管理界面,依次点击Setting->New Repository Sectet 。分别解释以下的字段和值

  • HOST:私有服务器公网IP地址
  • USERNAME:私有服务器公网用户名,笔者选择root
  • PASSWORD:私有服务器USERNAME对应密码
  • PORT:私有服务器公网端口,一般取22

image-20230225005751733

Project Setting

在项目的根目录下新增/.github/workflows/maven.yml

并在maven.yml添加以下内容.

# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

# 触发构建时机
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

# 任务
jobs:
  build:

    # Github Action CI/CD的机器选择。
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 8
        uses: actions/setup-java@v3
        with:
          java-version: '8.0.362'
          distribution: 'temurin'
          cache: maven
      # maven缓存,不加的话每次都会去重新拉取,会影响速度
      - name: Dependies Cache
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-
      # Maven 打包
      - name: Build with Maven
        run: mvn clean package --file pom.xml
      # 将打包后的Jar包从Github Action服务器传输至私人服务器中
      - name: Transfer jar packets
        uses: garygrossgarten/github-action-scp@release
        with:
          # Github Action 的服务器文件路径
          local: target/demo-0.0.1-SNAPSHOT.jar
          # 私有服务器文件路径
          remote: /home/demo-0.0.1-SNAPSHOT.jar
          # 私有服务器公网IP地址
          host: ${{ secrets.HOST }}
          # 私有服务器用户名
          username: ${{ secrets.USERNAME }}
          # 私有服务器用户密码
          password: ${{ secrets.PASSWORD }}
      # 部署运行
      - name: Deploy
        uses: appleboy/ssh-action@master
        with:
          # 同上述
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          port: ${{ secrets.PORT }}
          # ssh进入系统后执行什么样的操作。一般是关闭原有的服务在重启
          script: |
            cd /home
            ps -ef | grep  demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9
            # nohup /usr/local/java/jdk1.8.0_361/bin/java -jar /home/demo-0.0.1-SNAPSHOT.jar > nohup.out &
            nohup /usr/local/java/jdk1.8.0_361/bin/java -jar /home/demo-0.0.1-SNAPSHOT.jar > runtime.log 2>&1 &
            pwd

问题与解决

1问题. runtime.log日志显示

nohup: 无法运行命令“java“: 没有那个文件或目录  

1原因:无法识别$$JAVAHOME$的环境变量

1解决:用绝对路径/usr/local/java/jdk1.8.0_361/bin/java即可

参考链接

About

Use Github Action CI/CD Deploy SpringBoot Project On Private Server

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages