Merge branch 'master' of https://github.com/3xjn/TodoApp #91
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: Build and Push Docker Image to DigitalOcean Droplet | |
on: | |
push: | |
branches: | |
- master | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Cache Docker layers | |
id: docker-cache | |
uses: actions/cache@v3 | |
with: | |
path: | | |
/tmp/.docker-cache | |
key: "${{ runner.os }}-docker-${{ hashFiles('**/Dockerfile', '**/*.csproj') }}" | |
restore-keys: | | |
${{ runner.os }}-docker- | |
- name: Set up Docker Image Tag | |
run: | | |
echo "IMAGE_TAG=todoapp:$(date +%s)-${GITHUB_SHA::8}" >> $GITHUB_ENV | |
- name: Build Docker image | |
run: | | |
docker build --cache-from=type=local,src=/tmp/.docker-cache -t "${{ env.IMAGE_TAG }}" . | |
echo "Docker image built successfully:" | |
docker image ls --format "{{.Repository}}:{{.Tag}}" | grep "todoapp" || echo "Warning: Image not found in list, but build completed" | |
- name: Save Docker image to a tar file | |
run: | | |
docker save ${{ env.IMAGE_TAG }} -o image.tar | |
ls -lh image.tar | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.DROPLET_IP }} >> ~/.ssh/known_hosts | |
- name: Transfer docker image and docker-compose.yml | |
run: | | |
scp -i ~/.ssh/id_rsa image.tar ${{ secrets.DROPLET_USERNAME }}@${{ secrets.DROPLET_IP }}:/home/${{ secrets.DROPLET_USERNAME }}/image.tar | |
scp -i ~/.ssh/id_rsa docker-compose.yml ${{ secrets.DROPLET_USERNAME }}@${{ secrets.DROPLET_IP }}:/home/${{ secrets.DROPLET_USERNAME }}/docker-compose.yml | |
- name: SSH into Droplet and Deploy using Docker Compose | |
run: | | |
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=10 -i ~/.ssh/id_rsa ${{ secrets.DROPLET_USERNAME }}@${{ secrets.DROPLET_IP }} << EOF | |
set -e | |
echo "Starting deployment process..." | |
# Change to the directory where docker-compose.yml is located | |
cd /home/${{ secrets.DROPLET_USERNAME }} | |
echo "Current Directory: \$(pwd)" | |
echo "Contents of the Directory:" | |
ls -l | |
# Stop and remove the existing container | |
echo "Stopping existing container if it exists..." | |
if [ "\$(sudo docker ps -q -f name=todo-app)" ]; then | |
sudo docker-compose down | |
fi | |
echo "Loading Docker image..." | |
if [ -f /home/${{ secrets.DROPLET_USERNAME }}/image.tar ]; then | |
sudo docker load -i /home/${{ secrets.DROPLET_USERNAME }}/image.tar | |
else | |
echo "image.tar not found, skipping load step" | |
fi | |
echo "Loaded Docker images:" | |
sudo docker images | |
echo "Running Docker Compose..." | |
export IMAGE_TAG=${{ env.IMAGE_TAG }} | |
export CERT_PEM=\$(sudo cat /etc/letsencrypt/live/3xjn.dev/fullchain.pem) | |
export KEY_PEM=\$(sudo cat /etc/letsencrypt/live/3xjn.dev/privkey.pem) | |
# Export MongoDB environment variables | |
export MONGO_CONNECTION_STRING="${{ secrets.MONGO_CONNECTION_STRING }}" | |
export MONGO_DATABASE_NAME="${{ secrets.MONGO_DATABASE_NAME }}" | |
export MONGO_COLLECTION_NAME="${{ secrets.MONGO_COLLECTION_NAME }}" | |
# Run the Docker Compose command | |
sudo IMAGE_TAG=\$IMAGE_TAG CERT_PEM="\$CERT_PEM" KEY_PEM="\$KEY_PEM" \ | |
MONGO_CONNECTION_STRING="\$MONGO_CONNECTION_STRING" \ | |
MONGO_DATABASE_NAME="\$MONGO_DATABASE_NAME" \ | |
MONGO_COLLECTION_NAME="\$MONGO_COLLECTION_NAME" \ | |
docker-compose up -d | |
echo "Cleaning up..." | |
rm -f /home/${{ secrets.DROPLET_USERNAME }}/image.tar | |
echo "Deployment completed successfully!" | |
EOF |