feat(deploy): enhance deployment workflow with improved Docker setup and SSH actions
This commit is contained in:
@@ -10,59 +10,73 @@ permissions:
|
|||||||
contents: read
|
contents: read
|
||||||
packages: write
|
packages: write
|
||||||
|
|
||||||
|
env:
|
||||||
|
IMAGE_NAME: ghcr.io/${{ github.repository_owner }}/bete
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Normalize image name
|
||||||
|
run: echo "IMAGE_NAME=${IMAGE_NAME,,}" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Log in to GHCR
|
- name: Log in to GHCR
|
||||||
run: echo "${{ github.token }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Build and push Docker image
|
- name: Build and push Docker image
|
||||||
env:
|
uses: docker/build-push-action@v6
|
||||||
IMAGE_NAME: ghcr.io/mytheclipse/bete:${{ github.sha }}
|
with:
|
||||||
run: |
|
context: .
|
||||||
docker build -t "$IMAGE_NAME" .
|
push: true
|
||||||
docker push "$IMAGE_NAME"
|
tags: |
|
||||||
|
${{ env.IMAGE_NAME }}:latest
|
||||||
|
${{ env.IMAGE_NAME }}:${{ github.sha }}
|
||||||
|
|
||||||
- name: Deploy to VPS
|
- name: Deploy to VPS
|
||||||
|
uses: appleboy/ssh-action@v1.2.0
|
||||||
env:
|
env:
|
||||||
VPS_HOST: ${{ secrets.VPS_HOST }}
|
IMAGE_NAME: ${{ env.IMAGE_NAME }}
|
||||||
VPS_USERNAME: ${{ secrets.VPS_USERNAME }}
|
GHCR_USERNAME: ${{ github.actor }}
|
||||||
VPS_SSH_KEY: ${{ secrets.VPS_SSH_KEY }}
|
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
ENV_FILE: ${{ secrets.ENV_FILE }}
|
ENV_FILE: ${{ secrets.ENV_FILE }}
|
||||||
GITHUB_TOKEN: ${{ github.token }}
|
with:
|
||||||
GITHUB_ACTOR: ${{ github.actor }}
|
host: ${{ secrets.VPS_HOST }}
|
||||||
IMAGE_NAME: ghcr.io/mytheclipse/bete:${{ github.sha }}
|
username: ${{ secrets.VPS_USERNAME }}
|
||||||
run: |
|
key: ${{ secrets.VPS_SSH_KEY }}
|
||||||
# 1. Setup SSH Key
|
envs: IMAGE_NAME,GHCR_USERNAME,GHCR_TOKEN,ENV_FILE
|
||||||
mkdir -p ~/.ssh
|
script: |
|
||||||
echo "$VPS_SSH_KEY" > ~/.ssh/id_ed25519
|
set -eu
|
||||||
chmod 600 ~/.ssh/id_ed25519
|
|
||||||
ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts
|
|
||||||
|
|
||||||
# 2. Pull image and start containers on VPS
|
|
||||||
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no "$VPS_USERNAME@$VPS_HOST" "
|
|
||||||
set -e
|
|
||||||
APP_DIR=/opt/imphenbot
|
APP_DIR=/opt/imphenbot
|
||||||
mkdir -p \$APP_DIR
|
mkdir -p "$APP_DIR"
|
||||||
cd \$APP_DIR
|
cd "$APP_DIR"
|
||||||
|
|
||||||
echo \"$GITHUB_TOKEN\" | docker login ghcr.io -u \"$GITHUB_ACTOR\" --password-stdin
|
printf '%s\nIMAGE_NAME=%s:latest\n' "$ENV_FILE" "$IMAGE_NAME" > .env
|
||||||
|
|
||||||
printf '%s\nIMAGE_NAME=$IMAGE_NAME' \"$ENV_FILE\" > .env
|
cat > docker-compose.yml <<'EOF'
|
||||||
|
services:
|
||||||
|
bete:
|
||||||
|
image: ${IMAGE_NAME}
|
||||||
|
container_name: imphenbot
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
EOF
|
||||||
|
|
||||||
if command -v docker-compose &> /dev/null; then
|
echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin
|
||||||
docker-compose down || true
|
|
||||||
docker-compose pull
|
docker compose pull
|
||||||
docker-compose up -d
|
docker compose up -d
|
||||||
else
|
docker image prune -f
|
||||||
docker compose down || true
|
|
||||||
docker compose pull
|
|
||||||
docker compose up -d
|
|
||||||
fi
|
|
||||||
"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user