# Agent procedure: Deploy a Django repository to a registered server

Goal: Connect GitHub and a server, let Thales Ops detect and configure the Django application, then deploy a healthy version that is live.

Follow these steps in order. Before every step, check its guardrails. If a STOP guardrail would be broken, do not continue: stop and ask a human.

## 1. Connect GitHub and choose the repository
- Open Repositories, start the GitHub installation link, and wait for repositories to load.  _(why: Connecting GitHub is one of the two setup actions the expert identified before deployment.)_
- Filter the synced repository list for the desired repository and open deployment configuration for pay-with-thales.  _(why: The platform needs the application source before it can inspect and deploy it.)_
- If the GitHub connection is absent, reconnect it and confirm that repositories sync before selecting a deploy action.  _(why: Deployment setup depends on having the repository list available.)_
- Decision rule: if no GitHub account connection is shown, then reconnect GitHub and wait for the repository sync before choosing a repository
- Exception: If branch loading fails because no GitHub repository matches the query, return to the repository connection and selection flow.

## 2. Review detected application requirements
- Open deployment configuration and review the detected Django stack, PostgreSQL requirement, migrations, and required environment variables.  _(why: Thales Ops reads the code, including requirements and Django settings, to identify what the application needs.)_
- Decision rule: if the repository is a Django app, then review its detected requirements, settings, PostgreSQL need, migrations, and required environment variables

## 3. Register and prepare the server
- Open Servers, register a new server, enter its name and IP address, select Other as provider, and register it.  _(why: The server must be connected along with the repository before deployment.)_
- Run the generated agent installation command on the registered server and wait for installation to complete.  _(why: The installed agent is the backbone of how Thales Ops works and handles server-side work.)_
- Confirm the server is available and appears online in the Servers list before deploying to it.  _(why: An online server confirms that the agent installation and server connection completed.)_
- Decision rule: if the selected server does not yet show online, then wait for the agent installation and availability confirmation before deploying
- Decision rule: if a required deployment tool is not already on the server, then let the agent install it
- Exception: Choose a different target server when you want to use a different server for the application.
- **STOP:** Never require or collect an SSH key or private key to connect Thales Ops to the server. (The platform uses an installed agent running as a normal systemd process rather than SSH access.)
- **STOP:** Always install the generated agent command on a newly registered server before using it for deployment. (The agent performs the server-side work and makes the server available to Thales Ops.)

## 4. Choose target and build settings
- Select Sidecar as the target server and review the shown build method and exposed port.  _(why: The target depends on which server you want to use.)_
- Decision rule: if you need the application on a different server, then select the server you want to use

## 5. Set production configuration
- Open Environment Variables and set DEBUG to false before the first production deployment.  _(why: A true DEBUG setting can expose traceback details to clients and help attackers understand the system.)_
- Review the additional required environment-variable rows, including CSRF_TRUSTED_ORIGINS, and populate the required keys in the application settings.  _(why: Thales Ops detected the required environment variables from the application code.)_
- Decision rule: if deploying to production, then set DEBUG to false before deployment
- Exception: If the environment-variable list is blank after a repository or branch lookup failure, resolve the repository match before relying on the displayed variable list.
- **STOP:** Never deploy a production Django application with DEBUG set to true. (Errors can show tracebacks to clients, which attackers could use to maneuver the system.)

## 6. Build and release the application
- Start the deployment and monitor the build console through image building, exporting and unpacking image layers, database migrations, container start, health, and traffic routing.  _(why: The deployment sequence shown built the image, applied migrations, started the container, then made the version healthy before routing traffic.)_
- Treat an undefined-variable message in build output as a warning to review while continuing to track whether the build, migrations, and health checks succeed.  _(why: The warning appeared before the build completed successfully and the deployment became healthy.)_
- Decision rule: if the new version becomes healthy, then allow traffic to route to it
- Exception: An undefined-variable warning was shown during this build; the session shows that this warning did not prevent successful image build, migrations, container start, or health.

## 7. Confirm the deployment is live
- Confirm the live deployment notification after the new healthy version begins receiving traffic.  _(why: The notification confirmed that the application was running.)_
- Decision rule: if the live deployment notification appears after health and traffic routing, then treat the application as running

## 8. Review application operations
- Open the application operations settings and review domain and DNS configuration, environment variables, and deployment history.  _(why: The expert described Thales Ops as managing applications and monitoring them around the clock.)_
- Switch to the Staging Workspace application settings when moving to staging.  _(why: The expert explicitly moved to staging after the trial issue.)_
- Decision rule: if the production deployment cannot continue because the free trial ended, then move to the staging workspace
- Exception: The session does not show how to restore or extend an ended free trial; it only shows switching to staging.

## Always
- **WARN:** Never manually manage Caddy configuration for this deployment flow. (The expert states that Thales Ops handles deployment work autonomously.)

## Escalate to a human when
- a STOP guardrail would be broken
- the situation matches none of the steps or exceptions above
- unresolved: How an ended free trial is restored or extended was not shown.
- unresolved: The specific values to use for CSRF_TRUSTED_ORIGINS and the other required environment variables were not stated.
- unresolved: The session did not show why the undefined-variable build warning occurred or how to correct it.
