From f86e269d242fb09b81e37552ab5bb9629dccfc71 Mon Sep 17 00:00:00 2001 From: LeoR156 <277007013+LeoR156@users.noreply.github.com> Date: Sat, 12 Sep 2026 18:09:16 +0500 Subject: [PATCH] Update CI/CD --- .gitlab-ci.yml | 68 ++++++++++++++++++++++++------------ auto-deployment/A_deploy.yml | 7 ++-- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index eaa08b1..5f8b0e9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,36 +6,37 @@ stages: - tests - build-and-push - deploy - - notify tests_job1: stage: tests - image: python:3.11-slim + image: python:3.11-slim variables: PYTHONPATH: "." before_script: - - pip install --no-cache-dir -r requirements.txt + - pip install --no-cache-dir -r requirements.txt - pip install pytest script: - - pytest tests/ + - pytest tests/ push_job: - stage: build-and-push - image: - name: gcr.io/kaniko-project/executor:v1.23.0-debug - entrypoint: [""] - before_script: - - mkdir -p /kaniko/.docker - - cp "$DOCKER_AUTH_CONFIG" /kaniko/.docker/config.json - script: - - > - /kaniko/executor - --context "${CI_PROJECT_DIR}" - --dockerfile "${CI_PROJECT_DIR}/Dockerfile" - --destination "${DOCKERHUB_USER}/quote-bot:${CI_COMMIT_SHORT_SHA}" - --destination "${DOCKERHUB_USER}/quote-bot:latest" + resource_group: production + stage: build-and-push + image: + name: gcr.io/kaniko-project/executor:v1.23.0-debug + entrypoint: [""] + before_script: + - mkdir -p /kaniko/.docker + - cp "$DOCKER_AUTH_CONFIG" /kaniko/.docker/config.json + script: + - > + /kaniko/executor + --context "${CI_PROJECT_DIR}" + --dockerfile "${CI_PROJECT_DIR}/Dockerfile" + --destination "${DOCKERHUB_USER}/quote-bot:${CI_COMMIT_SHORT_SHA}" + --destination "${DOCKERHUB_USER}/quote-bot:latest" deploy_to_server: + resource_group: production stage: deploy image: ghcr.io/ansible/creator-ee:v0.22.0 variables: @@ -49,6 +50,32 @@ deploy_to_server: script: - cd auto-deployment/ - ansible-playbook -i inventory.ini A_deploy.yml + --extra-vars "image_tag=$CI_COMMIT_SHORT_SHA" + + environment: + name: production + url: http://$SERVER_IP + on_stop: stop_production + +stop_production: + stage: deploy + image: ghcr.io/ansible/creator-ee:v0.22.0 + variables: + ANSIBLE_HOST_KEY_CHECKING: "False" + before_script: + - mkdir -p ~/.ssh + - chmod 700 ~/.ssh + - echo "$ANSIBLE_PRIVATE_KEY" > ~/.ssh/id_ed25519 + - chmod 600 ~/.ssh/id_ed25519 + script: + - cd auto-deployment/ + - ansible-playbook -i inventory.ini A_deploy.yml + --extra-vars "container_state=absent" + when: manual + environment: + name: production + action: stop + variables: MSG_SUCCESS: "✅ Успех! Пайплайн проекта $CI_PROJECT_NAME завершен.\nВетка: $CI_COMMIT_REF_NAME\nКоммит: $CI_COMMIT_MESSAGE" @@ -57,7 +84,7 @@ variables: .notify_telegram: stage: .post - tags: + tags: - tg image: curlimages/curl:latest script: @@ -68,17 +95,14 @@ variables: -d parse_mode="HTML" \ -d disable_web_page_preview="true" - notify_success: extends: .notify_telegram variables: MESSAGE: $MSG_SUCCESS when: on_success - notify_failure: extends: .notify_telegram variables: MESSAGE: $MSG_FAILURE when: on_failure - \ No newline at end of file diff --git a/auto-deployment/A_deploy.yml b/auto-deployment/A_deploy.yml index e625500..c9df3be 100644 --- a/auto-deployment/A_deploy.yml +++ b/auto-deployment/A_deploy.yml @@ -3,8 +3,9 @@ hosts: all vars: - image_name: "leor156/quote-bot:latest" - container_name: "quote-bot" + image_name: "leor156/quote-bot:{{ image_tag | default('latest') }}" + container_name: "{{ container_name | default('quote-bot') }}" + container_state: "{{ container_state | default('started') }}" tasks: - name: Убеждаемся, что установлена библиотека Docker для Python @@ -22,7 +23,7 @@ community.docker.docker_container: name: "{{ container_name }}" image: "{{ image_name }}" - state: started + state: "{{ container_state }}" restart_policy: unless-stopped pull: always env: