Github Actions 使用Docker部署SpringBoot

代码里面有详细的注释,仅供参考

# 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://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
# 给工作流程起个名字
name: Java CI with Maven
# 触发时机
on:
  # 在push master分支时触发
  push:
    branches: master
# 要执行的任务 可以是多个    
jobs:
  # build 任务的名称 可以叫build2  test3
  build:
    # 在ubuntu-latest上运行
    runs-on: ubuntu-latest
    # 运行的步骤
    steps:
    # 拉取代码
    - uses: actions/checkout@v3
    # 步骤名称
    - name: Set up JDK 8
      # 设置maven Java
      uses: actions/setup-java@v3
      with:
        java-version: '8'
        distribution: 'temurin'
        # 启用缓存
        cache: maven
    - name: Build with Maven
      # 打包  这些步骤都是在工作目录下进行的 生成的jar文件在target下
      run: mvn -B package --file pom.xml
      # docker构建多平台
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
    - name: Set up Docker Buildx
      # 初始化构建环境
      uses: docker/setup-buildx-action@v2
    - name: Login to DockerHub
      # 账号密码登录dockerhub
      uses: docker/login-action@v2
      with:
        # 账号密码在Setting/secrets/Actions  New repository secret  如果命名不合规提交不会保存
        # ${{ secrets.你的KEY }}
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    - name: Build and push
      # 开始构建 在当前目录下找Dockerfile,也可以指定Dockerfile位置
      uses: docker/build-push-action@v3
      with:
        # 当前工作目录
        context: .
        # 构建完成后 push
        push: true
        # github账号 tag
        tags: ${{ secrets.DOCKER_USERNAME }}/wx:latest
      # ssh远程连接服务器
    - name: SSH REMOTE
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.HOST}}
        username: ${{ secrets.SSH_USERNAME }}
        # 使用密码不用key
        password: ${{ secrets.SSH_PASSWORD }}
        port: ${{ secrets.PORT }}
        # | 执行多行脚本命令。run.sh中可以写删除 拉取 构建镜像...
        script: |
          cd /home/wxserver
          sh run.sh
        

你可能感兴趣的:(java,docker,github,spring,boot)