open-scd icon indicating copy to clipboard operation
open-scd copied to clipboard

Preview PRs in github.io

Open juancho0202 opened this issue 10 months ago • 1 comments

As a stakeholder of the open-scd project I would like to test functional behavior of the PRs in a temporary URL which features a copy of OpenSCD made with the code in the branch of the pull request.

Acceptance Criteria:

  • A github workflow is added (runs on pull_request) which builds and deploys OpenSCD to a custom URL

juancho0202 avatar Apr 23 '24 09:04 juancho0202

There is a small chance that in the future we can count on an official github-pages solution for this https://github.com/actions/deploy-pages/pull/61

juancho0202 avatar Apr 23 '24 09:04 juancho0202

The pull request with the GitHub Action to generate a preview has been already merged:

  • https://github.com/openscd/open-scd/pull/1547

It currently deploys the wrong folder here: https://github.com/openscd/open-scd/blob/main/.github/workflows/pr-preview.yml#L33

I've already fixed it. It needs to deploy from /packages/distribution/build.

The next problem is that OpenSCD ist deployed under a path like .../pr-preview/pr-4/ and it should load files from this subfolder but it tries to load the files from the root.

The next step to set a base URL at build time so it loads the correct files.

trusz avatar Jul 08 '24 07:07 trusz

The solution is make the referenced files' path adjustable. With snowpack we can set the base URL: buildOptions.baseUrl Then we can use %PUBLIC_URL% to reference files:

- <script type="module" src="/main.js"></script>
+ <script type="module" src="%PUBLIC_URL%/main.js"></script>

To change this from the outside we can configure snowpack in snowpack.config.mjs like this:

  buildOptions: {
    baseUrl: process.env.PUBLIC_URL || '/',
  },

and set the path in when we start the build process eg.: PUBLIC_URL=open-scd/pr-preview/pr-1 npm run-script build

the reference to built-in plug-ins in plugins.ts we can figure out dynamically dependent on where OpenSCD is deployed:

const path = location.origin + location.pathname + plugin

The next step is to fix the failing tests, because of the changed plugin paths.

trusz avatar Jul 10 '24 09:07 trusz

The tests were failing because the pluginIds in the tests were still relative paths but OpenSCD now generates absolute paths and OpenSCD could not find them in local storage.

trusz avatar Jul 10 '24 21:07 trusz

The final problem was that GitHub Pages, by default, does not server files from folders that start with _. It has something to do with jekyll, but it was an easy fix because you can disable jekyll with a .nojekyll file at root level.

trusz avatar Jul 11 '24 10:07 trusz