From f03b4a800a2e887f81dbfd198fac243f1120a288 Mon Sep 17 00:00:00 2001 From: Cesar Varela Date: Mon, 27 Nov 2023 16:10:10 -0300 Subject: [PATCH] Refactor into reusable workflows --- .github/workflows/build.yml | 98 +++++++++ .github/workflows/deploy.yml | 191 ++---------------- .github/workflows/preview.yml | 16 ++ .github/workflows/test.yml | 139 +++++++++++-- .../e2e/integration/incidents/history.cy.js | 8 +- .../e2e/integration/reportHistory.cy.js | 4 +- site/gatsby-site/netlify.toml | 15 -- 7 files changed, 264 insertions(+), 207 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000000..c0f320c0ec --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,98 @@ +name: Build +on: + workflow_call: + inputs: + environment: + type: string + description: Environment to deploy + required: true + +jobs: + install-and-build: + name: NPM install and build site + environment: ${{ inputs.environment }} + runs-on: ubuntu-latest-16-core-64gb + defaults: + run: + shell: bash + working-directory: site/gatsby-site + steps: + + - name: Checkout + uses: actions/checkout@v4 + + # Cache 'node_modules' and '~/.cache/Cypress' folder + - name: Cache node modules + id: cache-nodemodules + uses: actions/cache@v3.0.5 + env: + cache-name: cache-install-folder + with: + # caching node_modules + path: | + site/gatsby-site/node_modules + ~/.cache/Cypress + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + + - name: Install NPM dependencies + if: steps.cache-nodemodules.outputs.cache-hit != 'true' + uses: cypress-io/github-action@v4 + with: + working-directory: site/gatsby-site + # just perform install + runTests: false + install-command: npm ci --legacy-peer-deps + + - name: Install Netlify CLI + run: npm install netlify-cli -g + + - name: Build using Netlify + run: netlify build --context deploy-preview + working-directory: site/gatsby-site + env: + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }} + CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} + E2E_ADMIN_USERNAME: ${{ secrets.E2E_ADMIN_USERNAME }} + E2E_ADMIN_PASSWORD: ${{ secrets.E2E_ADMIN_PASSWORD }} + ALGOLIA_ADMIN_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }} + GATSBY_ALGOLIA_APP_ID: ${{ secrets.GATSBY_ALGOLIA_APP_ID }} + GATSBY_ALGOLIA_SEARCH_KEY: ${{ secrets.GATSBY_ALGOLIA_SEARCH_KEY }} + GATSBY_AVAILABLE_LANGUAGES: ${{ secrets.GATSBY_AVAILABLE_LANGUAGES }} + GATSBY_REALM_APP_ID: ${{ secrets.GATSBY_REALM_APP_ID }} + GOOGLE_TRANSLATE_API_KEY: ${{ secrets.GOOGLE_TRANSLATE_API_KEY }} + MONGODB_CONNECTION_STRING: ${{ secrets.MONGODB_CONNECTION_STRING }} + MONGODB_REPLICA_SET: ${{ secrets.MONGODB_REPLICA_SET }} + MONGODB_TRANSLATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_TRANSLATIONS_CONNECTION_STRING }} + MONGODB_MIGRATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_MIGRATIONS_CONNECTION_STRING }} + GATSBY_REALM_APP_GRAPHQL_URL: ${{ secrets.GATSBY_REALM_APP_GRAPHQL_URL }} + GATSBY_PRISMIC_REPO_NAME: ${{ secrets.GATSBY_PRISMIC_REPO_NAME }} + PRISMIC_ACCESS_TOKEN: ${{ secrets.PRISMIC_ACCESS_TOKEN }} + NODE_OPTIONS: --dns-result-order=ipv4first + GATSBY_ROLLBAR_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }} + + # Extract commit hash to use as a cache key + - name: Extract commit hash + shell: bash + run: echo "##[set-output name=commit;]$(echo ${GITHUB_SHA})" + id: extract_commit_hash + + # Cache 'public' folder + - name: Cache public folder + uses: actions/cache@v3.0.5 + env: + cache-name: cache-public + with: + path: | + site/gatsby-site/public + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ steps.extract_commit_hash.outputs.commit }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 644cd79bb0..ec5b9aba7f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,77 +1,19 @@ -name: Deploy +name: Deploy to Netlify on: workflow_call: inputs: environment: type: string - description: Environment to deploy to required: true - jobs: - install-and-build: - name: NPM install and build site + + netlify-deploy: + runs-on: ubuntu-latest environment: ${{ inputs.environment }} - runs-on: ubuntu-latest-16-core-64gb - defaults: - run: - shell: bash - working-directory: site/gatsby-site + permissions: + pull-requests: write steps: - - name: Checkout - uses: actions/checkout@v4 - - # Cache 'node_modules' and '~/.cache/Cypress' folder - - name: Cache node modules - id: cache-nodemodules - uses: actions/cache@v3.0.5 - env: - cache-name: cache-install-folder - with: - # caching node_modules - path: | - site/gatsby-site/node_modules - ~/.cache/Cypress - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-build-${{ env.cache-name }}- - ${{ runner.os }}-build- - ${{ runner.os }}- - # Install NPM dependencies - - name: Install NPM dependencies - if: steps.cache-nodemodules.outputs.cache-hit != 'true' - uses: cypress-io/github-action@v4 - with: - working-directory: site/gatsby-site - # just perform install - runTests: false - install-command: npm ci --legacy-peer-deps - - # Build Gatbsy site - - name: Build site - run: npm run build - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }} - CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} - E2E_ADMIN_USERNAME: ${{ secrets.E2E_ADMIN_USERNAME }} - E2E_ADMIN_PASSWORD: ${{ secrets.E2E_ADMIN_PASSWORD }} - ALGOLIA_ADMIN_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }} - GATSBY_ALGOLIA_APP_ID: ${{ secrets.GATSBY_ALGOLIA_APP_ID }} - GATSBY_ALGOLIA_SEARCH_KEY: ${{ secrets.GATSBY_ALGOLIA_SEARCH_KEY }} - GATSBY_AVAILABLE_LANGUAGES: ${{ secrets.GATSBY_AVAILABLE_LANGUAGES }} - GATSBY_REALM_APP_ID: ${{ secrets.GATSBY_REALM_APP_ID }} - GOOGLE_TRANSLATE_API_KEY: ${{ secrets.GOOGLE_TRANSLATE_API_KEY }} - MONGODB_CONNECTION_STRING: ${{ secrets.MONGODB_CONNECTION_STRING }} - MONGODB_REPLICA_SET: ${{ secrets.MONGODB_REPLICA_SET }} - MONGODB_TRANSLATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_TRANSLATIONS_CONNECTION_STRING }} - MONGODB_MIGRATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_MIGRATIONS_CONNECTION_STRING }} - GATSBY_REALM_APP_GRAPHQL_URL: ${{ secrets.GATSBY_REALM_APP_GRAPHQL_URL }} - GATSBY_PRISMIC_REPO_NAME: ${{ secrets.GATSBY_PRISMIC_REPO_NAME }} - PRISMIC_ACCESS_TOKEN: ${{ secrets.PRISMIC_ACCESS_TOKEN }} - NODE_OPTIONS: --dns-result-order=ipv4first - GATSBY_ROLLBAR_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }} - # Extract commit hash to use as a cache key - name: Extract commit hash shell: bash @@ -91,118 +33,21 @@ jobs: ${{ runner.os }}-build-${{ env.cache-name }}- ${{ runner.os }}-build- ${{ runner.os }}- - test: - name: Run Cypress tests - environment: ${{ inputs.environment }} - runs-on: ubuntu-latest-16-core-64gb - needs: install-and-build - defaults: - run: - shell: bash - working-directory: site/gatsby-site - strategy: - # when one test fails, DO NOT cancel the other - # containers, because this will kill Cypress processes - # leaving the Dashboard hanging ... - # https://github.com/cypress-io/github-action/issues/48 - fail-fast: false - matrix: - # run 10 copies of the current job in parallel - containers: [1, 2, 3, 4] - # stop the job if it runs over 20 minutes - # to prevent a hanging process from using all your CI minutes - timeout-minutes: 20 - steps: - - name: Checkout - uses: actions/checkout@v4 - - # Cache node_modules folder - - name: Cache node modules - id: cache-nodemodules-2 - uses: actions/cache@v3.0.5 - env: - cache-name: cache-install-folder + - name: Upload to netlify + uses: netlify/actions/cli@master + id: deploy-netlify with: - # caching node_modules - path: | - site/gatsby-site/node_modules - ~/.cache/Cypress - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-build-${{ env.cache-name }}- - ${{ runner.os }}-build- - ${{ runner.os }}- - # Install NPM dependencies - - name: Install NPM dependencies - if: steps.cache-nodemodules-2.outputs.cache-hit != 'true' - uses: cypress-io/github-action@v4 - with: - working-directory: site/gatsby-site - # just perform install - runTests: false - install-command: npm ci --legacy-peer-deps - - # Extract commit hash to use as a cache key - - name: Extract commit hash - shell: bash - run: echo "##[set-output name=commit;]$(echo ${GITHUB_SHA})" - id: extract_commit_hash - - # Cache 'public' folder - - name: Cache public folder - uses: actions/cache@v3.0.5 + args: deploy --dir=site/gatsby-site/public env: - cache-name: cache-public - with: - path: | - site/gatsby-site/public - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ steps.extract_commit_hash.outputs.commit }} - restore-keys: | - ${{ runner.os }}-build-${{ env.cache-name }}- - ${{ runner.os }}-build- - ${{ runner.os }}- - # Extract branch name - - name: Extract branch name - shell: bash - run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" - id: extract_branch + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} - # Run all Cypress tests - - name: Cypress run - uses: cypress-io/github-action@v6 - with: - working-directory: site/gatsby-site - # we have already installed all dependencies above - install: false - config-file: cypress.config.js - record: true - parallel: true - group: "Cypress e2e tests" - tag: ${{ steps.extract_branch.outputs.branch }} - start: node node_modules/.bin/gatsby serve -p 8000 -H 127.0.0.1 - wait-on: http://127.0.0.1:8000 - # wait for 10 minutes for the server to respond - wait-on-timeout: 60 + - name: Netlify Preview URL + uses: unsplash/comment-on-pr@v1.3.0 env: - # Recommended: pass the GitHub token lets this action correctly - # determine the unique run id necessary to re-run the checks GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }} - CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} - E2E_ADMIN_USERNAME: ${{ secrets.E2E_ADMIN_USERNAME }} - E2E_ADMIN_PASSWORD: ${{ secrets.E2E_ADMIN_PASSWORD }} - ALGOLIA_ADMIN_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }} - GATSBY_ALGOLIA_APP_ID: ${{ secrets.GATSBY_ALGOLIA_APP_ID }} - GATSBY_ALGOLIA_SEARCH_KEY: ${{ secrets.GATSBY_ALGOLIA_SEARCH_KEY }} - GATSBY_AVAILABLE_LANGUAGES: ${{ secrets.GATSBY_AVAILABLE_LANGUAGES }} - GATSBY_REALM_APP_ID: ${{ secrets.GATSBY_REALM_APP_ID }} - GOOGLE_TRANSLATE_API_KEY: ${{ secrets.GOOGLE_TRANSLATE_API_KEY }} - MONGODB_CONNECTION_STRING: ${{ secrets.MONGODB_CONNECTION_STRING }} - MONGODB_REPLICA_SET: ${{ secrets.MONGODB_REPLICA_SET }} - MONGODB_TRANSLATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_TRANSLATIONS_CONNECTION_STRING }} - MONGODB_MIGRATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_MIGRATIONS_CONNECTION_STRING }} - GATSBY_REALM_APP_GRAPHQL_URL: ${{ secrets.GATSBY_REALM_APP_GRAPHQL_URL }} - # Since this is triggered on a pull request, we set the commit message to the pull request title - COMMIT_INFO_MESSAGE: ${{ github.event.pull_request.title }} - GATSBY_ROLLBAR_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }} \ No newline at end of file + OUTPUT: "This pull request is being automatically deployed to Netlify.\n\nšŸ” Inspect: ${{ steps.deploy-netlify.outputs.NETLIFY_LOGS_URL }}\nāœ… Preview: ${{ steps.deploy-netlify.outputs.NETLIFY_URL }}" + with: + msg: ${{ env.OUTPUT }} + check_for_duplicate_msg: false diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index d987e7afdd..b633d0a02d 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -5,8 +5,24 @@ on: branches: - staging jobs: + call-build: + uses: ./.github/workflows/build.yml + secrets: inherit + with: + environment: staging + + call-test: + uses: ./.github/workflows/test.yml + needs: call-build + secrets: inherit + with: + environment: staging + call-deploy: uses: ./.github/workflows/deploy.yml + needs: call-build secrets: inherit + permissions: + pull-requests: write with: environment: staging \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index daedff44dc..d5eb62dabf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,19 +1,124 @@ -name: Deploy Staging - +name: Deploy on: - push: - branches: - - feature-github-tests + workflow_call: + inputs: + environment: + type: string + required: true + +jobs: + + test: + name: Run Cypress tests + environment: ${{ inputs.environment }} + runs-on: ubuntu-latest-16-core-64gb + defaults: + run: + shell: bash + working-directory: site/gatsby-site + strategy: + # when one test fails, DO NOT cancel the other + # containers, because this will kill Cypress processes + # leaving the Dashboard hanging ... + # https://github.com/cypress-io/github-action/issues/48 + fail-fast: false + matrix: + # run 10 copies of the current job in parallel + containers: [1, 2, 3, 4] + # stop the job if it runs over 20 minutes + # to prevent a hanging process from using all your CI minutes + timeout-minutes: 20 + steps: + + - name: Checkout + uses: actions/checkout@v4 + + # Cache node_modules folder + - name: Cache node modules + id: cache-nodemodules-2 + uses: actions/cache@v3.0.5 + env: + cache-name: cache-install-folder + with: + # caching node_modules + path: | + site/gatsby-site/node_modules + ~/.cache/Cypress + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + # Install NPM dependencies + - name: Install NPM dependencies + if: steps.cache-nodemodules-2.outputs.cache-hit != 'true' + uses: cypress-io/github-action@v4 + with: + working-directory: site/gatsby-site + # just perform install + runTests: false + install-command: npm ci --legacy-peer-deps + + # Extract commit hash to use as a cache key + - name: Extract commit hash + shell: bash + run: echo "##[set-output name=commit;]$(echo ${GITHUB_SHA})" + id: extract_commit_hash + + # Cache 'public' folder + - name: Cache public folder + uses: actions/cache@v3.0.5 + env: + cache-name: cache-public + with: + path: | + site/gatsby-site/public + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ steps.extract_commit_hash.outputs.commit }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + # Extract branch name + - name: Extract branch name + shell: bash + run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" + id: extract_branch -jobs: - # call-realm: - # uses: ./.github/workflows/realm.yml - # secrets: inherit - # with: - # environment: staging - call-deploy: - uses: ./.github/workflows/deploy.yml - secrets: inherit - with: - environment: staging - # needs: call-realm + # Run all Cypress tests + - name: Cypress run + uses: cypress-io/github-action@v6 + with: + working-directory: site/gatsby-site + # we have already installed all dependencies above + install: false + config-file: cypress.config.js + record: true + parallel: true + group: "Cypress e2e tests" + tag: ${{ steps.extract_branch.outputs.branch }} + start: node node_modules/.bin/gatsby serve -p 8000 -H 127.0.0.1 + wait-on: http://127.0.0.1:8000 + # wait for 10 minutes for the server to respond + wait-on-timeout: 60 + env: + # Recommended: pass the GitHub token lets this action correctly + # determine the unique run id necessary to re-run the checks + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }} + CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} + E2E_ADMIN_USERNAME: ${{ secrets.E2E_ADMIN_USERNAME }} + E2E_ADMIN_PASSWORD: ${{ secrets.E2E_ADMIN_PASSWORD }} + ALGOLIA_ADMIN_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }} + GATSBY_ALGOLIA_APP_ID: ${{ secrets.GATSBY_ALGOLIA_APP_ID }} + GATSBY_ALGOLIA_SEARCH_KEY: ${{ secrets.GATSBY_ALGOLIA_SEARCH_KEY }} + GATSBY_AVAILABLE_LANGUAGES: ${{ secrets.GATSBY_AVAILABLE_LANGUAGES }} + GATSBY_REALM_APP_ID: ${{ secrets.GATSBY_REALM_APP_ID }} + GOOGLE_TRANSLATE_API_KEY: ${{ secrets.GOOGLE_TRANSLATE_API_KEY }} + MONGODB_CONNECTION_STRING: ${{ secrets.MONGODB_CONNECTION_STRING }} + MONGODB_REPLICA_SET: ${{ secrets.MONGODB_REPLICA_SET }} + MONGODB_TRANSLATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_TRANSLATIONS_CONNECTION_STRING }} + MONGODB_MIGRATIONS_CONNECTION_STRING: ${{ secrets.MONGODB_MIGRATIONS_CONNECTION_STRING }} + GATSBY_REALM_APP_GRAPHQL_URL: ${{ secrets.GATSBY_REALM_APP_GRAPHQL_URL }} + # Since this is triggered on a pull request, we set the commit message to the pull request title + COMMIT_INFO_MESSAGE: ${{ github.event.pull_request.title }} + GATSBY_ROLLBAR_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }} \ No newline at end of file diff --git a/site/gatsby-site/cypress/e2e/integration/incidents/history.cy.js b/site/gatsby-site/cypress/e2e/integration/incidents/history.cy.js index 7b78f935f4..36ab20a514 100644 --- a/site/gatsby-site/cypress/e2e/integration/incidents/history.cy.js +++ b/site/gatsby-site/cypress/e2e/integration/incidents/history.cy.js @@ -149,12 +149,16 @@ describe('Incidents', () => { it('Should display an error message if no Incident ID is provided', () => { cy.visit('/incidents/history?incident_id='); + cy.waitForStableDOM(); + cy.contains('Invalid Incident ID').should('exist'); }); it('Should display an error message if an invalid Incident ID is provided', () => { cy.visit('/incidents/history?incident_id=xxx'); + cy.waitForStableDOM(); + cy.contains('Invalid Incident ID').should('exist'); }); @@ -212,7 +216,9 @@ describe('Incidents', () => { cy.go('forward'); - cy.wait(['@FindIncidentHistory', '@FindEntities']); + cy.waitForStableDOM(); + + cy.wait(['@FindIncidentHistory', '@FindEntities'], { timeout: 8000 }); } ); diff --git a/site/gatsby-site/cypress/e2e/integration/reportHistory.cy.js b/site/gatsby-site/cypress/e2e/integration/reportHistory.cy.js index b07445c993..f450fa8791 100644 --- a/site/gatsby-site/cypress/e2e/integration/reportHistory.cy.js +++ b/site/gatsby-site/cypress/e2e/integration/reportHistory.cy.js @@ -167,7 +167,9 @@ describe('Report History', () => { cy.go('forward'); - cy.wait('@FindReportHistory'); + cy.waitForStableDOM(); + + cy.wait('@FindReportHistory', { timeout: 30000 }); } ); diff --git a/site/gatsby-site/netlify.toml b/site/gatsby-site/netlify.toml index 3a8be94acc..5f0e4f353c 100644 --- a/site/gatsby-site/netlify.toml +++ b/site/gatsby-site/netlify.toml @@ -1,24 +1,9 @@ -[build.environment] -CYPRESS_CACHE_FOLDER = "./node_modules/CypressBinary" -TERM = "xterm" - [[plugins]] package = "@netlify/plugin-gatsby" [[plugins]] package = "/plugins/netlify-plugin-create-admin-user" -[[plugins]] - package = "netlify-plugin-cypress" - [plugins.inputs] - enable = false - [plugins.inputs.postBuild] - enable = true - record = false - spa = true - start = 'gatsby serve -p 8080' - browser = 'chromium' - [[plugins]] package = "/plugins/netlify-plugin-process-notifications"