From b8c568fb3534b23b98a6721c4bfd694786b26c2d Mon Sep 17 00:00:00 2001 From: mistic100 Date: Tue, 5 Nov 2024 22:12:45 +0100 Subject: [PATCH] chore: add deployment --- .github/workflows/build.yml | 20 +++++------ .../shared/deploy-e2e-report/action.yml | 35 +++++++++++++++++++ .github/workflows/shared/setup/action.yml | 2 +- build/deploy-netlify.mjs | 33 +++++++++++++++-- cypress.config.ts | 5 +++ 5 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/shared/deploy-e2e-report/action.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3f1a616b..4c415668 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,7 +26,7 @@ jobs: - name: test summary uses: phoenix-actions/test-reporting@v15 - if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' }} + if: ${{ !cancelled() }} with: name: build output-to: step-summary @@ -50,24 +50,22 @@ jobs: yarn ci:build yarn ci:e2e - - name: test report - if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' && github.event_name != 'pull_request' }} - run: | - branch=$([ "${{ github.ref_name }}" == "main" ] && echo "" || echo "${{ github.ref_name }}") - node ./build/deploy-netlify.mjs --rootFolder=cypress/reports/html --exclude=.jsons --branch=$branch - env: - NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} - NETLIFY_SITE_ID: ${{ secrets.NETLIFY_REPORTS_SITE_ID }} - - name: test summary uses: phoenix-actions/test-reporting@v15 - if: ${{ (success() || failure()) && github.repository == 'mistic100/Photo-Sphere-Viewer' }} + if: ${{ !cancelled() }} with: name: e2e output-to: step-summary path: 'cypress/reports/html/.jsons/*.json' reporter: mochawesome-json + - name: deploy report + uses: ./.github/workflows/shared/deploy-e2e-report + if: ${{ !cancelled() && github.repository == 'mistic100/Photo-Sphere-Viewer' && github.event_name != 'pull_request' }} + with: + netlify-auth-token: ${{ secrets.NETLIFY_AUTH_TOKEN }} + netlify-site-id: ${{ secrets.NETLIFY_REPORTS_SITE_ID }} + build-doc: runs-on: ubuntu-latest needs: build diff --git a/.github/workflows/shared/deploy-e2e-report/action.yml b/.github/workflows/shared/deploy-e2e-report/action.yml new file mode 100644 index 00000000..9d0a6ad3 --- /dev/null +++ b/.github/workflows/shared/deploy-e2e-report/action.yml @@ -0,0 +1,35 @@ +name: deploy-e2e-report + +inputs: + netlify-auth-token: + required: true + netlify-site-id: + required: true + +runs: + using: composite + + steps: + - name: start deploy + uses: bobheadxi/deployments@v1 + id: deployment + with: + step: start + env: cypress + + - name: deploy + id: netlify + shell: bash + run: node ./build/deploy-netlify.mjs --rootFolder=cypress/reports/html --exclude=.jsons --branch=${{ github.ref_name }} + env: + NETLIFY_AUTH_TOKEN: ${{ inputs.netlify-auth-token }} + NETLIFY_SITE_ID: ${{ inputs.netlify-site-id }} + + - name: finish deploy + uses: bobheadxi/deployments@v1 + with: + step: finish + status: success + env: ${{ steps.deployment.outputs.env }} + deployment_id: ${{ steps.deployment.outputs.deployment_id }} + env_url: ${{ steps.netlify.url.deploy_url }} diff --git a/.github/workflows/shared/setup/action.yml b/.github/workflows/shared/setup/action.yml index 5ab1d12a..aaa0bb4c 100644 --- a/.github/workflows/shared/setup/action.yml +++ b/.github/workflows/shared/setup/action.yml @@ -2,7 +2,7 @@ name: setup inputs: turbo-cache: - default: True + default: true runs: using: composite diff --git a/build/deploy-netlify.mjs b/build/deploy-netlify.mjs index ed17c4d8..b3712463 100644 --- a/build/deploy-netlify.mjs +++ b/build/deploy-netlify.mjs @@ -7,6 +7,7 @@ import { createHash } from 'crypto'; import { createReadStream, existsSync } from 'fs'; import { readdir } from 'fs/promises'; +import { exec } from 'child_process'; import path from 'path'; import yargs from 'yargs'; import Queue from 'queue'; @@ -32,6 +33,10 @@ import Queue from 'queue'; throw `Folder ${config.rootFolder} does not exist`; } + if (config.branch === 'main') { + config.branch = null; + } + const files = await listFilesWithHashes(config.rootFolder, config.exclude, 'sha1'); // TODO zip functions const functions = {};// await listFilesWithHashes(config.functionsFolder, null, 'sha256'); @@ -40,6 +45,14 @@ import Queue from 'queue'; await uploadFiles(config.rootFolder, files, deploy); // await uploadFunctions(config.functionsFolder, functions, deploy); + + if (!config.branch) { + await publishDeploy(deploy); + } + + if (process.env.CI) { + exec(`echo "deploy_url=${config.branch ? deploy.deploy_ssl_url : deploy.ssl_url}" >> $GITHUB_OUTPUT`); + } })(); /** @@ -109,18 +122,34 @@ async function createDeploy(branch, files, functions) { files, functions: Object.entries(functions).reduce((res, [name, hash]) => ({ ...res, - [name.replace('.zip')]: hash, + [name.replace('.zip', '')]: hash, }), {}), }), }); const deploy = await result.json(); - console.log(`Created deploy #${deploy.id} (${deploy.deploy_url}). ${deploy.required.length} new files.`) + console.log(`Created deploy #${deploy.id} (${deploy.deploy_ssl_url}). ${deploy.required.length} new files.`) return deploy; } +/** + * Publish the deploy + */ +async function publishDeploy(deploy) { + const result = await fetch(`https://api.netlify.com/api/v1/sites/${process.env.NETLIFY_SITE_ID}/deploys/${deploy.id}/restore`, { + method: 'POST', + headers: { + 'Authorization': 'Bearer ' + process.env.NETLIFY_AUTH_TOKEN, + }, + }); + + deploy = await result.json(); + + console.log(`Published deploy #${deploy.id} (${deploy.ssl_url}).`) +} + /** * Upload new files to Netlify */ diff --git a/cypress.config.ts b/cypress.config.ts index 96bd2339..9e2768f7 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -3,6 +3,8 @@ import { configureVisualRegression } from 'cypress-visual-regression'; // @ts-ignore import cypressMochawesomeReporterPlugin from 'cypress-mochawesome-reporter/plugin'; +const BRANCH_NAME = process.env.GITHUB_REF_NAME ?? 'dev'; + export default defineConfig({ e2e: { viewportWidth: 1280, @@ -56,5 +58,8 @@ export default defineConfig({ // cypress-mochawesome-reporter removeJsonsFolderAfterMerge: false, cdn: true, + charts: true, + reportPageTitle: 'Photo Sphere Viewer', + reportTitle: `Photo Sphere Viewer (${BRANCH_NAME})`, }, });