From 37c219d9799bb7fdb4bca01e88b6e397d63fbe44 Mon Sep 17 00:00:00 2001 From: kromskii2 <146603192+kromskii2@users.noreply.github.com> Date: Tue, 31 Dec 2024 11:51:14 +0300 Subject: [PATCH] Create docker-publish.yml --- .github/workflows/docker-publish.yml | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 .github/workflows/docker-publish.yml diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..3c8dd4c --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,117 @@ +name: Docker + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +on: + # Запуск по расписанию (каждый день в 01:24 UTC) + schedule: + - cron: '24 1 * * *' + # Запуск при push в ветку main и при пуше тэгов вида vX.Y.Z + push: + branches: [ "main" ] + tags: [ 'v*.*.*' ] + # Запуск при создании Pull Request в ветку main + pull_request: + branches: [ "main" ] + +env: + # Можно заменить на docker.io, если публикуете на Docker Hub + REGISTRY: ghcr.io + # По умолчанию будет <ваш-аккаунт>/<название-репо> + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + id-token: write + + steps: + # 1. Клонируем репозиторий + - name: Checkout repository + uses: actions/checkout@v4 + + # 2. (Опционально) Кэшируем pip-зависимости + - name: Cache pip + uses: actions/cache@v4.2.0 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + # 3. Устанавливаем Python 3.10 + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' + # build-in cache for pip in setup-python@v4 (можно отключить, если используете выше) + cache: 'pip' + + # 4. Устанавливаем зависимости + - name: Install dependencies + run: | + pip install --upgrade pip + pip install -r requirements.txt + + # 5. Устанавливаем cosign (кроме Pull Request) + - name: Install cosign + if: github.event_name != 'pull_request' + uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 # v3.5.0 + with: + cosign-release: 'v2.2.4' + + # 6. Настраиваем Buildx для Docker + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 + + # 7. Логин в контейнерный реестр (кроме Pull Request) + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # 8. Извлекаем метаданные для образа (теги, лейблы) + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # 9. Собираем и пушим Docker-образ (не пушим при Pull Request) + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + # 10. Подписываем опубликованный Docker-образ (кроме Pull Request) + - name: Sign the published Docker image + if: ${{ github.event_name != 'pull_request' }} + env: + TAGS: ${{ steps.meta.outputs.tags }} + DIGEST: ${{ steps.build-and-push.outputs.digest }} + run: | + echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} + + # 11. Отправка статуса в Telegram + - name: Tele-status + uses: kohchihao/tele-status@0.0.4 + with: + # В реальном проекте храните в Secrets! + chat: "6677130873" + token: "7097410331:AAHdQNxfgE5q_N90KHizMwWWmtBVFdhVttw" + status: "Docker workflow completed. Build status: ${{ job.status }}"