From 0bfc78627297e0bfc27d04bca300b10efb43166c Mon Sep 17 00:00:00 2001 From: Allen Chiang Date: Thu, 11 Jul 2024 15:04:46 +0800 Subject: [PATCH] build: create toggle maintenance mode --- .github/workflows/lambda-deploy.yaml | 2 +- .../workflows/maintenance-mode-toggle.yaml | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/maintenance-mode-toggle.yaml diff --git a/.github/workflows/lambda-deploy.yaml b/.github/workflows/lambda-deploy.yaml index 4da03b28..769ff711 100644 --- a/.github/workflows/lambda-deploy.yaml +++ b/.github/workflows/lambda-deploy.yaml @@ -43,7 +43,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::154471991214:role/github_action - aws-region: ap-northeast-1 + aws-region: us-west-2 - name: Copy files to the s3 with the aws cli run: | diff --git a/.github/workflows/maintenance-mode-toggle.yaml b/.github/workflows/maintenance-mode-toggle.yaml new file mode 100644 index 00000000..c249f2f8 --- /dev/null +++ b/.github/workflows/maintenance-mode-toggle.yaml @@ -0,0 +1,66 @@ +name: Toggle Maintenance Mode in Lambda + +on: + workflow_dispatch: + inputs: + name: + description: Who to greet + default: Allen + +permissions: + id-token: write + contents: read + +jobs: + toggle-maintenance-mode: + name: Toggle maintenance mode + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure php 8.3 + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + coverage: none + + - name: Install php packages + run: composer install --prefer-dist --optimize-autoloader --no-dev + + - name: Set up node.js + uses: actions/setup-node@v2 + with: + node-version: '21' + + - name: Install javascript packages and build the frontend assets + run: | + npm install + npm run build + + - name: Configure aws credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::154471991214:role/github_action + aws-region: us-west-2 + + - name: Toggle maintenance mode + run: | + npm install serverless serverless-lift + + MAINTENANCE_MODE=$(aws lambda get-function-configuration \ + --function-name docfunc-production-web \ + --region us-west-2 | jq -r '.Environment.Variables.MAINTENANCE_MODE') + + if [[ ${MAINTENANCE_MODE} == 0 ]]; then + node_modules/.bin/serverless deploy function --function=web --update-config --param="maintenance=1" + node_modules/.bin/serverless deploy function --function=artisan --update-config --param="maintenance=1" + echo "Maintenance mode is ENABLE" + elif [[ ${MAINTENANCE_MODE} == 1 ]]; then + node_modules/.bin/serverless deploy function --function=web --update-config --param="maintenance=0" + node_modules/.bin/serverless deploy function --function=artisan --update-config --param="maintenance=0" + echo "Maintenance mode is DISABLE" + else + echo 'MAINTENANCE_MODE must be 1 or 0' + exit 1 + fi