diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml new file mode 100644 index 0000000..14f4112 --- /dev/null +++ b/.github/workflows/build-image.yml @@ -0,0 +1,46 @@ +on: + workflow_call: + inputs: + image_name: + required: true + type: string + secrets: + DOCKERHUB_USERNAME: + required: true + DOCKERHUB_TOKEN: + required: true + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Get sources + uses: actions/checkout@v4 + - name: Container meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ inputs.image_name }} + tags: | + type=schedule + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + - 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@v5 + with: + context: . + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + push: true diff --git a/.github/workflows/deploy-stage.yml b/.github/workflows/deploy-stage.yml new file mode 100644 index 0000000..869bdd6 --- /dev/null +++ b/.github/workflows/deploy-stage.yml @@ -0,0 +1,26 @@ +on: + push: + branches: + - master + +jobs: + build: + uses: ./.github/workflows/build-image.yml + with: + image_name: zinstack625/iscraweb-ttbot + secrets: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + deploy: + runs-on: ubuntu-latest + steps: + - name: Deploy to bleeding edge + uses: grebois/kube-tools@v1.5.2 + env: + KUBECONFIG_FILE: ${{ secrets.KUBECONFIG }} + with: + kubectl: 1.18.2 + command: | + echo -n "$KUBECONFIG_FILE" > .kubeconfig + export KUBECONFIG=$(pwd)/.kubeconfig + kubectl rollout restart -n iscrawebtt deployment ttbot-bleed diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..93b2341 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,25 @@ +on: + workflow_dispatch + +jobs: + build: + uses: ./.github/workflows/build-image.yml + with: + image_name: zinstack625/iscraweb-ttbot + secrets: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + deploy: + runs-on: ubuntu-latest + needs: build + steps: + - name: Deploy to stable + uses: grebois/kube-tools@v1.5.2 + env: + KUBECONFIG_FILE: ${{ secrets.KUBECONFIG }} + with: + kubectl: 1.18.2 + command: | + echo -n "$KUBECONFIG_FILE" > .kubeconfig + export KUBECONFIG=$(pwd)/.kubeconfig + kubectl rollout restart -n iscrawebtt deployment ttbot diff --git a/Dockerfile b/Dockerfile index 1956057..2bf0472 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,12 @@ FROM python:3-alpine WORKDIR /usr/src/app -COPY requirements.txt ./ -COPY ./bot ./ -COPY config.json ./ +RUN apk add envsubst +COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt +COPY ./bot ./ COPY iscrawebttbot_run.sh /usr/local/bin/ ENTRYPOINT [ "/usr/local/bin/iscrawebttbot_run.sh" ] diff --git a/config.json.tmpl b/config.json.tmpl new file mode 100644 index 0000000..d7d285a --- /dev/null +++ b/config.json.tmpl @@ -0,0 +1,12 @@ +{ + "refresh_period" : $REFRESH_PERIOD, + "sleep_timeout" : $SLEEP_TIMEOUT, + "notify_period" : $NOTIFY_PERIOD, + "use_https" : $HTTPS, + "redmine_root_url" : "$REDMINE_URL", + "user_db_path" : "./udb/localbase.json", + "scenery_path" : "./scenery.json", + "allowed_api_functions" : [], + "address" : "0.0.0.0", + "port" : 43210 +} diff --git a/docker_start.sh b/docker_start.sh new file mode 100755 index 0000000..5beb339 --- /dev/null +++ b/docker_start.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env sh + +[ -z "$CONTAINER_NAME" ] && CONTAINER_NAME=docker.io/Fe-Ti/iscraweb-ttbot:latest + +runner_args=(run --rm -v $(git rev-parse --show-toplevel)/config.json.tmpl:/config.json.tmpl -e REDMINE_URL=$REDMINE_URL -e CFG=/config.json.tmpl -e K=$K -e T=$T "$CONTAINER_NAME") + +case "$CONTAINER_RUNTIME" in + podman) + podman ${runner_args[@]};; + *) + docker ${runner_args[@]};; +esac diff --git a/iscrawebttbot_run.sh b/iscrawebttbot_run.sh index cc5a418..bf95959 100755 --- a/iscrawebttbot_run.sh +++ b/iscrawebttbot_run.sh @@ -1,15 +1,30 @@ #!/usr/bin/env sh # Define cleanup procedure cleanup() { - /usr/bin/env python ./bot_control.py -c $CFG shutdown - /usr/bin/env python ./bot_control.py -c $CFG exit + /usr/bin/env python ./bot_control.py -c ./config.json shutdown + /usr/bin/env python ./bot_control.py -c ./config.json exit ls -la } +die() { + echo "$1"; exit 1 +} + # Trap SIGTERM trap 'cleanup' SIGTERM -/usr/bin/env python ./bot.py -k $K -t $T -c $CFG -s +[ -z "$REFRESH_PERIOD" ] && export REFRESH_PERIOD=1000 +[ -z "$SLEEP_TIMEOUT" ] && export SLEEP_TIMEOUT=10 +[ -z "$NOTIFY_PERIOD" ] && export NOTIFY_PERIOD=43200 +[ -z "$HTTPS" ] && export HTTPS="true" +[ -z "$REDMINE_URL" ] && die 'Redmine root URL unset! Set envvar $REDMINE_URL' +[ -z "$K" ] && die 'Telegram token unset! Set envvar $K' +[ -z "$T" ] && die 'Redmine user key unset! Set envvar $T' + +envsubst < "$CFG" > ./config.json +cat ./config.json + +/usr/bin/env python ./bot.py -k "$T" -t "$K" -c ./config.json -s # Wait wait $!