Skip to content

add workflow_dispatch #26

add workflow_dispatch

add workflow_dispatch #26

Workflow file for this run

name: Build and Push Docker Image
on:
push:
branches:
- main
workflow_dispatch:
inputs:
environment:
description: "Environment to deploy to"
required: true
default: "production"
type: choice
options:
- production
- staging
- development
debug_enabled:
description: "Run the deployment with debug logging"
required: false
type: boolean
default: false
jobs:
rust-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Rust Cache
uses: Swatinem/[email protected]
with:
cache-all-crates: true
shared-key: rust-cache
cache-on-failure: true
- name: Setup Rust
run: rustup toolchain install stable --profile minimal --no-self-update
- name: Check formatting
run: cargo fmt --all -- --check
- name: Build
run: cargo build
- name: Run Clippy
run: cargo clippy -- -D warnings
docker-build:
needs: rust-checks
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Downcase REPO
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Restore cache
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest
type=sha,format=short
labels: |
org.opencontainers.image.title=Shuller Bot
org.opencontainers.image.description=Discord Bot
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
maintainer=towinok
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: |
type=local,src=/tmp/.buildx-cache
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: |
type=local,dest=/tmp/.buildx-cache-new,mode=max
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
outputs: |
type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Временный фикс для кэша
- name: Move cache
if: always()
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Check image
if: success()
run: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
docker image ls ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
deploy:
needs: docker-build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
CONTAINER_NAME: lum # Имя контейнера на сервере
steps:
- name: Downcase REPO
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
- name: Check secrets
run: |
if [ -z "${{ secrets.SERVER_IP }}" ]; then
echo "SERVER_IP is missing"
exit 1
fi
if [ -z "${{ secrets.SERVER_USERNAME }}" ]; then
echo "SERVER_USERNAME is missing"
exit 1
fi
if [ -z "${{ secrets.SERVER_PASSWORD }}" ]; then
echo "SERVER_PASSWORD is missing"
exit 1
fi
echo "All required secrets are present"
- name: Deploy to Server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
port: 22
debug: true
timeout: 30s
script: |
# Логин в GitHub Container Registry
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Проверка и остановка существующего контейнера
if docker ps -a | grep -q "${{ env.CONTAINER_NAME }}"; then
echo "Stopping and removing existing container..."
docker stop ${{ env.CONTAINER_NAME }}
docker rm ${{ env.CONTAINER_NAME }}
else
echo "No existing container found"
fi
# Проверка и удаление существующего образа
if docker image ls | grep -q "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"; then
echo "Removing existing image..."
docker image rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
else
echo "No existing image found"
fi
# Загрузка нового образа
echo "Pulling new image..."
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
# Запуск нового контейнера
echo "Starting new container..."
docker run -d \
--name ${{ env.CONTAINER_NAME }} \
--restart unless-stopped \
-e DS_TOKEN=${{ secrets.DS_TOKEN }} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
# Очистка неиспользуемых образов
echo "Cleaning up unused images..."
docker image prune -f
# Выход из registry
docker logout ${{ env.REGISTRY }}