Search code examples
javascriptgithub-actionsbuilding-github-actions

set GitHub Actions input value for local testing


I'm writing a GitHub action that receives a mandatory input field named file using @actions/core library.

const core = require("@actions/core");

async function run() {
    try {
        let file = core.getInput('file', {required: true});

        // rest of my action ...

I'm able to run it locally and it fails as expected as-is (no input provided). Is there a built-in way to provide inputs (similar to env-vars) so I could run and test it locally?

Error: Input required and not supplied: file
    at Object.getInput (.../node_modules/@actions/core/lib/core.js:78:15)
    at run (.../src/main.js:6:25)
    at Object.<anonymous> (.../src/main.js:40:5)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
::error::Input required and not supplied: file

Solution

  • If you look at the sources of getInput, you can see that it is using environment variables:

    const val: string =
        process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''
    

    Knowing this, you can just set this environment variable:

    const setInput = (name,value)=>
        process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`]=value;
    
    

    Alternatively, you can also provide the environment variable from the parent process, e.g. bash:

    export INPUT_YOUR_VARIABLE='whatever'
    node main.js