diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cae86fa0c829b7b5ac8d496bc4d165fed2179aa6..df5109a1a3ed60fcd1be784ce226e23a45129a09 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,66 +1,76 @@ stages: - - tools + - deploy - test - - cleanup_tests - build - - deploy -build_test: - stage: tools - tags: - - bots-deployer - script: - - make build-test +variables: + APP_VERSION: $CI_COMMIT_REF_SLUG + DOCKER_AUTH_CONFIG: "{\"auths\":{\"$DOCKER_REGISTRY\":{\"username\":\"$DOCKER_USER\",\"password\":\"$DOCKER_PASSWORD\"}}}" -flake8: +.test-template: stage: test + image: python:3.9-buster tags: - - bots-deployer + - kube + except: + - tags + before_script: + - pip install poetry==1.1.6 + - poetry config virtualenvs.create false + - poetry install + +flake8: + extends: + - .test-template script: - - make flake8 + - flake8 --count . pytest: - stage: test - tags: - - bots-deployer + extends: + - .test-template script: - - make pytest + - pytest -vv . black: - stage: test - tags: - - bots-deployer - script: - - make black - -cleanup_tests: - stage: cleanup_tests - tags: - - bots-deployer - when: always + extends: + - .test-template script: - - make clear-test + - black --check . build: stage: build + image: + name: gcr.io/kaniko-project/executor:v1.6.0-debug + entrypoint: [ "" ] + tags: + - kube only: - - master - tags - tags: - - bots-deployer script: - - make build push + - mkdir -p /kaniko/.docker + - echo "$DOCKER_AUTH_CONFIG" > /kaniko/.docker/config.json + - /kaniko/executor --context . + --dockerfile deploy/Dockerfile + --destination "$IMAGE_NAME:$APP_VERSION" + --force deploy: stage: deploy + image: docker.le-memese.com/kubectl:1.22.0 + tags: + - kube only: - - master - tags - when: manual environment: name: production url: https://${BOT_HOST}/ - tags: - - bots-deployer script: - - make deploy-prod + - env | grep "^BOT_" > .deploy-env + - kubectl delete configmap "$CONFIG_MAP" || true + - kubectl create configmap "$CONFIG_MAP" --from-env-file=.deploy-env + - echo "$DOCKER_AUTH_CONFIG" > .dockerauth.json + - kubectl delete secret "$PULL_SECRET" || true + - kubectl create secret generic "$PULL_SECRET" + --from-file=.dockerconfigjson=.dockerauth.json + --type=kubernetes.io/dockerconfigjson + - find deploy/kube -name "*.yml" | xargs cat | envsubst | kubectl apply -f - diff --git a/Makefile b/Makefile deleted file mode 100644 index 076d1716d57bde3ef7cab0c3ef489117ba7b5d47..0000000000000000000000000000000000000000 --- a/Makefile +++ /dev/null @@ -1,79 +0,0 @@ -.DEFAULT_GOAL := help - -.PHONY: help -help: ## Show help - @grep -E '^[\.a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ - | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' - -.PHONY: env_file -env_file: ## Generate .gen.env file from env with "BOT_" prefix. - touch .gen.env && env | grep -E '^BOT_' > ".gen.env" || echo "Ðет переменных Ñреды" - test -f .env && cat .env > .gen.env || echo "Файл .env не найден" - -.PHONY: build -build: env_file ## Build docker container image. - docker-compose \ - -f ./deploy/docker-compose.yml \ - --project-directory . \ - build --pull bot_service - -.PHONY: build_test -build-test: ## Build image for tests. - docker-compose \ - -f ./deploy/docker-compose.test.yml \ - --project-directory . \ - build --no-cache test_bot_service - -.PHONY: flake8 -flake8: ## Run flake8 check - docker-compose \ - -f ./deploy/docker-compose.test.yml \ - --project-directory . \ - run test_bot_service flake8 --count . - -.PHONY: black -black: ## Run black check - docker-compose \ - -f ./deploy/docker-compose.test.yml \ - --project-directory . \ - run test_bot_service black --check . - -.PHONY: pytest -pytest: ## Run pytest tests - docker-compose \ - -f ./deploy/docker-compose.test.yml \ - --project-directory . \ - run test_bot_service pytest -v --cov=bot_s3rius - -.PHONY: push -push: env_file ## Push container to the registry. - docker-compose \ - -f ./deploy/docker-compose.yml \ - --project-directory . \ - push bot_service - -.PHONY: pull -pull: env_file ## Download images from docker hub. - docker-compose \ - -f deploy/docker-compose.yml \ - --project-directory . \ - pull --ignore-pull-failures bot_service \ - || \ - docker-compose \ - -f deploy/docker-compose.yml \ - --project-directory . \ - pull bot_service - -.PHONY: deploy-prod -deploy-prod: pull ## deploy container in production. - docker-compose \ - -f ./deploy/docker-compose.yml \ - --project-directory . \ - up -d - -.PHONY: clear-test -clear-test: ## Remove containers and images for test - docker-compose \ - -f ./deploy/docker-compose.test.yml \ - --project-directory . \ - down --rmi=all diff --git a/deploy/Dockerfiles/Dockerfile b/deploy/Dockerfile similarity index 61% rename from deploy/Dockerfiles/Dockerfile rename to deploy/Dockerfile index e9be63e67580a14ba5dd615496ad2a370e3f7b62..701aa312fedf0eb2841db77c6cc5e3f1a87b6487 100644 --- a/deploy/Dockerfiles/Dockerfile +++ b/deploy/Dockerfile @@ -1,16 +1,11 @@ -FROM python:3.9-alpine3.13 +FROM python:3.9.6-slim-buster -RUN adduser --disabled-password bot - -RUN apk add --no-cache curl ffmpeg gcc musl-dev libffi-dev - -ENV POETRY_VERSION 1.1.6 +RUN useradd -m bot USER bot -RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - +RUN pip install poetry==1.1.6 ENV PATH="${PATH}:/home/bot/.poetry/bin:/home/bot/.local/bin" -RUN source "/home/bot/.poetry/env" # Installing requirements RUN poetry config virtualenvs.create false @@ -28,7 +23,6 @@ RUN pip install --use-feature=in-tree-build . WORKDIR /home/bot/app USER root -RUN apk del musl-dev libffi-dev gcc RUN rm -rf /home/bot/app/src RUN chown -R bot /home/bot RUN chmod -R 700 /home/bot diff --git a/deploy/Dockerfiles/test.Dockerfile b/deploy/Dockerfiles/test.Dockerfile deleted file mode 100644 index a49197cb03fc3423fde015ba7509cc16a929e401..0000000000000000000000000000000000000000 --- a/deploy/Dockerfiles/test.Dockerfile +++ /dev/null @@ -1,17 +0,0 @@ -FROM python:3.9-alpine3.13 - -RUN apk add --no-cache curl gcc musl-dev libffi-dev ffmpeg -ENV POETRY_VERSION 1.1.6 - -RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - - -ENV PATH="${PATH}:/root/.local/bin:/root/.poetry/bin" -RUN source "/root/.poetry/env" - -# Installing requirements -RUN poetry config virtualenvs.create false - -COPY . /app/ -WORKDIR /app/ - -RUN poetry install diff --git a/deploy/docker-compose.test.yml b/deploy/docker-compose.test.yml deleted file mode 100644 index 3e2dba770c491ab10377dc620f43e604c1407fde..0000000000000000000000000000000000000000 --- a/deploy/docker-compose.test.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: '3.7' - -services: - test_bot_service: - container_name: 'bot_s3rius_san_test' - build: - dockerfile: ./deploy/Dockerfiles/test.Dockerfile - context: . diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.yml deleted file mode 100644 index 6fc3c245d21fcc3216ae64f6e5cac755706a41f2..0000000000000000000000000000000000000000 --- a/deploy/docker-compose.yml +++ /dev/null @@ -1,33 +0,0 @@ -version: '3.7' -networks: - memes_network: - external: - name: memes_network - - -services: - bot_service: - container_name: 'bot_s3rius_san' - build: - dockerfile: ./deploy/Dockerfiles/Dockerfile - context: . - image: docker.le-memese.com/bots/s3rius-bot:${CI_COMMIT_REF_SLUG:-latest} - env_file: - - .gen.env - labels: - - traefik.enable=true - - - traefik.http.routers.s3_bot_router.rule=Host(`${BOT_HOST}`) - - traefik.http.routers.s3_bot_router.service=s3_bot - - traefik.http.routers.s3_bot_router.tls=true - - traefik.http.routers.s3_bot_router.entrypoints=https - - - traefik.http.services.s3_bot.loadbalancer.server.port=${BOT_FLASK_PORT:-8080} - - networks: - - traefik-shared - -networks: - traefik-shared: - name: traefik-shared - external: true diff --git a/deploy/kube/deployment.yml b/deploy/kube/deployment.yml new file mode 100644 index 0000000000000000000000000000000000000000..b28db0adfde65e8161491b2f575baf93de8c1820 --- /dev/null +++ b/deploy/kube/deployment.yml @@ -0,0 +1,35 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: s3riusbot + annotations: + app.gitlab.com/env: $CI_ENVIRONMENT_SLUG + app.gitlab.com/app: $CI_PROJECT_PATH_SLUG +spec: + replicas: 1 + selector: + matchLabels: + app: s3rius-bot + template: + metadata: + annotations: + app.gitlab.com/env: $CI_ENVIRONMENT_SLUG + app.gitlab.com/app: $CI_PROJECT_PATH_SLUG + labels: + app: s3rius-bot + spec: + imagePullSecrets: + - name: "$PULL_SECRET" + containers: + - name: s3rius-bot + image: docker.le-memese.com/bots/s3rius-bot:$APP_VERSION + resources: + limits: + cpu: 100m + memory: 100Mi + envFrom: + - configMapRef: + name: "$CONFIG_MAP" + ports: + - containerPort: $BOT_FLASK_PORT diff --git a/deploy/kube/ingress.yml b/deploy/kube/ingress.yml new file mode 100644 index 0000000000000000000000000000000000000000..d6bc67d2b52e1765e89487cb103d6360c804c886 --- /dev/null +++ b/deploy/kube/ingress.yml @@ -0,0 +1,19 @@ +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: s3rius-bot-ui +spec: + tls: + - secretName: $TLS_SECRET + rules: + - host: $BOT_HOST + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: s3rius-bot + port: + name: web diff --git a/deploy/kube/service.yml b/deploy/kube/service.yml new file mode 100644 index 0000000000000000000000000000000000000000..50f71dfa0d936fbc1d25e04bbb783df9f90bb28d --- /dev/null +++ b/deploy/kube/service.yml @@ -0,0 +1,12 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: s3rius-bot +spec: + selector: + app: s3rius-bot + ports: + - port: 80 + name: web + targetPort: $BOT_FLASK_PORT