name: Deploy to Remote Server on: # Trigger after successful build (using workflow_run) workflow_run: workflows: ["Build and Deploy Docker Image"] types: - completed branches: ["main"] # Allow manual trigger for redeployment workflow_dispatch: inputs: image_tag: description: "Image tag to deploy (default: latest)" required: false default: "latest" env: GITEA_INSTANCE_URL: git.deowl.ru IMAGE_NAME: vkrb/quantum_backend REMOTE_HOST: ${{ secrets.REMOTE_HOST }} REMOTE_USER: ${{ secrets.REMOTE_USER }} COMPOSE_PATH: /usr/local/share/vkrb/quantum_backend # Path to docker-compose.yml on remote server jobs: deploy: # Only run if the build workflow succeeded if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} runs-on: ubuntu-latest steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.REMOTE_HOST }} username: ${{ secrets.REMOTE_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} script_stop: true script: | cd ${{ env.COMPOSE_PATH }} # Get the tag to deploy if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then TAG="${{ github.event.inputs.image_tag }}" else TAG="${{ github.event.workflow_run.head_sha }}" fi # Use 'latest' if no specific tag if [ -z "$TAG" ]; then TAG="latest" fi echo "Deploying tag: $TAG" # Method 1: Using environment variable (recommended) export IMAGE_TAG=$TAG docker compose pull app # Pull only the app service # Redeploy with the new image docker compose up -d --no-deps --force-recreate app # Clean up old images docker image prune -f # Check container status docker compose ps