skip to Main Content

Given a .js file

const renderer = process.env.RENDERER;

if(!renderer) {
    console.log('missing');

    return;
}

console.log(renderer);

and a package.json

{
  "scripts": {
    "dev": "node app.js"
  }
}

I want to call the npm script inside my Github actions workflow but I want to pass the environment variable to it

name: Do

on:
  workflow_dispatch:
    inputs:
      renderer:
        description: 'A,B,C,...'
        required: true
        type: string

jobs:
  do:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 19.x

      - name: Install dependencies
        run: npm install

      - name: Set environment variable for renderer
        run: export RENDERER=${{ inputs.renderer }}

      - name: Test output
        run: npm run dev

When running the workflow the script writes

missing

to the console so it seems the npm script didn’t run with the environment variable.

How can I prepend RENDERER=${{ inputs.renderer }} to npm run dev?

2

Answers


  1. The | character allows you to specify a multi-line command :

    - name: Test output
      run: |
        export RENDERER=${{ inputs.renderer }}
        npm run dev
    
    Login or Signup to reply.
  2. Apart from export, there are two more alternatives:

    By specifying RENDERER using the env context:

    - name: Test output
      env:
        RENDERER: ${{ inputs.renderer }}
      run: npm run dev
    

    and, by specifying RENDERER=${{ inputs.renderer }} before the command itself:

    - name: Test output
      run: RENDERER=${{ inputs.renderer }} npm run dev
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search