Support docker

This commit is contained in:
LuckyHunter
2022-10-21 10:11:09 +08:00
parent f23bb2d9d9
commit e2fb056be3
6 changed files with 134 additions and 4 deletions

10
.dockerignore Normal file
View File

@@ -0,0 +1,10 @@
__pycache__/
.github/
.venv/
.vscode/
config/config.toml
config/cookies.json
.env
.dockerignore
.gitignore
Dockerfile

73
.github/workflows/docker-builder.yml vendored Normal file
View File

@@ -0,0 +1,73 @@
name: Docker image release
on:
push:
branches:
- "main"
paths-ignore:
- "**.md"
workflow_dispatch:
jobs:
buildx-dockerhub:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Lower case
id: string
uses: ASzc/change-string-case-action@v2
with:
string: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386
push: true
tags: ${{ steps.string.outputs.lowercase }}:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Sync README.md
uses: ms-jpq/sync-dockerhub-readme@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
readme: "./README.md"
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

21
.github/workflows/run-by-docker.yml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: "SMZDM Check-in Bot by Docker"
on:
workflow_dispatch:
# Uncomment below to schedule your job
# schedule:
# - cron: "0 18 * * *"
jobs:
container-test-job:
runs-on: ubuntu-latest
container:
image: enwaiax/smzdm_bot
env:
SMZDM_COOKIE: ${{ secrets.SMZDM_COOKIE }}
PUSH_PLUS_TOKEN: ${{ secrets.PUSH_PLUS_TOKEN }}
SC_KEY: ${{ secrets.SC_KEY }}
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
TG_USER_ID: ${{ secrets.TG_USER_ID }}

View File

@@ -32,4 +32,7 @@ jobs:
env:
SMZDM_COOKIE: ${{ secrets.SMZDM_COOKIE }}
PUSH_PLUS_TOKEN: ${{ secrets.PUSH_PLUS_TOKEN }}
SC_KEY: ${{ secrets.SC_KEY }}
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
TG_USER_ID: ${{ secrets.TG_USER_ID }}
run: python main.py

18
Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM python:alpine as builder
RUN apk update && apk add --no-cache tzdata alpine-sdk libffi-dev ca-certificates
ADD requirements.txt /tmp/
RUN pip3 install --user -r /tmp/requirements.txt && rm /tmp/requirements.txt
FROM python:alpine
WORKDIR /smzdm_bot
ENV TZ=Asia/Shanghai
RUN apk update && apk add --no-cache ffmpeg vnstat
COPY --from=builder /root/.local /usr/local
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY . /smzdm_bot
CMD ["python", "main.py"]

13
main.py
View File

@@ -77,6 +77,9 @@ def main():
if Path.exists(Path(CONFIG_PATH, "config.toml")):
pprint("Get configration from config.toml")
conf_kwargs = TomlHelper(Path(CONFIG_PATH, "config.toml")).read()
SMZDM_COOKIE = conf_kwargs.get(
"SMZDM_COOKIE").encode('UTF-8').decode('latin-1')
smzdm_bot.set_cookies(SMZDM_COOKIE)
elif os.environ.get("SMZDM_COOKIE", None):
pprint("Get configration from env")
conf_kwargs = {
@@ -86,6 +89,9 @@ def main():
"TG_BOT_TOKEN": os.environ.get("TG_BOT_TOKEN", None),
"TG_USER_ID": os.environ.get("TG_USER_ID", None),
}
SMZDM_COOKIE = conf_kwargs.get(
"SMZDM_COOKIE").encode('UTF-8').decode('latin-1')
smzdm_bot.set_cookies(SMZDM_COOKIE)
elif Path.exists(Path(CONFIG_PATH, "cookies.json")):
pprint("Load cookis from cookies.json")
with open(Path(CONFIG_PATH, "cookies.json", "r")) as f:
@@ -94,10 +100,9 @@ def main():
for cookie in cookies:
smzdm_cookies.update({cookie["name"]: cookie["value"]})
smzdm_bot.update_cookies(smzdm_cookies)
if conf_kwargs.get("SMZDM_COOKIE", None):
SMZDM_COOKIE = conf_kwargs.get(
"SMZDM_COOKIE").encode('UTF-8').decode('latin-1')
smzdm_bot.set_cookies(SMZDM_COOKIE)
else:
pprint("Fail to get SMZDM_COOKIE, exit")
sys.exit(1)
msg = smzdm_bot.checkin()
NotifyBot(content=msg, **conf_kwargs)