|
| 1 | +name: Deploy Control Plane |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'cmd/server/**' |
| 8 | + - 'internal/**' |
| 9 | + - 'web/**' |
| 10 | + - 'go.mod' |
| 11 | + - 'go.sum' |
| 12 | + - '.github/workflows/deploy-server.yml' |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +env: |
| 16 | + SSH_USER: ubuntu |
| 17 | + GOARCH: amd64 |
| 18 | + |
| 19 | +jobs: |
| 20 | + deploy: |
| 21 | + name: Build & Deploy Server |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - uses: actions/setup-go@v5 |
| 27 | + with: |
| 28 | + go-version: '1.23' |
| 29 | + |
| 30 | + - uses: actions/setup-node@v4 |
| 31 | + with: |
| 32 | + node-version: '20' |
| 33 | + |
| 34 | + - name: Build server binary |
| 35 | + run: CGO_ENABLED=0 GOOS=linux GOARCH=${{ env.GOARCH }} go build -o bin/opensandbox-server ./cmd/server/ |
| 36 | + |
| 37 | + - name: Build web dashboard |
| 38 | + run: cd web && npm ci && npm run build |
| 39 | + |
| 40 | + - name: Package web assets |
| 41 | + run: tar czf bin/web-dist.tar.gz -C web dist |
| 42 | + |
| 43 | + - name: Configure SSH |
| 44 | + run: | |
| 45 | + mkdir -p ~/.ssh |
| 46 | + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy.pem |
| 47 | + chmod 600 ~/.ssh/deploy.pem |
| 48 | + ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts |
| 49 | +
|
| 50 | + - name: Upload artifacts |
| 51 | + run: | |
| 52 | + scp -i ~/.ssh/deploy.pem bin/opensandbox-server ${{ env.SSH_USER }}@${{ secrets.SERVER_IP }}:/tmp/opensandbox-server |
| 53 | + scp -i ~/.ssh/deploy.pem bin/web-dist.tar.gz ${{ env.SSH_USER }}@${{ secrets.SERVER_IP }}:/tmp/web-dist.tar.gz |
| 54 | +
|
| 55 | + - name: Install and restart |
| 56 | + run: | |
| 57 | + ssh -i ~/.ssh/deploy.pem ${{ env.SSH_USER }}@${{ secrets.SERVER_IP }} ' |
| 58 | + sudo mv /tmp/opensandbox-server /usr/local/bin/opensandbox-server |
| 59 | + sudo chmod +x /usr/local/bin/opensandbox-server |
| 60 | + sudo mkdir -p /opt/opensandbox/web |
| 61 | + sudo tar xzf /tmp/web-dist.tar.gz -C /opt/opensandbox/web |
| 62 | + rm /tmp/web-dist.tar.gz |
| 63 | + sudo systemctl restart opensandbox-server |
| 64 | + ' |
| 65 | +
|
| 66 | + - name: Pull secrets from AWS Secrets Manager |
| 67 | + run: | |
| 68 | + ssh -i ~/.ssh/deploy.pem ${{ env.SSH_USER }}@${{ secrets.SERVER_IP }} ' |
| 69 | + SECRETS_ARN=$(grep OPENSANDBOX_SECRETS_ARN /etc/opensandbox/server.env 2>/dev/null | cut -d= -f2-) || true |
| 70 | + if [ -n "$SECRETS_ARN" ]; then |
| 71 | + SECRETS_JSON=$(aws --region us-east-2 secretsmanager get-secret-value \ |
| 72 | + --secret-id "$SECRETS_ARN" --query SecretString --output text 2>&1) || { |
| 73 | + echo "WARNING: Failed to pull secrets, using existing env file." |
| 74 | + exit 0 |
| 75 | + } |
| 76 | + echo "$SECRETS_JSON" | python3 -c " |
| 77 | + import json, sys |
| 78 | + secrets = json.load(sys.stdin) |
| 79 | + existing = {} |
| 80 | + try: |
| 81 | + with open(\"/etc/opensandbox/server.env\") as f: |
| 82 | + for line in f: |
| 83 | + line = line.strip() |
| 84 | + if line and not line.startswith(\"#\") and \"=\" in line: |
| 85 | + k, v = line.split(\"=\", 1) |
| 86 | + existing[k] = v |
| 87 | + except FileNotFoundError: |
| 88 | + pass |
| 89 | + existing.update(secrets) |
| 90 | + with open(\"/tmp/server.env.new\", \"w\") as f: |
| 91 | + for k, v in existing.items(): |
| 92 | + f.write(f\"{k}={v}\n\") |
| 93 | + " && sudo mv /tmp/server.env.new /etc/opensandbox/server.env |
| 94 | + echo "Secrets merged." |
| 95 | + fi |
| 96 | + ' |
| 97 | +
|
| 98 | + - name: Verify deployment |
| 99 | + run: | |
| 100 | + sleep 3 |
| 101 | + ssh -i ~/.ssh/deploy.pem ${{ env.SSH_USER }}@${{ secrets.SERVER_IP }} 'sudo systemctl is-active opensandbox-server' |
| 102 | + echo "Server deployed successfully!" |
0 commit comments