diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..74d2815 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,51 @@ +name: CI/CD Pipeline + +on: + push: + branches: [main] + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Setup variables + run: | + DOCKER_REGISTRY=$(echo "${{ gitea.server_url }}" | sed 's|https://||') + echo "DOCKER_REGISTRY=$DOCKER_REGISTRY" >> $GITHUB_ENV + REGISTRY_IMAGE="$DOCKER_REGISTRY/$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')" + echo "REGISTRY_IMAGE=$REGISTRY_IMAGE" >> $GITHUB_ENV + + - name: Login to Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.DOCKER_REGISTRY }} + username: ${{ secrets.CR_USER }} + password: ${{ secrets.CR_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY_IMAGE }} + tags: | + type=ref,event=branch + type=sha,prefix= + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..805922e --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,47 @@ +name: Deploy to Production + +on: + workflow_run: + workflows: ["CI/CD Pipeline"] + types: [completed] + branches: [main] + +jobs: + deploy: + runs-on: ubuntu-latest + if: github.event.workflow_run.conclusion == 'success' + + steps: + - name: Setup variables + run: | + DOCKER_REGISTRY=$(echo "${{ gitea.server_url }}" | sed 's|https://||') + echo "DOCKER_REGISTRY=$DOCKER_REGISTRY" >> $GITHUB_ENV + REGISTRY_IMAGE="$DOCKER_REGISTRY/$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')" + echo "REGISTRY_IMAGE=$REGISTRY_IMAGE" >> $GITHUB_ENV + + - name: Настройка SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key + chmod 600 ~/.ssh/deploy_key + ssh-keyscan -H 188.225.47.78 >> ~/.ssh/known_hosts + + - name: Login to Container Registry + run: | + ssh -i ~/.ssh/deploy_key root@188.225.47.78 "echo '${{ secrets.CR_TOKEN }}' | docker login ${{ env.DOCKER_REGISTRY }} -u '${{ secrets.CR_USER }}' --password-stdin" + + - name: Скачивание образа + run: | + ssh -i ~/.ssh/deploy_key root@188.225.47.78 "docker pull ${{ env.REGISTRY_IMAGE }}:latest" + + - name: Перезапуск контейнера + run: | + ssh -i ~/.ssh/deploy_key root@188.225.47.78 "docker stop frontend-style-guide || true && docker rm frontend-style-guide || true && docker run -d --name frontend-style-guide --network web --restart unless-stopped ${{ env.REGISTRY_IMAGE }}:latest" + + - name: Очистка + run: | + ssh -i ~/.ssh/deploy_key root@188.225.47.78 "docker image prune -f" + + - name: Статус + run: | + ssh -i ~/.ssh/deploy_key root@188.225.47.78 "docker ps --filter name=frontend-style-guide" diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..bde2896 --- /dev/null +++ b/Caddyfile @@ -0,0 +1,5 @@ +:8080 { + root * /srv + file_server + try_files {path} /index.html +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8577c52 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:24-alpine AS build +WORKDIR /app +COPY package*.json ./ +RUN npm ci +COPY . . +RUN npm run build + +FROM caddy:2-alpine +COPY Caddyfile /etc/caddy/Caddyfile +COPY --from=build /app/.vitepress/dist /srv