From 0be97a48c5c7209da9dc4e6419c7d1c06557f8a8 Mon Sep 17 00:00:00 2001 From: Darrel Pol Date: Sat, 2 Nov 2024 14:48:02 -0500 Subject: [PATCH] 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