Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
docs: explain details and limit to post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Feb 26, 2024
1 parent f3937e4 commit ca71d35
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- <your compose project directory>:/compose
environment:
# (optional, default 10) in seconds.
# only one update per timeout is allowed.
# GitHub Webhooks may send multiple requests.
- TIMEOUT=10
restart: always
```
Expand All @@ -36,7 +41,7 @@ services:
composehook.update: true
```

To trigger the update, send a GET or POST request to:
To trigger the update, send a POST request to:

```
http://localhost:8537/<compose-project>/<service-name>
Expand All @@ -45,5 +50,23 @@ http://localhost:8537/<compose-project>/<service-name>
I suggest using a reverse proxy to expose the webhook to the internet.
I use [caddy-docker-proxy](https://github.com/lucaslorentz/caddy-docker-proxy).

## HTTP status codes

- 200 OK: Update successful
- 400 Bad Request: label `composehook.update` not found or not set to true
- 404 Not Found: compose project or service not found
- 409 Conflict: Update not allowed by timeout or already in progress
- 500 Internal Server Error: couldn’t execute commands. Please open an issue.

## Details

When receiving a request, composehook will:

1. Check if the request is allowed by the timeout.
2. Check if the service exists (`docker compose ps -q <service>`)
3. Check if the service has the `composehook.update` label (`docker inspect <container_id>`)
4. Pull the latest image (`docker compose pull <service>`)
5. Recreate the container (`docker compose up -d <service>`)

Unfortunately composehook can’t update itself as docker containers can’t restart themselves.
If you know a solution, please open an issue.
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use std::{
sync::{Arc, Mutex},
};

use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use actix_web::{post, web, App, HttpResponse, HttpServer, Responder};
use chrono::{DateTime, Duration, Local};

struct State {
currently_updating: Arc<Mutex<HashMap<String, DateTime<Local>>>>,
timeout: Duration,
}

#[post("/{project}/{service}")]
async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) -> impl Responder {
let (project, service) = path.into_inner();
let path = Path::new("/compose/").join(&project);
Expand Down Expand Up @@ -147,13 +148,10 @@ async fn main() -> std::io::Result<()> {
);

HttpServer::new(move || {
App::new()
.route("/{project}/{container}", web::get().to(webhook))
.route("/{project}/{container}", web::post().to(webhook))
.app_data(web::Data::new(State {
currently_updating: currently_updating.clone(),
timeout,
}))
App::new().service(webhook).app_data(web::Data::new(State {
currently_updating: currently_updating.clone(),
timeout,
}))
})
.bind(("0.0.0.0", 8537))?
.run()
Expand Down

0 comments on commit ca71d35

Please sign in to comment.