adding deploy to staging workflow

This commit is contained in:
Darrel Pol 2024-11-02 14:48:02 -05:00
parent 802746cf5e
commit 0be97a48c5
2 changed files with 96 additions and 3 deletions

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

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