Skip to content

Commit

Permalink
build: update docker
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jul 26, 2024
1 parent 9a17fee commit c35cd2a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ on:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
Expand All @@ -23,6 +23,7 @@ jobs:
run: |
echo ${GHCR_TOKEN} | docker login ghcr.io -u ${USERNAME} --password-stdin
docker buildx build --push \
--cache-from=type=gha --cache-to=type=gha,mode=max \
--platform linux/amd64 \
-t ghcr.io/${USERNAME}/ghstats:main .
Expand All @@ -35,5 +36,6 @@ jobs:
ref="$(echo ${{ github.ref}} | cut -d'/' -f3)"
echo ${GHCR_TOKEN} | docker login ghcr.io -u ${USERNAME} --password-stdin
docker buildx build --push \
--cache-from=type=gha --cache-to=type=gha,mode=max \
--platform linux/amd64 \
-t ghcr.io/${USERNAME}/ghstats:${ref} -t ghcr.io/${USERNAME}/ghstats:latest .
44 changes: 17 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
FROM rust:1.79-alpine as builder
WORKDIR /app

RUN apk add --no-cache build-base musl-dev libressl-dev

WORKDIR /app
ADD Cargo.toml Cargo.lock ./
RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo build --release --locked

ADD . .
RUN touch src/main.rs && cargo build --release --frozen

FROM scratch
FROM alpine:latest
LABEL org.opencontainers.image.source https://github.com/vladkens/ghstats
COPY --from=builder /app/target/release/ghstats /app/ghstats

WORKDIR /app
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
dev:
cargo watch -x 'run'

image:
docker-build:
docker build -t ghstats .
docker images -q ghstats | xargs docker inspect -f '{{.Size}}' | xargs numfmt --to=iec

image-run:
docker rm --force $(shell docker ps -a -q --filter ancestor=ghstats) || true
docker run --network=host --name ghstats -d -t ghstats
docker-run:
docker rm --force ghstats || true
docker run -d -p 8080:8080 -v ./data:/data --env-file .env --name ghstats ghstats
40 changes: 35 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,48 @@
Self-hosted dashboard for tracking GitHub repos traffic history longer than 14 days.
<br />
<br />
<img src=".github/ghstats.png" alt="ghstats preview" />
<img src="https://github.com/vladkens/ghstats/blob/assets/preview.png?raw=true" alt="ghstats preview" />
</div>

### Usage
### 🌟 Features

- Collect & store traffic metrics for all your repos
- List of repos and informative dashboard for each
- No React / Next / Postres etc, just single and small Docker image (20MB) & SQLite

### 🚀 Usage

```sh
docker run -d -p 8080:8080 --name ghstats ghcr.io/vladkens/ghstats:latest
docker run -d --env-file .env -p 8080:8080 -v ./data:/app/data --name ghstats ghcr.io/vladkens/ghstats:latest
```

Or Docker Compose:

```yaml
services:
ghstats:
image: ghcr.io/vladkens/ghstats:latest
container_name: ghstats
restart: always
environment:
- GITHUB_TOKEN=???
env_file: .env # or with .env file
ports:
- 8080:8080
volumes:
- ./data:/app/data
```
### Github token generation
#### Github token generation
`ghstats` need Github Token to collect traffic data from API. Token can be obtained with following steps:

1. Go to https://github.com/settings/tokens
2. Generate new token > Generate new token (classic)
3. Enter name, eg: `ghstats`. Scopes: `public_repo`
4. Click genereate token & copy token
4. Click genereate token & copy it
5. Save token to `.env` file with name `GITHUB_TOKEN=???`

### 🤝 Contributing

All contributions are welcome! Feel free to open an issue or submit a pull request.
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct AppState {
impl AppState {
async fn new() -> Res<Self> {
let gh_token = std::env::var("GITHUB_TOKEN")?;
let db_path = std::env::var("DB_PATH").unwrap_or("ghstats.db".to_string());
let db_path = std::env::var("DB_PATH").unwrap_or("./data/ghstats.db".to_string());
tracing::info!("db_path: {}", db_path);

let db = DbClient::new(&db_path).await?;
let gh = GhClient::new(gh_token)?;
Expand Down Expand Up @@ -62,6 +63,8 @@ async fn update_metrics(db: &DbClient, gh: &GhClient) -> Res {
async fn start_cron(state: Arc<AppState>) -> Res {
use tokio_cron_scheduler::{Job, JobScheduler};

state.db.update_deltas().await?;

// if new db, update metrics immediately
let repos = state.db.get_repos().await?;
if repos.len() == 0 {
Expand Down Expand Up @@ -119,7 +122,7 @@ async fn main() -> Res {
let service = router.with_state(state.clone()).into_make_service();
start_cron(state.clone()).await?;

let addr = "127.0.0.1:8080";
let addr = "0.0.0.0:8080";
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
tracing::info!("listening on {}", addr);
axum::serve(listener, service).await.unwrap();
Expand Down

0 comments on commit c35cd2a

Please sign in to comment.