Automated Deployment Documentation for Constellation Backend via GitHub Actions
Prerequisites
- A remote server (VPS, dedicated server, etc.) running a Linux distribution (Ubuntu 20.04 LTS or newer recommended)
- Docker and Docker Compose installed on the server
- Git configured with the necessary permissions to clone and pull the repo
- A user with passwordless sudo privileges (to run Docker commands)
GitHub Action for Deployment
The Constellation backend deployment is automated via GitHub Actions.
The GitHub Action is defined in the .github/workflows/deploy.yml file of the repository.
Steps in the GitHub Action
- Clone the repository on the remote server (checkout)
- Set up the SSH key to connect to the remote server (SSH key retrieved from GitHub Secrets)
- Create the
.envfile from GitHub Secrets, then copy it to the remote server - Copy the deployment script
deployBack.shto the remote server - Execute the deployment script
GitHub Secrets
For the GitHub Action to work, add the following secrets to the repository:
secrets.DEPLOY_IP: IP address of the remote serversecrets.DEPLOY_USER: Username to log in to the remote serversecrets.EPITECH_REPO_SSH_KEY: Private SSH key to connect to the remote serversecrets.DEPLOY_ENV_PIPENV_VENV_IN_PROJECT:PIPENV_VENV_IN_PROJECTenvironment variable, to be placed in the.envfile on the remote serversecrets.DEPLOY_ENV_TARGET:TARGETenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_POSTGRES_USER:POSTGRES_USERenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_POSTGRES_PASSWORD:POSTGRES_PASSWORDenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_POSTGRES_HOST:POSTGRES_HOSTenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_POSTGRES_PORT:POSTGRES_PORTenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_POSTGRES_DB:POSTGRES_DBenvironment variable, to be placed in the.envfilesecrets.DEPLOY_ENV_JWT_SECRET_KEY:JWT_SECRET_KEYenvironment variable, to be placed in the.envfile
Deployment Script
The deployment script deployBack.sh is executed on the remote server by the GitHub Action.
Steps in the Deployment Script
- Verify that Git and Docker are installed on the server
- Check if the repository has already been cloned on the server
- If yes, pull the repository
- If no, clone the repository
- Copy the
.envfile created by the GitHub Action (deployDotEnv) into the repository - If the file
~/deployDotEnvexists on the server, copy it and rename it to.envin the repository - Otherwise, if
~/deployDotEnvis not present:- And the repository already contains a
.envfile, continue - And the repository does not contain a
.envfile, abort the deployment
- And the repository already contains a
- Change into the cloned repository directory
- Stop and remove existing Docker containers (
docker compose down) - Start the Docker containers (
docker compose up -d) - Create a
deployStatus.txtfile to indicate that deployment was completed and on which date
Deployment Diagram
graph TD;
subgraph Server
direction TB
A[Run deployBack.sh] --> B[Are Git and Docker installed?];
B -->|Yes| C[Is the repository already cloned?];
B -->|No| P[Error];
C --> D[Yes];
C --> E[No];
D -->|Pull| F;
E -->|Clone| F[Repository up to date];
F --> G[deployDotEnv exists];
F --> H[deployDotEnv does not exist];
subgraph .env Handling
G --> I[Copy and rename to .env];
H --> J[Existing .env in repo];
H --> K[Error];
end
J --> L[Change into repo directory];
I --> L;
L --> M[Stop and remove containers];
M --> N[Start containers];
N --> O[Create deployStatus.txt];
O -->|Deployment successful| Success((End Process));
end
subgraph GitHub_Actions
deploy.yml -->|Creates| deployDotEnv;
deployDotEnv -->|Copies| Server;
deploy.yml -->|Copies deployBack.sh| Server;
deploy.yml -->|SSH| Server;
end