name: Docker Build on: push: branches: - main - master tags: - 'v*' workflow_dispatch: jobs: build-and-push: name: Build and Push Docker Image runs-on: act-runner-4c6g env: RUNNER_TOOL_CACHE: /toolcache steps: - name: Install Docker CLI run: | if ! command -v docker &> /dev/null; then if command -v apk &> /dev/null; then apk add --no-cache docker-cli elif command -v apt-get &> /dev/null; then apt-get update && apt-get install -y docker.io else curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-24.0.7.tgz | tar xz -C /tmp mv /tmp/docker/docker /usr/local/bin/ chmod +x /usr/local/bin/docker fi fi docker --version - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Resolve tag & write VERSION id: version run: | if echo "${{ github.ref }}" | grep -q "^refs/tags/"; then TAG=${GITHUB_REF#refs/tags/} else SHORT_SHA=$(git rev-parse --short HEAD) TAG="dev-${SHORT_SHA}" fi echo "TAG=${TAG}" >> $GITHUB_ENV echo "${TAG}" > VERSION echo "Building tag: ${TAG}" cat VERSION - name: Login to Gitea Container Registry run: | echo "${{ secrets.PACKAGES_TOKEN }}" | docker login git.viaeon.com -u "${{ github.actor }}" --password-stdin - name: Build Docker image run: | echo "Building image with tag: ${{ env.TAG }}" docker build \ --label "org.opencontainers.image.source=https://git.viaeon.com/admin/new-api" \ --label "org.opencontainers.image.revision=${{ github.sha }}" \ -t git.viaeon.com/admin/new-api:${{ env.TAG }} \ -t git.viaeon.com/admin/new-api:latest . - name: Push Docker image run: | echo "Pushing ${{ env.TAG }}..." docker push git.viaeon.com/admin/new-api:${{ env.TAG }} echo "Pushing latest..." docker push git.viaeon.com/admin/new-api:latest - name: Cleanup Docker if: always() run: | echo "Removing local images..." docker rmi git.viaeon.com/admin/new-api:${{ env.TAG }} git.viaeon.com/admin/new-api:latest 2>/dev/null || true echo "Pruning unused Docker resources..." docker system prune -af --volumes 2>/dev/null || true echo "Docker disk usage:" docker system df - name: Deploy via SSH if: success() run: | if [ -z "${{ secrets.DEPLOY_SSH_HOST }}" ]; then echo "DEPLOY_SSH_HOST not set, skip deploy" exit 0 fi apk add --no-cache sshpass 2>/dev/null || apt-get update && apt-get install -y sshpass 2>/dev/null || true sshpass -p "${{ secrets.DEPLOY_SSH_PASS }}" ssh -o StrictHostKeyChecking=no -p ${{ secrets.DEPLOY_SSH_PORT || 22 }} ${{ secrets.DEPLOY_SSH_USER }}@${{ secrets.DEPLOY_SSH_HOST }} "cd ${{ secrets.DEPLOY_DIR || '/opt/new-api' }} && docker compose pull && docker compose up -d"