From 71e205538b2bf4de8d9a34f360a3963afe60caac Mon Sep 17 00:00:00 2001 From: DerDavidBohl Date: Fri, 17 Jan 2025 08:05:34 +0100 Subject: [PATCH] Added Scheduled All Deployments Start https://github.com/DerDavidBohl/dirigent-spring/issues/9 --- README.md | 40 +++++++++++++++---- .../service/DeploymentScheduler.java | 34 ++++++++++++++++ src/main/resources/application.properties | 4 +- 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/davidbohl/dirigent/deployments/service/DeploymentScheduler.java diff --git a/README.md b/README.md index 68deae8..3c637d4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ services: - DIRIGENT_DEPLOYMENTS_GIT_URL= - DIRIGENT_GIT_AUTHTOKEN= # optional - DIRIGENT_START_ALL_ON_STARTUP= # optional Default true + - DIRIGENT_DEPLOYMENTS_SCHEDULE_ENABLED= # optional Default true + - DIRIGENT_DEPLOYMENTS_SCHEDULE_CRON= # optional Default * */5 * * * * (Every 5th minute) ports: - 8080:8080 volumes: @@ -30,6 +32,8 @@ docker run -d \ -e DIRIGENT_DEPLOYMENTS_GIT_URL= \ -e DIRIGENT_GIT_AUTHTOKEN= \ -e DIRIGENT_STARTALL_ON_STARTUP= \ + -e DIRIGENT_DEPLOYMENTS_SCHEDULE_ENABLED= \ + -e DIRIGENT_DEPLOYMENTS_SCHEDULE_CRON= -v /path/to/config:/app/config \ -v /path/to/deployments:/app/deployments \ -v /var/run/docker.sock:/var/run/docker.sock \ @@ -38,11 +42,25 @@ docker run -d \ ### Environment Variables -| Variable | Description | Default | -|----------|-------------|---------| -| DIRIGENT_DEPLOYMENTS_GIT_URL | URL to your deployments git repository | | -| DIRIGENT_GIT_AUTHTOKEN | Auth token with access to your repos | | -| DIRIGENT_START_ALL_ON_STARTUP | Start all deployments on startup | true | +| Variable | Description | Default | +|----------|-------------------------------------------------------------------------------------------------------|---------| +| DIRIGENT_DEPLOYMENTS_GIT_URL | URL to your deployments git repository | | +| DIRIGENT_GIT_AUTHTOKEN | Auth token with access to your repos | | +| DIRIGENT_START_ALL_ON_STARTUP | Start all deployments on startup | true | +| DIRIGENT_DEPLOYMENTS_SCHEDULE_ENABLED | enable scheduled start of all deployments | true | +| DIRIGENT_DEPLOYMENTS_SCHEDULE_CRON | cron expression for scheduled start of all deployments (second minute hour day(month) month day(week) | * */5 * * * * | + +### deployments.yml +Example of a `deployments.yml` +```yaml +deployments: + - name: test1 + source: https://github.com/url/tomyrepo1.git + order: 20 + - name: test2 + source: https://github.com//url/tomyrepo2.git + order: 10 +``` ### Volumes @@ -52,14 +70,22 @@ docker run -d \ | /app/deployments | Deployments directory for Dirigent | | /var/run/docker.sock | Docker socket for Dirigent | -## Webhooks +## API -### Gitea +### Gitea Webhook 1. Create a new webhook in your repository 2. Set the URL to `http:///api/v1/gitea` 3. Done ;) +### Deployments + +#### Start All Deployments: +`POST` to `/api/v1/deployments/all/start` + +#### Start Deployment by name: +`POST` to `/api/v1/deployments/{name}/start` + ## Develop ### Setup for local Tests diff --git a/src/main/java/org/davidbohl/dirigent/deployments/service/DeploymentScheduler.java b/src/main/java/org/davidbohl/dirigent/deployments/service/DeploymentScheduler.java new file mode 100644 index 0000000..f9866b0 --- /dev/null +++ b/src/main/java/org/davidbohl/dirigent/deployments/service/DeploymentScheduler.java @@ -0,0 +1,34 @@ +package org.davidbohl.dirigent.deployments.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +@EnableScheduling +public class DeploymentScheduler { + + private final Logger logger = LoggerFactory.getLogger(DeploymentScheduler.class); + + @Value("${dirigent.delpoyments.schedule.enabled}") + boolean enabled; + + private final DeploymentsService deploymentsService; + + public DeploymentScheduler(@Autowired DeploymentsService deploymentsService) { + this.deploymentsService = deploymentsService; + } + + @Scheduled(cron = "${dirigent.delpoyments.schedule.cron}") + void runScheduledDeployments() { + if(enabled) { + logger.info("Starting all deployments scheduled"); + deploymentsService.startAllDeployments(); + } + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9435370..bd59130 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,6 @@ dirigent.compose.command=docker compose dirigent.deployments.cache.evict.interval=60000 dirigent.start.all.on.startup=true spring.profiles.active=local -dirigent.git.authToken= \ No newline at end of file +dirigent.git.authToken= +dirigent.delpoyments.schedule.enabled=true +dirigent.delpoyments.schedule.cron=* */5 * * * * \ No newline at end of file