From 6cc74eaa9af52d4aa1c6ef5014e726016a11657b Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria Date: Tue, 23 May 2023 17:02:41 +0200 Subject: [PATCH] Add github action for S3 deployment --- .github/workflows/node.yml | 77 ++++++++++++++----- .../github/prepare_production_deployment.sh | 17 ++++ 2 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 scripts/github/prepare_production_deployment.sh diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml index 33d667c..2fa5269 100644 --- a/.github/workflows/node.yml +++ b/.github/workflows/node.yml @@ -1,33 +1,68 @@ -# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions - name: Node.js CI - on: push: - branches: [ "main" ] + branches: + - master + - develop pull_request: - branches: [ "main" ] + release: + types: [ released ] jobs: build: runs-on: ubuntu-latest - strategy: matrix: - node-version: [14.x, 16.x, 18.x] - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + node-version: [18.x] steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'yarn' - - run: yarn install - - run: yarn build --if-present - - run: npx prettier --check src/ - - run: npx eslint src/ - # - run: yarn ci - - run: yarn test + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: 'yarn' + - run: yarn install + - run: yarn build --if-present + - run: npx prettier --check src/ + - run: npx eslint src/ + # - run: yarn ci + - run: yarn test + + release: + if: github.ref == 'refs/heads/master' || (github.event_name == 'release' && github.event.action == 'released') + runs-on: ubuntu-latest + needs: [build] + steps: + - uses: actions/checkout@v3 + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 18.x + cache: 'yarn' + - name: Build React App + run: yarn install && yarn build + - name: Deploy to Staging S3 bucket + if: github.ref == 'refs/heads/master' + run: aws s3 sync ./build/ "s3://$BUCKET_NAME" --delete + env: + BUCKET_NAME: ${{ secrets.AWS_STAGING_BUCKET_NAME }} + - name: Upload release build files for production + if: (github.event_name == 'release' && github.event.action == 'released') + run: aws s3 sync ./build/ "s3://$BUCKET_NAME/releases/$VERSION" --delete + env: + BUCKET_NAME: ${{ secrets.AWS_STAGING_BUCKET_NAME }} + VERSION: ${{ github.event.release.tag_name }} + - name: Call script to prepare production deployments + if: (github.event_name == 'release' && github.event.action == 'released') + run: bash ./scripts/github/prepare_production_deployment.sh + env: + PROD_DEPLOYMENT_HOOK_TOKEN: ${{ secrets.PROD_DEPLOYMENT_HOOK_TOKEN }} + PROD_DEPLOYMENT_HOOK_URL: ${{ secrets.PROD_DEPLOYMENT_HOOK_URL }} + VERSION_TAG: ${{ github.event.release.tag_name }} diff --git a/scripts/github/prepare_production_deployment.sh b/scripts/github/prepare_production_deployment.sh new file mode 100644 index 0000000..1d15edc --- /dev/null +++ b/scripts/github/prepare_production_deployment.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -ev + +# Only: +# - Tagged commits +# - Security env variables are available. +if [ -n "$VERSION_TAG" ] && [ -n "$PROD_DEPLOYMENT_HOOK_TOKEN" ] && [ -n "$PROD_DEPLOYMENT_HOOK_URL" ] +then + curl --silent --output /dev/null --write-out "%{http_code}" -X POST \ + -F token="$PROD_DEPLOYMENT_HOOK_TOKEN" \ + -F ref=master \ + -F "variables[TRIGGER_RELEASE_COMMIT_TAG]=$VERSION_TAG" \ + $PROD_DEPLOYMENT_HOOK_URL +else + echo "[ERROR] Production deployment could not be prepared" +fi