Skip to content

Commit

Permalink
Cronjob image with configurable cron expression
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-athome committed Aug 27, 2023
1 parent 4b472f8 commit 46f7f8c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 7 deletions.
75 changes: 69 additions & 6 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ on:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_NAME_COMMAND: ${{ github.repository }}
IMAGE_NAME_DAEMON: ${{ github.repository }}-daemon

jobs:

build:
build-command:
runs-on: ubuntu-latest

permissions:
Expand Down Expand Up @@ -45,20 +46,82 @@ jobs:
id: metadata
uses: docker/metadata-action@v4
with:
flavor: |
# Disable latest tag
latest=false
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
#flavor: |
# # Disable latest tag
# latest=false
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_COMMAND }}
tags: |
# Tag with branch name
type=ref,event=branch
# Tag with git tag on release
type=ref,event=tag
type=raw,value=release,enable=${{ github.event_name == 'release' }}
# Used for daemon image
type=sha,format=long
-
name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}

build-daemon:
runs-on: ubuntu-latest
needs:
- build-command

permissions:
contents: read
packages: write

steps:
-
name: Checkout repository
uses: actions/checkout@v3
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# Workaround to fix error:
# failed to push: failed to copy: io: read/write on closed pipe
# See https://github.com/docker/build-push-action/issues/761
with:
driver-opts: |
image=moby/buildkit:v0.12.0
-
name: Login to GitHub Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Generate docker image tags
id: metadata
uses: docker/metadata-action@v4
with:
#flavor: |
# # Disable latest tag
# latest=false
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_DAEMON }}
tags: |
# Tag with branch name
type=ref,event=branch
# Tag with git tag on release
type=ref,event=tag
type=raw,value=release,enable=${{ github.event_name == 'release' }}
-
name: Build and push
uses: docker/build-push-action@v4
with:
context: cronjob
# FIXME this should reflect the tag/branch/whatever we are building
build-args: |
COMMAND_VERSION=sha-${{ github.sha }}
push: true
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Configuration is provided via environment variables (`.env` file is supported).
| IMMICH_CARDDAV_CARDDAV_PASSWORD | Password of CardDAV server |
| IMMICH_CARDDAV_IMMICH_API_URL | Immich instance API URL (including "/api") |
| IMMICH_CARDDAV_IMMICH_API_KEY | Immich API key |
| IMMICH_CARDDAV_CRON_EXPRESSION | Cron expression (only for daemon image) |

Environment variable LOG_LEVEL will configure the logging level (standard Python logging levels: DEBUG, INFO, WARNING,
ERROR, CRITICAL).
Expand Down Expand Up @@ -48,9 +49,30 @@ docker run --rm --name immich-carddav-sync --env-file=immich-carddav-sync.env gh
The `immich-carddav-sync.env` file should contain all the necessary environment variables (see above). If you prefer you
can add those variables to the Immich `.env` file itself and reference that one.

## Integrate in Immich (docker compose)

Configure the variables described above in your `.env` file and add a service to your `docker-compose.yml` file using
the daemon image:

```yaml
services:
[...]

carddav-sync:
container_name: immich_carddav_sync
image: ghcr.io/daniele-athome/immich-carddav-sync-daemon:latest
env_file:
- .env
restart: always
depends_on:
- immich-server

[...]
```

## Roadmap

* docker-compose.yml tutorial based on the one provided by the Immich project (Docker image with cronjob inside)
* use s6-overlay instead of simple Debian cron (mainly because of vixie-cron not reacting to signals)
* support for duplicate contacts (ask interactively or assume a predefined behavior)
* match names with a custom (or multiple) vCard field(s)
* dry run mode
11 changes: 11 additions & 0 deletions cronjob/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ARG COMMAND_VERSION=latest
FROM ghcr.io/daniele-athome/immich-carddav-sync:${COMMAND_VERSION} as runtime

RUN apt-get update && apt-get install --no-install-recommends -y gettext-base cron

COPY crontab.tmpl /app/crontab.tmpl

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
9 changes: 9 additions & 0 deletions cronjob/crontab.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
${CRON_EXPRESSION} root cd / && PATH=/app/.venv/bin python -m immich_carddav_sync.main >/proc/1/fd/1 2>/proc/1/fd/2
8 changes: 8 additions & 0 deletions cronjob/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

env >> /etc/environment

export CRON_EXPRESSION=${IMMICH_CARDDAV_CRON_EXPRESSION}
envsubst < /app/crontab.tmpl >/etc/cron.d/immich-carddav-sync

exec cron -f

0 comments on commit 46f7f8c

Please sign in to comment.