Merge pull request #9 from Moonlite-Media/dockerizing

This commit is contained in:
Darrel Pol 2024-11-02 14:50:26 -05:00 committed by GitHub
commit 765d38630a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 137 additions and 3 deletions

View file

@ -0,0 +1,92 @@
name: Build and deploy to Staging
on:
push:
workflow_dispatch:
branches:
- main
paths:
- ".github/workflows/on_push_deploy_to_staging.yaml"
jobs:
deploy:
runs-on: ubuntu-latest
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
with:
aws-access-key-id: ${{ secrets.MOONLITE_AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.MOONLITE_AWS_SECRET_KEY }}
aws-region: ${{ secrets.MOONLITE_AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Create .env file
run: |
echo "SDAPI_USERNAME=${{ vars.STAGING_API_USERNAME }}" > .env
echo "SDAPI_PASSWORD=${{ vars.STAGING_API_PASSWORD }}" >> .env
- name: Build and tag Docker image
run: |
# Get the Git commit hash for tagging
IMAGE_TAG=$(echo $GITHUB_SHA | head -c 7)
# Build the Docker image
docker build -t ${{ secrets.STAGING_ECR_URI }}:$IMAGE_TAG .
# Tag the image as 'latest'
docker tag ${{ secrets.STAGING_ECR_URI }}:$IMAGE_TAG ${{ secrets.STAGING_ECR_URI }}:latest
- name: Push Docker image to ECR
run: |
# Get the Git commit hash for tagging
IMAGE_TAG=$(echo $GITHUB_SHA | head -c 7)
# Push both the specific tag and 'latest' to ECR
docker push ${{ secrets.STAGING_ECR_URI }}:$IMAGE_TAG
docker push ${{ secrets.STAGING_ECR_URI }}:latest
- name: Set up SSH Agent
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.STAGING_EC2_SSH_PRIVATE_KEY }}
- name: Copy .env to EC2 instance
run: |
scp -o StrictHostKeyChecking=no .env ${{ secrets.STAGING_EC2_USER }}@${{ secrets.STAGING_EC2_HOST }}:/home/${{ secrets.STAGING_EC2_USER }}/.env
- name: SSH into EC2 and run Docker image
run: |
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=60 -o StrictHostKeyChecking=no ${{ secrets.STAGING_EC2_USER }}@${{ secrets.STAGING_EC2_HOST }} << 'EOF1'
# Define image tag based on Git commit hash
IMAGE_TAG=$(echo $GITHUB_SHA | head -c 7)
# Log in to ECR
docker login -u AWS -p $(aws ecr get-login-password --region ${{ secrets.MOONLITE_AWS_REGION }}) ${{ secrets.STAGING_EC2_HOST }}
# Pull the latest Docker image
docker pull ${{ secrets.STAGING_ECR_URI }}:latest
# Stop and remove the current container if running
docker stop moonlite-sd || true
docker rm moonlite-sd || true
# Run the new Docker container
docker run -d -p 7861:7861 --name moonlite-sd \
--env-file /home/${{ secrets.STAGING_EC2_USER }}/.env \
${{ secrets.STAGING_ECR_URI }}:latest
# (Optional) Check if the container is running
docker ps -a
EOF1

View file

@ -1,16 +1,17 @@
name: Deploy Moonlite Stable Diffusion to EC2
on:
workflow_dispatch: # Enables manual trigger
push:
branches:
- main # Trigger on push to main branch
tags:
- "v*"
env:
STABLE_DIFFUSION_API_PORT: 7861
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout the repository

41
Dockerfile Normal file
View file

@ -0,0 +1,41 @@
# Use an official Python runtime as the base image
FROM nvidia/cuda:12.2.2-runtime-ubuntu22.04
# Set environment variables
ENV STABLE_DIFFUSION_API_PORT=7861
ENV WORKDIR /app
# Create a working directory and set permissions
RUN mkdir -p $WORKDIR
WORKDIR $WORKDIR
# Install Python, pip, and other necessary dependencies
RUN apt-get update && \
apt-get install -y python3 python3-pip git wget && \
ln -s /usr/bin/python3 /usr/bin/python && \
rm -rf /var/lib/apt/lists/*
# Clone the stable-diffusion-webui repository
RUN git clone https://github.com/Moonlite-Media/stable-diffusion-webui.git .
# Set up models folder and download required models
RUN mkdir -p models && \
cd models && \
wget -q https://huggingface.co/stabilityai/stable-diffusion-2/resolve/main/768-v-ema.safetensors
# Set up extensions folder and clone repositories
RUN mkdir -p extensions && \
cd extensions && \
git clone https://github.com/cheald/sd-webui-loractl.git && \
git clone https://github.com/Mikubill/sd-webui-controlnet && \
git clone https://github.com/deforum-art/sd-webui-deforum
# Install Python dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy the .env file from the GitHub Action if its created at build time
# COPY .env ./.env
# Run the application
CMD ["python", "launch.py", "--nowebui", "--deforum-api", "--api", "--skip-torch-cuda-test"]