build.yml 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. name: Build
  2. on:
  3. push:
  4. branches: [ master ]
  5. pull_request:
  6. branches: [ master ]
  7. env:
  8. IMAGE_NAME: registry.ap-northeast-1.aliyuncs.com/go-admin/go-admin-api # 镜像名称
  9. TAG: ${{ github.sha }}
  10. IMAGE_NAME_TAG: registry.ap-northeast-1.aliyuncs.com/go-admin/go-admin-api:${{ github.sha }}
  11. jobs:
  12. build:
  13. runs-on: ubuntu-latest
  14. steps:
  15. - uses: actions/checkout@v3
  16. - name: Set up Go
  17. uses: actions/setup-go@v3
  18. with:
  19. go-version: 1.18
  20. - name: Tidy
  21. run: go mod tidy
  22. - name: Build
  23. run: env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags "sqlite3,json1" --ldflags "-extldflags -static" -o main .
  24. - name: Build the Docker image and push
  25. run: |
  26. docker login --username=${{ secrets.DOCKER_USERNAME }} registry.ap-northeast-1.aliyuncs.com --password=${{ secrets.DOCKER_PASSWORD }}
  27. echo "************ docker login end"
  28. docker build -t go-admin-api:latest .
  29. echo "************ docker build end"
  30. docker tag go-admin-api ${{ env.IMAGE_NAME_TAG }}
  31. echo "************ docker tag end"
  32. docker images
  33. echo "************ docker images end"
  34. docker push ${{ env.IMAGE_NAME_TAG }} # 推送
  35. echo "************ docker push end"
  36. - name: Restart server # 第五步,重启服务
  37. uses: appleboy/ssh-action@master
  38. env:
  39. GITHUB_SHA_X: ${GITHUB_SHA}
  40. with:
  41. host: ${{ secrets.SSH_HOST }} # 下面三个配置与上一步类似
  42. username: ${{ secrets.SSH_USERNAME }}
  43. key: ${{ secrets.DEPLOY_KEY }}
  44. # 重启的脚本,根据自身情况做相应改动,一般要做的是migrate数据库以及重启服务器
  45. script: |
  46. sudo docker rm -f go-admin-api
  47. sudo docker login --username=${{ secrets.DOCKER_USERNAME }} registry.ap-northeast-1.aliyuncs.com --password=${{ secrets.DOCKER_PASSWORD }}
  48. sudo docker run -d -p 8000:8000 --name go-admin-api ${{ env.IMAGE_NAME_TAG }}