From 802746cf5eb80550cc74713b666eda54881433ac Mon Sep 17 00:00:00 2001 From: Darrel Pol Date: Wed, 30 Oct 2024 23:31:55 -0500 Subject: [PATCH 1/3] adding dockerfile --- Dockerfile | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..d45c8c3c5 --- /dev/null +++ b/Dockerfile @@ -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 it’s created at build time +# COPY .env ./.env + +# Run the application +CMD ["python", "launch.py", "--nowebui", "--deforum-api", "--api", "--skip-torch-cuda-test"] From 0be97a48c5c7209da9dc4e6419c7d1c06557f8a8 Mon Sep 17 00:00:00 2001 From: Darrel Pol Date: Sat, 2 Nov 2024 14:48:02 -0500 Subject: [PATCH 2/3] adding deploy to staging workflow --- .github/workflows/on_merge_to_master.yaml | 7 +- .../workflows/on_push_deploy_to_staging.yaml | 92 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/on_push_deploy_to_staging.yaml diff --git a/.github/workflows/on_merge_to_master.yaml b/.github/workflows/on_merge_to_master.yaml index 2a7a4281c..34ba5b0df 100644 --- a/.github/workflows/on_merge_to_master.yaml +++ b/.github/workflows/on_merge_to_master.yaml @@ -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 diff --git a/.github/workflows/on_push_deploy_to_staging.yaml b/.github/workflows/on_push_deploy_to_staging.yaml new file mode 100644 index 000000000..dfe495941 --- /dev/null +++ b/.github/workflows/on_push_deploy_to_staging.yaml @@ -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 From 0aebb9dfb020ebb55f2e968acd5ad7e5836c8d54 Mon Sep 17 00:00:00 2001 From: Darrel Pol Date: Sat, 2 Nov 2024 14:48:41 -0500 Subject: [PATCH 3/3] renaming production workflow --- .../{on_merge_to_master.yaml => on_tag_deploy_to_production.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{on_merge_to_master.yaml => on_tag_deploy_to_production.yaml} (100%) diff --git a/.github/workflows/on_merge_to_master.yaml b/.github/workflows/on_tag_deploy_to_production.yaml similarity index 100% rename from .github/workflows/on_merge_to_master.yaml rename to .github/workflows/on_tag_deploy_to_production.yaml