Deploy to EC2 #33
This file contains 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: Deploy to EC2 | |
on: | |
workflow_run: | |
workflows: ["Docker Build and Push"] | |
types: | |
- completed | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up SSH | |
run: | | |
# Create the .ssh directory if it doesn't exist | |
mkdir -p ~/.ssh | |
# Store the private key from GitHub secrets as the SSH key | |
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
# Add the EC2 host to known hosts to avoid host verification errors | |
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts | |
- name: Deploy Docker Compose to EC2 | |
run: | | |
# Run the SSH command with the correct private key and host | |
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no workflow@${{ secrets.EC2_HOST }} << 'EOF' | |
# Navigate to the smart-pesa directory | |
cd smartpesa || exit | |
# Pull and start the Docker containers in detached mode | |
docker-compose pull | |
docker-compose up -d | |
EOF |