Skip to content

Commit

Permalink
Merge pull request #6 from rtuszik/docker
Browse files Browse the repository at this point in the history
feat: add docker for automatic updates
  • Loading branch information
rtuszik authored Sep 19, 2024
2 parents 8e1facd + d3b3431 commit 770d7a8
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 102 deletions.
12 changes: 9 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
GITHUB_TOKEN=your_github_token
# GitHub Configuration
GITHUB_TOKEN=ghp_your_github_personal_access_token_here
GITHUB_USERNAME=your_github_username
LINKWARDEN_URL=your_linkwarden_api_url
LINKWARDEN_TOKEN=your_linkwarden_api_token
# Linkwarden Configuration
LINKWARDEN_URL=https://your-linkwarden-instance.com
LINKWARDEN_TOKEN=your_linkwarden_api_token_here

#### FOR CRONTAB: ####
COLLECTION_ID=30
CRON_SCHEDULE="0 6 * * *" # Cron Schedule (default is daily at 6am)
40 changes: 40 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: StarWarden Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

env:
DOCKER_IMAGE: rtuszik/starwarden

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.DOCKER_IMAGE }}:latest
cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache,mode=max

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
117 changes: 50 additions & 67 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# Created by https://www.toptal.com/developers/gitignore/api/Python
# Edit at https://www.toptal.com/developers/gitignore?templates=Python
### macOS ###
.DS_Store
.AppleDouble
.LSOverride
Icon

._*

.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
*.icloud

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
Expand All @@ -30,17 +49,12 @@ share/python-wheels/
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
Expand All @@ -55,75 +69,36 @@ coverage.xml
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
Expand All @@ -132,45 +107,53 @@ ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml

# ruff
.ruff_cache/

# LSP config files
pyrightconfig.json

# End of https://www.toptal.com/developers/gitignore/api/Python
### Vim ###
[._]*.s[a-v][a-z]
!*.svg
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

Session.vim
Sessionx.vim

.netrwhist
*~
tags
[._]*.un~

### Other ###
.vscode/
.idea/
*.swp
*.db
Thumbs.db



docker-compose.local.yml
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1
ENV GITHUB_TOKEN=""

Check warning on line 4 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "GITHUB_TOKEN") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/
ENV GITHUB_USERNAME=""
ENV LINKWARDEN_URL=""
ENV LINKWARDEN_TOKEN=""

Check warning on line 7 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "LINKWARDEN_TOKEN") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/
ENV COLLECTION_ID=""
ENV CRON_SCHEDULE="0 6 * * *"

WORKDIR /app

RUN apt-get update && apt-get install -y cron && rm -rf /var/lib/apt/lists/*

COPY . .

RUN pip install --no-cache-dir -r requirements.txt

# Create a cron job file
RUN echo '#!/bin/sh' > /app/cron_job.sh && \
echo 'set -a' >> /app/cron_job.sh && \
echo '. /app/.env' >> /app/cron_job.sh && \
echo 'set +a' >> /app/cron_job.sh && \
echo 'if [ -n "$COLLECTION_ID" ]; then' >> /app/cron_job.sh && \
echo ' /usr/local/bin/python /app/starwarden.py -id "$COLLECTION_ID" >> /app/starwarden.log 2>&1' >> /app/cron_job.sh && \
echo 'else' >> /app/cron_job.sh && \
echo ' echo "Error: COLLECTION_ID is not set" >> /app/starwarden.log' >> /app/cron_job.sh && \
echo 'fi' >> /app/cron_job.sh && \
chmod +x /app/cron_job.sh


# Create the log file to be able to run tail
RUN touch /app/starwarden.log

# Create start script
RUN echo '#!/bin/sh' > /app/start.sh && \
echo 'env | grep -v "PATH\|HOSTNAME\|HOME" > /app/.env' >> /app/start.sh && \
echo 'echo "$CRON_SCHEDULE root /app/cron_job.sh" > /etc/cron.d/starwarden-cron' >> /app/start.sh && \
echo 'chmod 0644 /etc/cron.d/starwarden-cron' >> /app/start.sh && \
echo 'crontab /etc/cron.d/starwarden-cron' >> /app/start.sh && \
echo 'cron' >> /app/start.sh && \
echo 'if [ -n "$COLLECTION_ID" ]; then' >> /app/start.sh && \
echo ' /usr/local/bin/python /app/starwarden.py -id "$COLLECTION_ID"' >> /app/start.sh && \
echo 'else' >> /app/start.sh && \
echo ' echo "Error: COLLECTION_ID is not set"' >> /app/start.sh && \
echo 'fi' >> /app/start.sh && \
echo 'tail -f /app/starwarden.log' >> /app/start.sh && \
chmod +x /app/start.sh

CMD ["/bin/sh", "-c", "/app/start.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

version: '3'

services:
starwarden:
build: .
environment:
- GITHUB_TOKEN=${GITHUB_TOKEN}
- GITHUB_USERNAME=${GITHUB_USERNAME}
- LINKWARDEN_URL=${LINKWARDEN_URL}
- LINKWARDEN_TOKEN=${LINKWARDEN_TOKEN}
- COLLECTION_ID=${COLLECTION_ID}
- CRON_SCHEDULE=${CRON_SCHEDULE:-0 0 * * *}
volumes:
- ./starwarden.log:/app/starwarden.log
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
starwarden:
image: rtuszik/starwarden
# env_file: .env ### for use with env file
environment:
- GITHUB_TOKEN=${GITHUB_TOKEN}
- GITHUB_USERNAME=${GITHUB_USERNAME}
- LINKWARDEN_URL=${LINKWARDEN_URL}
- LINKWARDEN_TOKEN=${LINKWARDEN_TOKEN}
- COLLECTION_ID=${COLLECTION_ID}
- CRON_SCHEDULE=${CRON_SCHEDULE:-0 0 * * *}
volumes:
- ./starwarden.log:/app/starwarden.log
Loading

0 comments on commit 770d7a8

Please sign in to comment.