main.yml 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. name: Java Deploy with Maven
  2. on:
  3. push:
  4. branches: [ master ]
  5. tags: [release-v*]
  6. jobs:
  7. build:
  8. runs-on: ubuntu-latest
  9. steps:
  10. - uses: actions/checkout@v4
  11. with:
  12. persist-credentials: false
  13. - name: Set up JDK 11
  14. # 这里使用java11的环境, 其他项目在github action中找到对应的语言环境就行
  15. uses: actions/setup-java@v2
  16. with:
  17. java-version: 11
  18. distribution: 'adopt'
  19. - name: Build with Maven
  20. # 这里maven的打包命令, 其他项目修改为对应的打包命令
  21. run: |
  22. mvn package
  23. - name: Push Docker
  24. run: |
  25. docker login --username=${{ secrets.USERNAME }} --password ${{ secrets.PASSWORD }} registry.cn-shanghai.aliyuncs.com
  26. docker build . -t ${{ secrets.REGISTRY }}:$GITHUB_RUN_NUMBER
  27. docker push ${{ secrets.REGISTRY }}:$GITHUB_RUN_NUMBER
  28. docker tag $(docker images ${{ secrets.REGISTRY }}:$GITHUB_RUN_NUMBER -q) ${{ secrets.REGISTRY }}:latest
  29. docker push ${{ secrets.REGISTRY }}:latest
  30. pull-docker:
  31. needs: [build]
  32. name: Pull Docker
  33. runs-on: ubuntu-latest
  34. steps:
  35. - name: Deploy
  36. uses: appleboy/ssh-action@master
  37. with:
  38. host: ${{ secrets.HOST }}
  39. username: ${{ secrets.USER }}
  40. password: ${{ secrets.PWD }}
  41. port: ${{ secrets.PORT }}
  42. script: |
  43. docker stop $(docker ps --filter ancestor=${{ secrets.REGISTRY }} -q)
  44. docker rm -f $(docker ps -a --filter ancestor=${{ secrets.REGISTRY }}:latest -q)
  45. docker rmi -f $(docker images ${{ secrets.REGISTRY }}:latest -q)
  46. docker login --username=${{ secrets.USERNAME }} --password ${{ secrets.PASSWORD }} registry.cn-shanghai.aliyuncs.com
  47. docker pull ${{ secrets.REGISTRY }}:latest
  48. docker run -d -p 8060:8060 ${{ secrets.REGISTRY }}:latest