remove unnecessary environment variables from project, adapt deployme… #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
push: | |
branches: [main, dev] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set environment variables | |
run: | | |
# Set common environment variables | |
echo "LOG_LEVEL=${{ secrets.LOG_LEVEL }}" >> $GITHUB_ENV | |
# Set branch-specific environment variables | |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then | |
echo "SETTING PROD VARS" | |
echo "DOCKER_HOST=${{ secrets.PROD_DOCKER_HOST }}" >> $GITHUB_ENV | |
echo "BETTERSTACK_API_TOKEN=${{ secrets.PROD_BETTERSTACK_API_TOKEN }}" >> $GITHUB_ENV | |
echo "CLERK_PUBLISHABLE_KEY=${{ secrets.PROD_CLERK_PUBLISHABLE_KEY }}" >> $GITHUB_ENV | |
echo "DOMAIN=${{ secrets.PROD_DOMAIN }}" >> $GITHUB_ENV | |
elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then | |
echo "SETTING DEV VARS" | |
echo "DOCKER_HOST=${{ secrets.DEV_DOCKER_HOST }}" >> $GITHUB_ENV | |
echo "BETTERSTACK_API_TOKEN=${{ secrets.DEV_BETTERSTACK_API_TOKEN }}" >> $GITHUB_ENV | |
echo "CLERK_PUBLISHABLE_KEY=${{ secrets.DEV_CLERK_PUBLISHABLE_KEY }}" >> $GITHUB_ENV | |
echo "DOMAIN=${{ secrets.DEV_DOMAIN }}" >> $GITHUB_ENV | |
fi | |
# put ssh key into right directory to connect to the docker host server | |
- name: Copy SSH key | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ env.DOCKER_HOST }} > ~/.ssh/known_hosts | |
# rsync local checkout to deploy directory | |
- name: Rsync | |
run: rsync -avz --delete --exclude .git/ --exclude .github/ . root@${{ env.DOCKER_HOST }}:/app | |
- name: Create .env file | |
run: | | |
ssh root@${{ env.DOCKER_HOST }} "cat > /app/.env <<EOL | |
LOG_LEVEL=${{ env.LOG_LEVEL }} | |
CLERK_PUBLISHABLE_KEY=${{ env.CLERK_PUBLISHABLE_KEY }} | |
BETTERSTACK_API_TOKEN=${{ env.BETTERSTACK_API_TOKEN }} | |
DOMAIN=${{ env.DOMAIN }} | |
EOL" | |
# build image with docker compose | |
- name: Build image | |
run: ssh root@${{ env.DOCKER_HOST}} "cd /app && docker compose build" | |
# Restart the webserver to serve new frontend | |
- name: Restart Webserver | |
run: ssh root@${{ env.DOCKER_HOST }} "cd /app && docker compose -f compose.yaml -f compose.prod.yaml up -d webserver --no-deps" | |
# Bring up the new app container | |
- name: Create new app container | |
run: ssh root@${{ env.DOCKER_HOST }} "cd /app && docker compose -f compose.yaml -f compose.prod.yaml up -d app --scale app=2 --no-recreate" | |
# Delete the old container after 30 seconds | |
- name: Delete old container | |
run: sleep 30 && ssh root@${{ env.DOCKER_HOST }} "cd /app && docker compose ps app --format '{{.Names}}' | head -n 1 | xargs docker rm -f && docker compose -f compose.yaml -f compose.prod.yaml up -d --scale app=1 --no-recreate" | |
# Clean up unused images, etc on server | |
- name: Docker system prune | |
run: ssh root@${{ env.DOCKER_HOST }} "docker system prune -f" |