forked from kovagoadi.hu/static.kovagoadi.hu
124 lines
4.1 KiB
YAML
124 lines
4.1 KiB
YAML
name: Remote Deployment Pipeline
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
pull_request:
|
|
types: [opened, synchronize, reopened, closed]
|
|
|
|
env:
|
|
REMOTE_DEPLOY_PATH: /var/www/app # Change this to your remote deploy base path
|
|
SSH_HOST: ${{ secrets.SSH_HOST }}
|
|
SSH_USER: ${{ secrets.SSH_USER }}
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
|
|
jobs:
|
|
prepare_deployment_vars:
|
|
name: Prepare deployment vars
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
deploy_path: ${{ steps.set-vars.outputs.deploy_path }}
|
|
steps:
|
|
- name: Set deployment variables
|
|
id: set-vars
|
|
run: |
|
|
if [[ "${{ gitea.event_name }}" == "pull_request" ]]; then
|
|
DEPLOY_DIR_NAME="pr-${{ gitea.event.pull_request.number }}"
|
|
else
|
|
DEPLOY_DIR_NAME="main"
|
|
fi
|
|
echo "DEPLOY_PATH=${REMOTE_DEPLOY_PATH}/${DEPLOY_DIR_NAME}" >> $GITHUB_ENV
|
|
echo "deploy_path=${REMOTE_DEPLOY_PATH}/${DEPLOY_DIR_NAME}" >> $GITHUB_OUTPUT
|
|
echo "DEPLOY_PATH will be: ${REMOTE_DEPLOY_PATH}/${DEPLOY_DIR_NAME}"
|
|
|
|
create_remote_directory:
|
|
name: Create remote directory
|
|
runs-on: ubuntu-latest
|
|
needs: prepare_deployment_vars
|
|
if: |
|
|
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false)
|
|
|| gitea.ref == 'refs/heads/main'
|
|
steps:
|
|
- name: Setup SSH
|
|
uses: webfactory/ssh-agent@v0.9.0
|
|
with:
|
|
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
|
|
|
|
- name: Add host to known_hosts
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
|
|
|
|
- name: Create directory on remote
|
|
run: ssh $SSH_USER@$SSH_HOST "mkdir -p ${{ needs.prepare_deployment_vars.outputs.deploy_path }}"
|
|
|
|
sync_repo_files:
|
|
name: Sync repository files
|
|
runs-on: ubuntu-latest
|
|
needs: create_remote_directory
|
|
if: |
|
|
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false)
|
|
|| gitea.ref == 'refs/heads/main'
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup SSH
|
|
uses: webfactory/ssh-agent@v0.9.0
|
|
with:
|
|
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
|
|
|
|
- name: Add host to known_hosts
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
|
|
|
|
- name: Sync files via rsync
|
|
run: |
|
|
sudo apt-get update && sudo apt-get install -y rsync
|
|
rsync -avz --delete -e "ssh" . $SSH_USER@$SSH_HOST:${{ needs.prepare_deployment_vars.outputs.deploy_path }}/
|
|
|
|
run_docker_compose:
|
|
name: Run docker-compose remotely
|
|
runs-on: ubuntu-latest
|
|
needs: sync_repo_files
|
|
if: |
|
|
(gitea.event_name == 'pull_request' && gitea.event.action != 'closed' && gitea.event.pull_request.merged == false)
|
|
|| gitea.ref == 'refs/heads/main'
|
|
steps:
|
|
- name: Setup SSH
|
|
uses: webfactory/ssh-agent@v0.9.0
|
|
with:
|
|
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
|
|
|
|
- name: Add host to known_hosts
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
|
|
|
|
- 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"
|
|
|
|
cleanup_mr_environment:
|
|
name: Cleanup MR environment
|
|
runs-on: ubuntu-latest
|
|
needs: prepare_deployment_vars
|
|
if: |
|
|
gitea.event_name == 'pull_request' &&
|
|
(gitea.event.action == 'closed' || gitea.event.pull_request.merged == true)
|
|
steps:
|
|
- name: Setup SSH
|
|
uses: webfactory/ssh-agent@v0.9.0
|
|
with:
|
|
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
|
|
|
|
- name: Add host to known_hosts
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
|
|
|
|
- name: Delete deployment directory
|
|
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"
|
|
|