Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variable received from GitHub actions

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!

I think that I have to pass the ENV variable through my Dockerfile... Right now my Dockerfile looks like this: 

FROM python:latest

# Add files to the image
ADD entrypoint.py /entrypoint.py
ADD requirements.txt /requirements.txt

# Save ENV var in a temp file
RUN $TEST_VAR > /temp_var

# Install dependencies and make script executable
RUN pip install -r requirements.txt
RUN chmod +x entrypoint.py

RUN echo "temp var: "
RUN cat /temp_var

# Run script with the ENV var
ENTRYPOINT export TEST_VAR="$TEST_VAR"; /entrypoint.py

But the variable isn't echoed and isn't passed to the pythons script either.. am I missing something? When I tried to set my $TEMP_VAR to a random piece of string, it is sent through to the Python script. Is this a mistake on my behalf or is the GitHub action not working as intended?

Here's the link to the test repo

like image 834
Param Avatar asked Feb 03 '23 17:02

Param


2 Answers

I think you are trying to read the wrong environment variable name. GitHub Actions adds INPUT_ to the name of the input variable. So try the following:

print(os.getenv('INPUT_TEST_VAR'))

From the documentation:

When you specify an input to an action in a workflow file or use a default input value, GitHub creates an environment variable for the input with the name INPUT_. The environment variable created converts input names to uppercase letters and replaces spaces with _ characters.

For example, if a workflow defined the numOctocats and octocatEyeColor inputs, the action code could read the values of the inputs using the INPUT_NUMOCTOCATS and INPUT_OCTOCATEYECOLOR environment variables.

https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs

like image 60
peterevans Avatar answered Feb 06 '23 07:02

peterevans


A bit late but for the next one, you can also use the env field :

name: CI

on: [push]

jobs:
  build:

runs-on: ubuntu-latest

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

which will be included during the creation of your docker and pass without the prefix INPUT_

like image 37
Quentin Avatar answered Feb 06 '23 07:02

Quentin