From 0b2d17175486255abf6e148ec7790211cf5d7295 Mon Sep 17 00:00:00 2001 From: Kumaran Rajendhiran Date: Thu, 9 Nov 2023 17:05:52 +0530 Subject: [PATCH] Add deploy scripts --- .github/workflows/pipeline.yml | 7 +-- Dockerfile | 1 - scripts/deploy.sh | 78 ++++++++++++++++++++++++++++++++++ scripts/start_webservice.sh | 2 + 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100755 scripts/deploy.sh mode change 100644 => 100755 scripts/start_webservice.sh diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 64a539b0..59879b5c 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -55,6 +55,7 @@ jobs: GITHUB_PASSWORD: ${{ secrets.GITHUB_TOKEN }} SSH_KEY: ${{ secrets.SSH_KEY }} AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + DEVELOPER_TOKEN: ${{ secrets.DEVELOPER_TOKEN }} steps: - uses: actions/checkout@v3 # This is to fix GIT not liking owner of the checkout dir - https://github.com/actions/runner/issues/2033#issuecomment-1204205989 @@ -66,13 +67,13 @@ jobs: - run: eval $(ssh-agent -s) - run: mkdir -p ~/.ssh - run: chmod 700 ~/.ssh - - run: ssh-keyscan "$BACKEND_DOMAIN" >> ~/.ssh/known_hosts + - run: ssh-keyscan "$DOMAIN" >> ~/.ssh/known_hosts - run: chmod 644 ~/.ssh/known_hosts - run: echo "$SSH_KEY" | base64 --decode > key.pem - run: chmod 600 key.pem # - run: if [[ $GITHUB_REF_NAME == "main" ]]; then echo "DOMAIN=api.airt.ai" >> $GITHUB_ENV ; else echo "DOMAIN=api.staging.airt.ai" >> $GITHUB_ENV ; fi; - - run: ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker images" - - run: sh scripts/deploy_backend.sh + - run: ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker images" + - run: sh scripts/deploy.sh - run: rm key.pem diff --git a/Dockerfile b/Dockerfile index 369c5044..0be27915 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,6 @@ RUN python3 -m pip install --upgrade pip COPY migrations ./migrations COPY application.py scripts/* fastapi_requirements.txt schema.prisma ./ RUN pip install -r fastapi_requirements.txt -RUN pip install airt_service-*-py3-none-any.whl EXPOSE ${PORT} diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 00000000..1887d7a7 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,78 @@ +#!/bin/bash + + +if test -z "$TAG" +then + echo "ERROR: TAG variable must be defined, exiting" + exit -1 +fi + +if test -z "$GITHUB_USERNAME" +then + echo "ERROR: GITHUB_USERNAME variable must be defined, exiting" + exit -1 +fi + +if test -z "$GITHUB_PASSWORD" +then + echo "ERROR: GITHUB_PASSWORD variable must be defined, exiting" + exit -1 +fi + + +if [ ! -f key.pem ]; then + echo "ERROR: key.pem file not found" + exit -1 +fi + + +if test -z "$DOMAIN" +then + echo "ERROR: DOMAIN variable must be defined, exiting" + exit -1 +fi + +if test -z "$PORT" +then + echo "ERROR: PORT variable must be defined, exiting" + exit -1 +fi + +if test -z "$DATABASE_URL" +then + echo "ERROR: DATABASE_URL variable must be defined, exiting" + exit -1 +fi + +if test -z "$CLIENT_SECRET" +then + echo "ERROR: CLIENT_SECRET variable must be defined, exiting" + exit -1 +fi + +if test -z "$AZURE_OPENAI_API_KEY" +then + echo "ERROR: AZURE_OPENAI_API_KEY variable must be defined, exiting" + exit -1 +fi + +if test -z "$DEVELOPER_TOKEN" +then + echo "ERROR: DEVELOPER_TOKEN variable must be defined, exiting" + exit -1 +fi + +echo "INFO: stopping already running docker container" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker stop gads || echo 'No containers available to stop'" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker container prune -f || echo 'No stopped containers to delete'" + +echo "INFO: pulling docker image" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "echo $GITHUB_PASSWORD | docker login -u '$GITHUB_USERNAME' --password-stdin '$REGISTRY'" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker pull ghcr.io/$GITHUB_REPOSITORY:'$TAG'" +sleep 10 + +echo "Deleting old image" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker system prune -f || echo 'No images to delete'" + +echo "INFO: starting docker container" +ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$DOMAIN" "docker run --name gads -p $PORT:$PORT -e PORT='$PORT' -e DATABASE_URL='$DATABASE_URL' -e CLIENT_SECRET='$CLIENT_SECRET' -e DEVELOPER_TOKEN='$DEVELOPER_TOKEN' -e AZURE_OPENAI_API_KEY='$AZURE_OPENAI_API_KEY' -d ghcr.io/$GITHUB_REPOSITORY:$TAG" diff --git a/scripts/start_webservice.sh b/scripts/start_webservice.sh old mode 100644 new mode 100755 index 09a71b0e..7e30ee63 --- a/scripts/start_webservice.sh +++ b/scripts/start_webservice.sh @@ -7,6 +7,8 @@ fi echo NUM_WORKERS set to $NUM_WORKERS +cat <<< "$CLIENT_SECRET" > client_secrets.json + prisma migrate deploy uvicorn application:app --port $PORT --host 0.0.0.0 --workers=$NUM_WORKERS --proxy-headers