From 6a4e75368a918d2f5160516bad8fb226570036d7 Mon Sep 17 00:00:00 2001 From: Kevin Foong <55353265+kevin9foong@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:10:09 +0800 Subject: [PATCH] feat: add script to populate value --- .github/workflows/deploy-ecs.yml | 3 +++ ecs-task-definition.json | 1 + generate-env-from-ecs-params.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 generate-env-from-ecs-params.sh diff --git a/.github/workflows/deploy-ecs.yml b/.github/workflows/deploy-ecs.yml index 545de80f98..05d93bba10 100644 --- a/.github/workflows/deploy-ecs.yml +++ b/.github/workflows/deploy-ecs.yml @@ -104,6 +104,9 @@ jobs: task-definition: ${{ env.ECS_TASK_DEFINITION }} container-name: ${{ env.CONTAINER_NAME }} image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} + environment-variables: | + ENV_TYPE=${{ contains(env.CURRENT_ENV, 'staging') && 'staging' || contains(env.CURRENT_ENV, 'prod') && 'prod' || contains(env.CURRENT_ENV, 'uat') && 'uat' || env.CURRENT_ENV }} + ENV_SITE_NAME=${{ env.CURRENT_ENV }} - name: Deploy Amazon ECS task definition env: # For ECS deployment diff --git a/ecs-task-definition.json b/ecs-task-definition.json index ae5d031c30..f8164adcf0 100644 --- a/ecs-task-definition.json +++ b/ecs-task-definition.json @@ -4,6 +4,7 @@ { "name": "formsg-app", "essential": true, + "command": "/bin/sh -c \"./generate-env-from-ecs-params.sh && npm start\"", "portMappings": [ { "containerPort": 3000 } ] diff --git a/generate-env-from-ecs-params.sh b/generate-env-from-ecs-params.sh new file mode 100644 index 0000000000..72100fade9 --- /dev/null +++ b/generate-env-from-ecs-params.sh @@ -0,0 +1,31 @@ +TARGET_DIR=/etc/formsg +ENV_TYPE=$ENV_TYPE +ENV_SITE_NAME=$ENV_SITE_NAME + +# create target dir if not exist +echo "Checking if ${TARGET_DIR} exists..." +if [ ! -d ${TARGET_DIR} ]; then + echo "Creating directory ${TARGET_DIR} ..." + mkdir -p ${TARGET_DIR} + if [ $? -ne 0 ]; then + echo 'ERROR: Directory creation failed!' + exit 1 + fi +else + echo "Directory ${TARGET_DIR} already exists!" +fi + +echo "${ENV_TYPE}-general" > $TARGET_DIR/.env +echo "${ENV_TYPE}-captcha" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-turnstile" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-ga" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-intranet" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-sms" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-ndi" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-verified-fields" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-webhook-verified-content" >> $TARGET_DIR/.env +echo "${ENV_TYPE}-wogaa" >> $TARGET_DIR/.env +echo "${ENV_SITE_NAME}-sgid" >> $TARGET_DIR/.env +echo "${ENV_SITE_NAME}-payment" >> $TARGET_DIR/.env +echo "${ENV_SITE_NAME}-cron-payment" >> $TARGET_DIR/.env +echo "${ENV_SITE_NAME}-openai" >> $TARGET_DIR/.env