This commit is contained in:
2025-10-21 21:07:03 +02:00
parent 21d0f4b2fc
commit 70997f6795

View File

@@ -8,10 +8,11 @@ on:
types: [opened, synchronize, reopened, closed] types: [opened, synchronize, reopened, closed]
env: env:
REMOTE_DEPLOY_PATH: /var/www/app # Change this to your remote deploy base path REMOTE_DEPLOY_PATH: /var/www/app # Change to your remote deploy base path
SSH_HOST: ${{ secrets.SSH_HOST }} SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_USER: ${{ secrets.SSH_USER }} SSH_USER: ${{ secrets.SSH_USER }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_PORT: ${{ secrets.SSH_PORT || 22 }} # Default to port 22 if not set
jobs: jobs:
prepare_deployment_vars: prepare_deployment_vars:
@@ -23,8 +24,8 @@ jobs:
- name: Set deployment variables - name: Set deployment variables
id: set-vars id: set-vars
run: | run: |
if [[ "${{ gitea.event_name }}" == "pull_request" ]]; then if [[ "${{ github.event_name }}" == "pull_request" ]]; then
DEPLOY_DIR_NAME="pr-${{ gitea.event.pull_request.number }}" DEPLOY_DIR_NAME="pr-${{ github.event.pull_request.number }}"
else else
DEPLOY_DIR_NAME="main" DEPLOY_DIR_NAME="main"
fi fi
@@ -37,8 +38,8 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: prepare_deployment_vars needs: prepare_deployment_vars
if: | if: |
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false) (github.event_name == 'pull_request' && github.event.action != 'closed' && github.event.pull_request.merged == false)
|| gitea.ref == 'refs/heads/main' || github.ref == 'refs/heads/main'
steps: steps:
- name: Setup SSH - name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0 uses: webfactory/ssh-agent@v0.9.0
@@ -48,18 +49,24 @@ jobs:
- name: Add host to known_hosts - name: Add host to known_hosts
run: | run: |
mkdir -p ~/.ssh mkdir -p ~/.ssh
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts chmod 700 ~/.ssh
echo "Scanning SSH host key for $SSH_HOST:$SSH_PORT..."
ssh-keyscan -p "$SSH_PORT" -T 10 -H "$SSH_HOST" >> ~/.ssh/known_hosts 2>/dev/null || {
echo "::error::Failed to ssh-keyscan $SSH_HOST:$SSH_PORT"
exit 1
}
chmod 644 ~/.ssh/known_hosts
- name: Create directory on remote - name: Create directory on remote
run: ssh $SSH_USER@$SSH_HOST "mkdir -p ${{ needs.prepare_deployment_vars.outputs.deploy_path }}" run: ssh -p "$SSH_PORT" $SSH_USER@$SSH_HOST "mkdir -p ${{ needs.prepare_deployment_vars.outputs.deploy_path }}"
sync_repo_files: sync_repo_files:
name: Sync repository files name: Sync repository files
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: create_remote_directory needs: create_remote_directory
if: | if: |
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false) (github.event_name == 'pull_request' && github.event.action != 'closed' && github.event.pull_request.merged == false)
|| gitea.ref == 'refs/heads/main' || github.ref == 'refs/heads/main'
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -71,20 +78,29 @@ jobs:
- name: Add host to known_hosts - name: Add host to known_hosts
run: | run: |
mkdir -p ~/.ssh mkdir -p ~/.ssh
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts chmod 700 ~/.ssh
echo "Scanning SSH host key for $SSH_HOST:$SSH_PORT..."
ssh-keyscan -p "$SSH_PORT" -T 10 -H "$SSH_HOST" >> ~/.ssh/known_hosts 2>/dev/null || {
echo "::error::Failed to ssh-keyscan $SSH_HOST:$SSH_PORT"
exit 1
}
chmod 644 ~/.ssh/known_hosts
- name: Install rsync
run: sudo apt-get update && sudo apt-get install -y rsync
- name: Sync files via rsync - name: Sync files via rsync
run: | run: |
sudo apt-get update && sudo apt-get install -y rsync echo "Syncing repository files to ${{ needs.prepare_deployment_vars.outputs.deploy_path }}..."
rsync -avz --delete -e "ssh" . $SSH_USER@$SSH_HOST:${{ needs.prepare_deployment_vars.outputs.deploy_path }}/ rsync -avz --delete -e "ssh -p $SSH_PORT" . $SSH_USER@$SSH_HOST:${{ needs.prepare_deployment_vars.outputs.deploy_path }}/
run_docker_compose: run_docker_compose:
name: Run docker-compose remotely name: Run docker-compose remotely
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: sync_repo_files needs: sync_repo_files
if: | if: |
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false) (github.event_name == 'pull_request' && github.event.action != 'closed' && github.event.pull_request.merged == false)
|| gitea.ref == 'refs/heads/main' || github.ref == 'refs/heads/main'
steps: steps:
- name: Setup SSH - name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0 uses: webfactory/ssh-agent@v0.9.0
@@ -94,18 +110,24 @@ jobs:
- name: Add host to known_hosts - name: Add host to known_hosts
run: | run: |
mkdir -p ~/.ssh mkdir -p ~/.ssh
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts chmod 700 ~/.ssh
echo "Scanning SSH host key for $SSH_HOST:$SSH_PORT..."
ssh-keyscan -p "$SSH_PORT" -T 10 -H "$SSH_HOST" >> ~/.ssh/known_hosts 2>/dev/null || {
echo "::error::Failed to ssh-keyscan $SSH_HOST:$SSH_PORT"
exit 1
}
chmod 644 ~/.ssh/known_hosts
- name: Run docker-compose on remote host - name: Run docker-compose on remote host
run: ssh $SSH_USER@$SSH_HOST "cd ${{ needs.prepare_deployment_vars.outputs.deploy_path }} && docker-compose up -d --build" run: ssh -p "$SSH_PORT" $SSH_USER@$SSH_HOST "cd ${{ needs.prepare_deployment_vars.outputs.deploy_path }} && docker-compose up -d --build"
cleanup_mr_environment: cleanup_mr_environment:
name: Cleanup MR environment name: Cleanup MR environment
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: prepare_deployment_vars needs: prepare_deployment_vars
if: | if: |
gitea.event_name == 'pull_request' && github.event_name == 'pull_request' &&
(gitea.event.action == 'closed' || gitea.event.pull_request.merged == true) (github.event.action == 'closed' || github.event.pull_request.merged == true)
steps: steps:
- name: Setup SSH - name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0 uses: webfactory/ssh-agent@v0.9.0
@@ -115,9 +137,15 @@ jobs:
- name: Add host to known_hosts - name: Add host to known_hosts
run: | run: |
mkdir -p ~/.ssh mkdir -p ~/.ssh
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts chmod 700 ~/.ssh
echo "Scanning SSH host key for $SSH_HOST:$SSH_PORT..."
ssh-keyscan -p "$SSH_PORT" -T 10 -H "$SSH_HOST" >> ~/.ssh/known_hosts 2>/dev/null || {
echo "::error::Failed to ssh-keyscan $SSH_HOST:$SSH_PORT"
exit 1
}
chmod 644 ~/.ssh/known_hosts
- name: Delete deployment directory - name: Delete deployment directory
run: | run: |
ssh $SSH_USER@$SSH_HOST "if [ -d '${{ needs.prepare_deployment_vars.outputs.deploy_path }}' ]; then rm -rf '${{ needs.prepare_deployment_vars.outputs.deploy_path }}'; echo 'Directory removed.'; else echo 'Directory not found, skipping.'; fi" ssh -p "$SSH_PORT" $SSH_USER@$SSH_HOST "if [ -d '${{ needs.prepare_deployment_vars.outputs.deploy_path }}' ]; then rm -rf '${{ needs.prepare_deployment_vars.outputs.deploy_path }}'; echo 'Directory removed.'; else echo 'Directory not found, skipping.'; fi"