Update main.yml #12
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: | | |
| # Clear any previously loaded keys | |
| ssh-add -D | |
| # Set up the SSH directory and key | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/deployment_key | |
| chmod 600 ~/.ssh/deployment_key | |
| eval "$(ssh-agent -s)" | |
| echo "$VM_PASSPHRASE" | ssh-add ~/.ssh/deployment_key | |
| - name: Configure SSH Settings | |
| run: | | |
| echo -e "Host *\n IdentitiesOnly yes\n StrictHostKeyChecking accept-new\n LogLevel VERBOSE" > ~/.ssh/config | |
| - 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 -F ~/.ssh/config -o LogLevel=DEBUG" | |
| run: | | |
| rsync -avz --checksum --exclude='.env' --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!" |