Skip to content

Commit

Permalink
chore: add deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Nov 5, 2024
1 parent 377d616 commit b8c568f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/shared/deploy-e2e-report/action.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion .github/workflows/shared/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: setup

inputs:
turbo-cache:
default: True
default: true

runs:
using: composite
Expand Down
33 changes: 31 additions & 2 deletions build/deploy-netlify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand All @@ -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`);
}
})();

/**
Expand Down Expand Up @@ -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
*/
Expand Down
5 changes: 5 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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})`,
},
});

0 comments on commit b8c568f

Please sign in to comment.