diff --git a/README.md b/README.md index c356290..7918407 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ jobs: | `project-name` | Name of the project for identification | No | `${{ github.repository }}` | | `environment-variables` | Environment variables (KEY=value format, newline separated) | No | - | | `docker-compose-path` | Path to docker-compose.yml file (will be detected automatically otherwise) | No | - | +| `force-delete-project` | Delete original project if exists, to allow a clean deployment | No | `false` | ## Setting up Secrets diff --git a/action.yaml b/action.yaml index d2cf01a..6668cad 100644 --- a/action.yaml +++ b/action.yaml @@ -1,4 +1,4 @@ -name: Deploy on Hostinger VPS +name: Deploy on Hostinger VPS Project Down author: Hostinger description: Deploy your dockerized application on Hostinger VPS instance @@ -25,6 +25,10 @@ inputs: description: 'Relative path to docker-compose.yml file from repository root' required: false default: "" + force-delete-project: + description: 'Force delete the running project if exists, before deployment' + required: false + default: "false" runs: using: composite @@ -52,6 +56,58 @@ runs: echo "url=$URL" >> $GITHUB_OUTPUT + - name: Delete project if force-delete-project enabled + if: inputs.force-delete-project == 'true' + shell: bash + run: | + echo "đŸ—‘ī¸ Force delete enabled. Attempting to delete running container..." + echo "đŸ“Ļ Project: ${{ inputs.project-name }}" + echo "đŸ–Ĩī¸ VM: ${{ inputs.virtual-machine }}" + + RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE \ + -H "Authorization: Bearer ${{ inputs.api-key }}" \ + -H "Content-Type: application/json" \ + "https://developers.hostinger.com/api/vps/v1/virtual-machines/${{ inputs.virtual-machine }}/docker/${{ inputs.project-name }}/down") + + # Extract response body and status code + HTTP_BODY=$(echo "$RESPONSE" | sed '$d') + HTTP_STATUS=$(echo "$RESPONSE" | tail -n1) + + # Check if API response + if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then + echo "✅ Container deleted successfully!" + else + echo "âš ī¸ Failed to delete container with status code: $HTTP_STATUS" + + # Try to parse error message + if command -v jq &> /dev/null && [ -n "$HTTP_BODY" ]; then + ERROR_MSG=$(echo "$HTTP_BODY" | jq -r '.message' 2>/dev/null || echo "") + if [ -n "$ERROR_MSG" ]; then + echo "Error: $ERROR_MSG" + else + echo "$HTTP_BODY" + fi + else + echo "$HTTP_BODY" + fi + + case $HTTP_STATUS in + 401) + echo "🔑 Hint: Check if your API key is valid and has the necessary permissions." + ;; + 404) + echo "🔍 Hint: Verify that the virtual machine ID '${{ inputs.virtual-machine }}' exists." + ;; + 422) + echo "âš ī¸ Hint: The request data might be invalid. Check your docker-compose file format." + ;; + 500|502|503) + echo "🔧 Hint: Server error. The Hostinger API might be experiencing issues. Try again later." + ;; + esac + echo "â„šī¸ Continuing with deployment anyway..." + fi + - name: Deploy to Hostinger shell: bash run: | @@ -127,4 +183,4 @@ runs: echo "đŸ–Ĩī¸ VM: ${{ inputs.virtual-machine }}" echo "🔄 Commit: ${{ github.sha }}" echo "👤 Triggered by: ${{ github.actor }}" - echo "======================================" \ No newline at end of file + echo "======================================"