CAPEC Rag Deployment #3
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)" | |
| # Use expect to send the passphrase | |
| expect -c " | |
| spawn ssh-add ~/.ssh/deployment_key | |
| expect \"Enter passphrase for ~/.ssh/deployment_key:\" | |
| send \"$VM_PASSPHRASE\r\" | |
| interact | |
| " | |
| - name: Add Azure 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" | |
| 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!" |