Skip to content

refactor: improve logging system with context variables and color for… #285

refactor: improve logging system with context variables and color for…

refactor: improve logging system with context variables and color for… #285

Workflow file for this run

name: Deploy whisker server to AWS
on:
workflow_run:
workflows: ["server-ci"]
types: ["completed"]
branches: ["test","main","preview","test/*","preview/*"]
push:
branches: ["test","main","preview","test/*","preview/*"]
paths:
- 'server/**'
- '.github/workflows/server-deploy.yml'
- '.aws/server_samconfig.toml'
- 'template_server.yml'
- 'docker/Dockerfile.aws.server'
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
actions: write
env:
NODE_VERSION: '20'
PACKAGE_NAME: '@petercat/whiskerrag-client'
jobs:
determine-env:
runs-on: ubuntu-latest
outputs:
environment: ${{ github.ref == 'refs/heads/main' && 'Production' || 'preview' }}
npm_tag: ${{ github.ref == 'refs/heads/main' && 'latest' || 'dev' }}
steps:
- name: Determine environment
id: env
run: |
echo "Environment: ${{ github.ref == 'refs/heads/main' && 'Production' || 'preview' }}"
echo "NPM tag: ${{ github.ref == 'refs/heads/main' && 'latest' || 'dev' }}"
server-deploy:
needs: determine-env
runs-on: ubuntu-latest
environment: ${{ needs.determine-env.outputs.environment }}
env:
ENVIRONMENT: ${{ needs.determine-env.outputs.environment }}
strategy:
fail-fast: true
outputs:
api_url: ${{ steps.get_api_url.outputs.api_url }}
steps:
- name: Print specific variables
run: |
echo "Current github.ref : ${{ github.ref }}"
echo "Deploying to ${{ env.ENVIRONMENT }}"
echo "Current AWS_REGION: ${{ vars.AWS_REGION }}"
echo "Current WHISKER_ENV: ${{ vars.WHISKER_ENV }}"
echo "GitHub actor: ${{ github.actor }}"
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::654654285942:role/Github-OIDC
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
# Build inside Docker containers
- run: sam build --use-container --template template_server.yml --config-file .aws/server_samconfig.toml
# Prevent prompts and failure when the stack is unchanged
- run: |
sam deploy \
--no-confirm-changeset \
--config-env ${{ vars.WHISKER_ENV}} \
--no-fail-on-empty-changeset \
--config-file .aws/server_samconfig.toml \
--parameter-overrides \
ParameterKey=supabaseServiceKey,ParameterValue=${{ secrets.SUPABASE_SERVICE_KEY }} \
ParameterKey=supabaseUrl,ParameterValue=${{ secrets.SUPABASE_URL }} \
ParameterKey=s3TempBucketName,ParameterValue=${{ vars.S3_TEMP_BUCKET_NAME }} \
ParameterKey=webUrl,ParameterValue=${{ vars.WEB_URL }} \
ParameterKey=knowledgeTableName,ParameterValue=${{ vars.KNOWLEDGE_TABLE_NAME }} \
ParameterKey=ApiKeyTableName,ParameterValue=${{ vars.API_KEY_TABLE_NAME }} \
ParameterKey=chunkTableName,ParameterValue=${{ vars.CHUNK_TABLE_NAME }} \
ParameterKey=taskTableName,ParameterValue=${{ vars.TASK_TABLE_NAME }} \
ParameterKey=tenantTableName,ParameterValue=${{ vars.TENANT_TABLE_NAME }} \
ParameterKey=openAIKey,ParameterValue=${{ secrets.OPENAI_API_KEY }} \
ParameterKey=dbEngineClassName,ParameterValue=${{ vars.DB_ENGINE_CLASSNAME }} \
ParameterKey=fastapiEngineClassName,ParameterValue=${{ vars.FASTAPI_ENGINE_CLASSNAME }} \
ParameterKey=taskEngineClassName,ParameterValue=${{ vars.TASK_ENGINE_CLASSNAME }} \
ParameterKey=whiskerEnv,ParameterValue=${{ vars.WHISKER_ENV }} \
ParameterKey=sqsQueueUrl,ParameterValue=${{ vars.SQS_QUEUE_URL }} \
- name: Get Stack Name
id: get_stack_name
run: |
if [ "${{ env.ENVIRONMENT }}" = "Production" ]; then
CONFIG_SECTION="prod"
else
CONFIG_SECTION="preview"
fi
STACK_NAME=$(sed -n "/\[$CONFIG_SECTION\.deploy\.parameters\]/,/^\[/p" .aws/server_samconfig.toml | grep 'stack_name' | cut -d'"' -f2)
if [ -z "$STACK_NAME" ]; then
echo "::error::can not get stack name from .aws/server_samconfig.toml, check it first"
exit 1
fi
echo "Get Stack: ${STACK_NAME}"
echo "STACK_NAME=${STACK_NAME}" >> $GITHUB_ENV
- name: Get API URL
id: get_api_url
run: |
API_URL=$(aws cloudformation describe-stacks \
--stack-name "${{ env.STACK_NAME }}" \
--query 'Stacks[0].Outputs[?OutputKey==`WhiskerFunctionUrl`].OutputValue' \
--output text)
echo "=== CloudFormation ouput ==="
aws cloudformation describe-stacks \
--stack-name "${{ env.STACK_NAME }}" \
--query 'Stacks[0].Outputs' \
--output table
if [[ -z "$API_URL" || "$API_URL" == "None" ]]; then
echo "::error::GET API URL ERROR (stack: ${{ env.STACK_NAME }})"
exit 1
fi
echo "api_url=${API_URL}" >> $GITHUB_OUTPUT
echo "success get API URL: ${API_URL}"
generate-client:
needs: [determine-env, server-deploy]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'
- name: Install swagger-typescript-api
run: npm install -g swagger-typescript-api@latest
- name: Generate and Publish Client
env:
NPM_AUTH_TOKEN: ${{ secrets.WHISKER_NPM_TOKEN }}
working-directory: ${{ github.workspace }}
run: |
${GITHUB_WORKSPACE}/generate-client.sh \
--publish \
--ENVIRONMENT="${{ needs.determine-env.outputs.environment }}" \
--API_URL="${{ needs.server-deploy.outputs.api_url }}" \