GitHub Actions environment variable not set with input

In my action.yml I defined an input:

name: 'test action'
author: Param Thakkar
description: 'test'

inputs:
  test_var:
    description: 'A test variable'
    required: true

runs:
  using: 'docker'
  image: 'Dockerfile'

And in my workflow I passed the test_var:

name: CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - name: Test the GH action
        uses: paramt/github-actions-playground@master
        with:
          test_var: "this is just a test"

So there should be an environment variable that’s created when the workflow runs, right? But when I run this short python script:

import os

print(os.getenv('TEST_VAR'))
print("It works!")

exit(0)

It prints:

None
It works!

Do I have to pass the env variable to my python script through the Dockerfile? Right now my Dockerfile looks like this: 

FROM python:latest

ADD entrypoint.py /entrypoint.py
ADD requirements.txt /requirements.txt

RUN echo "$TEST_VAR"

RUN pip install -r requirements.txt
RUN chmod +x entrypoint.py
ENTRYPOINT ["/entrypoint.py"]

Any help is appreciated. Here’s the link to the repo

2 Likes

Hi @paramt,

Thank you for being here and apologies for the delay in response, I’m wondering if you are still having this issue? I think it might be a syntax problem on the variable name.

Yup, got the same problem in a different manifestation.

I followed the manual https://help.github.com/en/github/automating-your-workflow-with-github-actions/creating-a-docker-container-action

but altered a few details to get it working for my purpose. 
My Use Case: Execute Ansible Playbooks to execute Deployments. The name of the Ansible Playbook is the input:

# action.yml
name: 'Ansible Playbook'
description: 'Executes an Ansible Playbook as Github Action'
inputs:
  playbookName:
    description: 'Name of the playbook in your workspace.'
    required: true
    default: 'Playbook-Name-Not-Set'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.playbookName }}

Here’s the entrypoint.sh

#!/bin/sh

ansible-playbook "$INPUT_PLAYBOOKNAME"

Here’s the example workflow to test the whole thing:

on: [push]

jobs:
  ansible_playbook_job:
    runs-on: ubuntu-latest
    name: A job to execute a sample playbook
    steps:
      - uses: actions/checkout@v1
      - uses: ./
      - name: Execute Playbook Action Step
        id: Execution
        uses: myorga/ansible-playbook-docker-action@master
        with:
          playbookName: "simple-playbook.yml"

Here’s my output:

ERROR! the playbook: Playbook-Name-Not-Set could not be found

You can see it’s using the default value defined in the actions metadata.

Even if i try to use the variable as $1 I get the same result.

And here’s the thing: If i don’t default the value it doesn’t fail. There’s just an empty output like this:

ERROR! the playbook: could not be found

Maybe there’s something wrong in my syntax. 
Any suggestions?

Ok, Solved it myself. It was a Problem with cribbing the whole manual and adapting it wrongly.
wrong_action.PNG

The line with

- uses: ./

does the same as the one below but with no parameters given. So it defaults them.

Still there’s the issue with undefaulted required parameters. I would expect Github to complain about this at execution time.

@paramt I think in your case it’s quite simple to solve. You missed to hand your inputs over into your Docker Container.
You’d need to adapt your action.yml like this (pay attention to the args section):

name: 'test action'
author: Param Thakkar
description: 'test'

inputs:
  test_var:
    description: 'A test variable'
    required: true

runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.test_var }}

hth
Patrick

Yep, the problem was that the environment variable needed the ‘INPUT_’ prefix…

So instead of looking for the TEST_VAR environment variable, I tried INPUT_TEST_VAR and it worked.

1 Like