Skip to content

Commit

Permalink
Add deploy scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Nov 9, 2023
1 parent 7859c62 commit 0b2d171
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
78 changes: 78 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions scripts/start_webservice.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b2d171

Please sign in to comment.