Update main.yml #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deployment CAPEC-Rag | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| deploy-to-vm: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v3 | |
| - name: Install expect | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y expect | |
| - name: Set up SSH with passphrase | |
| env: | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| VM_PASSPHRASE: ${{ secrets.VM_PASSPHRASE }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/deployment_key | |
| chmod 600 ~/.ssh/deployment_key | |
| eval "$(ssh-agent -s)" | |
| # Clear the agent to ensure no other keys are used | |
| ssh-add -D | |
| # Add only the correct key to the agent | |
| ssh-add ~/.ssh/deployment_key | |
| # Use expect to send the passphrase for the key | |
| expect -c " | |
| spawn ssh-add ~/.ssh/deployment_key | |
| expect \"Enter passphrase for ~/.ssh/deployment_key:\" | |
| send \"$VM_PASSPHRASE\r\" | |
| interact | |
| " | |
| - name: Add VM to known_hosts | |
| run: | | |
| ssh-keyscan -H ${{ secrets.VM_HOST }} >> ~/.ssh/known_hosts | |
| - name: Synchronize Code with VM Using rsync | |
| env: | |
| RSYNC_RSH: "ssh -i ~/.ssh/deployment_key -o IdentitiesOnly=yes" # Explicitly specify the SSH key and disable other key attempts | |
| run: | | |
| rsync -avz --checksum --exclude='.git' ./ ${{ secrets.VM_USER }}@${{ secrets.VM_HOST }}:/home/dev/ | |
| - name: Restart Docker Compose Services | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.VM_HOST }} | |
| username: ${{ secrets.VM_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| echo "Stopping existing Docker Compose services..." | |
| cd /home/dev/ | |
| sudo docker-compose down | |
| echo "Starting Docker Compose services..." | |
| sudo docker-compose build --no-cache | |
| sudo docker-compose up -d | |
| echo "Successfully deployed the application!" |