Update main.yml #8
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 SSH Agent and Tools | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y openssh-client | |
| - 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)" | |
| # Add the private key to the agent and provide the passphrase non-interactively | |
| echo "$VM_PASSPHRASE" | ssh-add ~/.ssh/deployment_key | |
| - 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 for rsync | |
| 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!" |