Skip to content

Commit 020183a

Browse files
authored
Merge pull request #40 from hrideshmg/docker
Dockerise root
2 parents 9bc685f + 36a96e8 commit 020183a

File tree

6 files changed

+103
-15
lines changed

6 files changed

+103
-15
lines changed

.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Dockerfile
2+
target
3+
docs
4+
5+
.dockerignore
6+
.env.sample
7+
.gitignore
8+
.git

.env.sample

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Postgres env
2+
POSTGRES_PASSWORD=
3+
POSTGRES_USER=
4+
POSTGRES_DB=
5+
POSTGRES_HOST=localhost
6+
7+
# Root env
8+
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}
9+
RUST_ENV=development
10+
ROOT_SECRET=insecuresecret123 # Used to verify origin of attendance mutations
11+
ROOT_PORT=3000

.github/workflows/ghcr-deploy.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Inspired from: https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images
2+
name: Create and publish Docker image to GHCR
3+
4+
# Configures this workflow to run every time a change is pushed to the branch called `release`.
5+
on:
6+
workflow_dispatch:
7+
push:
8+
branches: ['master']
9+
10+
jobs:
11+
build-and-push-image:
12+
runs-on: ubuntu-latest
13+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
14+
permissions:
15+
contents: read
16+
packages: write
17+
id-token: write
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
# Uses the `docker/login-action` action to log in to the Github Container Registry
24+
- name: Log in to the Container registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ghcr.io
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
# This step uses `docker/metadata-action` to extract tags and labels that will be applied to the specified image.
32+
# The `id` "meta" allows the output of this step to be referenced in a subsequent step.
33+
# The `images` value provides the base name for the tags and labels.
34+
- name: Extract metadata (tags, labels) for Docker
35+
id: meta
36+
uses: docker/metadata-action@v5
37+
with:
38+
images: ghcr.io/amfoss/root
39+
tags: |
40+
# set latest tag for master branch
41+
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }},priority=2000
42+
type=schedule,pattern={{date 'YYYYMMDD'}}
43+
type=ref,event=tag
44+
type=ref,event=pr
45+
type=sha
46+
47+
# This step uses the `docker/build-push-action` action to build the image. If the build succeeds, it pushes the image to GitHub Packages.
48+
- name: Build and push Docker image
49+
id: push
50+
uses: docker/build-push-action@v6
51+
with:
52+
context: .
53+
push: true
54+
tags: ${{ steps.meta.outputs.tags }}
55+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Build Stage
2+
FROM rust:slim-bullseye AS builder
3+
WORKDIR /build
4+
5+
# Compile deps in a separate layer (for caching)
6+
COPY Cargo.toml Cargo.lock ./
7+
# Cargo requires at least one source file for compiling dependencies
8+
RUN mkdir src && echo "fn main() { println!(\"Hello, world!\"); }" > src/main.rs
9+
RUN apt-get update
10+
RUN apt install -y pkg-config libssl-dev
11+
RUN cargo build --release
12+
13+
# Compile for release
14+
COPY ./src ./src
15+
COPY ./migrations ./migrations
16+
RUN rm ./target/release/deps/root*
17+
RUN cargo build --release
18+
19+
# Release Stage
20+
FROM debian:bullseye-slim AS release
21+
COPY --from=builder /build/target/release/root /usr/local/bin
22+
CMD ["/usr/local/bin/root"]

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ Root is our club's backend, responsible for collecting and distributing data fro
1616

1717
2. Configure environment:
1818
```bash
19-
touch .env
19+
cp .env.sample .env
2020
```
21-
22-
The following environment variables are required:
23-
* DATABASE_URL: Connection string to your DB.
24-
* RUST_ENV: Use "development" or "production" as applicable.
25-
* ROOT_SECRET: Used to verify the origin of mutation requests on attendance. Ask the maintainers for it.
26-
* BIND_ADDRESS: The IP address for `axum` to serve to. Typically `0.0.0.0:3000` for local deployments.
21+
- Make sure that you have a postgres database running with the specified credentials.
2722

2823
3. Setup database:
2924
```bash

src/main.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ struct Config {
2424
env: String,
2525
secret_key: String,
2626
database_url: String,
27-
bind_address: String,
27+
port: String,
2828
}
2929

3030
impl Config {
3131
fn from_env() -> Self {
32-
// Currently, we need the DATABASE_URL to be loaded in through the .env.
33-
// In the future, if we use any other configuration (say Github Secrets), we
34-
// can allow dotenv() to err.
35-
dotenv::dotenv().expect(".ENV file must be set up.");
32+
let _ = dotenv::dotenv();
3633
Self {
3734
// RUST_ENV is used to check if it's in production to avoid unnecessary logging and exposing the
3835
// graphiql interface. Make sure to set it to "production" before deployment.
@@ -41,8 +38,8 @@ impl Config {
4138
secret_key: std::env::var("ROOT_SECRET").expect("ROOT_SECRET must be set."),
4239
// DATABASE_URL provides the connection string for the PostgreSQL database.
4340
database_url: std::env::var("DATABASE_URL").expect("DATABASE_URL must be set."),
44-
// BIND_ADDRESS is used to determine the IP address for the server's socket to bind to.
45-
bind_address: std::env::var("BIND_ADDRESS").expect("BIND_ADDRESS must be set."),
41+
// ROOT_PORT is used to determine the port that root binds to
42+
port: std::env::var("ROOT_PORT").expect("ROOT_PORT must be set."),
4643
}
4744
}
4845
}
@@ -63,7 +60,7 @@ async fn main() {
6360
let router = setup_router(schema, cors, config.env == "development");
6461

6562
info!("Starting Root...");
66-
let listener = tokio::net::TcpListener::bind(config.bind_address)
63+
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.port))
6764
.await
6865
.unwrap();
6966
axum::serve(listener, router).await.unwrap();

0 commit comments

Comments
 (0)