|
| 1 | +name: Deploy to VM via Tailscale |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["main"] |
| 6 | + workflow_dispatch: {} |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: deploy-to-vm |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + deploy: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout repo (optional) |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + # Start Tailscale on the runner using the official GitHub Action. |
| 20 | + # The action reads the client id / secret from env here (you said you already put them as secrets). |
| 21 | + - name: Start Tailscale |
| 22 | + uses: tailscale/github-action@v4 |
| 23 | + env: |
| 24 | + TS_OAUTH_CLIENT_ID: ${{ secrets.TS_OAUTH_CLIENT_ID }} |
| 25 | + TS_OAUTH_SECRET: ${{ secrets.TS_OAUTH_SECRET }} |
| 26 | + # no "with" required here; env vars let the action perform the auth/login step. |
| 27 | + |
| 28 | + - name: Wait a moment for Tailscale to come up |
| 29 | + run: | |
| 30 | + # give tailscale a short moment to stabilize and show status for debugging |
| 31 | + sleep 3 |
| 32 | + tailscale status || true |
| 33 | +
|
| 34 | + - name: SSH to VM and deploy |
| 35 | + # we use a single ssh call that runs a small bash deployment script on the remote host |
| 36 | + run: | |
| 37 | + ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_IP }} << 'REMOTE' |
| 38 | + set -euo pipefail |
| 39 | +
|
| 40 | + echo "Entering project directory..." |
| 41 | + cd '${{ secrets.PROJECT_PATH }}/frontend' |
| 42 | +
|
| 43 | + echo "Pulling latest from origin/main..." |
| 44 | + git fetch origin main |
| 45 | + git reset --hard origin/main |
| 46 | +
|
| 47 | + echo "Stopping frontend service (if running)..." |
| 48 | + docker compose -f compose.yml stop frontend || true |
| 49 | +
|
| 50 | + echo "Removing old container (if exists)..." |
| 51 | + docker compose -f compose.yml rm -f frontend || true |
| 52 | +
|
| 53 | + echo "Building the frontend container..." |
| 54 | + docker compose -f compose.yml build frontend |
| 55 | +
|
| 56 | + echo "Starting the frontend container in headless/detached mode..." |
| 57 | + docker compose -f compose.yml up -d --no-deps --force-recreate frontend |
| 58 | +
|
| 59 | + echo "Cleaning up unused images and containers..." |
| 60 | + docker image prune -af || true |
| 61 | + docker container prune -f || true |
| 62 | + docker system prune -f || true |
| 63 | +
|
| 64 | + echo "Deployment finished successfully." |
| 65 | + exit 0 |
| 66 | + REMOTE |
| 67 | + # ensure secrets are available to the step's runner context (they are by default) |
0 commit comments