Skip to content

Commit

Permalink
added ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed May 6, 2024
1 parent faa89f1 commit e518822
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
.env
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
MASTER_BACKEND_PORT=1234
MASTER_BACKEND_HOST=example.com
MASTER_BACKEND_URL=${MASTER_BACKEND_HOST}:${MASTER_BACKEND_PORT}
DATABASE_PORT=5432
DATABASE_NAME=mydatabase
DATABASE_USER=myuser
DATABASE_PASSWORD=mysecretpassword
DATABASE_HOST=db.example.com
DATABASE_URL=postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
JWT_ACCESS_SECRET=myaccesssecret
JWT_REFRESH_SECRET=myrefreshsecret
RUST_ENV=production
LOGGER_URL=http://logs.example.com
75 changes: 75 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Build, Test and Deploy to DigitalOcean

on:
push:
branches:
- production

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Build
run: cargo build --release

- name: Login to GitHub Container Registry
run: docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GHCR_PAT }}

- name: Set ghcr.io url
run: echo "REPO=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}"

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build the Docker image
run: |
docker build . -t ghcr.io/${{ env.REPO }}:${{ github.sha }}
docker tag ghcr.io/${{ env.REPO }}:${{ github.sha }} ghcr.io/${{ env.REPO }}:latest
- name: Push the Docker image
run: |
docker push ghcr.io/${{ env.REPO }}:${{ github.sha }}
docker push ghcr.io/${{ env.REPO }}:latest
# deploy:
# needs: build
# runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@v3

# - name: Add SSH key
# run: |
# mkdir -p ~/.ssh
# echo "${{ secrets.HELIOS_SSH }}" > ~/.ssh/helios_ssh
# chmod 600 ~/.ssh/helios_ssh

# - name: Setup to DigitalOcean
# run: |
# ssh -i ~/.ssh/helios_ssh -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GHCR_PAT }}"
# ssh -i ~/.ssh/helios_ssh -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "rm -f docker-compose.yml"
# scp -i ~/.ssh/helios_ssh -P ${{ secrets.SSH_PORT }} docker-compose.yml ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:~/docker-compose.yml

# - name: Deploy to DigitalOcean
# run: |
# ssh -i ~/.ssh/helios_ssh -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "docker-compose down"
# ssh -i ~/.ssh/helios_ssh -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "docker-compose pull"
# ssh -i ~/.ssh/helios_ssh -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "docker-compose up -d"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.env*
.env
.env.production
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM clux/muslrust:stable AS chef
ARG MASTER_BACKEND_PORT
RUN cargo install cargo-chef
WORKDIR /master-backend

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /master-backend/recipe.json recipe.json
RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl

FROM scratch
COPY --from=builder /master-backend/target/x86_64-unknown-linux-musl/release/helios-master-backend /master-backend
ENTRYPOINT ["/master-backend"]
EXPOSE $MASTER_BACKEND_PORT
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Helios | Rust master backend

[![AGPL-3.0 License](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://choosealicense.com/licenses/agpl-3.0/)

Rust implementation of the Helios backend.
67 changes: 67 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: "master-backend"

services:
master-backend:
build:
context: .
args:
- MASTER_BACKEND_PORT=${MASTER_BACKEND_PORT}
dockerfile: Dockerfile
restart: always
ports:
- "${MASTER_BACKEND_PORT}:${MASTER_BACKEND_PORT}"
depends_on:
- database

database:
image: postgres:12
restart: always
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
command: -p ${DATABASE_PORT}
volumes:
- database-volume:/var/lib/postgresql/data

logger:
image: ghcr.io/heliosshieldproject/logger-rust:latest
restart: always
environment:
- MONGO_URL=${MONGO_URL}
- LOGGER_URI=${LOGGER_URI}
ports:
- "${LOGGER_PORT}:${LOGGER_PORT}"

logs-database:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${LOGS_DATABASE_USER}
MONGO_INITDB_ROOT_PASSWORD: ${LOGS_DATABASE_PASSWORD}
volumes:
- logs-database-volume:/data/db
command:
- --port
- ${LOGS_DATABASE_PORT}
ports:
- "${LOGS_DATABASE_PORT}:${LOGS_DATABASE_PORT}"

logs-viewer:
image: mongo-express
restart: always
ports:
- ${LOGS_VIEWER_PORT}:8081
environment:
ME_CONFIG_MONGODB_URL: ${MONGO_URL}
ME_CONFIG_OPTIONS_EDITORTHEME: nord
ME_CONFIG_BASICAUTH_USERNAME: ${LOGS_VIEWER_USER}
ME_CONFIG_BASICAUTH_PASSWORD: ${LOGS_VIEWER_PASSWORD}
depends_on:
- logs-database

volumes:
database-volume:
logs-database-volume:
7 changes: 2 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
#[derive(Debug, Clone)]
pub struct Config {
/// The port the master backend listens on.
pub master_backend_port: u16,
pub master_backend_url: String,
/// The URL of the database.
pub database_url: String,
/// The secret used to sign access tokens.
Expand All @@ -21,10 +21,7 @@ pub struct Config {
pub static ENV: Lazy<Config> = Lazy::new(|| {
dotenv().ok();
Config {
master_backend_port: env::var("MASTER_BACKEND_PORT")
.expect("MASTER_BACKEND_PORT must be set")
.parse()
.expect("MASTER_BACKEND_PORT must be a number"),
master_backend_url: env::var("MASTER_BACKEND_URL").expect("MASTER_BACKEND_URL must be set"),
database_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
jwt_access_secret: env::var("JWT_ACCESS_SECRET").expect("JWT_ACCESS_SECRET must be set"),
jwt_refresh_secret: env::var("JWT_REFRESH_SECRET").expect("JWT_REFRESH_SECRET must be set"),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn main() {
_ => app_router(state.clone()).with_state(state),
};

let listener = TcpListener::bind(format!("localhost:{}", ENV.master_backend_port))
let listener = TcpListener::bind(&ENV.master_backend_url)
.await
.unwrap();

Expand Down

0 comments on commit e518822

Please sign in to comment.