From 4717a0927341e1fc3d2221f19069bd3a17d6145a Mon Sep 17 00:00:00 2001 From: Anton Klimanov Date: Thu, 21 Sep 2023 14:54:07 +0300 Subject: [PATCH 1/5] [ci/cd] initial build and deploy workflow --- .github/workflows/build-image.yml | 43 ++++++++++++++++++++++++++++++ .github/workflows/deploy-stage.yml | 29 ++++++++++++++++++++ .github/workflows/deploy.yml | 27 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 .github/workflows/build-image.yml create mode 100644 .github/workflows/deploy-stage.yml create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml new file mode 100644 index 0000000..ac674f0 --- /dev/null +++ b/.github/workflows/build-image.yml @@ -0,0 +1,43 @@ +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: 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 }} diff --git a/.github/workflows/deploy-stage.yml b/.github/workflows/deploy-stage.yml new file mode 100644 index 0000000..db2bfce --- /dev/null +++ b/.github/workflows/deploy-stage.yml @@ -0,0 +1,29 @@ +on: + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Get sources + uses: actions/checkout@v4 + - name: Build container + 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: ${{ secrets.KUBECONFIG }} + with: + kubectl: 1.18.2 + command: | + kubectl rollout restart -n iscraweb deployment ttbot-bleed diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..07de7eb --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,27 @@ +on: + workflow_dispatch + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Get sources + uses: actions/checkout@v4 + - name: Build container + 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 stable + uses: grebois/kube-tools@v1.5.2 + env: + KUBECONFIG: ${{ secrets.KUBECONFIG }} + with: + kubectl: 1.18.2 + command: | + kubectl rollout restart -n iscraweb deployment ttbot From 616ef16e33d68216ee92af16a4c1179f85e5b6b0 Mon Sep 17 00:00:00 2001 From: Anton Klimanov Date: Thu, 21 Sep 2023 15:54:04 +0300 Subject: [PATCH 2/5] [docker] additional container parametrization --- Dockerfile | 6 +++--- config.json.tmpl | 12 ++++++++++++ docker_start.sh | 12 ++++++++++++ iscrawebttbot_run.sh | 17 ++++++++++++++++- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 config.json.tmpl create mode 100755 docker_start.sh 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..62528ad 100755 --- a/iscrawebttbot_run.sh +++ b/iscrawebttbot_run.sh @@ -6,10 +6,25 @@ cleanup() { 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 "$K" -t "$T" -c ./config.json -s # Wait wait $! From 1e78d4b6983b88cddef94b8d526a6fcd5ad47e73 Mon Sep 17 00:00:00 2001 From: Anton Klimanov Date: Fri, 22 Sep 2023 11:18:41 +0300 Subject: [PATCH 3/5] [ci] add kubeconfig --- .github/workflows/deploy-stage.yml | 4 +++- .github/workflows/deploy.yml | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-stage.yml b/.github/workflows/deploy-stage.yml index db2bfce..032dadf 100644 --- a/.github/workflows/deploy-stage.yml +++ b/.github/workflows/deploy-stage.yml @@ -26,4 +26,6 @@ jobs: with: kubectl: 1.18.2 command: | - kubectl rollout restart -n iscraweb deployment ttbot-bleed + echo -n "$KUBECONFIG" > .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 index 07de7eb..73d7c76 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -24,4 +24,6 @@ jobs: with: kubectl: 1.18.2 command: | - kubectl rollout restart -n iscraweb deployment ttbot + echo -n "$KUBECONFIG" > .kubeconfig + export KUBECONFIG=$(pwd)/.kubeconfig + kubectl rollout restart -n iscrawebtt deployment ttbot From b6dae996d68e8f6284313e364f3c57b5eee8a80f Mon Sep 17 00:00:00 2001 From: Anton Klimanov Date: Fri, 22 Sep 2023 11:19:35 +0300 Subject: [PATCH 4/5] [docker] fixup entrypoint for deployment --- iscrawebttbot_run.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iscrawebttbot_run.sh b/iscrawebttbot_run.sh index 62528ad..bf95959 100755 --- a/iscrawebttbot_run.sh +++ b/iscrawebttbot_run.sh @@ -1,8 +1,8 @@ #!/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 } @@ -24,7 +24,7 @@ trap 'cleanup' SIGTERM envsubst < "$CFG" > ./config.json cat ./config.json -/usr/bin/env python ./bot.py -k "$K" -t "$T" -c ./config.json -s +/usr/bin/env python ./bot.py -k "$T" -t "$K" -c ./config.json -s # Wait wait $! From fd7ce2fd69dd0b5049b342cf187594339d64f2e4 Mon Sep 17 00:00:00 2001 From: Anton Klimanov Date: Fri, 22 Sep 2023 11:35:29 +0300 Subject: [PATCH 5/5] [ci] fixup workflows syntax --- .github/workflows/build-image.yml | 3 +++ .github/workflows/deploy-stage.yml | 23 +++++++++-------------- .github/workflows/deploy.yml | 22 +++++++++------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml index ac674f0..14f4112 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/build-image.yml @@ -14,6 +14,8 @@ jobs: build: runs-on: ubuntu-latest steps: + - name: Get sources + uses: actions/checkout@v4 - name: Container meta id: meta uses: docker/metadata-action@v5 @@ -41,3 +43,4 @@ jobs: 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 index 032dadf..869bdd6 100644 --- a/.github/workflows/deploy-stage.yml +++ b/.github/workflows/deploy-stage.yml @@ -1,31 +1,26 @@ on: - pull_request: + push: branches: - master jobs: build: - runs-on: ubuntu-latest - steps: - - name: Get sources - uses: actions/checkout@v4 - - name: Build container - uses: ./.github/workflows/build-image.yml - with: - image_name: zinstack625/iscraweb-ttbot - secrets: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + 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: ${{ secrets.KUBECONFIG }} + KUBECONFIG_FILE: ${{ secrets.KUBECONFIG }} with: kubectl: 1.18.2 command: | - echo -n "$KUBECONFIG" > .kubeconfig + 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 index 73d7c76..93b2341 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,27 +3,23 @@ on: jobs: build: - runs-on: ubuntu-latest - steps: - - name: Get sources - uses: actions/checkout@v4 - - name: Build container - uses: ./.github/workflows/build-image.yml - with: - image_name: zinstack625/iscraweb-ttbot - secrets: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + 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: ${{ secrets.KUBECONFIG }} + KUBECONFIG_FILE: ${{ secrets.KUBECONFIG }} with: kubectl: 1.18.2 command: | - echo -n "$KUBECONFIG" > .kubeconfig + echo -n "$KUBECONFIG_FILE" > .kubeconfig export KUBECONFIG=$(pwd)/.kubeconfig kubectl rollout restart -n iscrawebtt deployment ttbot