|
| 1 | +name: Test and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master] |
| 6 | + pull_request: |
| 7 | + branches: [master] |
| 8 | + |
| 9 | +jobs: |
| 10 | + test: |
| 11 | + name: Test |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - uses: actions/checkout@v6 |
| 15 | + |
| 16 | + - name: Set up Go |
| 17 | + uses: actions/setup-go@v6 |
| 18 | + with: |
| 19 | + go-version-file: go.mod |
| 20 | + |
| 21 | + - name: Download dependencies |
| 22 | + run: go mod download |
| 23 | + |
| 24 | + - name: Build |
| 25 | + run: go build ./... |
| 26 | + |
| 27 | + - name: Run tests |
| 28 | + run: go test -v ./... |
| 29 | + |
| 30 | + deploy: |
| 31 | + name: Deploy to Production |
| 32 | + needs: test |
| 33 | + runs-on: ubuntu-latest |
| 34 | + if: github.ref == 'refs/heads/master' && github.event_name == 'push' |
| 35 | + |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@v6 |
| 38 | + |
| 39 | + - name: Set up Go |
| 40 | + uses: actions/setup-go@v6 |
| 41 | + with: |
| 42 | + go-version-file: go.mod |
| 43 | + |
| 44 | + - name: Download dependencies |
| 45 | + run: go mod download |
| 46 | + |
| 47 | + - name: Build Linux binary |
| 48 | + run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o train . |
| 49 | + |
| 50 | + - name: Copy files to server |
| 51 | + uses: appleboy/scp-action@v0.1.7 |
| 52 | + with: |
| 53 | + host: ${{ secrets.DEPLOY_HOST }} |
| 54 | + username: ${{ secrets.DEPLOY_USER }} |
| 55 | + key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 56 | + port: ${{ secrets.DEPLOY_PORT || '22' }} |
| 57 | + source: "train,templates/,static/,scripts/deploy-train" |
| 58 | + target: "/tmp/train-deploy" |
| 59 | + overwrite: true |
| 60 | + |
| 61 | + - name: Stop service before deploy |
| 62 | + continue-on-error: true |
| 63 | + uses: appleboy/ssh-action@v1.0.3 |
| 64 | + with: |
| 65 | + host: ${{ secrets.DEPLOY_HOST }} |
| 66 | + username: ${{ secrets.DEPLOY_USER }} |
| 67 | + key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 68 | + port: ${{ secrets.DEPLOY_PORT || '22' }} |
| 69 | + script: sudo systemctl stop train |
| 70 | + |
| 71 | + - name: Install and restart service |
| 72 | + uses: appleboy/ssh-action@v1.0.3 |
| 73 | + with: |
| 74 | + host: ${{ secrets.DEPLOY_HOST }} |
| 75 | + username: ${{ secrets.DEPLOY_USER }} |
| 76 | + key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 77 | + port: ${{ secrets.DEPLOY_PORT || '22' }} |
| 78 | + script: sudo /usr/local/bin/deploy-train /tmp/train-deploy |
0 commit comments