Skip to content

Merge pull request #24 from bescka/workflows #4

Merge pull request #24 from bescka/workflows

Merge pull request #24 from bescka/workflows #4

name: Deploy on Push
on:
push:
branches:
- main
jobs:
run_pull:
name: Run Pull and Manage Containers
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install SSH keys
run: |
echo "Installing SSH keys..."
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "${{ secrets.EC2_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
echo "SSH keys installed."
- name: Debug List .ssh directory contents
run: |
echo "Listing ~/.ssh directory contents..."
ls -la ~/.ssh
- name: Create .ssh directory on remote server
run: |
echo "Creating .ssh directory on remote server..."
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "mkdir -p ~/.ssh && chmod 700 ~/.ssh"
echo ".ssh directory created on remote server."
- name: Pull latest code
run: |
echo "Pulling latest code from remote repository..."
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull"
echo "Code pulled successfully."
- name: Manage Docker Compose on Remote Server
run: |
echo "Managing Docker Compose on remote server..."
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
set -e
cd ${{ secrets.WORK_DIR }}
echo "Stopping and removing Docker containers..."
docker-compose down
echo "Removing Docker images..."
docker rmi file_uploader-backend:latest || true
docker rmi file_uploader-frontend:latest || true
echo "Starting Docker containers..."
docker-compose up -d
echo "Containers started."
EOF
echo "Docker Compose managed successfully."
- name: Cleanup SSH keys
run: |
echo "Cleaning up SSH keys..."
rm -rf ~/.ssh/id_ed25519
echo "SSH keys cleaned up."