From 51a0a402299db05301d63be029072ec1cd21cf41 Mon Sep 17 00:00:00 2001 From: matheusgnreis Date: Fri, 19 Apr 2024 16:51:05 -0300 Subject: [PATCH] ci(build-and-deploy): deploy to firebase --- .github/firebase-deploy | 1 + .../workflows/.firebase/build-and-deploy.yml | 117 ---------------- .github/workflows/build-and-deploy.yml | 131 +++++------------- 3 files changed, 32 insertions(+), 217 deletions(-) create mode 100644 .github/firebase-deploy delete mode 100644 .github/workflows/.firebase/build-and-deploy.yml diff --git a/.github/firebase-deploy b/.github/firebase-deploy new file mode 100644 index 00000000..27ba77dd --- /dev/null +++ b/.github/firebase-deploy @@ -0,0 +1 @@ +true diff --git a/.github/workflows/.firebase/build-and-deploy.yml b/.github/workflows/.firebase/build-and-deploy.yml deleted file mode 100644 index f41f6206..00000000 --- a/.github/workflows/.firebase/build-and-deploy.yml +++ /dev/null @@ -1,117 +0,0 @@ -name: Build and deploy - -on: - push: - branches: - - master - paths: - - 'content/**' - - 'template/**' - - 'package-lock.json' - - '.ecomplus.cd.json' - - '.functions/**' - - 'firebase.json' - - '.github/build-and-deploy' - - '.github/workflows/build-and-deploy.yml' - -jobs: - build-and-deploy: - name: Build Storefront and deploy - runs-on: ubuntu-latest - timeout-minutes: 30 - if: | - github.event.head_commit.message != 'Initial commit' && - !contains(github.event.head_commit.message, '[skip ci]') - - steps: - - name: Set remote repo URL - env: - TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - echo "::set-output name=url::https://${GITHUB_ACTOR}:${TOKEN}@github.com/${GITHUB_REPOSITORY}.git" - id: remote_repo - - - name: Checkout - uses: actions/checkout@v2 - with: - ref: ${{ github.ref }} - - - name: Setup Node - uses: actions/setup-node@v2 - with: - node-version: '14.x' - - - name: Cache dependencies - uses: actions/cache@v4 - with: - path: ~/.npm - key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install root dependencies - run: npm ci --only=production - - - name: Install functions dependencies - run: cd .functions && npm ci --only=production && cd ../.. - - - name: Check last changed files - uses: lots0logs/gh-action-get-changed-files@2.2.2 - with: - token: ${{ secrets.GITHUB_TOKEN }} - id: changed_files - - - name: Run partial build - env: - CHANGED_FILES: ${{ steps.changed_files.outputs.all }} - run: | - IS_ECOMPLUS_CD=$(node -p "const f = JSON.parse('${CHANGED_FILES}'); f.length === 1 && f[0] === '.ecomplus.cd.json'"); - if [ $IS_ECOMPLUS_CD != false ]; then - BUILD_SLUG=$(node -p "require('./.ecomplus.cd.json').slug || false"); - STORE_DOMAIN=$(node -p "require('./content/settings.json').domain || false"); - if [ $BUILD_SLUG != false ] && [ $STORE_DOMAIN != false ]; then - IFS=","; - read -ra slug <<< "$BUILD_SLUG"; - for i in "${slug[@]}"; do - curl -X PURGE "https://$STORE_DOMAIN/$i" - done; - echo "::set-output name=isg::2"; - exit 0; - fi - fi - npm run build -- --prerender=index,app/index,search,404,blog,admin/index --prerender-limit=0; - echo "::set-output name=isg::0"; - id: partial_build - - - name: Config Git and check diff - run: | - git config --local user.email 'action@github.com' - git config --local user.name 'GitHub Action' - git add content template - (git diff-index --quiet HEAD && echo "::set-output name=diff::0") || echo "::set-output name=diff::1" - id: git_diff - - - name: Commit local file changes - continue-on-error: true - if: steps.git_diff.outputs.diff == 1 - run: | - git commit -m 'chore(post-build): update template and content defaults [skip ci]' - - - name: Push changes - if: steps.git_diff.outputs.diff == 1 - continue-on-error: true - env: - REMOTE_REPO: ${{ steps.remote_repo.outputs.url }} - run: git push "${REMOTE_REPO}" HEAD:master --follow-tags - - - name: Deploy to Firebase - if: steps.partial_build.outputs.isg == 0 - env: - GCP_ACCOUNT_KEY: ${{ secrets.GCP_ACCOUNT_KEY }} - run: | - if [ -f .bundles.json ] && [ -f dist/storefront.js ]; then - export GOOGLE_APPLICATION_CREDENTIALS=".firebase-adminsdk.json"; - echo $GCP_ACCOUNT_KEY > $GOOGLE_APPLICATION_CREDENTIALS; - PROJECT_ID=$(node -p "require('./$GOOGLE_APPLICATION_CREDENTIALS').project_id"); - GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS npx firebase deploy --project=$PROJECT_ID; - fi diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index 42a49792..f41f6206 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -9,7 +9,8 @@ on: - 'template/**' - 'package-lock.json' - '.ecomplus.cd.json' - - '.netlify/**' + - '.functions/**' + - 'firebase.json' - '.github/build-and-deploy' - '.github/workflows/build-and-deploy.yml' @@ -31,14 +32,14 @@ jobs: id: remote_repo - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v2 with: ref: ${{ github.ref }} - name: Setup Node - uses: actions/setup-node@v4 + uses: actions/setup-node@v2 with: - node-version: '16.x' + node-version: '14.x' - name: Cache dependencies uses: actions/cache@v4 @@ -48,25 +49,12 @@ jobs: restore-keys: | ${{ runner.os }}-node- - - name: Get ISG state - id: isg_state - run: | - if [ -f .isg ]; then - echo "::set-output name=rand::$(cat .isg)"; - else - echo "::set-output name=rand::0"; - fi - - - name: Cache output - continue-on-error: true - uses: actions/cache@v4 - with: - path: .deploy/publish - key: ${{ hashFiles('.bundles.json') }}-${{ hashFiles('template/p*/**') }}-${{ hashFiles('content/**') }}-${{ steps.isg_state.outputs.rand }} - - - name: Install dependencies + - name: Install root dependencies run: npm ci --only=production + - name: Install functions dependencies + run: cd .functions && npm ci --only=production && cd ../.. + - name: Check last changed files uses: lots0logs/gh-action-get-changed-files@2.2.2 with: @@ -78,46 +66,36 @@ jobs: CHANGED_FILES: ${{ steps.changed_files.outputs.all }} run: | IS_ECOMPLUS_CD=$(node -p "const f = JSON.parse('${CHANGED_FILES}'); f.length === 1 && f[0] === '.ecomplus.cd.json'"); - if [ $IS_ECOMPLUS_CD != false ] && [ -f .bundles.json ] && [ -f .deploy/publish/storefront.js ]; then + if [ $IS_ECOMPLUS_CD != false ]; then BUILD_SLUG=$(node -p "require('./.ecomplus.cd.json').slug || false"); - if [ $BUILD_SLUG != false ]; then - npm run build -- --prerender=index,$BUILD_SLUG --no-bundler; + STORE_DOMAIN=$(node -p "require('./content/settings.json').domain || false"); + if [ $BUILD_SLUG != false ] && [ $STORE_DOMAIN != false ]; then + IFS=","; + read -ra slug <<< "$BUILD_SLUG"; + for i in "${slug[@]}"; do + curl -X PURGE "https://$STORE_DOMAIN/$i" + done; echo "::set-output name=isg::2"; - else - npm run build -- --prerender-limit=1000 --no-bundler; - echo "::set-output name=isg::1"; + exit 0; fi - else - npm run build -- --prerender=index,app/index,search,404 --prerender-limit=300; - echo "::set-output name=isg::0"; fi + npm run build -- --prerender=index,app/index,search,404,blog,admin/index --prerender-limit=0; + echo "::set-output name=isg::0"; id: partial_build - - name: Output ISG state - id: new_isg_state - env: - ISG_ENUM: ${{ steps.partial_build.outputs.isg }} - run: | - if [ $ISG_ENUM != 0 ] || [ ! -f .isg ]; then - ISG_STATE=$RANDOM; - echo $ISG_STATE > .isg; - else - ISG_STATE=$(cat .isg); - fi - echo "::set-output name=rand::$ISG_STATE"; - - name: Config Git and check diff run: | git config --local user.email 'action@github.com' git config --local user.name 'GitHub Action' - git add .isg .bundles.json content template + git add content template (git diff-index --quiet HEAD && echo "::set-output name=diff::0") || echo "::set-output name=diff::1" id: git_diff - name: Commit local file changes + continue-on-error: true if: steps.git_diff.outputs.diff == 1 run: | - git commit -m 'chore(post-build): update template defaults, bundles and isg states [skip ci]' + git commit -m 'chore(post-build): update template and content defaults [skip ci]' - name: Push changes if: steps.git_diff.outputs.diff == 1 @@ -126,61 +104,14 @@ jobs: REMOTE_REPO: ${{ steps.remote_repo.outputs.url }} run: git push "${REMOTE_REPO}" HEAD:master --follow-tags - - name: Clear cache if not ISG + - name: Deploy to Firebase if: steps.partial_build.outputs.isg == 0 - continue-on-error: true - run: rm -rf .deploy - - - name: Setup output directory to deploy - run: | - mkdir -p .deploy/publish - cp .nvmrc .deploy/ 2>/dev/null || : - cp -r dist/* .deploy/publish/ - rm -rf .deploy/publish/netlify.toml - cp -r .netlify/* .deploy/ - cp -r template .deploy/functions/ssr/ - rm -rf .deploy/functions/ssr/template/public - rm -rf .deploy/functions/ssr/template/scss - cp -r content .deploy/functions/ssr/ - cp .bundles.json .deploy/functions/ssr/bundles.json - mkdir -p .deploy/functions/ssr/template/pages/@ecomplus/storefront-template/template - cp -r node_modules/@ecomplus/widget-* .deploy/functions/ssr/template/pages/@ecomplus/ 2>/dev/null || : - cp -r node_modules/@ecomplus/storefront-template/template/pages .deploy/functions/ssr/template/pages/@ecomplus/storefront-template/template/ || : - - - name: Clear functions directory - continue-on-error: true - run: rm -rf .deploy/functions/ssr/template/pages/**/node_modules - - - name: Zip Lambda functions - run: | - npx zip-it-and-ship-it .deploy/functions .deploy/functions-zip - rm -rf .deploy/functions - - - name: Reset dist to clear Git ref - if: steps.partial_build.outputs.isg == 0 - continue-on-error: true env: - REMOTE_REPO: ${{ steps.remote_repo.outputs.url }} + GCP_ACCOUNT_KEY: ${{ secrets.GCP_ACCOUNT_KEY }} run: | - git push "${REMOTE_REPO}" --delete dist; - git checkout --orphan dist - git rm -rf . - git commit --no-verify --allow-empty -m "Reset dist [skip ci]" - git push "${REMOTE_REPO}" HEAD:dist --force --follow-tags - git checkout master - - - name: Push to branch dist to trigger deploy - uses: JamesIves/github-pages-deploy-action@releases/v3 - with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - BRANCH: dist - FOLDER: .deploy - CLEAN: true - - - name: Cache output force update - if: steps.new_isg_state.outputs.rand != steps.isg_state.outputs.rand - continue-on-error: true - uses: actions/cache@v4 - with: - path: .deploy/publish - key: ${{ hashFiles('.bundles.json') }}-${{ hashFiles('template/p*/**') }}-${{ hashFiles('content/**') }}-${{ steps.new_isg_state.outputs.rand }} + if [ -f .bundles.json ] && [ -f dist/storefront.js ]; then + export GOOGLE_APPLICATION_CREDENTIALS=".firebase-adminsdk.json"; + echo $GCP_ACCOUNT_KEY > $GOOGLE_APPLICATION_CREDENTIALS; + PROJECT_ID=$(node -p "require('./$GOOGLE_APPLICATION_CREDENTIALS').project_id"); + GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS npx firebase deploy --project=$PROJECT_ID; + fi