From 6a92f1fd8d00aa5e5dcfbbecef1c9d27428f36d7 Mon Sep 17 00:00:00 2001 From: Darrel Pol Date: Sat, 2 Nov 2024 21:43:24 -0500 Subject: [PATCH] adding production workflow --- .../on_tag_deploy_to_production.yaml | 128 ++++++++---------- 1 file changed, 58 insertions(+), 70 deletions(-) diff --git a/.github/workflows/on_tag_deploy_to_production.yaml b/.github/workflows/on_tag_deploy_to_production.yaml index ebfd1df4f..2c724834e 100644 --- a/.github/workflows/on_tag_deploy_to_production.yaml +++ b/.github/workflows/on_tag_deploy_to_production.yaml @@ -5,90 +5,78 @@ on: tags: - "v*" -env: - STABLE_DIFFUSION_API_PORT: 7861 - jobs: deploy: runs-on: ubuntu-latest environment: production + env: + IMAGE_TAG: ${{ github.sha }} steps: - - name: Checkout the repository - uses: actions/checkout@v2 + - name: Checkout code + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83 + with: + aws-access-key-id: ${{ secrets.PROD_AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.PROD_AWS_SECRET_KEY }} + aws-region: ${{ secrets.PROD_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=${{ secrets.PROD_API_USERNAME }}" > .env + echo "SDAPI_PASSWORD=${{ secrets.PROD_API_PASSWORD }}" >> .env + + - name: Build and tag Docker image + run: | + # Build the Docker image + docker build -t ${{ secrets.PROD_ECR_URI }}:$IMAGE_TAG . + + # Tag the image as 'latest' + docker tag ${{ secrets.PROD_ECR_URI }}:$IMAGE_TAG ${{ secrets.PROD_ECR_URI }}:latest + + - name: Push Docker image to ECR + run: | + # Push both the specific tag and 'latest' to ECR + docker push ${{ secrets.PROD_ECR_URI }}:$IMAGE_TAG + docker push ${{ secrets.PROD_ECR_URI }}:latest - name: Set up SSH Agent uses: webfactory/ssh-agent@v0.9.0 with: - ssh-private-key: ${{ secrets.MOONLITE_AWS_EC2_SSH_KEY }} + ssh-private-key: ${{ secrets.PROD_EC2_SSH_PRIVATE_KEY }} - - name: Create .env file + - name: Copy .env to EC2 instance run: | - echo "SDAPI_USERNAME=${{ secrets.MOONLITE_SDAPI_USERNAME }}" > .env - echo "SDAPI_PASSWORD=${{ secrets.MOONLITE_SDAPI_PASSWORD }}" >> .env + scp -o StrictHostKeyChecking=no .env ${{ secrets.PROD_EC2_USER }}@${{ secrets.PROD_EC2_HOST }}:/home/${{ secrets.PROD_EC2_USER }}/.env - - name: Copy .env file to EC2 instance + - name: SSH into EC2 and run Docker image run: | - scp -o StrictHostKeyChecking=no .env ${{ secrets.MOONLITE_AWS_EC2_SSH_USER }}@${{ secrets.MOONLITE_AWS_EC2_SSH_HOST }}:apps/stable-diffusion-webui/ + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=60 -o StrictHostKeyChecking=no ${{ secrets.PROD_EC2_USER }}@${{ secrets.PROD_EC2_HOST }} << 'EOF1' + # Log in to ECR + docker login -u AWS -p $(aws ecr get-login-password --region ${{ secrets.PROD_AWS_REGION }}) ${{ secrets.PROD_ECR_URI }} - - name: SSH into EC2 and deploy the app - run: | - ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=60 -o StrictHostKeyChecking=no ${{ secrets.MOONLITE_AWS_EC2_SSH_USER }}@${{ secrets.MOONLITE_AWS_EC2_SSH_HOST }} << 'EOF' - cd /home/ec2-user/apps + + # Pull the latest Docker image + docker pull ${{ secrets.PROD_ECR_URI }}:latest + + # Stop and remove the current container if running + docker stop moonlite-sd || true + docker rm moonlite-sd || true - # Terminate any running instances of the Python app - PORT=${{ env.STABLE_DIFFUSION_API_PORT }} - PID=$(lsof -t -i:$PORT) - if [ ! -z "$PID" ]; then - echo "Terminating process running on port $PORT with PID $PID..." - kill -9 $PID - else - echo "No process is running on port $PORT." - fi - - # Check if the stable-diffusion-webui folder exists - if [ ! -d "stable-diffusion-webui" ]; then - echo "Cloning stable-diffusion-webui from GitHub..." - git clone git@github.com:Moonlite-Media/stable-diffusion-webui.git - else - echo "stable-diffusion-webui folder already exists, pulling the latest changes..." - cd stable-diffusion-webui - git pull origin master - fi + # Run the new Docker container + docker run --gpus all -d -p 7861:7861 --name moonlite-sd \ + --env-file /home/${{ secrets.PROD_EC2_USER }}/.env \ + ${{ secrets.PROD_ECR_URI }}:latest - conda activate myenv - - # Install missing Python packages - pip install insightface - pip install python-dotenv - - # Navigate to the models folder and download required files - cd models - # Example: Downloading a model file (adjust the URL) - # if [ ! -f model_file_name ]; then - # wget https://huggingface.co/stabilityai/stable-diffusion-2/resolve/main/768-v-ema.safetensors - # fi - - # Navigate to the extensions folder and clone repos - cd ../extensions - if [ ! -d "sd-webui-loractl/.git" ]; then - git clone https://github.com/cheald/sd-webui-loractl.git - fi - - if [ ! -d "sd-webui-controlnet/.git" ]; then - git clone https://github.com/Mikubill/sd-webui-controlnet - fi - - if [ ! -d "sd-webui-deforum/.git" ]; then - git clone https://github.com/deforum-art/sd-webui-deforum - fi - - # Return to the main project directory - cd .. - - # Run the application in the background using nohup - nohup python launch.py --nowebui --deforum-api --api --port $PORT > nohup.out 2>&1 & - - # Detach from the SSH session to prevent hanging - exit - EOF + # (Optional) Check if the container is running + docker ps -a + EOF1