Skip to content

Commit

Permalink
Add job and script to deploy backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Nov 3, 2023
1 parent 6fc8538 commit cf0c660
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ env:
WASP_WEB_CLIENT_URL: ${{ vars.WASP_WEB_CLIENT_URL }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
BACKEND_DOMAIN: ${{ vars.BACKEND_DOMAIN }}

jobs:
docker_build_push:
Expand Down Expand Up @@ -42,3 +43,39 @@ jobs:
- name: Push only if branch name is main
if: github.ref_name == 'main'
run: docker push ghcr.io/$GITHUB_REPOSITORY --all-tags

deploy_backend:
runs-on: ubuntu-22.04
defaults:
run:
shell: bash
needs: [docker_build_push]
if: github.ref_name == 'main'
container:
image: python:3.7-stretch
env:
GITHUB_USERNAME: ${{ github.actor }}
GITHUB_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
PROD_SSH_KEY: ${{ secrets.PROD_SSH_KEY }}
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
- run: chown -R $(id -u):$(id -g) $PWD
- run: echo "TAG=latest" >> $GITHUB_ENV
# - run: if [[ $GITHUB_REF_NAME == "main" ]]; then printenv PROD_CONFIG > "$(pwd)/.env" ; else printenv STAGING_CONFIG > "$(pwd)/.env" ; fi;
- run: sed 's/\"//g' .env >> $GITHUB_ENV
- run: echo "PATH=$PATH:/github/home/.local/bin" >> $GITHUB_ENV
- run: 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
- run: eval $(ssh-agent -s)
- run: mkdir -p ~/.ssh
- run: chmod 700 ~/.ssh
- 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: rm key.pem
72 changes: 72 additions & 0 deletions scripts/deploy_backend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/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 "$BACKEND_DOMAIN"
then
echo "ERROR: BACKEND_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 "$WASP_WEB_CLIENT_URL"
then
echo "ERROR: WASP_WEB_CLIENT_URL variable must be defined, exiting"
exit -1
fi

if test -z "$JWT_SECRET"
then
echo "ERROR: JWT_SECRET variable must be defined, exiting"
exit -1
fi

echo "INFO: stopping already running docker container"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker stop wasp-backend || echo 'No containers available to stop'"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker container prune -f || echo 'No stopped containers to delete'"

echo "INFO: pulling docker image"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "echo $GITHUB_PASSWORD | docker login -u '$GITHUB_USERNAME' --password-stdin '$REGISTRY'"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker pull ghcr.io/$GITHUB_REPOSITORY:'$TAG'"
sleep 10

echo "Deleting old image"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker system prune -f || echo 'No images to delete'"

echo "INFO: starting docker container"
ssh -o StrictHostKeyChecking=no -i key.pem azureuser@"$BACKEND_DOMAIN" "docker run --rm --name wasp-backend -e PORT -e DATABASE_URL -e WASP_WEB_CLIENT_URL -e JWT_SECRET ghcr.io/$GITHUB_REPOSITORY:$TAG -d"

0 comments on commit cf0c660

Please sign in to comment.