I'm trying to create a GitHub Action which :
My problem is: My Node script works well in a local environment, but shows an error on loading of JSON files :
Error: Cannot find module './GeoJSON_schemas/LineString.json'
Require stack:
- /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8105:19
at /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8118:3
at Object.<anonymous> (/home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8121:12)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js'
]
}
Here is the Action code:
const core = require('@actions/core');
const fs = require('fs');
//const path = require('path');
const schema_path = core.getInput('main_schema_path');
const schemas_dir = core.getInput('additional_schemas_dir');
const data = core.getInput('data_path');
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const ajv = new Ajv({allErrors: true, strict: false});
addFormats(ajv);
let add_schemas = fs.readdirSync(schemas_dir);
for (let add_schema of add_schemas) {
add_schema_path = schemas_dir + add_schema
ajv.addSchema(require(add_schema_path), add_schema); //THIS REQUIRE IS THE ONE THAT DOESN'T WORK
};
const schema = require(schema_path);
const validate = ajv.compile(schema);
test(require(data_path));
function test(data) {
const valid = validate(data);
if (valid) core.setOutput('validity', "Valid!")
else core.setOutput('validity', "Invalid: " + ajv.errorsText(validate.errors))
};
Link of the GitHub Action repo: ( https://github.com/IdrissaD/jsonschema_validator ) Link of the workflow error: ( https://github.com/PnX-SI/schema_randonnee/runs/3144851785 )
So the JSON file I'm trying to load is not a module (at least I believe so), and locally my script loads it perfectly with the "same" code. The difference is that I build the path with inputs from the yml workflow file.
I don't understand if the script just don't get to the right place, thus doesn't find any file called LineString.json, or if it gets to the right folder but doesn't manage to load the file as it takes it for a module?
I believe it can have something to do with the way I should write the fs.readDirSync in a GitHUb context (comparing to a local one), but I struggle to understand the solution.
Ok I got it, the directory in which the script was run was actually /home/runner/work/schema_randonnee/schema_randonnee/
(visible in the workflow logs of the actions/checkout@v2
run). So by adding /home/runner/work/schema_randonnee/schema_randonnee/GeoJSON_schemas/
before all my input values ( main_schema_path
, additional_schemas_dir
and data_path
), the script managed to reach the files in the workflow repo!
The action checkout@v2
checks out the repository under $GITHUB_WORKSPACE
(cf. README ) and this env value is defined by default on /home/runner/work/my-repo-name/my-repo-name
as visible in this GitHub doc . Without changement on the run of that checkout
action, it's the directory to indicate to the GitHub Action you're calling (which is by default running at the same level than /home
if I understand well).
Edit: even better than the previous solution, I should use the generated $GITHUB_WORKSPACE
variable that way:
steps:
- name: checking out repo
uses: actions/checkout@v2
- name: build the schema and validate the data
uses: idrissad/jsonschema_validator@main
id: validation
with:
main_schema_path: ${{ github.workspace }}/schema.json
additional_schemas_dir: ${{ github.workspace }}/GeoJSON_schemas/
data_path: ${{ github.workspace }}/exemple-valide.json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.