diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..e6a10d1 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,103 @@ +name: Deploy Resona + +on: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: resona-production + cancel-in-progress: false + +jobs: + test-and-deploy: + # Only the canonical repository holds the production deploy secrets + # (SSH_PRIVATE_KEY, etc.); forks must skip this job instead of failing. + if: github.repository == 'Exabyte-Technologies/Resona' + runs-on: ubuntu-latest + environment: production + timeout-minutes: 30 + env: + DEPLOY_HOST: 157.245.192.56 + DEPLOY_USER: resonahost + APP_DIR: /opt/resona + SECRET_KEY: ${{ secrets.SECRET_KEY }} + ADMIN_USERNAME: ${{ secrets.ADMIN_USERNAME }} + ADMIN_PASSWORD: ${{ secrets.ADMIN_PASSWORD }} + CLOSEAI_API_KEY: ${{ secrets.CLOSEAI_API_KEY }} + RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }} + RESEND_FROM_EMAIL: ${{ secrets.RESEND_FROM_EMAIL }} + LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }} + + steps: + - name: Check out source + uses: actions/checkout@v4 + + - name: Verify deployment revision + run: | + echo "Deployment revision: $GITHUB_SHA" + echo "Finalizer transport: streamed-v2" + bash -n deploy/bootstrap-ubuntu.sh + bash -n deploy/finalize-ubuntu.sh + bash -n deploy/certbot-reload-nginx.sh + + - name: Test application + run: | + python3 -m venv /tmp/resona-test-venv + /tmp/resona-test-venv/bin/python -m pip install --upgrade pip + /tmp/resona-test-venv/bin/python -m pip install --requirement requirements.txt + /tmp/resona-test-venv/bin/python -m pytest -q + /tmp/resona-test-venv/bin/python -m pip check + + - name: Verify runner dependencies + run: | + missing=() + for command in ssh ssh-keyscan rsync python3; do + command -v "$command" >/dev/null 2>&1 || missing+=("$command") + done + if (( ${#missing[@]} )); then + sudo apt-get update + sudo apt-get install -y openssh-client rsync python3 + fi + + - name: Configure SSH + env: + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} + run: | + test -n "$SSH_PRIVATE_KEY" || { echo "Missing SSH_PRIVATE_KEY secret" >&2; exit 1; } + install -d -m 0700 "$HOME/.ssh" + printf '%s\n' "$SSH_PRIVATE_KEY" > "$HOME/.ssh/id_ed25519" + chmod 0600 "$HOME/.ssh/id_ed25519" + ssh-keyscan -H "$DEPLOY_HOST" >> "$HOME/.ssh/known_hosts" + chmod 0600 "$HOME/.ssh/known_hosts" + + - name: Render production environment + run: | + test -n "$LETSENCRYPT_EMAIL" || { echo "Missing LETSENCRYPT_EMAIL secret" >&2; exit 1; } + [[ "$LETSENCRYPT_EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]] || { echo "LETSENCRYPT_EMAIL is not a valid email address" >&2; exit 1; } + python3 deploy/render_env.py /tmp/resona-production.env + + - name: Bootstrap Ubuntu host + run: | + ssh "$DEPLOY_USER@$DEPLOY_HOST" sudo bash -s -- "$DEPLOY_USER" "$APP_DIR" < deploy/bootstrap-ubuntu.sh + + - name: Sync application without persistent data + run: | + rsync -az --delete \ + --exclude='.git/' \ + --exclude='.github/' \ + --exclude='.env' \ + --exclude='.venv/' \ + --exclude='instance/' \ + --exclude='__pycache__/' \ + --exclude='.pytest_cache/' \ + ./ "$DEPLOY_USER@$DEPLOY_HOST:$APP_DIR/" + rsync -az --chmod=F600 /tmp/resona-production.env "$DEPLOY_USER@$DEPLOY_HOST:/tmp/resona-production.env" + rm -f /tmp/resona-production.env + + - name: Install dependencies and restart services + run: | + ssh "$DEPLOY_USER@$DEPLOY_HOST" sudo bash -s -- "$DEPLOY_USER" "$APP_DIR" /tmp/resona-production.env < deploy/finalize-ubuntu.sh diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml new file mode 100644 index 0000000..2780ca6 --- /dev/null +++ b/.github/workflows/security-scan.yml @@ -0,0 +1,35 @@ +name: Security Scan + +# Supply-chain / dependency vulnerability scanning. +# Runs independently of deploy.yml so that adding this security gate can +# never block or break a production deployment. +on: + push: + branches: [main] + pull_request: + branches: [main] + # Weekly audit: catches newly-disclosed CVEs even when no code changes land. + schedule: + - cron: "23 4 * * 1" + workflow_dispatch: + +permissions: + contents: read + # Required to upload SARIF results to the code-scanning dashboard. + security-events: write + +jobs: + dependency-audit: + name: Dependency vulnerability audit + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Check out source + uses: actions/checkout@v4 + + - name: Audit pinned dependencies with pip-audit + uses: pypa/gh-action-pip-audit@v1.1.0 + with: + # requirements.txt pins exact versions, so the audit is deterministic. + inputs: requirements.txt + summary: true