deploy.yml
2,299 bytes
| 1 | name: Deploy |
|---|---|
| 2 | |
| 3 | # Build a container image, push it to GHCR, then SSH into the VPS and roll it |
| 4 | # out with docker compose. Requires these repository secrets: |
| 5 | # VPS_HOST, VPS_USER, VPS_SSH_KEY, VPS_APP_DIR |
| 6 | # (VPS_APP_DIR is the directory on the box containing docker-compose.yml + .env) |
| 7 | |
| 8 | on: |
| 9 | # Manual-only for now so it never runs (or fails) until a VPS + the |
| 10 | # VPS_HOST / VPS_USER / VPS_SSH_KEY / VPS_APP_DIR secrets are set. When you're |
| 11 | # ready, add back: |
| 12 | # push: |
| 13 | # branches: [main] |
| 14 | workflow_dispatch: |
| 15 | |
| 16 | concurrency: |
| 17 | group: deploy-${{ github.ref }} |
| 18 | cancel-in-progress: true |
| 19 | |
| 20 | jobs: |
| 21 | build-and-push: |
| 22 | runs-on: ubuntu-latest |
| 23 | permissions: |
| 24 | contents: read |
| 25 | packages: write |
| 26 | outputs: |
| 27 | image: ${{ steps.image.outputs.ref }} |
| 28 | steps: |
| 29 | - uses: actions/checkout@v4 |
| 30 | |
| 31 | - name: Lowercase image name |
| 32 | id: image |
| 33 | run: echo "ref=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT" |
| 34 | |
| 35 | - uses: docker/setup-buildx-action@v3 |
| 36 | |
| 37 | - uses: docker/login-action@v3 |
| 38 | with: |
| 39 | registry: ghcr.io |
| 40 | username: ${{ github.actor }} |
| 41 | password: ${{ secrets.GITHUB_TOKEN }} |
| 42 | |
| 43 | - id: meta |
| 44 | uses: docker/metadata-action@v5 |
| 45 | with: |
| 46 | images: ${{ steps.image.outputs.ref }} |
| 47 | tags: | |
| 48 | type=sha |
| 49 | type=raw,value=latest,enable={{is_default_branch}} |
| 50 | |
| 51 | - uses: docker/build-push-action@v6 |
| 52 | with: |
| 53 | context: . |
| 54 | push: true |
| 55 | tags: ${{ steps.meta.outputs.tags }} |
| 56 | labels: ${{ steps.meta.outputs.labels }} |
| 57 | cache-from: type=gha |
| 58 | cache-to: type=gha,mode=max |
| 59 | |
| 60 | deploy: |
| 61 | needs: build-and-push |
| 62 | runs-on: ubuntu-latest |
| 63 | steps: |
| 64 | - name: Deploy over SSH |
| 65 | uses: appleboy/ssh-action@v1 |
| 66 | with: |
| 67 | host: ${{ secrets.VPS_HOST }} |
| 68 | username: ${{ secrets.VPS_USER }} |
| 69 | key: ${{ secrets.VPS_SSH_KEY }} |
| 70 | script: | |
| 71 | set -e |
| 72 | cd "${{ secrets.VPS_APP_DIR }}" |
| 73 | echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin |
| 74 | export APP_IMAGE="${{ needs.build-and-push.outputs.image }}:latest" |
| 75 | docker compose pull app |
| 76 | docker compose up -d |
| 77 | docker image prune -f |
| 78 | |