diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07ea231..1983112 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,13 +7,20 @@ jobs: name: A test job to read a value from json as a variable steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Read a json file uses: ./ id: read with: input-file: 'package.json' input-property: 'author.name' # Exp: 'fiddlermikey' + - name: Read a json file + uses: ./ + id: read-nofail + with: + input-file: 'package.json' + input-property: 'foo' # does not exist + required-value: 'false' # Ignore if not present - name: Display property value for input-property in input-file id: write run: echo "The value for ${{ steps.read.outputs.output-property }} is ${{ steps.read.outputs.output-value }}" diff --git a/action.yml b/action.yml index 4ca4599..55a77a6 100644 --- a/action.yml +++ b/action.yml @@ -11,6 +11,10 @@ inputs: description: 'Root-level or nested property using dot notation' required: true default: 'package.json' + required-value: + description: 'Fail if not found' + required: false + default: 'false' outputs: output-property: description: 'The value of input-property' diff --git a/dist/index.js b/dist/index.js index 2b28c64..a424cb0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2836,12 +2836,14 @@ const fs = __nccwpck_require__(147); const core = __nccwpck_require__(186); try { const item = core.getInput('input-property') || 'foo' + const requiredValue = core.getInput('required-value') || 'false' const inputFile = core.getInput('input-file') || 'package.json' const newdata = JSON.parse(fs.readFileSync(inputFile)) - const outValue = eval("newdata." + item) + var outValue = '' + outValue = eval("newdata." + item) core.setOutput('output-property', item); core.setOutput('output-value', outValue); - if (outValue === undefined) { + if (outValue === undefined && requiredValue == 'true') { core.setFailed('Property: ' + item + ' does not exist in ' + inputFile); } } diff --git a/main.js b/main.js index c5953e2..ed55d61 100644 --- a/main.js +++ b/main.js @@ -2,12 +2,14 @@ const fs = require('fs'); const core = require('@actions/core'); try { const item = core.getInput('input-property') || 'foo' + const requiredValue = core.getInput('required-value') || 'false' const inputFile = core.getInput('input-file') || 'package.json' const newdata = JSON.parse(fs.readFileSync(inputFile)) - const outValue = eval("newdata." + item) + var outValue = '' + outValue = eval("newdata." + item) core.setOutput('output-property', item); core.setOutput('output-value', outValue); - if (outValue === undefined) { + if (outValue === undefined && requiredValue == 'true') { core.setFailed('Property: ' + item + ' does not exist in ' + inputFile); } } diff --git a/package.json b/package.json index a8a9ad5..7eb5a3b 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "precommit": "npm run build && git add dist/" }, "author": { - "name": "fiddlermikey" + "name": "fiddlermikey", + "email": "fake@fakestreet.org" }, "devDependencies": { "eslint": "^8.21.0"