2024-11-17 14:43:48 +01:00
|
|
|
name: PR Deployment cleanup
|
2024-11-16 23:24:00 +01:00
|
|
|
|
|
|
|
on:
|
|
|
|
pull_request:
|
|
|
|
types: [opened, synchronize, reopened, closed]
|
|
|
|
|
2025-01-02 19:22:14 +01:00
|
|
|
permissions:
|
|
|
|
contents: read
|
2024-11-16 23:24:00 +01:00
|
|
|
|
|
|
|
env:
|
2025-01-13 23:26:05 +01:00
|
|
|
SERVER_IP: ${{ secrets.VPS_IP }} # Add this to your GitHub secrets
|
|
|
|
CLEANUP_PERFORMED: "false" # Add flag to track if cleanup occurred
|
2024-11-16 23:24:00 +01:00
|
|
|
|
|
|
|
jobs:
|
|
|
|
cleanup:
|
|
|
|
runs-on: ubuntu-latest
|
2024-12-22 00:33:41 +01:00
|
|
|
permissions:
|
|
|
|
contents: write
|
|
|
|
pull-requests: write
|
2024-11-16 23:24:00 +01:00
|
|
|
if: github.event.action == 'closed'
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
steps:
|
2024-12-21 13:28:35 +01:00
|
|
|
- name: Harden Runner
|
2025-01-21 12:11:01 +01:00
|
|
|
uses: step-security/harden-runner@cb605e52c26070c328afc4562f0b4ada7618a84e # v2.10.4
|
2024-12-21 13:28:35 +01:00
|
|
|
with:
|
|
|
|
egress-policy: audit
|
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
- name: Set up SSH
|
|
|
|
run: |
|
|
|
|
mkdir -p ~/.ssh/
|
|
|
|
echo "${{ secrets.VPS_SSH_KEY }}" > ../private.key
|
|
|
|
sudo chmod 600 ../private.key
|
|
|
|
|
|
|
|
- name: Cleanup PR deployment
|
2024-11-20 09:41:29 +01:00
|
|
|
id: cleanup
|
2024-11-16 23:24:00 +01:00
|
|
|
run: |
|
2025-01-30 19:40:05 +01:00
|
|
|
ssh -i ../private.key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -T ${{ secrets.VPS_USERNAME }}@${{ secrets.VPS_HOST }} << 'ENDSSH'
|
2024-11-16 23:24:00 +01:00
|
|
|
if [ -d "/stirling/PR-${{ github.event.pull_request.number }}" ]; then
|
|
|
|
echo "Found PR directory, proceeding with cleanup..."
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
# Stop and remove containers
|
|
|
|
cd /stirling/PR-${{ github.event.pull_request.number }}
|
|
|
|
docker-compose down || true
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
# Go back to root before removal
|
|
|
|
cd /
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
# Remove PR-specific directories
|
|
|
|
rm -rf /stirling/PR-${{ github.event.pull_request.number }}
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-16 23:24:00 +01:00
|
|
|
# Remove the Docker image
|
2024-11-17 14:43:48 +01:00
|
|
|
docker rmi --no-prune ${{ secrets.DOCKER_HUB_USERNAME }}/test:pr-${{ github.event.pull_request.number }} || true
|
2024-12-21 15:34:07 +01:00
|
|
|
|
2024-11-20 09:41:29 +01:00
|
|
|
echo "PERFORMED_CLEANUP"
|
2024-11-16 23:24:00 +01:00
|
|
|
else
|
|
|
|
echo "PR directory not found, nothing to clean up"
|
2024-11-20 09:41:29 +01:00
|
|
|
echo "NO_CLEANUP_NEEDED"
|
2024-11-16 23:24:00 +01:00
|
|
|
fi
|
|
|
|
ENDSSH
|