Fix Forgejo Actions workflow for proper CI/CD
- Fix registry URL format for Forgejo (host:port/owner/repo) - Create deploy-forgejo.sh with correct env variables - Rename CI variables from GitLab CI format to Forgejo/GitHub format - Simplify workflow structure - Add proper error handling in deploy script - Fix SSH key permissions (chmod 600) - Add ssh-keyscan to avoid StrictHostKeyChecking issues
This commit is contained in:
@@ -37,13 +37,7 @@ jobs:
|
|||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirements-dev.txt
|
pip install -r requirements-dev.txt
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: pytest -q tests
|
run: pytest -q tests -v
|
||||||
- name: Upload coverage
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: coverage-report
|
|
||||||
path: htmlcov/
|
|
||||||
retention-days: 7
|
|
||||||
|
|
||||||
type-check:
|
type-check:
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
|
|||||||
@@ -4,24 +4,57 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
|
||||||
environment:
|
|
||||||
description: 'Deployment environment'
|
|
||||||
required: true
|
|
||||||
default: 'production'
|
|
||||||
type: choice
|
|
||||||
options:
|
|
||||||
- production
|
|
||||||
- staging
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: ${{ vars.REGISTRY || format('{0}', github.repository_owner) }}
|
REGISTRY: ${{ vars.REGISTRY || '10.100.0.2:3000' }}
|
||||||
IMAGE_NAME: ${{ github.repository }}
|
IMAGE_NAME: ${{ github.repository }}
|
||||||
IMAGE_TAG: ${{ github.sha }}
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: docker
|
||||||
|
container:
|
||||||
|
image: python:3.12-slim
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install ruff black
|
||||||
|
- name: Run ruff
|
||||||
|
run: ruff check app tests scripts
|
||||||
|
- name: Run black
|
||||||
|
run: black --check app tests scripts
|
||||||
|
|
||||||
|
test:
|
||||||
|
runs-on: docker
|
||||||
|
container:
|
||||||
|
image: python:3.12-slim
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements-dev.txt
|
||||||
|
- name: Run tests
|
||||||
|
run: pytest -q tests -v
|
||||||
|
|
||||||
|
type-check:
|
||||||
|
runs-on: docker
|
||||||
|
container:
|
||||||
|
image: python:3.12-slim
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install mypy
|
||||||
|
pip install -r requirements.txt
|
||||||
|
- name: Run mypy
|
||||||
|
run: mypy app scripts --ignore-missing-imports
|
||||||
|
|
||||||
build:
|
build:
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
|
needs: [lint, test, type-check]
|
||||||
container:
|
container:
|
||||||
image: catthehacker/ubuntu:act-latest
|
image: catthehacker/ubuntu:act-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -37,45 +70,19 @@ jobs:
|
|||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.REGISTRY_PASSWORD || secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.REGISTRY_PASSWORD || secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Extract metadata
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
type=sha,prefix=
|
|
||||||
type=raw,value=latest,enable={{is_default_branch}}
|
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: |
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
|
||||||
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:cache
|
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
|
||||||
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:cache,mode=max
|
provenance: false
|
||||||
|
|
||||||
- name: Output image digest
|
|
||||||
run: echo "IMAGE_DIGEST=${{ steps.meta.outputs.tags }}" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
security-scan:
|
|
||||||
runs-on: docker
|
|
||||||
needs: build
|
|
||||||
container:
|
|
||||||
image: aquasec/trivy:0.61.1
|
|
||||||
steps:
|
|
||||||
- name: Scan image
|
|
||||||
run: |
|
|
||||||
trivy image --exit-code 1 \
|
|
||||||
--severity HIGH,CRITICAL \
|
|
||||||
--username "${{ github.actor }}" \
|
|
||||||
--password "${{ secrets.REGISTRY_PASSWORD || secrets.GITHUB_TOKEN }}" \
|
|
||||||
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ env.IMAGE_DIGEST }}"
|
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
runs-on: docker
|
runs-on: docker
|
||||||
needs: [build, security-scan]
|
needs: build
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
container:
|
container:
|
||||||
image: python:3.12-alpine
|
image: python:3.12-alpine
|
||||||
@@ -85,18 +92,10 @@ jobs:
|
|||||||
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT || '22' }}
|
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT || '22' }}
|
||||||
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH || '/opt/vault-dash' }}
|
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH || '/opt/vault-dash' }}
|
||||||
DEPLOY_SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}
|
DEPLOY_SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}
|
||||||
|
APP_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
|
||||||
APP_ENV: production
|
APP_ENV: production
|
||||||
APP_NAME: Vault Dashboard
|
APP_NAME: Vault Dashboard
|
||||||
APP_PORT: "8000"
|
APP_PORT: "8000"
|
||||||
APP_BIND_ADDRESS: "127.0.0.1"
|
|
||||||
DEFAULT_SYMBOL: GLD
|
|
||||||
CACHE_TTL: "300"
|
|
||||||
WEBSOCKET_INTERVAL_SECONDS: "5"
|
|
||||||
NICEGUI_MOUNT_PATH: /
|
|
||||||
NICEGUI_STORAGE_SECRET: ${{ secrets.NICEGUI_STORAGE_SECRET }}
|
|
||||||
CORS_ORIGINS: ${{ secrets.CORS_ORIGINS || '*' }}
|
|
||||||
REGISTRY: ${{ env.REGISTRY }}
|
|
||||||
IMAGE_TAG: ${{ env.IMAGE_TAG }}
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
@@ -110,16 +109,10 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
printf '%s' "$DEPLOY_SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_ed25519
|
printf '%s' "$DEPLOY_SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_ed25519
|
||||||
chmod 600 ~/.ssh/id_ed25519
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
ssh-keyscan -p "${DEPLOY_PORT:-22}" "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null || true
|
||||||
|
|
||||||
- name: Deploy
|
- name: Deploy
|
||||||
run: |
|
run: |
|
||||||
test -n "$DEPLOY_HOST" || (echo "DEPLOY_HOST must be set" && exit 1)
|
test -n "$DEPLOY_HOST" || (echo "DEPLOY_HOST must be set" && exit 1)
|
||||||
bash scripts/deploy.sh
|
test -n "$DEPLOY_SSH_PRIVATE_KEY" || (echo "DEPLOY_SSH_PRIVATE_KEY must be set" && exit 1)
|
||||||
|
bash scripts/deploy-forgejo.sh
|
||||||
- name: Health check
|
|
||||||
if: ${{ secrets.EXTERNAL_HEALTHCHECK_URL != '' }}
|
|
||||||
run: |
|
|
||||||
python scripts/healthcheck.py "${{ secrets.EXTERNAL_HEALTHCHECK_URL }}" \
|
|
||||||
--timeout 120 \
|
|
||||||
--expect-status ok \
|
|
||||||
--expect-environment production
|
|
||||||
111
scripts/deploy-forgejo.sh
Executable file
111
scripts/deploy-forgejo.sh
Executable file
@@ -0,0 +1,111 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -Eeuo pipefail
|
||||||
|
|
||||||
|
# Deploy script for Forgejo Actions
|
||||||
|
# Uses Forgejo environment variables instead of GitLab CI variables
|
||||||
|
|
||||||
|
: "${DEPLOY_USER:?DEPLOY_USER is required}"
|
||||||
|
: "${DEPLOY_HOST:?DEPLOY_HOST is required}"
|
||||||
|
: "${APP_IMAGE:?APP_IMAGE is required}"
|
||||||
|
: "${DEPLOY_SSH_PRIVATE_KEY:?DEPLOY_SSH_PRIVATE_KEY is required}"
|
||||||
|
|
||||||
|
DEPLOY_PORT="${DEPLOY_PORT:-22}"
|
||||||
|
DEPLOY_PATH="${DEPLOY_PATH:-/opt/vault-dash}"
|
||||||
|
COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.deploy.yml}"
|
||||||
|
COMPOSE_SERVICE="${COMPOSE_SERVICE:-vault-dash}"
|
||||||
|
DEPLOY_TIMEOUT="${DEPLOY_TIMEOUT:-120}"
|
||||||
|
HEALTHCHECK_URL="${HEALTHCHECK_URL:-http://127.0.0.1:${APP_PORT:-8000}/health}"
|
||||||
|
REMOTE_ENV_FILE="${REMOTE_ENV_FILE:-$DEPLOY_PATH/.env}"
|
||||||
|
|
||||||
|
SSH_OPTS=(-p "$DEPLOY_PORT" -o StrictHostKeyChecking=accept-new)
|
||||||
|
|
||||||
|
REMOTE_TARGET="${DEPLOY_USER}@${DEPLOY_HOST}"
|
||||||
|
|
||||||
|
echo "=== Deploying vault-dash ==="
|
||||||
|
echo "Host: $DEPLOY_HOST:$DEPLOY_PORT"
|
||||||
|
echo "Path: $DEPLOY_PATH"
|
||||||
|
echo "Image: $APP_IMAGE"
|
||||||
|
|
||||||
|
# Create deployment directory
|
||||||
|
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" "mkdir -p '$DEPLOY_PATH'"
|
||||||
|
|
||||||
|
# Write environment file
|
||||||
|
echo "=== Writing environment file ==="
|
||||||
|
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" "cat > '$REMOTE_ENV_FILE'" <<EOF
|
||||||
|
APP_IMAGE=$APP_IMAGE
|
||||||
|
APP_ENV=${APP_ENV:-production}
|
||||||
|
APP_NAME=${APP_NAME:-Vault Dashboard}
|
||||||
|
APP_PORT=${APP_PORT:-8000}
|
||||||
|
APP_BIND_ADDRESS=${APP_BIND_ADDRESS:-127.0.0.1}
|
||||||
|
REDIS_URL=${REDIS_URL:-}
|
||||||
|
DEFAULT_SYMBOL=${DEFAULT_SYMBOL:-GLD}
|
||||||
|
CACHE_TTL=${CACHE_TTL:-300}
|
||||||
|
WEBSOCKET_INTERVAL_SECONDS=${WEBSOCKET_INTERVAL_SECONDS:-5}
|
||||||
|
NICEGUI_MOUNT_PATH=${NICEGUI_MOUNT_PATH:-/}
|
||||||
|
NICEGUI_STORAGE_SECRET=${NICEGUI_STORAGE_SECRET:-}
|
||||||
|
CORS_ORIGINS=${CORS_ORIGINS:-*}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Upload docker-compose file
|
||||||
|
echo "=== Uploading docker-compose.deploy.yml ==="
|
||||||
|
scp "${SSH_OPTS[@]}" docker-compose.deploy.yml "$REMOTE_TARGET:$DEPLOY_PATH/$COMPOSE_FILE"
|
||||||
|
|
||||||
|
# Deploy on remote
|
||||||
|
echo "=== Running deployment on remote ==="
|
||||||
|
ssh "${SSH_OPTS[@]}" "$REMOTE_TARGET" "bash -s" <<EOF
|
||||||
|
set -Eeuo pipefail
|
||||||
|
|
||||||
|
cd "$DEPLOY_PATH"
|
||||||
|
|
||||||
|
# Save current image for rollback
|
||||||
|
if [[ -f .last_successful_image ]]; then
|
||||||
|
PREVIOUS_IMAGE="\$(cat .last_successful_image)"
|
||||||
|
echo "Previous image: \$PREVIOUS_IMAGE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Pull new image
|
||||||
|
echo "Pulling $APP_IMAGE..."
|
||||||
|
docker pull "$APP_IMAGE" || {
|
||||||
|
echo "Failed to pull image"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Save new image for rollback
|
||||||
|
echo "$APP_IMAGE" > .last_successful_image
|
||||||
|
|
||||||
|
# Stop existing container
|
||||||
|
echo "Stopping existing containers..."
|
||||||
|
docker compose -f "$COMPOSE_FILE" --env-file "$REMOTE_ENV_FILE" down --remove-orphans || true
|
||||||
|
|
||||||
|
# Start new container
|
||||||
|
echo "Starting new container..."
|
||||||
|
docker compose -f "$COMPOSE_FILE" --env-file "$REMOTE_ENV_FILE" up -d --remove-orphans
|
||||||
|
|
||||||
|
# Wait for health check
|
||||||
|
echo "Waiting for health check..."
|
||||||
|
COUNTER=0
|
||||||
|
MAX_WAIT=$DEPLOY_TIMEOUT
|
||||||
|
while [ \$COUNTER -lt \$MAX_WAIT ]; do
|
||||||
|
if curl -sf "${HEALTHCHECK_URL}" > /dev/null 2>&1; then
|
||||||
|
echo "Health check passed after \${COUNTER}s"
|
||||||
|
echo "=== Deployment successful ==="
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
COUNTER=\$((COUNTER + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Health check failed - rollback
|
||||||
|
echo "Health check failed after \${MAX_WAIT}s"
|
||||||
|
|
||||||
|
if [[ -n "\${PREVIOUS_IMAGE:-}" ]]; then
|
||||||
|
echo "Rolling back to \$PREVIOUS_IMAGE..."
|
||||||
|
sed -i.bak "s|^APP_IMAGE=.*|APP_IMAGE=\$PREVIOUS_IMAGE|" "$REMOTE_ENV_FILE"
|
||||||
|
docker compose -f "$COMPOSE_FILE" --env-file "$REMOTE_ENV_FILE" up -d --remove-orphans
|
||||||
|
echo "Rollback complete"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 1
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "=== Deployment complete ==="
|
||||||
Reference in New Issue
Block a user