name: Deploy to Production run-name: Deploy to Production - ${{ github.ref_name }} on: workflow_dispatch: inputs: image_tag: description: 'Image tag to deploy (leave empty for latest)' required: false default: 'latest' branch: description: 'Branch to deploy from' required: false default: 'main' auto_deploy: description: 'Auto-deploy after successful build' type: boolean required: false default: false workflow_run: workflows: ["Build Docker Image"] types: - completed branches: [main, develop] env: REGISTRY: registry.michaelschiemer.de IMAGE_NAME: framework DEPLOYMENT_HOST: 94.16.110.151 jobs: deploy: name: Deploy to Production Server runs-on: ubuntu-latest environment: name: production url: https://michaelschiemer.de # Only run if triggered manually OR if build workflow succeeded if: | github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') steps: - name: Checkout deployment scripts run: | REF_NAME="${{ github.ref_name || inputs.branch || 'main' }}" REPO="${{ github.repository }}" if [ -n "${{ secrets.CI_TOKEN }}" ]; then git clone --depth 1 --branch "$REF_NAME" \ "https://${{ secrets.CI_TOKEN }}@git.michaelschiemer.de/${REPO}.git" \ /workspace/repo else git clone --depth 1 --branch "$REF_NAME" \ "https://git.michaelschiemer.de/${REPO}.git" \ /workspace/repo || \ git clone --depth 1 \ "https://git.michaelschiemer.de/${REPO}.git" \ /workspace/repo fi cd /workspace/repo - name: Determine image tag id: image_tag shell: bash run: | # Priority: # 1. Manual input (workflow_dispatch) # 2. From workflow_run (build workflow outputs) # 3. Latest if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ inputs.image_tag }}" ]; then IMAGE_TAG="${{ inputs.image_tag }}" echo "Using manually specified tag: $IMAGE_TAG" elif [ "${{ github.event_name }}" = "workflow_run" ]; then # Try to get from build workflow run BUILD_RUN_ID="${{ github.event.workflow_run.id }}" echo "Build workflow run ID: $BUILD_RUN_ID" # Note: Getting outputs from workflow_run might need API call # For now, use latest if triggered by workflow_run IMAGE_TAG="latest" echo "Using latest tag (from workflow_run trigger)" else IMAGE_TAG="latest" echo "Using latest tag (default)" fi echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_OUTPUT echo "📦 Deploying image tag: $IMAGE_TAG" - name: Setup SSH key run: | mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/production chmod 600 ~/.ssh/production ssh-keyscan -H ${{ env.DEPLOYMENT_HOST }} >> ~/.ssh/known_hosts - name: Deploy via SSH run: | set -e DEPLOYMENT_HOST="${{ env.DEPLOYMENT_HOST }}" REGISTRY="${{ env.REGISTRY }}" IMAGE_NAME="${{ env.IMAGE_NAME }}" IMAGE_TAG="${{ steps.image_tag.outputs.IMAGE_TAG }}" FULL_IMAGE="${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}" STACK_PATH="~/deployment/stacks/application" echo "🚀 Starting deployment..." echo " Image: ${FULL_IMAGE}" echo " Tag: ${IMAGE_TAG}" echo " Host: ${DEPLOYMENT_HOST}" echo " Stack: ${STACK_PATH}" ssh -i ~/.ssh/production \ -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null \ deploy@${DEPLOYMENT_HOST} <